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