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"
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 NSNetServiceBrowser delegate methods
32 // We broadcast the willChangeValueForKey: and didChangeValueForKey: for the NSTableView binding to work.
34 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
35 if (![_serviceList containsObject:aNetService]) {
36 [self willChangeValueForKey:@"serviceList"];
37 [_serviceList addObject:aNetService];
38 [self didChangeValueForKey:@"serviceList"];
42 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
43 if ([_serviceList containsObject:aNetService]) {
44 [self willChangeValueForKey:@"serviceList"];
45 [_serviceList removeObject:aNetService];
46 [self didChangeValueForKey:@"serviceList"];
51 #pragma mark BLIPConnection support
53 /* Opens a BLIP connection to the given address. */
54 - (void)openConnection: (NSNetService*)service
56 _connection = [[BLIPConnection alloc] initToNetService: service];
63 /* Closes the currently open BLIP connection. */
64 - (void)closeConnection
67 [_connection release];
72 #pragma mark GUI action methods
74 - (IBAction)serverClicked:(id)sender {
75 NSTableView * table = (NSTableView *)sender;
76 int selectedRow = [table selectedRow];
78 [self closeConnection];
79 if (-1 != selectedRow)
80 [self openConnection: [_serviceList objectAtIndex:selectedRow]];
83 /* Send a BLIP request containing the string in the textfield */
84 - (IBAction)sendText:(id)sender
86 BLIPRequest *r = [_connection request];
87 r.bodyString = [sender stringValue];
88 BLIPResponse *response = [r send];
89 response.onComplete = $target(self,gotResponse:);
92 /* Receive the response to the BLIP request, and put its contents into the response field */
93 - (void) gotResponse: (BLIPResponse*)response
95 [responseField setObjectValue: response.bodyString];
101 int main(int argc, char *argv[])
103 return NSApplicationMain(argc, (const char **) argv);