BLIP/BLIPMessage.h
author Jens Alfke <jens@mooseyard.com>
Sun Jul 13 10:42:50 2008 -0700 (2008-07-13)
changeset 22 8b883753394a
parent 16 6f608b552b77
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.
     1 //
     2 //  BLIPMessage.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 <Foundation/Foundation.h>
    10 @class BLIPConnection, BLIPProperties, BLIPMutableProperties;
    11 
    12 
    13 /** NSError domain and codes for BLIP */
    14 extern NSString* const BLIPErrorDomain;
    15 enum {
    16     kBLIPError_BadData = 1,
    17     kBLIPError_BadFrame,
    18     kBLIPError_Disconnected,
    19     kBLIPError_PeerNotAllowed,
    20     
    21     kBLIPError_Misc = 99,
    22     
    23     // errors returned in responses:
    24     kBLIPError_BadRequest = 400,
    25     kBLIPError_Forbidden = 403,
    26     kBLIPError_NotFound = 404,
    27     kBLIPError_BadRange = 416,
    28     
    29     kBLIPError_HandlerFailed = 501,
    30     kBLIPError_Unspecified = 599            // peer didn't send any detailed error info
    31 };
    32 
    33 NSError *BLIPMakeError( int errorCode, NSString *message, ... ) __attribute__ ((format (__NSString__, 2, 3)));
    34 
    35 
    36 /** Abstract superclass for <a href=".#blipdesc">BLIP</a> requests and responses. */
    37 @interface BLIPMessage : NSObject 
    38 {
    39     BLIPConnection *_connection;
    40     UInt16 _flags;
    41     UInt32 _number;
    42     BLIPProperties *_properties;
    43     NSData *_body;
    44     NSMutableData *_encodedBody;
    45     NSMutableData *_mutableBody;
    46     BOOL _isMine, _isMutable, _sent, _propertiesAvailable, _complete;
    47     SInt32 _bytesWritten;
    48     id _representedObject;
    49 };
    50 
    51 /** The BLIPConnection associated with this message. */
    52 @property (readonly,retain) BLIPConnection *connection;
    53 
    54 /** This message's serial number in its connection.
    55     A BLIPRequest's number is initially zero, then assigned when it's sent.
    56     A BLIPResponse is automatically assigned the same number as the request it replies to. */
    57 @property (readonly) UInt32 number;
    58 
    59 /** Is this a message sent by me (as opposed to the peer)? */
    60 @property (readonly) BOOL isMine;
    61 
    62 /** Has this message been sent yet? (Only makes sense when isMe is set.) */
    63 @property (readonly) BOOL sent;
    64 
    65 /** Has enough of the message arrived to read its properies? */
    66 @property (readonly) BOOL propertiesAvailable;
    67 
    68 /** Has the entire message, including the body, arrived? */
    69 @property (readonly) BOOL complete;
    70 
    71 /** Should the message body be compressed with gzip?
    72     This property can only be set <i>before</i> sending the message. */
    73 @property BOOL compressed;
    74 
    75 /** Should the message be sent ahead of normal-priority messages?
    76     This property can only be set <i>before</i> sending the message. */
    77 @property BOOL urgent;
    78 
    79 /** Can this message be changed? (Only true for outgoing messages, before you send them.) */
    80 @property (readonly) BOOL isMutable;
    81 
    82 /** The message body, a blob of arbitrary data. */
    83 @property (copy) NSData *body;
    84 
    85 /** Appends data to the body. */
    86 - (void) addToBody: (NSData*)data;
    87 
    88 /** The message body as an NSString.
    89     The UTF-8 character encoding is used to convert. */
    90 @property (copy) NSString *bodyString;
    91 
    92 /** An arbitrary object that you can associate with this message for your own purposes.
    93     The message retains it, but doesn't do anything else with it. */
    94 @property (retain) id representedObject;
    95 
    96 #pragma mark PROPERTIES:
    97 
    98 /** The message's properties, a dictionary-like object.
    99     Message properties are much like the headers in HTTP, MIME and RFC822. */
   100 @property (readonly) BLIPProperties* properties;
   101 
   102 /** Mutable version of the message's properties; only available if this mesage is mutable. */
   103 @property (readonly) BLIPMutableProperties* mutableProperties;
   104 
   105 /** The value of the "Content-Type" property, which is by convention the MIME type of the body. */
   106 @property (copy) NSString *contentType;
   107 
   108 /** The value of the "Profile" property, which by convention identifies the purpose of the message. */
   109 @property (copy) NSString *profile;
   110 
   111 /** A shortcut to get the value of a property. */
   112 - (NSString*) valueOfProperty: (NSString*)property;
   113 
   114 /** A shortcut to set the value of a property. A nil value deletes that property. */
   115 - (void) setValue: (NSString*)value ofProperty: (NSString*)property;
   116 
   117 /** Similar to -description, but also shows the properties and their values. */
   118 @property (readonly) NSString* descriptionWithProperties;
   119 
   120 
   121 @end