1.1 --- a/BLIP/Demo/BLIPEchoClient.m Sun May 25 13:43:03 2008 -0700
1.2 +++ b/BLIP/Demo/BLIPEchoClient.m Mon May 04 23:21:26 2009 -0700
1.3 @@ -9,44 +9,36 @@
1.4 //
1.5
1.6 #import "BLIPEchoClient.h"
1.7 -#import "BLIP.h"
1.8 +#import "MYBonjourBrowser.h"
1.9 +#import "MYBonjourService.h"
1.10 +#import "CollectionUtils.h"
1.11 +#import "MYNetwork.h"
1.12 #import "Target.h"
1.13
1.14
1.15 @implementation BLIPEchoClient
1.16
1.17 -@synthesize serviceList=_serviceList;
1.18
1.19 - (void)awakeFromNib
1.20 {
1.21 - _serviceBrowser = [[NSNetServiceBrowser alloc] init];
1.22 - _serviceList = [[NSMutableArray alloc] init];
1.23 - [_serviceBrowser setDelegate:self];
1.24 -
1.25 - [_serviceBrowser searchForServicesOfType:@"_blipecho._tcp." inDomain:@""];
1.26 + [self.serviceBrowser start];
1.27 }
1.28
1.29 -#pragma mark -
1.30 -#pragma mark NSNetServiceBrowser delegate methods
1.31 -
1.32 -// We broadcast the willChangeValueForKey: and didChangeValueForKey: for the NSTableView binding to work.
1.33 -
1.34 -- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
1.35 - if (![_serviceList containsObject:aNetService]) {
1.36 - [self willChangeValueForKey:@"serviceList"];
1.37 - [_serviceList addObject:aNetService];
1.38 - [self didChangeValueForKey:@"serviceList"];
1.39 - }
1.40 +- (MYBonjourBrowser*) serviceBrowser {
1.41 + if (!_serviceBrowser)
1.42 + _serviceBrowser = [[MYBonjourBrowser alloc] initWithServiceType: @"_blipecho._tcp."];
1.43 + return _serviceBrowser;
1.44 }
1.45
1.46 -- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
1.47 - if ([_serviceList containsObject:aNetService]) {
1.48 - [self willChangeValueForKey:@"serviceList"];
1.49 - [_serviceList removeObject:aNetService];
1.50 - [self didChangeValueForKey:@"serviceList"];
1.51 - }
1.52 +- (NSArray*) serviceList {
1.53 + return [_serviceBrowser.services.allObjects sortedArrayUsingSelector: @selector(compare:)];
1.54 }
1.55
1.56 ++ (NSArray*) keyPathsForValuesAffectingServiceList {
1.57 + return $array(@"serviceBrowser.services");
1.58 +}
1.59 +
1.60 +
1.61 #pragma mark -
1.62 #pragma mark BLIPConnection support
1.63
1.64 @@ -54,9 +46,10 @@
1.65 - (void)openConnection: (NSNetService*)service
1.66 {
1.67 _connection = [[BLIPConnection alloc] initToNetService: service];
1.68 - if( _connection )
1.69 + if( _connection ) {
1.70 + _connection.delegate = self;
1.71 [_connection open];
1.72 - else
1.73 + } else
1.74 NSBeep();
1.75 }
1.76
1.77 @@ -64,10 +57,35 @@
1.78 - (void)closeConnection
1.79 {
1.80 [_connection close];
1.81 - [_connection release];
1.82 - _connection = nil;
1.83 }
1.84
1.85 +/** Called after the connection successfully opens. */
1.86 +- (void) connectionDidOpen: (TCPConnection*)connection {
1.87 + if (connection==_connection) {
1.88 + [inputField setEnabled: YES];
1.89 + [responseField setEnabled: YES];
1.90 + [inputField.window makeFirstResponder: inputField];
1.91 + }
1.92 +}
1.93 +
1.94 +/** Called after the connection fails to open due to an error. */
1.95 +- (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error {
1.96 + [serverTableView.window presentError: error];
1.97 +}
1.98 +
1.99 +/** Called after the connection closes. */
1.100 +- (void) connectionDidClose: (TCPConnection*)connection {
1.101 + if (connection==_connection) {
1.102 + if (connection.error)
1.103 + [serverTableView.window presentError: connection.error];
1.104 + [_connection release];
1.105 + _connection = nil;
1.106 + [inputField setEnabled: NO];
1.107 + [responseField setEnabled: NO];
1.108 + }
1.109 +}
1.110 +
1.111 +
1.112 #pragma mark -
1.113 #pragma mark GUI action methods
1.114
1.115 @@ -77,7 +95,7 @@
1.116
1.117 [self closeConnection];
1.118 if (-1 != selectedRow)
1.119 - [self openConnection: [_serviceList objectAtIndex:selectedRow]];
1.120 + [self openConnection: [[self.serviceList objectAtIndex:selectedRow] netService]];
1.121 }
1.122
1.123 /* Send a BLIP request containing the string in the textfield */
1.124 @@ -86,7 +104,11 @@
1.125 BLIPRequest *r = [_connection request];
1.126 r.bodyString = [sender stringValue];
1.127 BLIPResponse *response = [r send];
1.128 - response.onComplete = $target(self,gotResponse:);
1.129 + if (response) {
1.130 + response.onComplete = $target(self,gotResponse:);
1.131 + [inputField setStringValue: @""];
1.132 + } else
1.133 + NSBeep();
1.134 }
1.135
1.136 /* Receive the response to the BLIP request, and put its contents into the response field */
1.137 @@ -100,5 +122,6 @@
1.138
1.139 int main(int argc, char *argv[])
1.140 {
1.141 + //RunTestCases(argc,(const char**)argv);
1.142 return NSApplicationMain(argc, (const char **) argv);
1.143 }