BLIP/BLIPMessage.h
author Jens Alfke <jens@mooseyard.com>
Sun Jul 13 10:59:06 2008 -0700 (2008-07-13)
changeset 23 ffdaa33a408a
parent 16 6f608b552b77
child 26 cb9cdf247239
permissions -rw-r--r--
Merged 1.0 final into 1.1 branch.
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@22
    48
    id _representedObject;
jens@0
    49
};
jens@0
    50
jens@0
    51
/** The BLIPConnection associated with this message. */
jens@0
    52
@property (readonly,retain) BLIPConnection *connection;
jens@0
    53
jens@0
    54
/** This message's serial number in its connection.
jens@0
    55
    A BLIPRequest's number is initially zero, then assigned when it's sent.
jens@0
    56
    A BLIPResponse is automatically assigned the same number as the request it replies to. */
jens@0
    57
@property (readonly) UInt32 number;
jens@0
    58
jens@0
    59
/** Is this a message sent by me (as opposed to the peer)? */
jens@0
    60
@property (readonly) BOOL isMine;
jens@0
    61
jens@0
    62
/** Has this message been sent yet? (Only makes sense when isMe is set.) */
jens@0
    63
@property (readonly) BOOL sent;
jens@0
    64
jens@0
    65
/** Has enough of the message arrived to read its properies? */
jens@0
    66
@property (readonly) BOOL propertiesAvailable;
jens@0
    67
jens@0
    68
/** Has the entire message, including the body, arrived? */
jens@0
    69
@property (readonly) BOOL complete;
jens@0
    70
jens@2
    71
/** Should the message body be compressed with gzip?
jens@2
    72
    This property can only be set <i>before</i> sending the message. */
jens@0
    73
@property BOOL compressed;
jens@0
    74
jens@0
    75
/** Should the message be sent ahead of normal-priority messages?
jens@2
    76
    This property can only be set <i>before</i> sending the message. */
jens@0
    77
@property BOOL urgent;
jens@0
    78
jens@0
    79
/** Can this message be changed? (Only true for outgoing messages, before you send them.) */
jens@0
    80
@property (readonly) BOOL isMutable;
jens@0
    81
jens@0
    82
/** The message body, a blob of arbitrary data. */
jens@0
    83
@property (copy) NSData *body;
jens@0
    84
jens@0
    85
/** Appends data to the body. */
jens@0
    86
- (void) addToBody: (NSData*)data;
jens@0
    87
jens@3
    88
/** The message body as an NSString.
jens@3
    89
    The UTF-8 character encoding is used to convert. */
jens@3
    90
@property (copy) NSString *bodyString;
jens@3
    91
jens@22
    92
/** An arbitrary object that you can associate with this message for your own purposes.
jens@22
    93
    The message retains it, but doesn't do anything else with it. */
jens@22
    94
@property (retain) id representedObject;
jens@22
    95
jens@0
    96
#pragma mark PROPERTIES:
jens@0
    97
jens@2
    98
/** The message's properties, a dictionary-like object.
jens@2
    99
    Message properties are much like the headers in HTTP, MIME and RFC822. */
jens@0
   100
@property (readonly) BLIPProperties* properties;
jens@0
   101
jens@0
   102
/** Mutable version of the message's properties; only available if this mesage is mutable. */
jens@0
   103
@property (readonly) BLIPMutableProperties* mutableProperties;
jens@0
   104
jens@0
   105
/** The value of the "Content-Type" property, which is by convention the MIME type of the body. */
jens@0
   106
@property (copy) NSString *contentType;
jens@0
   107
jens@0
   108
/** The value of the "Profile" property, which by convention identifies the purpose of the message. */
jens@0
   109
@property (copy) NSString *profile;
jens@0
   110
jens@0
   111
/** A shortcut to get the value of a property. */
jens@0
   112
- (NSString*) valueOfProperty: (NSString*)property;
jens@0
   113
jens@0
   114
/** A shortcut to set the value of a property. A nil value deletes that property. */
jens@0
   115
- (void) setValue: (NSString*)value ofProperty: (NSString*)property;
jens@0
   116
jens@0
   117
/** Similar to -description, but also shows the properties and their values. */
jens@0
   118
@property (readonly) NSString* descriptionWithProperties;
jens@0
   119
jens@0
   120
jens@0
   121
@end