BLIP/BLIPRequest.h
author Jens Alfke <jens@mooseyard.com>
Sun Jul 13 10:52:48 2008 -0700 (2008-07-13)
changeset 20 02224e981209
parent 2 9fdd8dba529c
child 49 20cccc7c26ee
permissions -rw-r--r--
* Fixed link error in BLIPClient target.
* Fixed *Ph*n* #ifdef issue (thanks, Jimmy)
     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
    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     The body or properties may be nil.
    29     The request is not associated with any BLIPConnection yet, so you must either set its
    30     connection property before calling -send, or pass the request as a parameter to
    31     -[BLIPConnection sendRequest:]. */
    32 + (BLIPRequest*) requestWithBody: (NSData*)body
    33                       properties: (NSDictionary*)properties;
    34 
    35 /** BLIPRequest extends the -connection property to be settable.
    36     This allows a request to be created without a connection (i.e. before the connection is created).
    37     It can later be sent by setting the connection property and calling -send. */
    38 @property (retain) BLIPConnection *connection;
    39 
    40 /** Sends this request over its connection.
    41     (Actually, the connection queues it to be sent; this method always returns immediately.)
    42     Its matching response object will be returned, or nil if the request couldn't be sent.
    43     If this request has not been assigned to a connection, an exception will be raised. */
    44 - (BLIPResponse*) send;
    45 
    46 /** Does this request not need a response?
    47     This property can only be set before sending the request. */
    48 @property BOOL noReply;
    49 
    50 /** Returns YES if you've replied to this request (by accessing its -response property.) */
    51 @property (readonly) BOOL repliedTo;
    52 
    53 /** The request's response. This can be accessed at any time, even before sending the request,
    54     but the contents of the response won't be filled in until it arrives, of course. */
    55 @property (readonly) BLIPResponse *response;
    56 
    57 /** Call this when a request arrives, to indicate that you want to respond to it later.
    58     It will prevent a default empty response from being sent upon return from the request handler. */
    59 - (void) deferResponse;
    60 
    61 /** Shortcut to respond to this request with the given data.
    62     The contentType, if not nil, is stored in the "Content-Type" property. */
    63 - (void) respondWithData: (NSData*)data contentType: (NSString*)contentType;
    64 
    65 /** Shortcut to respond to this request with the given string (which will be encoded in UTF-8). */
    66 - (void) respondWithString: (NSString*)string;
    67 
    68 /** Shortcut to respond to this request with an error. */
    69 - (void) respondWithError: (NSError*)error;
    70 
    71 /** Shortcut to respond to this request with the given error code and message.
    72     The BLIPErrorDomain is assumed. */
    73 - (void) respondWithErrorCode: (int)code message: (NSString*)message; //, ... __attribute__ ((format (__NSString__, 2,3)));;
    74 
    75 /** Shortcut to respond to this message with an error indicating that an exception occurred. */
    76 - (void) respondWithException: (NSException*)exception;
    77 
    78 @end
    79 
    80 
    81 
    82 
    83 /** A reply to a BLIPRequest, in the <a href=".#blipdesc">BLIP</a> protocol. */
    84 @interface BLIPResponse : BLIPMessage
    85 {
    86     @private
    87     NSError *_error;
    88     MYTarget *_onComplete;
    89 }
    90 
    91 /** Sends this response. */
    92 - (BOOL) send;
    93 
    94 /** The error returned by the peer, or nil if the response is successful. */
    95 @property (retain) NSError* error;
    96 
    97 /** Sets a target/action to be called when an incoming response is complete.
    98     Use this on the response returned from -[BLIPRequest send], to be notified when the response is available. */
    99 @property (retain) MYTarget *onComplete;
   100 
   101 
   102 @end