BLIP/Demo/BLIPEchoClient.m
author snej@snej.local
Tue Dec 02 22:42:56 2008 -0800 (2008-12-02)
changeset 25 a4875607a3a0
parent 4 2bd9d60a2c46
child 26 cb9cdf247239
permissions -rw-r--r--
Added iPhone demo project
     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 "BLIP.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 NSNetServiceBrowser delegate methods
    31 
    32 // We broadcast the willChangeValueForKey: and didChangeValueForKey: for the NSTableView binding to work.
    33 
    34 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
    35     if (![_serviceList containsObject:aNetService]) {
    36         [self willChangeValueForKey:@"serviceList"];
    37         [_serviceList addObject:aNetService];
    38         [self didChangeValueForKey:@"serviceList"];
    39     }
    40 }
    41 
    42 - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
    43     if ([_serviceList containsObject:aNetService]) {
    44         [self willChangeValueForKey:@"serviceList"];
    45         [_serviceList removeObject:aNetService];
    46         [self didChangeValueForKey:@"serviceList"];
    47     }
    48 }
    49 
    50 #pragma mark -
    51 #pragma mark BLIPConnection support
    52 
    53 /* Opens a BLIP connection to the given address. */
    54 - (void)openConnection: (NSNetService*)service 
    55 {
    56     _connection = [[BLIPConnection alloc] initToNetService: service];
    57     if( _connection )
    58         [_connection open];
    59     else
    60         NSBeep();
    61 }
    62 
    63 /* Closes the currently open BLIP connection. */
    64 - (void)closeConnection
    65 {
    66     [_connection close];
    67     [_connection release];
    68     _connection = nil;
    69 }
    70 
    71 #pragma mark -
    72 #pragma mark GUI action methods
    73 
    74 - (IBAction)serverClicked:(id)sender {
    75     NSTableView * table = (NSTableView *)sender;
    76     int selectedRow = [table selectedRow];
    77     
    78     [self closeConnection];
    79     if (-1 != selectedRow)
    80         [self openConnection: [_serviceList objectAtIndex:selectedRow]];
    81 }
    82 
    83 /* Send a BLIP request containing the string in the textfield */
    84 - (IBAction)sendText:(id)sender 
    85 {
    86     BLIPRequest *r = [_connection request];
    87     r.bodyString = [sender stringValue];
    88     BLIPResponse *response = [r send];
    89     response.onComplete = $target(self,gotResponse:);
    90 }
    91 
    92 /* Receive the response to the BLIP request, and put its contents into the response field */
    93 - (void) gotResponse: (BLIPResponse*)response
    94 {
    95     [responseField setObjectValue: response.bodyString];
    96 }    
    97 
    98 
    99 @end
   100 
   101 int main(int argc, char *argv[])
   102 {
   103     return NSApplicationMain(argc,  (const char **) argv);
   104 }