BLIP/BLIPConnection.h
author Jens Alfke <jens@mooseyard.com>
Wed Jun 04 21:09:45 2008 -0700 (2008-06-04)
changeset 14 bb5faa9995d5
parent 3 76f302097a75
child 18 3be241de1630
permissions -rw-r--r--
Python: Optimized frame sending somewhat (frame buffers are generated on the fly as the socket has room.)
     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     BLIPDispatcher *_dispatcher;
    23 }
    24 
    25 /** The delegate object that will be called when the connection opens, closes or receives messages. */
    26 @property (assign) id<BLIPConnectionDelegate> delegate;
    27 
    28 /** The connection's request dispatcher. By default it's not configured to do anything; but you
    29     can add rules to the dispatcher to call specific target methods based on properties of the
    30     incoming requests.
    31  
    32     Requests that aren't handled by the dispatcher (i.e. all of them, by default) will be
    33     passed to the delegate's connection:receivedRequest: method; or if there's no delegate,
    34     a generic error response will be returned. */
    35 @property (readonly) BLIPDispatcher *dispatcher;
    36 
    37 /** Creates a new, empty outgoing request.
    38     You should add properties and/or body data to the request, before sending it by
    39     calling its -send method. */
    40 - (BLIPRequest*) request;
    41 
    42 /** Creates a new outgoing request.
    43     The body or properties may be nil; you can add additional data or properties by calling
    44     methods on the request itself, before sending it by calling its -send method. */
    45 - (BLIPRequest*) requestWithBody: (NSData*)body
    46                       properties: (NSDictionary*)properies;
    47 
    48 /** Sends a request over this connection.
    49     (Actually, it queues it to be sent; this method always returns immediately.)
    50     Call this instead of calling -send on the request itself, if the request was created with
    51     +[BLIPRequest requestWithBody:] and hasn't yet been assigned to any connection.
    52     This method will assign it to this connection before sending it.
    53     The request's matching response object will be returned, or nil if the request couldn't be sent. */
    54 - (BLIPResponse*) sendRequest: (BLIPRequest*)request;
    55 @end
    56 
    57 
    58 
    59 /** The delegate messages that BLIPConnection will send,
    60     in addition to the ones inherited from TCPConnectionDelegate.
    61     All methods are optional. */
    62 @protocol BLIPConnectionDelegate <TCPConnectionDelegate>
    63 @optional
    64 
    65 /** Called when a BLIPRequest is received from the peer, if there is no BLIPDispatcher
    66     rule to handle it.
    67     The delegate should get the request's response object, fill in its data and properties
    68     or error property, and send it.
    69     If it doesn't explicitly send a response, a default empty one will be sent;
    70     to prevent this, call -deferResponse on the request if you want to send a response later. */
    71 - (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request;
    72 
    73 /** Called when a BLIPResponse (to one of your requests) is received from the peer.
    74     This is called <i>after</i> the response object's onComplete target, if any, is invoked.*/
    75 - (void) connection: (BLIPConnection*)connection receivedResponse: (BLIPResponse*)response;
    76 @end
    77 
    78 
    79 
    80 
    81 /** A "server" that listens on a TCP socket for incoming <a href=".#blipdesc">BLIP</a> connections and creates
    82     BLIPConnection instances to handle them.
    83     Most of the API is inherited from TCPListener. */
    84 @interface BLIPListener : TCPListener
    85 {
    86     BLIPDispatcher *_dispatcher;
    87 }
    88 
    89 /** The default request dispatcher that will be inherited by all BLIPConnections opened by this
    90     listener.
    91     If a connection's own dispatcher doesn't have a rule to match a message, this inherited
    92     dispatcher will be checked next. Only if it fails too will the delegate be called. */
    93 @property (readonly) BLIPDispatcher *dispatcher;
    94 
    95 @end