More documentation.
5 // Created by Jens Alfke on 5/24/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
7 // Adapted from Apple sample code "CocoaEcho":
8 // http://developer.apple.com/samplecode/CocoaEcho/index.html
11 #import "BLIPEchoClient.h"
17 @implementation BLIPEchoClient
19 @synthesize serviceList=_serviceList;
23 _serviceBrowser = [[NSNetServiceBrowser alloc] init];
24 _serviceList = [[NSMutableArray alloc] init];
25 [_serviceBrowser setDelegate:self];
27 [_serviceBrowser searchForServicesOfType:@"_blipecho._tcp." inDomain:@""];
31 #pragma mark BLIPConnection support
33 /* Opens a BLIP connection to the given address. */
34 - (void)openConnection: (IPAddress*)address
36 _connection = [[BLIPConnection alloc] initToAddress: address];
40 /* Closes the currently open BLIP connection. */
41 - (void)closeConnection
44 [_connection release];
49 #pragma mark NSNetServiceBrowser delegate methods
51 // We broadcast the willChangeValueForKey: and didChangeValueForKey: for the NSTableView binding to work.
53 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
54 if (![_serviceList containsObject:aNetService]) {
55 [self willChangeValueForKey:@"serviceList"];
56 [_serviceList addObject:aNetService];
57 [self didChangeValueForKey:@"serviceList"];
61 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
62 if ([_serviceList containsObject:aNetService]) {
63 [self willChangeValueForKey:@"serviceList"];
64 [_serviceList removeObject:aNetService];
65 [self didChangeValueForKey:@"serviceList"];
70 #pragma mark NSNetService delegate methods
72 /* Stop any current Bonjour address resolution */
75 if( _resolvingService ) {
76 _resolvingService.delegate = nil;
77 [_resolvingService stop];
78 [_resolvingService release];
79 _resolvingService = nil;
83 /* Ask Bonjour to resolve (look up) the IP address of the given service. */
84 - (void)startResolving: (NSNetService*)service
87 _resolvingService = [service retain];
88 _resolvingService.delegate = self;
89 [_resolvingService resolveWithTimeout: 5.0];
93 /* NSNetService delegate method that will be called when address resolution finishes. */
94 - (void)netServiceDidResolveAddress:(NSNetService *)sender
96 if( sender == _resolvingService ) {
97 // Get the first address, which is an NSData containing a struct sockaddr:
98 NSArray *addresses = _resolvingService.addresses;
99 if( addresses.count > 0 ) {
100 NSData *addressData = [addresses objectAtIndex: 0];
101 IPAddress *address = [[IPAddress alloc] initWithSockAddr: addressData.bytes];
102 [self openConnection: address];
105 [self stopResolving];
110 #pragma mark GUI action methods
112 - (IBAction)serverClicked:(id)sender {
113 NSTableView * table = (NSTableView *)sender;
114 int selectedRow = [table selectedRow];
116 [self closeConnection];
117 [self stopResolving];
119 if (-1 != selectedRow) {
120 [self startResolving: [_serviceList objectAtIndex:selectedRow]];
124 /* Send a BLIP request containing the string in the textfield */
125 - (IBAction)sendText:(id)sender
127 BLIPRequest *r = [_connection requestWithBody: nil];
128 r.bodyString = [sender stringValue];
129 BLIPResponse *response = [r send];
130 response.onComplete = $target(self,gotResponse:);
133 /* Receive the response to the BLIP request, and put its contents into the response field */
134 - (void) gotResponse: (BLIPResponse*)response
136 [responseField setObjectValue: response.bodyString];
142 int main(int argc, char *argv[])
144 return NSApplicationMain(argc, (const char **) argv);