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