BLIP/BLIPConnection.m
author Jens Alfke <jens@mooseyard.com>
Fri Jul 24 14:06:28 2009 -0700 (2009-07-24)
changeset 63 5e4855a592ee
parent 49 20cccc7c26ee
permissions -rw-r--r--
* 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.
jens@0
     1
//
jens@0
     2
//  BLIPConnection.m
jens@0
     3
//  MYNetwork
jens@0
     4
//
jens@0
     5
//  Created by Jens Alfke on 5/10/08.
jens@0
     6
//  Copyright 2008 Jens Alfke. All rights reserved.
jens@0
     7
//
jens@0
     8
jens@0
     9
#import "BLIPConnection.h"
jens@0
    10
#import "BLIP_Internal.h"
jens@19
    11
#import "TCP_Internal.h"
jens@0
    12
#import "BLIPReader.h"
jens@0
    13
#import "BLIPWriter.h"
jens@0
    14
#import "BLIPDispatcher.h"
jens@0
    15
jens@0
    16
#import "Logging.h"
jens@0
    17
#import "Test.h"
jens@0
    18
#import "ExceptionUtils.h"
jens@18
    19
#import "Target.h"
jens@0
    20
jens@0
    21
jens@0
    22
NSString* const BLIPErrorDomain = @"BLIP";
jens@0
    23
jens@0
    24
NSError *BLIPMakeError( int errorCode, NSString *message, ... )
jens@0
    25
{
jens@0
    26
    va_list args;
jens@0
    27
    va_start(args,message);
jens@0
    28
    message = [[NSString alloc] initWithFormat: message arguments: args];
jens@0
    29
    va_end(args);
jens@0
    30
    LogTo(BLIP,@"BLIPError #%i: %@",errorCode,message);
jens@0
    31
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject: message
jens@0
    32
                                                         forKey: NSLocalizedDescriptionKey];
jens@0
    33
    [message release];
jens@0
    34
    return [NSError errorWithDomain: BLIPErrorDomain code: errorCode userInfo: userInfo];
jens@0
    35
}
jens@0
    36
jens@0
    37
jens@18
    38
@interface BLIPConnection ()
jens@18
    39
- (void) _handleCloseRequest: (BLIPRequest*)request;
jens@18
    40
@end
jens@0
    41
jens@0
    42
jens@0
    43
@implementation BLIPConnection
jens@0
    44
jens@18
    45
jens@0
    46
- (void) dealloc
jens@0
    47
{
jens@0
    48
    [_dispatcher release];
jens@0
    49
    [super dealloc];
jens@0
    50
}
jens@0
    51
jens@0
    52
- (Class) readerClass                                       {return [BLIPReader class];}
jens@0
    53
- (Class) writerClass                                       {return [BLIPWriter class];}
jens@0
    54
- (id<BLIPConnectionDelegate>) delegate                     {return (id)_delegate;}
jens@0
    55
- (void) setDelegate: (id<BLIPConnectionDelegate>)delegate  {_delegate = delegate;}
jens@0
    56
jens@18
    57
jens@18
    58
#pragma mark -
jens@18
    59
#pragma mark RECEIVING:
jens@18
    60
jens@18
    61
jens@0
    62
- (BLIPDispatcher*) dispatcher
jens@0
    63
{
jens@0
    64
    if( ! _dispatcher ) {
jens@0
    65
        _dispatcher = [[BLIPDispatcher alloc] init];
jens@0
    66
        _dispatcher.parent = ((BLIPListener*)self.server).dispatcher;
jens@0
    67
    }
jens@0
    68
    return _dispatcher;
jens@0
    69
}
jens@0
    70
jens@0
    71
jens@63
    72
- (BOOL) _dispatchMetaRequest: (BLIPRequest*)request
jens@18
    73
{
jens@18
    74
    NSString* profile = request.profile;
jens@63
    75
    if( [profile isEqualToString: kBLIPProfile_Bye] ) {
jens@18
    76
        [self _handleCloseRequest: request];
jens@63
    77
        return YES;
jens@63
    78
    }
jens@63
    79
    return NO;
jens@18
    80
}
jens@18
    81
jens@18
    82
jens@0
    83
- (void) _dispatchRequest: (BLIPRequest*)request
jens@0
    84
{
jens@0
    85
    LogTo(BLIP,@"Received all of %@",request.descriptionWithProperties);
jens@0
    86
    @try{
jens@63
    87
        BOOL handled;
jens@18
    88
        if( request._flags & kBLIP_Meta )
jens@63
    89
            handled =[self _dispatchMetaRequest: request];
jens@63
    90
        else {
jens@63
    91
            handled = [self.dispatcher dispatchMessage: request];
jens@63
    92
            if (!handled && [_delegate respondsToSelector: @selector(connection:receivedRequest:)])
jens@63
    93
                handled = [_delegate connection: self receivedRequest: request];
jens@63
    94
        }
jens@63
    95
        
jens@63
    96
        if (!handled) {
jens@63
    97
            LogTo(BLIP,@"No handler found for incoming %@",request);
jens@63
    98
            [request respondWithErrorCode: kBLIPError_NotFound message: @"No handler was found"];
jens@63
    99
        } else if( ! request.noReply && ! request.repliedTo ) {
jens@0
   100
            LogTo(BLIP,@"Returning default empty response to %@",request);
jens@2
   101
            [request respondWithData: nil contentType: nil];
jens@0
   102
        }
jens@0
   103
    }@catch( NSException *x ) {
jens@0
   104
        MYReportException(x,@"Dispatching BLIP request");
jens@0
   105
        [request respondWithException: x];
jens@0
   106
    }
jens@0
   107
}
jens@0
   108
jens@0
   109
- (void) _dispatchResponse: (BLIPResponse*)response
jens@0
   110
{
jens@0
   111
    LogTo(BLIP,@"Received all of %@",response);
jens@0
   112
    [self tellDelegate: @selector(connection:receivedResponse:) withObject: response];
jens@0
   113
}
jens@0
   114
jens@0
   115
jens@18
   116
#pragma mark -
jens@18
   117
#pragma mark SENDING:
jens@18
   118
jens@18
   119
jens@5
   120
- (BLIPRequest*) request
jens@0
   121
{
jens@5
   122
    return [[[BLIPRequest alloc] _initWithConnection: self body: nil properties: nil] autorelease];
jens@0
   123
}
jens@0
   124
jens@0
   125
- (BLIPRequest*) requestWithBody: (NSData*)body
jens@0
   126
                      properties: (NSDictionary*)properties
jens@0
   127
{
jens@0
   128
    return [[[BLIPRequest alloc] _initWithConnection: self body: body properties: properties] autorelease];
jens@0
   129
}
jens@0
   130
jens@0
   131
- (BLIPResponse*) sendRequest: (BLIPRequest*)request
jens@0
   132
{
jens@49
   133
    if (!request.isMine || request.sent) {
jens@49
   134
        // This was an incoming request that I'm being asked to forward or echo;
jens@49
   135
        // or it's an outgoing request being sent to multiple connections.
jens@49
   136
        // Since a particular BLIPRequest can only be sent once, make a copy of it to send:
jens@49
   137
        request = [[request mutableCopy] autorelease];
jens@49
   138
    }
jens@0
   139
    BLIPConnection *itsConnection = request.connection;
jens@0
   140
    if( itsConnection==nil )
jens@0
   141
        request.connection = self;
jens@0
   142
    else
jens@0
   143
        Assert(itsConnection==self,@"%@ is already assigned to a different BLIPConnection",request);
jens@0
   144
    return [request send];
jens@0
   145
}
jens@0
   146
jens@0
   147
jens@18
   148
#pragma mark -
jens@18
   149
#pragma mark CLOSING:
jens@18
   150
jens@18
   151
jens@18
   152
- (void) _beginClose
jens@18
   153
{
jens@18
   154
    // Override of TCPConnection method. Instead of closing the socket, send a 'bye' request:
jens@18
   155
    if( ! _blipClosing ) {
jens@18
   156
        LogTo(BLIPVerbose,@"Sending close request...");
jens@18
   157
        BLIPRequest *r = [self request];
jens@18
   158
        [r _setFlag: kBLIP_Meta value: YES];
jens@18
   159
        r.profile = kBLIPProfile_Bye;
jens@18
   160
        BLIPResponse *response = [r send];
jens@18
   161
        response.onComplete = $target(self,_receivedCloseResponse:);
jens@18
   162
    }
jens@18
   163
    // Put the writer in close mode, to prevent client from sending any more requests:
jens@18
   164
    [self.writer close];
jens@18
   165
}
jens@18
   166
jens@18
   167
- (void) _receivedCloseResponse: (BLIPResponse*)response
jens@18
   168
{
jens@18
   169
    NSError *error = response.error;
jens@18
   170
    LogTo(BLIPVerbose,@"Received close response: error=%@",error);
jens@18
   171
    if( error ) {
jens@19
   172
        [self _unclose];
jens@19
   173
        [self tellDelegate: @selector(connection:closeRequestFailedWithError:) withObject: error];
jens@18
   174
    } else {
jens@18
   175
        // Now finally close the socket:
jens@18
   176
        [super _beginClose];
jens@18
   177
    }
jens@18
   178
}
jens@18
   179
jens@18
   180
jens@18
   181
- (void) _handleCloseRequest: (BLIPRequest*)request
jens@18
   182
{
jens@18
   183
    LogTo(BLIPVerbose,@"Received a close request");
jens@18
   184
    if( [_delegate respondsToSelector: @selector(connectionReceivedCloseRequest:)] )
jens@18
   185
        if( ! [_delegate connectionReceivedCloseRequest: self] ) {
jens@18
   186
            LogTo(BLIPVerbose,@"Responding with denial of close request");
jens@18
   187
            [request respondWithErrorCode: kBLIPError_Forbidden message: @"Close request denied"];
jens@18
   188
            return;
jens@18
   189
        }
jens@18
   190
    
jens@18
   191
    LogTo(BLIPVerbose,@"Close request accepted");
jens@18
   192
    _blipClosing = YES; // this prevents _beginClose from sending a close request back
jens@18
   193
    [self close];
jens@18
   194
}
jens@18
   195
jens@18
   196
jens@0
   197
@end
jens@0
   198
jens@0
   199
jens@0
   200
jens@0
   201
jens@18
   202
#pragma mark -
jens@0
   203
@implementation BLIPListener
jens@0
   204
jens@49
   205
- (id) init
jens@0
   206
{
jens@49
   207
    self = [super init];
jens@0
   208
    if (self != nil) {
jens@0
   209
        self.connectionClass = [BLIPConnection class];
jens@0
   210
    }
jens@0
   211
    return self;
jens@0
   212
}
jens@0
   213
jens@0
   214
- (void) dealloc
jens@0
   215
{
jens@0
   216
    [_dispatcher release];
jens@0
   217
    [super dealloc];
jens@0
   218
}
jens@0
   219
jens@0
   220
- (BLIPDispatcher*) dispatcher
jens@0
   221
{
jens@0
   222
    if( ! _dispatcher )
jens@0
   223
        _dispatcher = [[BLIPDispatcher alloc] init];
jens@0
   224
    return _dispatcher;
jens@0
   225
}
jens@0
   226
jens@0
   227
@end
jens@0
   228
jens@0
   229
jens@0
   230
/*
jens@0
   231
 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
jens@0
   232
 
jens@0
   233
 Redistribution and use in source and binary forms, with or without modification, are permitted
jens@0
   234
 provided that the following conditions are met:
jens@0
   235
 
jens@0
   236
 * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@0
   237
 and the following disclaimer.
jens@0
   238
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
jens@0
   239
 and the following disclaimer in the documentation and/or other materials provided with the
jens@0
   240
 distribution.
jens@0
   241
 
jens@0
   242
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@0
   243
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@0
   244
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@0
   245
 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@0
   246
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@0
   247
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@0
   248
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@0
   249
 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@0
   250
 */