Added a BLIP port of Apple's "CocoaEcho" sample code.
5 // Created by Jens Alfke on 5/24/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
7 // Adapted from Apple sample code "CocoaEcho".
10 #import "BLIPEchoClient.h"
16 @implementation BLIPEchoClient
18 @synthesize serviceList=_serviceList;
22 _serviceBrowser = [[NSNetServiceBrowser alloc] init];
23 _serviceList = [[NSMutableArray alloc] init];
24 [_serviceBrowser setDelegate:self];
26 [_serviceBrowser searchForServicesOfType:@"_blipecho._tcp." inDomain:@""];
30 #pragma mark BLIPConnection support
32 - (void)openConnection: (IPAddress*)address
34 _connection = [[BLIPConnection alloc] initToAddress: address];
38 - (void)closeConnection
41 [_connection release];
46 #pragma mark NSNetServiceBrowser delegate methods
48 // We broadcast the willChangeValueForKey: and didChangeValueForKey: for the NSTableView binding to work.
50 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
51 if (![_serviceList containsObject:aNetService]) {
52 [self willChangeValueForKey:@"serviceList"];
53 [_serviceList addObject:aNetService];
54 [self didChangeValueForKey:@"serviceList"];
58 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
59 if ([_serviceList containsObject:aNetService]) {
60 [self willChangeValueForKey:@"serviceList"];
61 [_serviceList removeObject:aNetService];
62 [self didChangeValueForKey:@"serviceList"];
67 #pragma mark NSNetService delegate methods
71 if( _resolvingService ) {
72 _resolvingService.delegate = nil;
73 [_resolvingService stop];
74 [_resolvingService release];
75 _resolvingService = nil;
79 - (void)startResolving: (NSNetService*)service
82 _resolvingService = [service retain];
83 _resolvingService.delegate = self;
84 [_resolvingService resolveWithTimeout: 5.0];
88 - (void)netServiceDidResolveAddress:(NSNetService *)sender
90 if( sender == _resolvingService ) {
91 NSArray *addresses = _resolvingService.addresses;
92 if( addresses.count > 0 ) {
93 NSData *addressData = [addresses objectAtIndex: 0];
94 IPAddress *address = [[IPAddress alloc] initWithSockAddr: addressData.bytes];
95 [self openConnection: address];
103 #pragma mark GUI action methods
105 - (IBAction)serverClicked:(id)sender {
106 NSTableView * table = (NSTableView *)sender;
107 int selectedRow = [table selectedRow];
109 [self closeConnection];
110 [self stopResolving];
112 if (-1 != selectedRow) {
113 [self startResolving: [_serviceList objectAtIndex:selectedRow]];
117 - (IBAction)sendText:(id)sender
119 BLIPRequest *r = [_connection requestWithBody: nil];
120 r.bodyString = [sender stringValue];
121 BLIPResponse *response = [r send];
122 response.onComplete = $target(self,gotResponse:);
125 - (void) gotResponse: (BLIPResponse*)response
127 [responseField setObjectValue: response.bodyString];
133 int main(int argc, char *argv[])
135 return NSApplicationMain(argc, (const char **) argv);