BLIP/Demo/BLIPEchoClient.m
author Jens Alfke <jens@mooseyard.com>
Wed Apr 22 16:45:39 2009 -0700 (2009-04-22)
changeset 26 cb9cdf247239
parent 7 5936db2c1987
child 42 c1cf9df64c70
permissions -rw-r--r--
* Added MYBonjourBrowser and MYBonjourService.
* Added MYPortMapper.
* Added -[TCPEndpoint setPeerToPeerIdentity:].
* Created a static-library target.
     1 //
     2 //  BLIPEchoClient.m
     3 //  MYNetwork
     4 //
     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
     9 //
    10 
    11 #import "BLIPEchoClient.h"
    12 #import "MYBonjourBrowser.h"
    13 #import "MYBonjourService.h"
    14 #import "CollectionUtils.h"
    15 #import "MYNetwork.h"
    16 #import "Target.h"
    17 
    18 
    19 @implementation BLIPEchoClient
    20 
    21 
    22 - (void)awakeFromNib 
    23 {
    24     [self.serviceBrowser start];
    25 }
    26 
    27 - (MYBonjourBrowser*) serviceBrowser {
    28     if (!_serviceBrowser)
    29         _serviceBrowser = [[MYBonjourBrowser alloc] initWithServiceType: @"_blipecho._tcp."];
    30     return _serviceBrowser;
    31 }
    32 
    33 - (NSArray*) serviceList {
    34     return [_serviceBrowser.services.allObjects sortedArrayUsingSelector: @selector(compare:)];
    35 }
    36 
    37 + (NSArray*) keyPathsForValuesAffectingServiceList {
    38     return $array(@"serviceBrowser.services");
    39 }
    40 
    41 
    42 #pragma mark -
    43 #pragma mark BLIPConnection support
    44 
    45 /* Opens a BLIP connection to the given address. */
    46 - (void)openConnection: (NSNetService*)service 
    47 {
    48     _connection = [[BLIPConnection alloc] initToNetService: service];
    49     if( _connection ) {
    50         _connection.delegate = self;
    51         [_connection open];
    52     } else
    53         NSBeep();
    54 }
    55 
    56 /* Closes the currently open BLIP connection. */
    57 - (void)closeConnection
    58 {
    59     [_connection close];
    60 }
    61 
    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];
    68     }
    69 }
    70 
    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];
    74 }
    75 
    76 /** Called after the connection closes. */
    77 - (void) connectionDidClose: (TCPConnection*)connection {
    78     if (connection==_connection) {
    79         if (connection.error)
    80             [serverTableView.window presentError: connection.error];
    81         [_connection release];
    82         _connection = nil;
    83         [inputField setEnabled: NO];
    84         [responseField setEnabled: NO];
    85     }
    86 }
    87 
    88 
    89 #pragma mark -
    90 #pragma mark GUI action methods
    91 
    92 - (IBAction)serverClicked:(id)sender {
    93     NSTableView * table = (NSTableView *)sender;
    94     int selectedRow = [table selectedRow];
    95     
    96     [self closeConnection];
    97     if (-1 != selectedRow)
    98         [self openConnection: [[self.serviceList objectAtIndex:selectedRow] netService]];
    99 }
   100 
   101 /* Send a BLIP request containing the string in the textfield */
   102 - (IBAction)sendText:(id)sender 
   103 {
   104     BLIPRequest *r = [_connection request];
   105     r.bodyString = [sender stringValue];
   106     BLIPResponse *response = [r send];
   107     if (response) {
   108         response.onComplete = $target(self,gotResponse:);
   109         [inputField setStringValue: @""];
   110     } else
   111         NSBeep();
   112 }
   113 
   114 /* Receive the response to the BLIP request, and put its contents into the response field */
   115 - (void) gotResponse: (BLIPResponse*)response
   116 {
   117     [responseField setObjectValue: response.bodyString];
   118 }    
   119 
   120 
   121 @end
   122 
   123 int main(int argc, char *argv[])
   124 {
   125     //RunTestCases(argc,(const char**)argv);
   126     return NSApplicationMain(argc,  (const char **) argv);
   127 }