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