BLIP/Demo/BLIPEchoClient.m
author Jens Alfke <jens@mooseyard.com>
Sat May 24 21:26:09 2008 -0700 (2008-05-24)
changeset 3 76f302097a75
child 4 2bd9d60a2c46
permissions -rw-r--r--
Added a BLIP port of Apple's "CocoaEcho" sample code.
     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 //
     9 
    10 #import "BLIPEchoClient.h"
    11 #import "BLIP.h"
    12 #import "IPAddress.h"
    13 #import "Target.h"
    14 
    15 
    16 @implementation BLIPEchoClient
    17 
    18 @synthesize serviceList=_serviceList;
    19 
    20 - (void)awakeFromNib 
    21 {
    22     _serviceBrowser = [[NSNetServiceBrowser alloc] init];
    23     _serviceList = [[NSMutableArray alloc] init];
    24     [_serviceBrowser setDelegate:self];
    25     
    26     [_serviceBrowser searchForServicesOfType:@"_blipecho._tcp." inDomain:@""];
    27 }
    28 
    29 #pragma mark -
    30 #pragma mark BLIPConnection support
    31 
    32 - (void)openConnection: (IPAddress*)address 
    33 {
    34     _connection = [[BLIPConnection alloc] initToAddress: address];
    35     [_connection open];
    36 }
    37 
    38 - (void)closeConnection
    39 {
    40     [_connection close];
    41     [_connection release];
    42     _connection = nil;
    43 }
    44 
    45 #pragma mark -
    46 #pragma mark NSNetServiceBrowser delegate methods
    47 
    48 // We broadcast the willChangeValueForKey: and didChangeValueForKey: for the NSTableView binding to work.
    49 
    50 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
    51     if (![_serviceList containsObject:aNetService]) {
    52         [self willChangeValueForKey:@"serviceList"];
    53         [_serviceList addObject:aNetService];
    54         [self didChangeValueForKey:@"serviceList"];
    55     }
    56 }
    57 
    58 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
    59     if ([_serviceList containsObject:aNetService]) {
    60         [self willChangeValueForKey:@"serviceList"];
    61         [_serviceList removeObject:aNetService];
    62         [self didChangeValueForKey:@"serviceList"];
    63     }
    64 }
    65 
    66 #pragma mark -
    67 #pragma mark NSNetService delegate methods
    68 
    69 - (void)stopResolving
    70 {
    71     if( _resolvingService ) {
    72         _resolvingService.delegate = nil;
    73         [_resolvingService stop];
    74         [_resolvingService release];
    75         _resolvingService = nil;
    76     }
    77 }    
    78 
    79 - (void)startResolving: (NSNetService*)service
    80 {
    81     [self stopResolving];
    82     _resolvingService = [service retain];
    83     _resolvingService.delegate = self;
    84     [_resolvingService resolveWithTimeout: 5.0];
    85     
    86 }
    87 
    88 - (void)netServiceDidResolveAddress:(NSNetService *)sender
    89 {
    90     if( sender == _resolvingService ) {
    91         NSArray *addresses = _resolvingService.addresses;
    92         if( addresses.count > 0 ) {
    93             NSData *addressData = [addresses objectAtIndex: 0];
    94             IPAddress *address = [[IPAddress alloc] initWithSockAddr: addressData.bytes];
    95             [self openConnection: address];
    96             [address release];
    97         }
    98         [self stopResolving];
    99     }
   100 }
   101 
   102 #pragma mark -
   103 #pragma mark GUI action methods
   104 
   105 - (IBAction)serverClicked:(id)sender {
   106     NSTableView * table = (NSTableView *)sender;
   107     int selectedRow = [table selectedRow];
   108     
   109     [self closeConnection];
   110     [self stopResolving];
   111     
   112     if (-1 != selectedRow) {
   113         [self startResolving: [_serviceList objectAtIndex:selectedRow]];
   114     }
   115 }
   116 
   117 - (IBAction)sendText:(id)sender 
   118 {
   119     BLIPRequest *r = [_connection requestWithBody: nil];
   120     r.bodyString = [sender stringValue];
   121     BLIPResponse *response = [r send];
   122     response.onComplete = $target(self,gotResponse:);
   123 }
   124 
   125 - (void) gotResponse: (BLIPResponse*)response
   126 {
   127     [responseField setObjectValue: response.bodyString];
   128 }    
   129 
   130 
   131 @end
   132 
   133 int main(int argc, char *argv[])
   134 {
   135     return NSApplicationMain(argc,  (const char **) argv);
   136 }