Added -[TCPConnection initToNetService:] to make it easier to use with Bonjour. This allowed me to simplify BLIPEchoClient quite a lot.
5 // Created by Jens Alfke on 5/10/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import "BLIPConnection.h"
10 #import "BLIP_Internal.h"
11 #import "BLIPReader.h"
12 #import "BLIPWriter.h"
13 #import "BLIPDispatcher.h"
17 #import "ExceptionUtils.h"
20 NSString* const BLIPErrorDomain = @"BLIP";
22 NSError *BLIPMakeError( int errorCode, NSString *message, ... )
25 va_start(args,message);
26 message = [[NSString alloc] initWithFormat: message arguments: args];
28 LogTo(BLIP,@"BLIPError #%i: %@",errorCode,message);
29 NSDictionary *userInfo = [NSDictionary dictionaryWithObject: message
30 forKey: NSLocalizedDescriptionKey];
32 return [NSError errorWithDomain: BLIPErrorDomain code: errorCode userInfo: userInfo];
38 @implementation BLIPConnection
42 [_dispatcher release];
46 - (Class) readerClass {return [BLIPReader class];}
47 - (Class) writerClass {return [BLIPWriter class];}
48 - (id<BLIPConnectionDelegate>) delegate {return (id)_delegate;}
49 - (void) setDelegate: (id<BLIPConnectionDelegate>)delegate {_delegate = delegate;}
51 - (BLIPDispatcher*) dispatcher
54 _dispatcher = [[BLIPDispatcher alloc] init];
55 _dispatcher.parent = ((BLIPListener*)self.server).dispatcher;
61 - (void) _dispatchRequest: (BLIPRequest*)request
63 LogTo(BLIP,@"Received all of %@",request.descriptionWithProperties);
65 if( ! [self.dispatcher dispatchMessage: request] )
66 [self tellDelegate: @selector(connection:receivedRequest:) withObject: request];
67 if( ! request.noReply && ! request.repliedTo ) {
68 LogTo(BLIP,@"Returning default empty response to %@",request);
69 [request respondWithData: nil contentType: nil];
71 }@catch( NSException *x ) {
72 MYReportException(x,@"Dispatching BLIP request");
73 [request respondWithException: x];
77 - (void) _dispatchResponse: (BLIPResponse*)response
79 LogTo(BLIP,@"Received all of %@",response);
80 [self tellDelegate: @selector(connection:receivedResponse:) withObject: response];
84 - (BLIPRequest*) request
86 return [[[BLIPRequest alloc] _initWithConnection: self body: nil properties: nil] autorelease];
89 - (BLIPRequest*) requestWithBody: (NSData*)body
90 properties: (NSDictionary*)properties
92 return [[[BLIPRequest alloc] _initWithConnection: self body: body properties: properties] autorelease];
95 - (BLIPResponse*) sendRequest: (BLIPRequest*)request
97 BLIPConnection *itsConnection = request.connection;
98 if( itsConnection==nil )
99 request.connection = self;
101 Assert(itsConnection==self,@"%@ is already assigned to a different BLIPConnection",request);
102 return [request send];
111 @implementation BLIPListener
113 - (id) initWithPort: (UInt16)port
115 self = [super initWithPort: port];
117 self.connectionClass = [BLIPConnection class];
124 [_dispatcher release];
128 - (BLIPDispatcher*) dispatcher
131 _dispatcher = [[BLIPDispatcher alloc] init];
139 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
141 Redistribution and use in source and binary forms, with or without modification, are permitted
142 provided that the following conditions are met:
144 * Redistributions of source code must retain the above copyright notice, this list of conditions
145 and the following disclaimer.
146 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
147 and the following disclaimer in the documentation and/or other materials provided with the
150 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
151 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
152 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
153 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
154 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
155 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
156 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
157 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.