BLIP/Demo/BLIPEchoClient.m
author Jens Alfke <jens@mooseyard.com>
Sun Jul 13 10:42:50 2008 -0700 (2008-07-13)
changeset 22 8b883753394a
parent 4 2bd9d60a2c46
child 26 cb9cdf247239
permissions -rw-r--r--
* Fixed: Responses still pending when a connection closed were not calling their onComplete targets.
* Fixed: BLIPTestClient target failed to build because it didn't link against zlib.
* If TCPListener.bonjourServiceName is changed while the listener is open, it now re-publishes the service with the new name.
* Added a TCPListener.bonjourService property.
* Added a BLIPMessage.representedObject property.
* Fixed a memory leak.
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@3
    12
#import "BLIP.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 NSNetServiceBrowser delegate methods
jens@3
    31
jens@3
    32
// We broadcast the willChangeValueForKey: and didChangeValueForKey: for the NSTableView binding to work.
jens@3
    33
jens@3
    34
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
jens@3
    35
    if (![_serviceList containsObject:aNetService]) {
jens@3
    36
        [self willChangeValueForKey:@"serviceList"];
jens@3
    37
        [_serviceList addObject:aNetService];
jens@3
    38
        [self didChangeValueForKey:@"serviceList"];
jens@3
    39
    }
jens@3
    40
}
jens@3
    41
jens@3
    42
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing {
jens@3
    43
    if ([_serviceList containsObject:aNetService]) {
jens@3
    44
        [self willChangeValueForKey:@"serviceList"];
jens@3
    45
        [_serviceList removeObject:aNetService];
jens@3
    46
        [self didChangeValueForKey:@"serviceList"];
jens@3
    47
    }
jens@3
    48
}
jens@3
    49
jens@3
    50
#pragma mark -
jens@7
    51
#pragma mark BLIPConnection support
jens@3
    52
jens@7
    53
/* Opens a BLIP connection to the given address. */
jens@7
    54
- (void)openConnection: (NSNetService*)service 
jens@3
    55
{
jens@7
    56
    _connection = [[BLIPConnection alloc] initToNetService: service];
jens@7
    57
    if( _connection )
jens@7
    58
        [_connection open];
jens@7
    59
    else
jens@7
    60
        NSBeep();
jens@3
    61
}
jens@3
    62
jens@7
    63
/* Closes the currently open BLIP connection. */
jens@7
    64
- (void)closeConnection
jens@3
    65
{
jens@7
    66
    [_connection close];
jens@7
    67
    [_connection release];
jens@7
    68
    _connection = nil;
jens@3
    69
}
jens@3
    70
jens@3
    71
#pragma mark -
jens@3
    72
#pragma mark GUI action methods
jens@3
    73
jens@3
    74
- (IBAction)serverClicked:(id)sender {
jens@3
    75
    NSTableView * table = (NSTableView *)sender;
jens@3
    76
    int selectedRow = [table selectedRow];
jens@3
    77
    
jens@3
    78
    [self closeConnection];
jens@7
    79
    if (-1 != selectedRow)
jens@7
    80
        [self openConnection: [_serviceList objectAtIndex:selectedRow]];
jens@3
    81
}
jens@3
    82
jens@4
    83
/* Send a BLIP request containing the string in the textfield */
jens@3
    84
- (IBAction)sendText:(id)sender 
jens@3
    85
{
jens@7
    86
    BLIPRequest *r = [_connection request];
jens@3
    87
    r.bodyString = [sender stringValue];
jens@3
    88
    BLIPResponse *response = [r send];
jens@3
    89
    response.onComplete = $target(self,gotResponse:);
jens@3
    90
}
jens@3
    91
jens@4
    92
/* Receive the response to the BLIP request, and put its contents into the response field */
jens@3
    93
- (void) gotResponse: (BLIPResponse*)response
jens@3
    94
{
jens@3
    95
    [responseField setObjectValue: response.bodyString];
jens@3
    96
}    
jens@3
    97
jens@3
    98
jens@3
    99
@end
jens@3
   100
jens@3
   101
int main(int argc, char *argv[])
jens@3
   102
{
jens@3
   103
    return NSApplicationMain(argc,  (const char **) argv);
jens@3
   104
}