1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/BLIP/BLIPConnection.m Sat May 24 13:26:02 2008 -0700
1.3 @@ -0,0 +1,158 @@
1.4 +//
1.5 +// BLIPConnection.m
1.6 +// MYNetwork
1.7 +//
1.8 +// Created by Jens Alfke on 5/10/08.
1.9 +// Copyright 2008 Jens Alfke. All rights reserved.
1.10 +//
1.11 +
1.12 +#import "BLIPConnection.h"
1.13 +#import "BLIP_Internal.h"
1.14 +#import "BLIPReader.h"
1.15 +#import "BLIPWriter.h"
1.16 +#import "BLIPDispatcher.h"
1.17 +
1.18 +#import "Logging.h"
1.19 +#import "Test.h"
1.20 +#import "ExceptionUtils.h"
1.21 +
1.22 +
1.23 +NSString* const BLIPErrorDomain = @"BLIP";
1.24 +
1.25 +NSError *BLIPMakeError( int errorCode, NSString *message, ... )
1.26 +{
1.27 + va_list args;
1.28 + va_start(args,message);
1.29 + message = [[NSString alloc] initWithFormat: message arguments: args];
1.30 + va_end(args);
1.31 + LogTo(BLIP,@"BLIPError #%i: %@",errorCode,message);
1.32 + NSDictionary *userInfo = [NSDictionary dictionaryWithObject: message
1.33 + forKey: NSLocalizedDescriptionKey];
1.34 + [message release];
1.35 + return [NSError errorWithDomain: BLIPErrorDomain code: errorCode userInfo: userInfo];
1.36 +}
1.37 +
1.38 +
1.39 +
1.40 +
1.41 +@implementation BLIPConnection
1.42 +
1.43 +- (void) dealloc
1.44 +{
1.45 + [_dispatcher release];
1.46 + [super dealloc];
1.47 +}
1.48 +
1.49 +- (Class) readerClass {return [BLIPReader class];}
1.50 +- (Class) writerClass {return [BLIPWriter class];}
1.51 +- (id<BLIPConnectionDelegate>) delegate {return (id)_delegate;}
1.52 +- (void) setDelegate: (id<BLIPConnectionDelegate>)delegate {_delegate = delegate;}
1.53 +
1.54 +- (BLIPDispatcher*) dispatcher
1.55 +{
1.56 + if( ! _dispatcher ) {
1.57 + _dispatcher = [[BLIPDispatcher alloc] init];
1.58 + _dispatcher.parent = ((BLIPListener*)self.server).dispatcher;
1.59 + }
1.60 + return _dispatcher;
1.61 +}
1.62 +
1.63 +
1.64 +- (void) _dispatchRequest: (BLIPRequest*)request
1.65 +{
1.66 + LogTo(BLIP,@"Received all of %@",request.descriptionWithProperties);
1.67 + @try{
1.68 + if( ! [self.dispatcher dispatchMessage: request] )
1.69 + [self tellDelegate: @selector(connection:receivedRequest:) withObject: request];
1.70 + if( ! request.noReply && ! request.repliedTo ) {
1.71 + LogTo(BLIP,@"Returning default empty response to %@",request);
1.72 + [request respondWithData: nil];
1.73 + }
1.74 + }@catch( NSException *x ) {
1.75 + MYReportException(x,@"Dispatching BLIP request");
1.76 + [request respondWithException: x];
1.77 + }
1.78 +}
1.79 +
1.80 +- (void) _dispatchResponse: (BLIPResponse*)response
1.81 +{
1.82 + LogTo(BLIP,@"Received all of %@",response);
1.83 + [self tellDelegate: @selector(connection:receivedResponse:) withObject: response];
1.84 +}
1.85 +
1.86 +
1.87 +- (BLIPRequest*) requestWithBody: (NSData*)body
1.88 +{
1.89 + return [[[BLIPRequest alloc] _initWithConnection: self body: body properties: nil] autorelease];
1.90 +}
1.91 +
1.92 +- (BLIPRequest*) requestWithBody: (NSData*)body
1.93 + properties: (NSDictionary*)properties
1.94 +{
1.95 + return [[[BLIPRequest alloc] _initWithConnection: self body: body properties: properties] autorelease];
1.96 +}
1.97 +
1.98 +- (BLIPResponse*) sendRequest: (BLIPRequest*)request
1.99 +{
1.100 + BLIPConnection *itsConnection = request.connection;
1.101 + if( itsConnection==nil )
1.102 + request.connection = self;
1.103 + else
1.104 + Assert(itsConnection==self,@"%@ is already assigned to a different BLIPConnection",request);
1.105 + return [request send];
1.106 +}
1.107 +
1.108 +
1.109 +@end
1.110 +
1.111 +
1.112 +
1.113 +
1.114 +@implementation BLIPListener
1.115 +
1.116 +- (id) initWithPort: (UInt16)port
1.117 +{
1.118 + self = [super initWithPort: port];
1.119 + if (self != nil) {
1.120 + self.connectionClass = [BLIPConnection class];
1.121 + }
1.122 + return self;
1.123 +}
1.124 +
1.125 +- (void) dealloc
1.126 +{
1.127 + [_dispatcher release];
1.128 + [super dealloc];
1.129 +}
1.130 +
1.131 +- (BLIPDispatcher*) dispatcher
1.132 +{
1.133 + if( ! _dispatcher )
1.134 + _dispatcher = [[BLIPDispatcher alloc] init];
1.135 + return _dispatcher;
1.136 +}
1.137 +
1.138 +@end
1.139 +
1.140 +
1.141 +/*
1.142 + Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
1.143 +
1.144 + Redistribution and use in source and binary forms, with or without modification, are permitted
1.145 + provided that the following conditions are met:
1.146 +
1.147 + * Redistributions of source code must retain the above copyright notice, this list of conditions
1.148 + and the following disclaimer.
1.149 + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
1.150 + and the following disclaimer in the documentation and/or other materials provided with the
1.151 + distribution.
1.152 +
1.153 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
1.154 + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
1.155 + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
1.156 + BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1.157 + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1.158 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1.159 + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
1.160 + THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.161 + */