jens@3: // jens@3: // BLIPEchoClient.m jens@3: // MYNetwork jens@3: // jens@3: // Created by Jens Alfke on 5/24/08. jens@3: // Copyright 2008 Jens Alfke. All rights reserved. jens@4: // Adapted from Apple sample code "CocoaEcho": jens@4: // http://developer.apple.com/samplecode/CocoaEcho/index.html jens@3: // jens@3: jens@3: #import "BLIPEchoClient.h" jens@26: #import "MYBonjourBrowser.h" jens@26: #import "MYBonjourService.h" jens@26: #import "CollectionUtils.h" jens@26: #import "MYNetwork.h" jens@3: #import "Target.h" jens@3: jens@3: jens@3: @implementation BLIPEchoClient jens@3: jens@3: jens@3: - (void)awakeFromNib jens@3: { jens@26: [self.serviceBrowser start]; jens@3: } jens@3: jens@26: - (MYBonjourBrowser*) serviceBrowser { jens@26: if (!_serviceBrowser) jens@26: _serviceBrowser = [[MYBonjourBrowser alloc] initWithServiceType: @"_blipecho._tcp."]; jens@26: return _serviceBrowser; jens@3: } jens@3: jens@26: - (NSArray*) serviceList { jens@26: return [_serviceBrowser.services.allObjects sortedArrayUsingSelector: @selector(compare:)]; jens@3: } jens@3: jens@26: + (NSArray*) keyPathsForValuesAffectingServiceList { jens@26: return $array(@"serviceBrowser.services"); jens@26: } jens@26: jens@26: jens@3: #pragma mark - jens@7: #pragma mark BLIPConnection support jens@3: jens@7: /* Opens a BLIP connection to the given address. */ jens@7: - (void)openConnection: (NSNetService*)service jens@3: { jens@7: _connection = [[BLIPConnection alloc] initToNetService: service]; jens@26: if( _connection ) { jens@26: _connection.delegate = self; jens@7: [_connection open]; jens@26: } else jens@7: NSBeep(); jens@3: } jens@3: jens@7: /* Closes the currently open BLIP connection. */ jens@7: - (void)closeConnection jens@3: { jens@7: [_connection close]; jens@3: } jens@3: jens@26: /** Called after the connection successfully opens. */ jens@26: - (void) connectionDidOpen: (TCPConnection*)connection { jens@26: if (connection==_connection) { jens@26: [inputField setEnabled: YES]; jens@26: [responseField setEnabled: YES]; jens@26: [inputField.window makeFirstResponder: inputField]; jens@26: } jens@26: } jens@26: jens@26: /** Called after the connection fails to open due to an error. */ jens@26: - (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error { jens@26: [serverTableView.window presentError: error]; jens@26: } jens@26: jens@26: /** Called after the connection closes. */ jens@26: - (void) connectionDidClose: (TCPConnection*)connection { jens@26: if (connection==_connection) { jens@26: if (connection.error) jens@26: [serverTableView.window presentError: connection.error]; jens@26: [_connection release]; jens@26: _connection = nil; jens@26: [inputField setEnabled: NO]; jens@26: [responseField setEnabled: NO]; jens@26: } jens@26: } jens@26: jens@26: jens@3: #pragma mark - jens@3: #pragma mark GUI action methods jens@3: jens@3: - (IBAction)serverClicked:(id)sender { jens@3: NSTableView * table = (NSTableView *)sender; jens@3: int selectedRow = [table selectedRow]; jens@3: jens@3: [self closeConnection]; jens@7: if (-1 != selectedRow) jens@26: [self openConnection: [[self.serviceList objectAtIndex:selectedRow] netService]]; jens@3: } jens@3: jens@4: /* Send a BLIP request containing the string in the textfield */ jens@3: - (IBAction)sendText:(id)sender jens@3: { jens@7: BLIPRequest *r = [_connection request]; jens@3: r.bodyString = [sender stringValue]; jens@3: BLIPResponse *response = [r send]; jens@26: if (response) { jens@26: response.onComplete = $target(self,gotResponse:); jens@26: [inputField setStringValue: @""]; jens@26: } else jens@26: NSBeep(); jens@3: } jens@3: jens@4: /* Receive the response to the BLIP request, and put its contents into the response field */ jens@3: - (void) gotResponse: (BLIPResponse*)response jens@3: { jens@3: [responseField setObjectValue: response.bodyString]; jens@3: } jens@3: jens@3: jens@3: @end jens@3: jens@3: int main(int argc, char *argv[]) jens@3: { jens@26: //RunTestCases(argc,(const char**)argv); jens@3: return NSApplicationMain(argc, (const char **) argv); jens@3: }