author | morrowa |
Tue Jun 23 11:55:50 2009 -0700 (2009-06-23) | |
changeset 52 | d2e6fb7192ac |
parent 26 | cb9cdf247239 |
permissions | -rw-r--r-- |
jens@3 | 1 |
// |
jens@3 | 2 |
// BLIPEchoClient.m |
jens@3 | 3 |
// MYNetwork |
jens@3 | 4 |
// |
jens@3 | 5 |
// Created by Jens Alfke on 5/24/08. |
jens@3 | 6 |
// Copyright 2008 Jens Alfke. All rights reserved. |
jens@4 | 7 |
// Adapted from Apple sample code "CocoaEcho": |
jens@4 | 8 |
// http://developer.apple.com/samplecode/CocoaEcho/index.html |
jens@3 | 9 |
// |
jens@3 | 10 |
|
jens@3 | 11 |
#import "BLIPEchoClient.h" |
jens@26 | 12 |
#import "MYBonjourBrowser.h" |
jens@26 | 13 |
#import "MYBonjourService.h" |
jens@26 | 14 |
#import "CollectionUtils.h" |
jens@26 | 15 |
#import "MYNetwork.h" |
jens@3 | 16 |
#import "Target.h" |
jens@3 | 17 |
|
jens@3 | 18 |
|
jens@3 | 19 |
@implementation BLIPEchoClient |
jens@3 | 20 |
|
jens@3 | 21 |
|
jens@3 | 22 |
- (void)awakeFromNib |
jens@3 | 23 |
{ |
jens@26 | 24 |
[self.serviceBrowser start]; |
jens@3 | 25 |
} |
jens@3 | 26 |
|
jens@26 | 27 |
- (MYBonjourBrowser*) serviceBrowser { |
jens@26 | 28 |
if (!_serviceBrowser) |
jens@26 | 29 |
_serviceBrowser = [[MYBonjourBrowser alloc] initWithServiceType: @"_blipecho._tcp."]; |
jens@26 | 30 |
return _serviceBrowser; |
jens@3 | 31 |
} |
jens@3 | 32 |
|
jens@26 | 33 |
- (NSArray*) serviceList { |
jens@26 | 34 |
return [_serviceBrowser.services.allObjects sortedArrayUsingSelector: @selector(compare:)]; |
jens@3 | 35 |
} |
jens@3 | 36 |
|
jens@26 | 37 |
+ (NSArray*) keyPathsForValuesAffectingServiceList { |
jens@26 | 38 |
return $array(@"serviceBrowser.services"); |
jens@26 | 39 |
} |
jens@26 | 40 |
|
jens@26 | 41 |
|
jens@3 | 42 |
#pragma mark - |
jens@7 | 43 |
#pragma mark BLIPConnection support |
jens@3 | 44 |
|
jens@7 | 45 |
/* Opens a BLIP connection to the given address. */ |
jens@42 | 46 |
- (void)openConnection: (MYBonjourService*)service |
jens@3 | 47 |
{ |
jens@42 | 48 |
_connection = [[BLIPConnection alloc] initToBonjourService: service]; |
jens@26 | 49 |
if( _connection ) { |
jens@26 | 50 |
_connection.delegate = self; |
jens@7 | 51 |
[_connection open]; |
jens@26 | 52 |
} else |
jens@7 | 53 |
NSBeep(); |
jens@3 | 54 |
} |
jens@3 | 55 |
|
jens@7 | 56 |
/* Closes the currently open BLIP connection. */ |
jens@7 | 57 |
- (void)closeConnection |
jens@3 | 58 |
{ |
jens@7 | 59 |
[_connection close]; |
jens@3 | 60 |
} |
jens@3 | 61 |
|
jens@26 | 62 |
/** Called after the connection successfully opens. */ |
jens@26 | 63 |
- (void) connectionDidOpen: (TCPConnection*)connection { |
jens@26 | 64 |
if (connection==_connection) { |
jens@26 | 65 |
[inputField setEnabled: YES]; |
jens@26 | 66 |
[responseField setEnabled: YES]; |
jens@26 | 67 |
[inputField.window makeFirstResponder: inputField]; |
jens@26 | 68 |
} |
jens@26 | 69 |
} |
jens@26 | 70 |
|
jens@26 | 71 |
/** Called after the connection fails to open due to an error. */ |
jens@26 | 72 |
- (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error { |
jens@26 | 73 |
[serverTableView.window presentError: error]; |
jens@26 | 74 |
} |
jens@26 | 75 |
|
jens@26 | 76 |
/** Called after the connection closes. */ |
jens@26 | 77 |
- (void) connectionDidClose: (TCPConnection*)connection { |
jens@26 | 78 |
if (connection==_connection) { |
jens@26 | 79 |
if (connection.error) |
jens@26 | 80 |
[serverTableView.window presentError: connection.error]; |
jens@26 | 81 |
[_connection release]; |
jens@26 | 82 |
_connection = nil; |
jens@26 | 83 |
[inputField setEnabled: NO]; |
jens@26 | 84 |
[responseField setEnabled: NO]; |
jens@26 | 85 |
} |
jens@26 | 86 |
} |
jens@26 | 87 |
|
jens@26 | 88 |
|
jens@3 | 89 |
#pragma mark - |
jens@3 | 90 |
#pragma mark GUI action methods |
jens@3 | 91 |
|
jens@3 | 92 |
- (IBAction)serverClicked:(id)sender { |
jens@3 | 93 |
NSTableView * table = (NSTableView *)sender; |
jens@3 | 94 |
int selectedRow = [table selectedRow]; |
jens@3 | 95 |
|
jens@3 | 96 |
[self closeConnection]; |
jens@7 | 97 |
if (-1 != selectedRow) |
jens@42 | 98 |
[self openConnection: [self.serviceList objectAtIndex:selectedRow]]; |
jens@3 | 99 |
} |
jens@3 | 100 |
|
jens@4 | 101 |
/* Send a BLIP request containing the string in the textfield */ |
jens@3 | 102 |
- (IBAction)sendText:(id)sender |
jens@3 | 103 |
{ |
jens@7 | 104 |
BLIPRequest *r = [_connection request]; |
jens@3 | 105 |
r.bodyString = [sender stringValue]; |
jens@3 | 106 |
BLIPResponse *response = [r send]; |
jens@26 | 107 |
if (response) { |
jens@26 | 108 |
response.onComplete = $target(self,gotResponse:); |
jens@26 | 109 |
[inputField setStringValue: @""]; |
jens@26 | 110 |
} else |
jens@26 | 111 |
NSBeep(); |
jens@3 | 112 |
} |
jens@3 | 113 |
|
jens@4 | 114 |
/* Receive the response to the BLIP request, and put its contents into the response field */ |
jens@3 | 115 |
- (void) gotResponse: (BLIPResponse*)response |
jens@3 | 116 |
{ |
jens@3 | 117 |
[responseField setObjectValue: response.bodyString]; |
jens@3 | 118 |
} |
jens@3 | 119 |
|
jens@3 | 120 |
|
jens@3 | 121 |
@end |
jens@3 | 122 |
|
jens@3 | 123 |
int main(int argc, char *argv[]) |
jens@3 | 124 |
{ |
jens@26 | 125 |
//RunTestCases(argc,(const char**)argv); |
jens@3 | 126 |
return NSApplicationMain(argc, (const char **) argv); |
jens@3 | 127 |
} |