BLIP/BLIPConnection.h
author Jens Alfke <jens@mooseyard.com>
Sun May 10 19:00:50 2009 -0700 (2009-05-10)
changeset 45 8efb48eabd08
parent 19 16454d63d4c2
child 63 5e4855a592ee
permissions -rw-r--r--
Fixed MYAddressLookup to allocate an NSSet, and to send correct KV notifications. (Based on Jim Roepke's patch, but outsourcing the KV grunge to CollectionUtils.)
     1 //
     2 //  BLIPConnection.h
     3 //  MYNetwork
     4 //
     5 //  Created by Jens Alfke on 5/10/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "TCPConnection.h"
    10 #import "TCPListener.h"
    11 @class BLIPRequest, BLIPResponse, BLIPDispatcher;
    12 @protocol BLIPConnectionDelegate;
    13 
    14 
    15 /** Represents a connection to a peer, using the <a href=".#blipdesc">BLIP</a> protocol over a TCP socket.
    16     Outgoing connections are made simply by instantiating a BLIPConnection via -initToAddress:.
    17     Incoming connections are usually set up by a BLIPListener and passed to the listener's
    18     delegate.
    19     Most of the API is inherited from TCPConnection. */
    20 @interface BLIPConnection : TCPConnection
    21 {
    22     @private
    23     BLIPDispatcher *_dispatcher;
    24     BOOL _blipClosing;
    25 }
    26 
    27 /** The delegate object that will be called when the connection opens, closes or receives messages. */
    28 @property (assign) id<BLIPConnectionDelegate> delegate;
    29 
    30 /** The connection's request dispatcher. By default it's not configured to do anything; but you
    31     can add rules to the dispatcher to call specific target methods based on properties of the
    32     incoming requests.
    33  
    34     Requests that aren't handled by the dispatcher (i.e. all of them, by default) will be
    35     passed to the delegate's connection:receivedRequest: method; or if there's no delegate,
    36     a generic error response will be returned. */
    37 @property (readonly) BLIPDispatcher *dispatcher;
    38 
    39 /** Creates a new, empty outgoing request.
    40     You should add properties and/or body data to the request, before sending it by
    41     calling its -send method. */
    42 - (BLIPRequest*) request;
    43 
    44 /** Creates a new outgoing request.
    45     The body or properties may be nil; you can add additional data or properties by calling
    46     methods on the request itself, before sending it by calling its -send method. */
    47 - (BLIPRequest*) requestWithBody: (NSData*)body
    48                       properties: (NSDictionary*)properies;
    49 
    50 /** Sends a request over this connection.
    51     (Actually, it queues it to be sent; this method always returns immediately.)
    52     Call this instead of calling -send on the request itself, if the request was created with
    53     +[BLIPRequest requestWithBody:] and hasn't yet been assigned to any connection.
    54     This method will assign it to this connection before sending it.
    55     The request's matching response object will be returned, or nil if the request couldn't be sent. */
    56 - (BLIPResponse*) sendRequest: (BLIPRequest*)request;
    57 @end
    58 
    59 
    60 
    61 /** The delegate messages that BLIPConnection will send,
    62     in addition to the ones inherited from TCPConnectionDelegate.
    63     All methods are optional. */
    64 @protocol BLIPConnectionDelegate <TCPConnectionDelegate>
    65 @optional
    66 
    67 /** Called when a BLIPRequest is received from the peer, if there is no BLIPDispatcher
    68     rule to handle it.
    69     The delegate should get the request's response object, fill in its data and properties
    70     or error property, and send it.
    71     If it doesn't explicitly send a response, a default empty one will be sent;
    72     to prevent this, call -deferResponse on the request if you want to send a response later. */
    73 - (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request;
    74 
    75 /** Called when a BLIPResponse (to one of your requests) is received from the peer.
    76     This is called <i>after</i> the response object's onComplete target, if any, is invoked.*/
    77 - (void) connection: (BLIPConnection*)connection receivedResponse: (BLIPResponse*)response;
    78 
    79 /** Called when the peer wants to close the connection. Return YES to allow, NO to prevent. */
    80 - (BOOL) connectionReceivedCloseRequest: (BLIPConnection*)connection;
    81 
    82 /** Called if the peer refuses a close request. 
    83     The typical error is kBLIPError_Forbidden. */
    84 - (void) connection: (BLIPConnection*)connection closeRequestFailedWithError: (NSError*)error;
    85 @end
    86 
    87 
    88 
    89 
    90 /** A "server" that listens on a TCP socket for incoming <a href=".#blipdesc">BLIP</a> connections and creates
    91     BLIPConnection instances to handle them.
    92     Most of the API is inherited from TCPListener. */
    93 @interface BLIPListener : TCPListener
    94 {
    95     BLIPDispatcher *_dispatcher;
    96 }
    97 
    98 /** The default request dispatcher that will be inherited by all BLIPConnections opened by this
    99     listener.
   100     If a connection's own dispatcher doesn't have a rule to match a message, this inherited
   101     dispatcher will be checked next. Only if it fails too will the delegate be called. */
   102 @property (readonly) BLIPDispatcher *dispatcher;
   103 
   104 @end