BLIP/Demo/BLIPEchoServer.m
author Jens Alfke <jens@mooseyard.com>
Sun May 25 13:43:03 2008 -0700 (2008-05-25)
changeset 7 5936db2c1987
child 26 cb9cdf247239
permissions -rw-r--r--
Added -[TCPConnection initToNetService:] to make it easier to use with Bonjour. This allowed me to simplify BLIPEchoClient quite a lot.
jens@3
     1
//
jens@3
     2
//  BLIPEchoServer.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
//
jens@3
     8
jens@3
     9
#import "BLIPEchoServer.h"
jens@3
    10
#import "BLIP.h"
jens@3
    11
jens@3
    12
jens@3
    13
@implementation BLIPEchoServer
jens@3
    14
jens@3
    15
jens@3
    16
- (id) init
jens@3
    17
{
jens@3
    18
    self = [super init];
jens@3
    19
    if (self != nil) {
jens@3
    20
        _listener = [[BLIPListener alloc] initWithPort: 12345];
jens@3
    21
        _listener.delegate = self;
jens@3
    22
        _listener.pickAvailablePort = YES;
jens@3
    23
        _listener.bonjourServiceType = @"_blipecho._tcp";
jens@3
    24
        [_listener open];
jens@3
    25
        NSLog(@"%@ is listening...",self);
jens@3
    26
    }
jens@3
    27
    return self;
jens@3
    28
}
jens@3
    29
jens@3
    30
- (void) dealloc
jens@3
    31
{
jens@3
    32
    [_listener close];
jens@3
    33
    [_listener release];
jens@3
    34
    [super dealloc];
jens@3
    35
}
jens@3
    36
jens@3
    37
- (void) listener: (TCPListener*)listener failedToOpen: (NSError*)error
jens@3
    38
{
jens@3
    39
    NSLog(@"** %@ failed to open: %@",self,error);
jens@3
    40
}
jens@3
    41
jens@3
    42
- (void) listener: (TCPListener*)listener didAcceptConnection: (TCPConnection*)connection
jens@3
    43
{
jens@3
    44
    NSLog(@"** %@ accepted %@",self,connection);
jens@3
    45
    connection.delegate = self;
jens@3
    46
}
jens@3
    47
jens@3
    48
- (void) connection: (TCPConnection*)connection failedToOpen: (NSError*)error
jens@3
    49
{
jens@3
    50
    NSLog(@"** %@ failedToOpen: %@",connection,error);
jens@3
    51
}
jens@3
    52
jens@3
    53
- (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request
jens@3
    54
{
jens@3
    55
    NSLog(@"***** %@ received %@",connection,request);
jens@3
    56
    [request respondWithData: request.body contentType: request.contentType];
jens@3
    57
}
jens@3
    58
jens@3
    59
jens@3
    60
@end
jens@3
    61
jens@3
    62
jens@3
    63
int main( int argc, const char **argv )
jens@3
    64
{
jens@3
    65
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
jens@3
    66
    BLIPEchoServer *listener = [[BLIPEchoServer alloc] init];
jens@3
    67
    [[NSRunLoop currentRunLoop] run];
jens@3
    68
    [listener release];
jens@3
    69
    [pool drain];
jens@3
    70
}