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