# HG changeset patch # User Jens Alfke # Date 1248469588 25200 # Node ID 5e4855a592eee76f03db695b85805947fd2a3ea5 # Parent 8713f2d6a4c5bceffa53d32d98c5e0f087135011 * The BLIPConnection receivedRequest: delegate method now returns BOOL. If the method returns NO (or if the method isn't implemented in the delegate), that means it didn't handle the message at all; an error will be returned to the sender. * If the connection closes unexpectedly due to an error, then the auto-generated responses to pending requests will contain that error. This makes it easier to display a meaningful error message in the handler for the request. diff -r 8713f2d6a4c5 -r 5e4855a592ee BLIP/BLIPConnection.h --- a/BLIP/BLIPConnection.h Tue Jul 21 15:06:15 2009 -0700 +++ b/BLIP/BLIPConnection.h Fri Jul 24 14:06:28 2009 -0700 @@ -66,11 +66,13 @@ /** Called when a BLIPRequest is received from the peer, if there is no BLIPDispatcher rule to handle it. + If the delegate wants to accept the request it should return YES; if it returns NO, + a kBLIPError_NotFound error will be returned to the sender. The delegate should get the request's response object, fill in its data and properties or error property, and send it. If it doesn't explicitly send a response, a default empty one will be sent; to prevent this, call -deferResponse on the request if you want to send a response later. */ -- (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request; +- (BOOL) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request; /** Called when a BLIPResponse (to one of your requests) is received from the peer. This is called after the response object's onComplete target, if any, is invoked.*/ diff -r 8713f2d6a4c5 -r 5e4855a592ee BLIP/BLIPConnection.m --- a/BLIP/BLIPConnection.m Tue Jul 21 15:06:15 2009 -0700 +++ b/BLIP/BLIPConnection.m Fri Jul 24 14:06:28 2009 -0700 @@ -69,13 +69,14 @@ } -- (void) _dispatchMetaRequest: (BLIPRequest*)request +- (BOOL) _dispatchMetaRequest: (BLIPRequest*)request { NSString* profile = request.profile; - if( [profile isEqualToString: kBLIPProfile_Bye] ) + if( [profile isEqualToString: kBLIPProfile_Bye] ) { [self _handleCloseRequest: request]; - else - [request respondWithErrorCode: kBLIPError_NotFound message: @"Unknown meta profile"]; + return YES; + } + return NO; } @@ -83,11 +84,19 @@ { LogTo(BLIP,@"Received all of %@",request.descriptionWithProperties); @try{ + BOOL handled; if( request._flags & kBLIP_Meta ) - [self _dispatchMetaRequest: request]; - else if( ! [self.dispatcher dispatchMessage: request] ) - [self tellDelegate: @selector(connection:receivedRequest:) withObject: request]; - if( ! request.noReply && ! request.repliedTo ) { + handled =[self _dispatchMetaRequest: request]; + else { + handled = [self.dispatcher dispatchMessage: request]; + if (!handled && [_delegate respondsToSelector: @selector(connection:receivedRequest:)]) + handled = [_delegate connection: self receivedRequest: request]; + } + + if (!handled) { + LogTo(BLIP,@"No handler found for incoming %@",request); + [request respondWithErrorCode: kBLIPError_NotFound message: @"No handler was found"]; + } else if( ! request.noReply && ! request.repliedTo ) { LogTo(BLIP,@"Returning default empty response to %@",request); [request respondWithData: nil contentType: nil]; } diff -r 8713f2d6a4c5 -r 5e4855a592ee BLIP/BLIPRequest.m --- a/BLIP/BLIPRequest.m Tue Jul 21 15:06:15 2009 -0700 +++ b/BLIP/BLIPRequest.m Fri Jul 24 14:06:28 2009 -0700 @@ -271,13 +271,17 @@ { [super _connectionClosed]; if( !_isMine && !_complete ) { + NSError *error = _connection.error; + if (!error) + error = BLIPMakeError(kBLIPError_Disconnected, + @"Connection closed before response was received"); // Change incoming response to an error: _isMutable = YES; [_properties autorelease]; _properties = [_properties mutableCopy]; - [self _setError: BLIPMakeError(kBLIPError_Disconnected, - @"Connection closed before response was received")]; + [self _setError: error]; _isMutable = NO; + self.complete = YES; // Calls onComplete target } } diff -r 8713f2d6a4c5 -r 5e4855a592ee BLIP/BLIPTest.m --- a/BLIP/BLIPTest.m Tue Jul 21 15:06:15 2009 -0700 +++ b/BLIP/BLIPTest.m Fri Jul 24 14:06:28 2009 -0700 @@ -127,6 +127,18 @@ { if( _conn.status==kTCP_Open || _conn.status==kTCP_Opening ) { if(_pending.count<100) { + Log(@"** Sending a message that will fail to be handled..."); + BLIPRequest *q = [_conn requestWithBody: nil + properties: $dict({@"Profile", @"BLIPTest/DontHandleMe"}, + {@"User-Agent", @"BLIPConnectionTester"}, + {@"Date", [[NSDate date] description]})]; + BLIPResponse *response = [q send]; + Assert(response); + Assert(q.number>0); + Assert(response.number==q.number); + [_pending setObject: [NSNull null] forKey: $object(q.number)]; + response.onComplete = $target(self,responseArrived:); + Log(@"** Sending another %i messages...", kNBatchedMessages); for( int i=0; i