Updating ignore patterns.
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 "TCP_Internal.h"
12 #import "BLIPReader.h"
13 #import "BLIPWriter.h"
14 #import "BLIPDispatcher.h"
18 #import "ExceptionUtils.h"
22 NSString* const BLIPErrorDomain = @"BLIP";
24 NSError *BLIPMakeError( int errorCode, NSString *message, ... )
27 va_start(args,message);
28 message = [[NSString alloc] initWithFormat: message arguments: args];
30 LogTo(BLIP,@"BLIPError #%i: %@",errorCode,message);
31 NSDictionary *userInfo = [NSDictionary dictionaryWithObject: message
32 forKey: NSLocalizedDescriptionKey];
34 return [NSError errorWithDomain: BLIPErrorDomain code: errorCode userInfo: userInfo];
38 @interface BLIPConnection ()
39 - (void) _handleCloseRequest: (BLIPRequest*)request;
43 @implementation BLIPConnection
48 [_dispatcher release];
52 - (Class) readerClass {return [BLIPReader class];}
53 - (Class) writerClass {return [BLIPWriter class];}
54 - (id<BLIPConnectionDelegate>) delegate {return (id)_delegate;}
55 - (void) setDelegate: (id<BLIPConnectionDelegate>)delegate {_delegate = delegate;}
59 #pragma mark RECEIVING:
62 - (BLIPDispatcher*) dispatcher
65 _dispatcher = [[BLIPDispatcher alloc] init];
66 _dispatcher.parent = ((BLIPListener*)self.server).dispatcher;
72 - (void) _dispatchMetaRequest: (BLIPRequest*)request
74 NSString* profile = request.profile;
75 if( [profile isEqualToString: kBLIPProfile_Bye] )
76 [self _handleCloseRequest: request];
78 [request respondWithErrorCode: kBLIPError_NotFound message: @"Unknown meta profile"];
82 - (void) _dispatchRequest: (BLIPRequest*)request
84 LogTo(BLIP,@"Received all of %@",request.descriptionWithProperties);
86 if( request._flags & kBLIP_Meta )
87 [self _dispatchMetaRequest: request];
88 else if( ! [self.dispatcher dispatchMessage: request] )
89 [self tellDelegate: @selector(connection:receivedRequest:) withObject: request];
90 if( ! request.noReply && ! request.repliedTo ) {
91 LogTo(BLIP,@"Returning default empty response to %@",request);
92 [request respondWithData: nil contentType: nil];
94 }@catch( NSException *x ) {
95 MYReportException(x,@"Dispatching BLIP request");
96 [request respondWithException: x];
100 - (void) _dispatchResponse: (BLIPResponse*)response
102 LogTo(BLIP,@"Received all of %@",response);
103 [self tellDelegate: @selector(connection:receivedResponse:) withObject: response];
108 #pragma mark SENDING:
111 - (BLIPRequest*) request
113 return [[[BLIPRequest alloc] _initWithConnection: self body: nil properties: nil] autorelease];
116 - (BLIPRequest*) requestWithBody: (NSData*)body
117 properties: (NSDictionary*)properties
119 return [[[BLIPRequest alloc] _initWithConnection: self body: body properties: properties] autorelease];
122 - (BLIPResponse*) sendRequest: (BLIPRequest*)request
124 if (!request.isMine || request.sent) {
125 // This was an incoming request that I'm being asked to forward or echo;
126 // or it's an outgoing request being sent to multiple connections.
127 // Since a particular BLIPRequest can only be sent once, make a copy of it to send:
128 request = [[request mutableCopy] autorelease];
130 BLIPConnection *itsConnection = request.connection;
131 if( itsConnection==nil )
132 request.connection = self;
134 Assert(itsConnection==self,@"%@ is already assigned to a different BLIPConnection",request);
135 return [request send];
140 #pragma mark CLOSING:
145 // Override of TCPConnection method. Instead of closing the socket, send a 'bye' request:
146 if( ! _blipClosing ) {
147 LogTo(BLIPVerbose,@"Sending close request...");
148 BLIPRequest *r = [self request];
149 [r _setFlag: kBLIP_Meta value: YES];
150 r.profile = kBLIPProfile_Bye;
151 BLIPResponse *response = [r send];
152 response.onComplete = $target(self,_receivedCloseResponse:);
154 // Put the writer in close mode, to prevent client from sending any more requests:
158 - (void) _receivedCloseResponse: (BLIPResponse*)response
160 NSError *error = response.error;
161 LogTo(BLIPVerbose,@"Received close response: error=%@",error);
164 [self tellDelegate: @selector(connection:closeRequestFailedWithError:) withObject: error];
166 // Now finally close the socket:
172 - (void) _handleCloseRequest: (BLIPRequest*)request
174 LogTo(BLIPVerbose,@"Received a close request");
175 if( [_delegate respondsToSelector: @selector(connectionReceivedCloseRequest:)] )
176 if( ! [_delegate connectionReceivedCloseRequest: self] ) {
177 LogTo(BLIPVerbose,@"Responding with denial of close request");
178 [request respondWithErrorCode: kBLIPError_Forbidden message: @"Close request denied"];
182 LogTo(BLIPVerbose,@"Close request accepted");
183 _blipClosing = YES; // this prevents _beginClose from sending a close request back
194 @implementation BLIPListener
200 self.connectionClass = [BLIPConnection class];
207 [_dispatcher release];
211 - (BLIPDispatcher*) dispatcher
214 _dispatcher = [[BLIPDispatcher alloc] init];
222 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
224 Redistribution and use in source and binary forms, with or without modification, are permitted
225 provided that the following conditions are met:
227 * Redistributions of source code must retain the above copyright notice, this list of conditions
228 and the following disclaimer.
229 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
230 and the following disclaimer in the documentation and/or other materials provided with the
233 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
234 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
235 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
236 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
237 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
238 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
239 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
240 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.