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