BLIP/BLIPRequest.h
author morrowa
Tue Jun 23 12:46:40 2009 -0700 (2009-06-23)
changeset 53 e9f209a24d53
parent 5 2c4bb6968927
permissions -rw-r--r--
Connections opened by listeners now close correctly.
     1 //
     2 //  BLIPRequest.h
     3 //  MYNetwork
     4 //
     5 //  Created by Jens Alfke on 5/22/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "BLIPMessage.h"
    10 @class BLIPResponse, MYTarget;
    11 
    12 
    13 /** A Request, or initiating message, in the <a href=".#blipdesc">BLIP</a> protocol. */
    14 @interface BLIPRequest : BLIPMessage <NSMutableCopying>
    15 {
    16     @private
    17     BLIPResponse *_response;
    18 }
    19 
    20 /** Creates an outgoing request.
    21     The body may be nil.
    22     The request is not associated with any BLIPConnection yet, so you must either set its
    23     connection property before calling -send, or pass the request as a parameter to
    24     -[BLIPConnection sendRequest:]. */
    25 + (BLIPRequest*) requestWithBody: (NSData*)body;
    26 
    27 /** Creates an outgoing request.
    28     This is just like requestWithBody: except that you supply a string. */
    29 + (BLIPRequest*) requestWithBodyString: (NSString*)bodyString;
    30 
    31 /** Creates an outgoing request.
    32     The body or properties may be nil.
    33     The request is not associated with any BLIPConnection yet, so you must either set its
    34     connection property before calling -send, or pass the request as a parameter to
    35     -[BLIPConnection sendRequest:]. */
    36 + (BLIPRequest*) requestWithBody: (NSData*)body
    37                       properties: (NSDictionary*)properties;
    38 
    39 /** BLIPRequest extends the -connection property to be settable.
    40     This allows a request to be created without a connection (i.e. before the connection is created).
    41     It can later be sent by setting the connection property and calling -send. */
    42 @property (retain) BLIPConnection *connection;
    43 
    44 /** Sends this request over its connection.
    45     (Actually, the connection queues it to be sent; this method always returns immediately.)
    46     Its matching response object will be returned, or nil if the request couldn't be sent.
    47     If this request has not been assigned to a connection, an exception will be raised. */
    48 - (BLIPResponse*) send;
    49 
    50 /** Does this request not need a response?
    51     This property can only be set before sending the request. */
    52 @property BOOL noReply;
    53 
    54 /** Returns YES if you've replied to this request (by accessing its -response property.) */
    55 @property (readonly) BOOL repliedTo;
    56 
    57 /** The request's response. This can be accessed at any time, even before sending the request,
    58     but the contents of the response won't be filled in until it arrives, of course. */
    59 @property (readonly) BLIPResponse *response;
    60 
    61 /** Call this when a request arrives, to indicate that you want to respond to it later.
    62     It will prevent a default empty response from being sent upon return from the request handler. */
    63 - (void) deferResponse;
    64 
    65 /** Shortcut to respond to this request with the given data.
    66     The contentType, if not nil, is stored in the "Content-Type" property. */
    67 - (void) respondWithData: (NSData*)data contentType: (NSString*)contentType;
    68 
    69 /** Shortcut to respond to this request with the given string (which will be encoded in UTF-8). */
    70 - (void) respondWithString: (NSString*)string;
    71 
    72 /** Shortcut to respond to this request with an error. */
    73 - (void) respondWithError: (NSError*)error;
    74 
    75 /** Shortcut to respond to this request with the given error code and message.
    76     The BLIPErrorDomain is assumed. */
    77 - (void) respondWithErrorCode: (int)code message: (NSString*)message; //, ... __attribute__ ((format (__NSString__, 2,3)));;
    78 
    79 /** Shortcut to respond to this message with an error indicating that an exception occurred. */
    80 - (void) respondWithException: (NSException*)exception;
    81 
    82 @end
    83 
    84 
    85 
    86 
    87 /** A reply to a BLIPRequest, in the <a href=".#blipdesc">BLIP</a> protocol. */
    88 @interface BLIPResponse : BLIPMessage
    89 {
    90     @private
    91     NSError *_error;
    92     MYTarget *_onComplete;
    93 }
    94 
    95 /** Sends this response. */
    96 - (BOOL) send;
    97 
    98 /** The error returned by the peer, or nil if the response is successful. */
    99 @property (retain) NSError* error;
   100 
   101 /** Sets a target/action to be called when an incoming response is complete.
   102     Use this on the response returned from -[BLIPRequest send], to be notified when the response is available. */
   103 @property (retain) MYTarget *onComplete;
   104 
   105 
   106 @end