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@3: #import "BLIP.h" jens@3: #import "Target.h" jens@3: jens@3: jens@3: @implementation BLIPEchoClient jens@3: jens@3: @synthesize serviceList=_serviceList; jens@3: jens@3: - (void)awakeFromNib jens@3: { jens@3: _serviceBrowser = [[NSNetServiceBrowser alloc] init]; jens@3: _serviceList = [[NSMutableArray alloc] init]; jens@3: [_serviceBrowser setDelegate:self]; jens@3: jens@3: [_serviceBrowser searchForServicesOfType:@"_blipecho._tcp." inDomain:@""]; jens@3: } jens@3: jens@3: #pragma mark - jens@3: #pragma mark NSNetServiceBrowser delegate methods jens@3: jens@3: // We broadcast the willChangeValueForKey: and didChangeValueForKey: for the NSTableView binding to work. jens@3: jens@3: - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing { jens@3: if (![_serviceList containsObject:aNetService]) { jens@3: [self willChangeValueForKey:@"serviceList"]; jens@3: [_serviceList addObject:aNetService]; jens@3: [self didChangeValueForKey:@"serviceList"]; jens@3: } jens@3: } jens@3: jens@3: - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing { jens@3: if ([_serviceList containsObject:aNetService]) { jens@3: [self willChangeValueForKey:@"serviceList"]; jens@3: [_serviceList removeObject:aNetService]; jens@3: [self didChangeValueForKey:@"serviceList"]; jens@3: } jens@3: } jens@3: 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@7: if( _connection ) jens@7: [_connection open]; jens@7: 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@7: [_connection release]; jens@7: _connection = nil; jens@3: } jens@3: 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@7: [self openConnection: [_serviceList objectAtIndex:selectedRow]]; 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@3: response.onComplete = $target(self,gotResponse:); 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@3: return NSApplicationMain(argc, (const char **) argv); jens@3: }