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"
12 #import "MYBonjourBrowser.h"
13 #import "MYBonjourService.h"
14 #import "CollectionUtils.h"
19 @implementation BLIPEchoClient
24 [self.serviceBrowser start];
27 - (MYBonjourBrowser*) serviceBrowser {
29 _serviceBrowser = [[MYBonjourBrowser alloc] initWithServiceType: @"_blipecho._tcp."];
30 return _serviceBrowser;
33 - (NSArray*) serviceList {
34 return [_serviceBrowser.services.allObjects sortedArrayUsingSelector: @selector(compare:)];
37 + (NSArray*) keyPathsForValuesAffectingServiceList {
38 return $array(@"serviceBrowser.services");
43 #pragma mark BLIPConnection support
45 /* Opens a BLIP connection to the given address. */
46 - (void)openConnection: (MYBonjourService*)service
48 _connection = [[BLIPConnection alloc] initToBonjourService: service];
50 _connection.delegate = self;
56 /* Closes the currently open BLIP connection. */
57 - (void)closeConnection
62 /** Called after the connection successfully opens. */
63 - (void) connectionDidOpen: (TCPConnection*)connection {
64 if (connection==_connection) {
65 [inputField setEnabled: YES];
66 [responseField setEnabled: YES];
67 [inputField.window makeFirstResponder: inputField];
71 /** Called after the connection fails to open due to an error. */
72 - (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error {
73 [serverTableView.window presentError: error];
76 /** Called after the connection closes. */
77 - (void) connectionDidClose: (TCPConnection*)connection {
78 if (connection==_connection) {
80 [serverTableView.window presentError: connection.error];
81 [_connection release];
83 [inputField setEnabled: NO];
84 [responseField setEnabled: NO];
90 #pragma mark GUI action methods
92 - (IBAction)serverClicked:(id)sender {
93 NSTableView * table = (NSTableView *)sender;
94 int selectedRow = [table selectedRow];
96 [self closeConnection];
97 if (-1 != selectedRow)
98 [self openConnection: [self.serviceList objectAtIndex:selectedRow]];
101 /* Send a BLIP request containing the string in the textfield */
102 - (IBAction)sendText:(id)sender
104 BLIPRequest *r = [_connection request];
105 r.bodyString = [sender stringValue];
106 BLIPResponse *response = [r send];
108 response.onComplete = $target(self,gotResponse:);
109 [inputField setStringValue: @""];
114 /* Receive the response to the BLIP request, and put its contents into the response field */
115 - (void) gotResponse: (BLIPResponse*)response
117 [responseField setObjectValue: response.bodyString];
123 int main(int argc, char *argv[])
125 //RunTestCases(argc,(const char**)argv);
126 return NSApplicationMain(argc, (const char **) argv);