BLIP/BLIPConnection.h
author Jens Alfke <jens@mooseyard.com>
Sat May 24 13:26:02 2008 -0700 (2008-05-24)
changeset 1 8267d5c429c4
child 2 9fdd8dba529c
permissions -rw-r--r--
Added #imports of utility headers, so source files will compile without requiring a custom prefix (MYUtilities.pch.)
     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 BLIP 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 @property (readonly) BLIPDispatcher *dispatcher;
    29 
    30 /** Creates an outgoing request, with no properties.
    31     The body may be nil.
    32     To send it, call -send. */
    33 - (BLIPRequest*) requestWithBody: (NSData*)body;
    34 
    35 /** Creates an outgoing request.
    36     The body or properties may be nil.
    37     To send it, call -send. */
    38 - (BLIPRequest*) requestWithBody: (NSData*)body
    39                       properties: (NSDictionary*)properies;
    40 
    41 /** Sends a request over this connection.
    42     (Actually, it queues it to be sent; this method always returns immediately.)
    43     Call this instead of calling -send on the request itself, if the request was created with
    44     +[BLIPRequest requestWithBody:] and hasn't yet been assigned to any connection.
    45     This method will assign it to this connection before sending it.
    46     The request's matching response object will be returned, or nil if the request couldn't be sent. */
    47 - (BLIPResponse*) sendRequest: (BLIPRequest*)request;
    48 @end
    49 
    50 
    51 
    52 /** The delegate messages that BLIPConnection will send,
    53     in addition to the ones inherited from TCPConnectionDelegate. */
    54 @protocol BLIPConnectionDelegate <TCPConnectionDelegate>
    55 
    56 /** Called when a BLIPRequest is received from the peer, if there is no BLIPDispatcher
    57     rule to handle it.
    58     The delegate should get the request's response object, fill in its data and properties
    59     or error property, and send it.
    60     If it doesn't explicitly send a response, a default empty one will be sent;
    61     to prevent this, call -deferResponse on the request if you want to send a response later. */
    62 - (void) connection: (BLIPConnection*)connection receivedRequest: (BLIPRequest*)request;
    63 
    64 @optional
    65 /** Called when a BLIPResponse (to one of your requests) is received from the peer.
    66     This is called <i>after</i> the response object's onComplete target, if any, is invoked. */
    67 - (void) connection: (BLIPConnection*)connection receivedResponse: (BLIPResponse*)response;
    68 @end
    69 
    70 
    71 
    72 
    73 /** A "server" that listens on a TCP socket for incoming BLIP connections and creates
    74     BLIPConnection instances to handle them.
    75     Most of the API is inherited from TCPListener. */
    76 @interface BLIPListener : TCPListener
    77 {
    78     BLIPDispatcher *_dispatcher;
    79 }
    80 
    81 @property (readonly) BLIPDispatcher *dispatcher;
    82 
    83 @end