BLIP/BLIPConnection.m
author Jens Alfke <jens@mooseyard.com>
Fri May 30 13:54:38 2008 -0700 (2008-05-30)
changeset 9 980beba83fb7
parent 2 9fdd8dba529c
child 18 3be241de1630
permissions -rw-r--r--
Fixed a serious bug - a race condition where a data buffer in the writer's queue could be dealloced (not the NSData, but its bytes themselves) before the writer sent it, resulting in an EFAULT error.
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@0
    18
jens@0
    19
jens@0
    20
NSString* const BLIPErrorDomain = @"BLIP";
jens@0
    21
jens@0
    22
NSError *BLIPMakeError( int errorCode, NSString *message, ... )
jens@0
    23
{
jens@0
    24
    va_list args;
jens@0
    25
    va_start(args,message);
jens@0
    26
    message = [[NSString alloc] initWithFormat: message arguments: args];
jens@0
    27
    va_end(args);
jens@0
    28
    LogTo(BLIP,@"BLIPError #%i: %@",errorCode,message);
jens@0
    29
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject: message
jens@0
    30
                                                         forKey: NSLocalizedDescriptionKey];
jens@0
    31
    [message release];
jens@0
    32
    return [NSError errorWithDomain: BLIPErrorDomain code: errorCode userInfo: userInfo];
jens@0
    33
}
jens@0
    34
jens@0
    35
jens@0
    36
jens@0
    37
jens@0
    38
@implementation BLIPConnection
jens@0
    39
jens@0
    40
- (void) dealloc
jens@0
    41
{
jens@0
    42
    [_dispatcher release];
jens@0
    43
    [super dealloc];
jens@0
    44
}
jens@0
    45
jens@0
    46
- (Class) readerClass                                       {return [BLIPReader class];}
jens@0
    47
- (Class) writerClass                                       {return [BLIPWriter class];}
jens@0
    48
- (id<BLIPConnectionDelegate>) delegate                     {return (id)_delegate;}
jens@0
    49
- (void) setDelegate: (id<BLIPConnectionDelegate>)delegate  {_delegate = delegate;}
jens@0
    50
jens@0
    51
- (BLIPDispatcher*) dispatcher
jens@0
    52
{
jens@0
    53
    if( ! _dispatcher ) {
jens@0
    54
        _dispatcher = [[BLIPDispatcher alloc] init];
jens@0
    55
        _dispatcher.parent = ((BLIPListener*)self.server).dispatcher;
jens@0
    56
    }
jens@0
    57
    return _dispatcher;
jens@0
    58
}
jens@0
    59
jens@0
    60
jens@0
    61
- (void) _dispatchRequest: (BLIPRequest*)request
jens@0
    62
{
jens@0
    63
    LogTo(BLIP,@"Received all of %@",request.descriptionWithProperties);
jens@0
    64
    @try{
jens@0
    65
        if( ! [self.dispatcher dispatchMessage: request] )
jens@0
    66
            [self tellDelegate: @selector(connection:receivedRequest:) withObject: request];
jens@0
    67
        if( ! request.noReply && ! request.repliedTo ) {
jens@0
    68
            LogTo(BLIP,@"Returning default empty response to %@",request);
jens@2
    69
            [request respondWithData: nil contentType: nil];
jens@0
    70
        }
jens@0
    71
    }@catch( NSException *x ) {
jens@0
    72
        MYReportException(x,@"Dispatching BLIP request");
jens@0
    73
        [request respondWithException: x];
jens@0
    74
    }
jens@0
    75
}
jens@0
    76
jens@0
    77
- (void) _dispatchResponse: (BLIPResponse*)response
jens@0
    78
{
jens@0
    79
    LogTo(BLIP,@"Received all of %@",response);
jens@0
    80
    [self tellDelegate: @selector(connection:receivedResponse:) withObject: response];
jens@0
    81
}
jens@0
    82
jens@0
    83
jens@5
    84
- (BLIPRequest*) request
jens@0
    85
{
jens@5
    86
    return [[[BLIPRequest alloc] _initWithConnection: self body: nil properties: nil] autorelease];
jens@0
    87
}
jens@0
    88
jens@0
    89
- (BLIPRequest*) requestWithBody: (NSData*)body
jens@0
    90
                      properties: (NSDictionary*)properties
jens@0
    91
{
jens@0
    92
    return [[[BLIPRequest alloc] _initWithConnection: self body: body properties: properties] autorelease];
jens@0
    93
}
jens@0
    94
jens@0
    95
- (BLIPResponse*) sendRequest: (BLIPRequest*)request
jens@0
    96
{
jens@0
    97
    BLIPConnection *itsConnection = request.connection;
jens@0
    98
    if( itsConnection==nil )
jens@0
    99
        request.connection = self;
jens@0
   100
    else
jens@0
   101
        Assert(itsConnection==self,@"%@ is already assigned to a different BLIPConnection",request);
jens@0
   102
    return [request send];
jens@0
   103
}
jens@0
   104
jens@0
   105
jens@0
   106
@end
jens@0
   107
jens@0
   108
jens@0
   109
jens@0
   110
jens@0
   111
@implementation BLIPListener
jens@0
   112
jens@0
   113
- (id) initWithPort: (UInt16)port
jens@0
   114
{
jens@0
   115
    self = [super initWithPort: port];
jens@0
   116
    if (self != nil) {
jens@0
   117
        self.connectionClass = [BLIPConnection class];
jens@0
   118
    }
jens@0
   119
    return self;
jens@0
   120
}
jens@0
   121
jens@0
   122
- (void) dealloc
jens@0
   123
{
jens@0
   124
    [_dispatcher release];
jens@0
   125
    [super dealloc];
jens@0
   126
}
jens@0
   127
jens@0
   128
- (BLIPDispatcher*) dispatcher
jens@0
   129
{
jens@0
   130
    if( ! _dispatcher )
jens@0
   131
        _dispatcher = [[BLIPDispatcher alloc] init];
jens@0
   132
    return _dispatcher;
jens@0
   133
}
jens@0
   134
jens@0
   135
@end
jens@0
   136
jens@0
   137
jens@0
   138
/*
jens@0
   139
 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
jens@0
   140
 
jens@0
   141
 Redistribution and use in source and binary forms, with or without modification, are permitted
jens@0
   142
 provided that the following conditions are met:
jens@0
   143
 
jens@0
   144
 * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@0
   145
 and the following disclaimer.
jens@0
   146
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
jens@0
   147
 and the following disclaimer in the documentation and/or other materials provided with the
jens@0
   148
 distribution.
jens@0
   149
 
jens@0
   150
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@0
   151
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@0
   152
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@0
   153
 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@0
   154
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@0
   155
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@0
   156
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@0
   157
 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@0
   158
 */