jens@0
|
1 |
//
|
jens@0
|
2 |
// BLIPMessage.m
|
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 "BLIPMessage.h"
|
jens@0
|
10 |
#import "BLIP_Internal.h"
|
jens@0
|
11 |
#import "BLIPReader.h"
|
jens@0
|
12 |
#import "BLIPWriter.h"
|
jens@0
|
13 |
|
jens@0
|
14 |
#import "ExceptionUtils.h"
|
jens@0
|
15 |
#import "Target.h"
|
jens@0
|
16 |
|
jens@0
|
17 |
// From Google Toolbox For Mac <http://code.google.com/p/google-toolbox-for-mac/>
|
jens@0
|
18 |
#import "GTMNSData+zlib.h"
|
jens@0
|
19 |
|
jens@0
|
20 |
|
jens@0
|
21 |
@implementation BLIPMessage
|
jens@0
|
22 |
|
jens@0
|
23 |
|
jens@0
|
24 |
- (id) _initWithConnection: (BLIPConnection*)connection
|
jens@0
|
25 |
isMine: (BOOL)isMine
|
jens@0
|
26 |
flags: (BLIPMessageFlags)flags
|
jens@0
|
27 |
number: (UInt32)msgNo
|
jens@0
|
28 |
body: (NSData*)body
|
jens@0
|
29 |
{
|
jens@0
|
30 |
self = [super init];
|
jens@0
|
31 |
if (self != nil) {
|
jens@0
|
32 |
_connection = [connection retain];
|
jens@0
|
33 |
_isMine = isMine;
|
jens@0
|
34 |
_flags = flags;
|
jens@0
|
35 |
_number = msgNo;
|
jens@0
|
36 |
if( isMine ) {
|
jens@0
|
37 |
_body = body.copy;
|
jens@0
|
38 |
_properties = [[BLIPMutableProperties alloc] init];
|
jens@0
|
39 |
_propertiesAvailable = YES;
|
jens@0
|
40 |
} else {
|
jens@0
|
41 |
_encodedBody = body.mutableCopy;
|
jens@0
|
42 |
}
|
jens@0
|
43 |
LogTo(BLIPVerbose,@"INIT %@",self);
|
jens@0
|
44 |
}
|
jens@0
|
45 |
return self;
|
jens@0
|
46 |
}
|
jens@0
|
47 |
|
jens@0
|
48 |
- (void) dealloc
|
jens@0
|
49 |
{
|
jens@0
|
50 |
LogTo(BLIPVerbose,@"DEALLOC %@",self);
|
jens@0
|
51 |
[_properties release];
|
jens@0
|
52 |
[_encodedBody release];
|
jens@0
|
53 |
[_mutableBody release];
|
jens@0
|
54 |
[_body release];
|
jens@0
|
55 |
[_connection release];
|
jens@0
|
56 |
[super dealloc];
|
jens@0
|
57 |
}
|
jens@0
|
58 |
|
jens@0
|
59 |
|
jens@0
|
60 |
- (NSString*) description
|
jens@0
|
61 |
{
|
jens@0
|
62 |
NSUInteger length = (_body.length ?: _mutableBody.length) ?: _encodedBody.length;
|
jens@0
|
63 |
NSMutableString *desc = [NSMutableString stringWithFormat: @"%@[#%u, %u bytes",
|
jens@0
|
64 |
self.class,_number, length];
|
jens@0
|
65 |
if( _flags & kBLIP_Compressed ) {
|
jens@0
|
66 |
if( _encodedBody && _encodedBody.length != length )
|
jens@0
|
67 |
[desc appendFormat: @" (%u gzipped)", _encodedBody.length];
|
jens@0
|
68 |
else
|
jens@0
|
69 |
[desc appendString: @", gzipped"];
|
jens@0
|
70 |
}
|
jens@0
|
71 |
if( _flags & kBLIP_Urgent )
|
jens@0
|
72 |
[desc appendString: @", urgent"];
|
jens@0
|
73 |
if( _flags & kBLIP_NoReply )
|
jens@0
|
74 |
[desc appendString: @", noreply"];
|
jens@0
|
75 |
[desc appendString: @"]"];
|
jens@0
|
76 |
return desc;
|
jens@0
|
77 |
}
|
jens@0
|
78 |
|
jens@0
|
79 |
- (NSString*) descriptionWithProperties
|
jens@0
|
80 |
{
|
jens@0
|
81 |
NSMutableString *desc = (NSMutableString*)self.description;
|
jens@0
|
82 |
[desc appendFormat: @" %@", self.properties.allProperties];
|
jens@0
|
83 |
return desc;
|
jens@0
|
84 |
}
|
jens@0
|
85 |
|
jens@0
|
86 |
|
jens@0
|
87 |
#pragma mark -
|
jens@0
|
88 |
#pragma mark PROPERTIES & METADATA:
|
jens@0
|
89 |
|
jens@0
|
90 |
|
jens@0
|
91 |
@synthesize connection=_connection, number=_number, isMine=_isMine, isMutable=_isMutable,
|
jens@0
|
92 |
_bytesWritten, sent=_sent, propertiesAvailable=_propertiesAvailable, complete=_complete;
|
jens@0
|
93 |
|
jens@0
|
94 |
|
jens@0
|
95 |
- (void) _setFlag: (BLIPMessageFlags)flag value: (BOOL)value
|
jens@0
|
96 |
{
|
jens@0
|
97 |
Assert(_isMine && _isMutable);
|
jens@0
|
98 |
if( value )
|
jens@0
|
99 |
_flags |= flag;
|
jens@0
|
100 |
else
|
jens@0
|
101 |
_flags &= ~flag;
|
jens@0
|
102 |
}
|
jens@0
|
103 |
|
jens@0
|
104 |
- (BOOL) compressed {return (_flags & kBLIP_Compressed) != 0;}
|
jens@0
|
105 |
- (BOOL) urgent {return (_flags & kBLIP_Urgent) != 0;}
|
jens@0
|
106 |
- (void) setCompressed: (BOOL)compressed {[self _setFlag: kBLIP_Compressed value: compressed];}
|
jens@0
|
107 |
- (void) setUrgent: (BOOL)high {[self _setFlag: kBLIP_Urgent value: high];}
|
jens@0
|
108 |
|
jens@0
|
109 |
|
jens@0
|
110 |
- (NSData*) body
|
jens@0
|
111 |
{
|
jens@0
|
112 |
if( ! _body && _isMine )
|
jens@0
|
113 |
return [[_mutableBody copy] autorelease];
|
jens@0
|
114 |
else
|
jens@0
|
115 |
return _body;
|
jens@0
|
116 |
}
|
jens@0
|
117 |
|
jens@0
|
118 |
- (void) setBody: (NSData*)body
|
jens@0
|
119 |
{
|
jens@0
|
120 |
Assert(_isMine && _isMutable);
|
jens@0
|
121 |
if( _mutableBody )
|
jens@0
|
122 |
[_mutableBody setData: body];
|
jens@0
|
123 |
else
|
jens@0
|
124 |
_mutableBody = [body mutableCopy];
|
jens@0
|
125 |
}
|
jens@0
|
126 |
|
jens@0
|
127 |
- (void) _addToBody: (NSData*)data
|
jens@0
|
128 |
{
|
jens@0
|
129 |
if( data.length ) {
|
jens@0
|
130 |
if( _mutableBody )
|
jens@0
|
131 |
[_mutableBody appendData: data];
|
jens@0
|
132 |
else
|
jens@0
|
133 |
_mutableBody = [data mutableCopy];
|
jens@0
|
134 |
setObj(&_body,nil);
|
jens@0
|
135 |
}
|
jens@0
|
136 |
}
|
jens@0
|
137 |
|
jens@0
|
138 |
- (void) addToBody: (NSData*)data
|
jens@0
|
139 |
{
|
jens@0
|
140 |
Assert(_isMine && _isMutable);
|
jens@0
|
141 |
[self _addToBody: data];
|
jens@0
|
142 |
}
|
jens@0
|
143 |
|
jens@0
|
144 |
|
jens@0
|
145 |
- (BLIPProperties*) properties
|
jens@0
|
146 |
{
|
jens@0
|
147 |
return _properties;
|
jens@0
|
148 |
}
|
jens@0
|
149 |
|
jens@0
|
150 |
- (BLIPMutableProperties*) mutableProperties
|
jens@0
|
151 |
{
|
jens@0
|
152 |
Assert(_isMine && _isMutable);
|
jens@0
|
153 |
return (BLIPMutableProperties*)_properties;
|
jens@0
|
154 |
}
|
jens@0
|
155 |
|
jens@0
|
156 |
- (NSString*) valueOfProperty: (NSString*)property
|
jens@0
|
157 |
{
|
jens@0
|
158 |
return [_properties valueOfProperty: property];
|
jens@0
|
159 |
}
|
jens@0
|
160 |
|
jens@0
|
161 |
- (void) setValue: (NSString*)value ofProperty: (NSString*)property
|
jens@0
|
162 |
{
|
jens@0
|
163 |
[self.mutableProperties setValue: value ofProperty: property];
|
jens@0
|
164 |
}
|
jens@0
|
165 |
|
jens@0
|
166 |
- (NSString*) contentType {return [_properties valueOfProperty: @"Content-Type"];}
|
jens@0
|
167 |
- (void) setContentType: (NSString*)t {[self setValue: t ofProperty: @"Content-Type"];}
|
jens@0
|
168 |
- (NSString*) profile {return [_properties valueOfProperty: @"Profile"];}
|
jens@0
|
169 |
- (void) setProfile: (NSString*)p {[self setValue: p ofProperty: @"Profile"];}
|
jens@0
|
170 |
|
jens@0
|
171 |
|
jens@0
|
172 |
#pragma mark -
|
jens@0
|
173 |
#pragma mark I/O:
|
jens@0
|
174 |
|
jens@0
|
175 |
|
jens@0
|
176 |
- (void) _encode
|
jens@0
|
177 |
{
|
jens@0
|
178 |
Assert(_isMine && _isMutable);
|
jens@0
|
179 |
_isMutable = NO;
|
jens@0
|
180 |
|
jens@0
|
181 |
BLIPProperties *oldProps = _properties;
|
jens@0
|
182 |
_properties = [oldProps copy];
|
jens@0
|
183 |
[oldProps release];
|
jens@0
|
184 |
|
jens@0
|
185 |
_encodedBody = [_properties.encodedData mutableCopy];
|
jens@0
|
186 |
Assert(_encodedBody.length>=2);
|
jens@0
|
187 |
|
jens@0
|
188 |
NSData *body = _body ?: _mutableBody;
|
jens@0
|
189 |
NSUInteger length = body.length;
|
jens@0
|
190 |
if( length > 0 ) {
|
jens@0
|
191 |
if( self.compressed ) {
|
jens@0
|
192 |
body = [NSData gtm_dataByGzippingData: body compressionLevel: 5];
|
jens@0
|
193 |
LogTo(BLIPVerbose,@"Compressed %@ to %u bytes (%.0f%%)", self,body.length,
|
jens@0
|
194 |
body.length*100.0/length);
|
jens@0
|
195 |
}
|
jens@0
|
196 |
[_encodedBody appendData: body];
|
jens@0
|
197 |
}
|
jens@0
|
198 |
}
|
jens@0
|
199 |
|
jens@0
|
200 |
|
jens@0
|
201 |
- (void) _assignedNumber: (UInt32)number
|
jens@0
|
202 |
{
|
jens@0
|
203 |
Assert(_number==0,@"%@ has already been sent",self);
|
jens@0
|
204 |
_number = number;
|
jens@0
|
205 |
_isMutable = NO;
|
jens@0
|
206 |
}
|
jens@0
|
207 |
|
jens@0
|
208 |
|
jens@0
|
209 |
- (BOOL) _writeFrameTo: (BLIPWriter*)writer maxSize: (UInt16)maxSize
|
jens@0
|
210 |
{
|
jens@0
|
211 |
Assert(_number!=0);
|
jens@0
|
212 |
Assert(_isMine);
|
jens@0
|
213 |
Assert(_encodedBody);
|
jens@0
|
214 |
if( _bytesWritten==0 )
|
jens@0
|
215 |
LogTo(BLIP,@"Now sending %@",self);
|
jens@0
|
216 |
ssize_t lengthToWrite = _encodedBody.length - _bytesWritten;
|
jens@0
|
217 |
if( lengthToWrite <= 0 && _bytesWritten > 0 )
|
jens@0
|
218 |
return NO; // done
|
jens@0
|
219 |
Assert(maxSize > sizeof(BLIPFrameHeader));
|
jens@0
|
220 |
maxSize -= sizeof(BLIPFrameHeader);
|
jens@0
|
221 |
UInt16 flags = _flags;
|
jens@0
|
222 |
if( lengthToWrite > maxSize ) {
|
jens@0
|
223 |
lengthToWrite = maxSize;
|
jens@0
|
224 |
flags |= kBLIP_MoreComing;
|
jens@0
|
225 |
LogTo(BLIPVerbose,@"%@ pushing frame, bytes %u-%u", self, _bytesWritten, _bytesWritten+lengthToWrite);
|
jens@0
|
226 |
} else {
|
jens@0
|
227 |
flags &= ~kBLIP_MoreComing;
|
jens@0
|
228 |
LogTo(BLIPVerbose,@"%@ pushing frame, bytes %u-%u (finished)", self, _bytesWritten, _bytesWritten+lengthToWrite);
|
jens@0
|
229 |
}
|
jens@0
|
230 |
|
jens@0
|
231 |
// First write the frame header:
|
jens@0
|
232 |
BLIPFrameHeader header = { EndianU32_NtoB(kBLIPFrameHeaderMagicNumber),
|
jens@0
|
233 |
EndianU32_NtoB(_number),
|
jens@0
|
234 |
EndianU16_NtoB(flags),
|
jens@0
|
235 |
EndianU16_NtoB(sizeof(BLIPFrameHeader) + lengthToWrite) };
|
jens@0
|
236 |
|
jens@0
|
237 |
[writer writeData: [NSData dataWithBytes: &header length: sizeof(header)]];
|
jens@0
|
238 |
|
jens@0
|
239 |
// Then write the body:
|
jens@0
|
240 |
if( lengthToWrite > 0 ) {
|
jens@0
|
241 |
[writer writeData: [NSData dataWithBytesNoCopy: (UInt8*)_encodedBody.bytes + _bytesWritten
|
jens@0
|
242 |
length: lengthToWrite
|
jens@0
|
243 |
freeWhenDone: NO]];
|
jens@0
|
244 |
_bytesWritten += lengthToWrite;
|
jens@0
|
245 |
}
|
jens@0
|
246 |
return (flags & kBLIP_MoreComing) != 0;
|
jens@0
|
247 |
}
|
jens@0
|
248 |
|
jens@0
|
249 |
|
jens@0
|
250 |
- (BOOL) _receivedFrameWithHeader: (const BLIPFrameHeader*)header body: (NSData*)body
|
jens@0
|
251 |
{
|
jens@0
|
252 |
Assert(!_isMine);
|
jens@0
|
253 |
AssertEq(header->number,_number);
|
jens@0
|
254 |
Assert(_flags & kBLIP_MoreComing);
|
jens@0
|
255 |
|
jens@0
|
256 |
BLIPMessageType frameType = (header->flags & kBLIP_TypeMask), curType = (_flags & kBLIP_TypeMask);
|
jens@0
|
257 |
if( frameType != curType ) {
|
jens@0
|
258 |
Assert(curType==kBLIP_RPY && frameType==kBLIP_ERR && _mutableBody.length==0,
|
jens@0
|
259 |
@"Incoming frame's type %i doesn't match %@",frameType,self);
|
jens@0
|
260 |
_flags = (_flags & ~kBLIP_TypeMask) | frameType;
|
jens@0
|
261 |
}
|
jens@0
|
262 |
|
jens@0
|
263 |
if( _encodedBody )
|
jens@0
|
264 |
[_encodedBody appendData: body];
|
jens@0
|
265 |
else
|
jens@0
|
266 |
_encodedBody = [body mutableCopy];
|
jens@0
|
267 |
LogTo(BLIPVerbose,@"%@ rcvd bytes %u-%u", self, _encodedBody.length-body.length, _encodedBody.length);
|
jens@0
|
268 |
|
jens@0
|
269 |
if( ! _properties ) {
|
jens@0
|
270 |
// Try to extract the properties:
|
jens@0
|
271 |
ssize_t usedLength;
|
jens@0
|
272 |
setObj(&_properties, [BLIPProperties propertiesWithEncodedData: _encodedBody usedLength: &usedLength]);
|
jens@0
|
273 |
if( _properties ) {
|
jens@0
|
274 |
[_encodedBody replaceBytesInRange: NSMakeRange(0,usedLength)
|
jens@0
|
275 |
withBytes: NULL length: 0];
|
jens@0
|
276 |
} else if( usedLength < 0 )
|
jens@0
|
277 |
return NO;
|
jens@0
|
278 |
self.propertiesAvailable = YES;
|
jens@0
|
279 |
}
|
jens@0
|
280 |
|
jens@0
|
281 |
if( ! (header->flags & kBLIP_MoreComing) ) {
|
jens@0
|
282 |
// After last frame, decode the data:
|
jens@0
|
283 |
_flags &= ~kBLIP_MoreComing;
|
jens@0
|
284 |
if( ! _properties )
|
jens@0
|
285 |
return NO;
|
jens@0
|
286 |
unsigned encodedLength = _encodedBody.length;
|
jens@0
|
287 |
if( self.compressed && encodedLength>0 ) {
|
jens@0
|
288 |
_body = [[NSData gtm_dataByInflatingData: _encodedBody] copy];
|
jens@0
|
289 |
if( ! _body )
|
jens@0
|
290 |
return NO;
|
jens@0
|
291 |
LogTo(BLIPVerbose,@"Uncompressed %@ from %u bytes (%.1fx)", self, encodedLength,
|
jens@0
|
292 |
_body.length/(float)encodedLength);
|
jens@0
|
293 |
} else {
|
jens@0
|
294 |
_body = [_encodedBody copy];
|
jens@0
|
295 |
}
|
jens@0
|
296 |
setObj(&_encodedBody,nil);
|
jens@0
|
297 |
self.propertiesAvailable = self.complete = YES;
|
jens@0
|
298 |
}
|
jens@0
|
299 |
return YES;
|
jens@0
|
300 |
}
|
jens@0
|
301 |
|
jens@0
|
302 |
|
jens@0
|
303 |
- (void) _connectionClosed
|
jens@0
|
304 |
{
|
jens@0
|
305 |
if( _isMine ) {
|
jens@0
|
306 |
_bytesWritten = 0;
|
jens@0
|
307 |
_flags |= kBLIP_MoreComing;
|
jens@0
|
308 |
}
|
jens@0
|
309 |
}
|
jens@0
|
310 |
|
jens@0
|
311 |
|
jens@0
|
312 |
@end
|
jens@0
|
313 |
|
jens@0
|
314 |
|
jens@0
|
315 |
/*
|
jens@0
|
316 |
Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
|
jens@0
|
317 |
|
jens@0
|
318 |
Redistribution and use in source and binary forms, with or without modification, are permitted
|
jens@0
|
319 |
provided that the following conditions are met:
|
jens@0
|
320 |
|
jens@0
|
321 |
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
jens@0
|
322 |
and the following disclaimer.
|
jens@0
|
323 |
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
|
jens@0
|
324 |
and the following disclaimer in the documentation and/or other materials provided with the
|
jens@0
|
325 |
distribution.
|
jens@0
|
326 |
|
jens@0
|
327 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
jens@0
|
328 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
jens@0
|
329 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
|
jens@0
|
330 |
BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
jens@0
|
331 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
jens@0
|
332 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
jens@0
|
333 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
jens@0
|
334 |
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
jens@0
|
335 |
*/
|