|
Wired Networking
2.0
Objective-C implementation of the Wired 2.0 protocol
|
00001 /* $Id$ */ 00002 00003 /* 00004 * Copyright (c) 2008-2009 Axel Andersson 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00017 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 00020 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00022 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00023 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00024 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00025 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00026 * POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00029 #import <WiredNetworking/NSNumber-WINetworking.h> 00030 #import <WiredNetworking/NSString-WINetworking.h> 00031 #import <WiredNetworking/WIP7Message.h> 00032 #import <WiredNetworking/WIP7Spec.h> 00033 #import <WiredNetworking/WIP7Socket.h> 00034 00035 @implementation WIP7Message 00036 00037 + (void)initialize { 00038 wi_p7_message_debug = true; 00039 } 00040 00041 00042 00043 #pragma mark - 00044 00045 + (id)messageWithName:(NSString *)name spec:(WIP7Spec *)spec { 00046 return [[[self alloc] initWithName:name spec:spec] autorelease]; 00047 } 00048 00049 00050 00051 + (id)messageWithMessage:(wi_p7_message_t *)message spec:(WIP7Spec *)spec { 00052 return [[[self alloc] initWithMessage:message spec:spec] autorelease]; 00053 } 00054 00055 00056 00057 #pragma mark - 00058 00059 - (id)initWithName:(NSString *)name spec:(WIP7Spec *)spec { 00060 wi_string_t *string; 00061 00062 self = [super init]; 00063 00064 string = wi_string_init_with_cstring(wi_string_alloc(), [name UTF8String]); 00065 _message = wi_p7_message_init_with_name(wi_p7_message_alloc(), string, [spec spec]); 00066 wi_release(string); 00067 00068 _name = [name retain]; 00069 _spec = [spec retain]; 00070 00071 return self; 00072 } 00073 00074 00075 00076 - (id)initWithMessage:(wi_p7_message_t *)message spec:(WIP7Spec *)spec { 00077 self = [super init]; 00078 00079 _message = wi_retain(message); 00080 _name = [[NSString alloc] initWithWiredString:wi_p7_message_name(message)]; 00081 _spec = [spec retain]; 00082 00083 return self; 00084 } 00085 00086 00087 00088 - (void)dealloc { 00089 wi_release(_message); 00090 00091 [_name release]; 00092 [_spec release]; 00093 00094 [super dealloc]; 00095 } 00096 00097 00098 00099 - (NSString *)description { 00100 NSMutableString *description; 00101 NSEnumerator *enumerator; 00102 NSDictionary *fields; 00103 NSString *name, *value; 00104 00105 fields = [self fields]; 00106 description = [NSMutableString stringWithFormat:@"%@ = {%@", [self name], ([fields count] > 0) ? @"\n" : @" "]; 00107 enumerator = [[[fields allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectEnumerator]; 00108 00109 while((name = [enumerator nextObject])) { 00110 value = [fields objectForKey:name]; 00111 00112 [description appendFormat:@"\t%@ = %@\n", name, value]; 00113 } 00114 00115 [description appendString:@"}"]; 00116 00117 return description; 00118 } 00119 00120 00121 00122 #pragma mark - 00123 00124 - (NSString *)name { 00125 return _name; 00126 } 00127 00128 00129 00130 - (wi_p7_message_t *)message { 00131 return _message; 00132 } 00133 00134 00135 00136 #pragma mark - 00137 00138 - (NSDictionary *)fields { 00139 NSMutableDictionary *dictionary; 00140 wi_pool_t *pool; 00141 wi_enumerator_t *enumerator; 00142 wi_dictionary_t *fields; 00143 wi_string_t *name, *value; 00144 00145 pool = wi_pool_init(wi_pool_alloc()); 00146 dictionary = [NSMutableDictionary dictionary]; 00147 fields = wi_p7_message_fields(_message); 00148 enumerator = wi_dictionary_key_enumerator(fields); 00149 00150 while((name = wi_enumerator_next_data(enumerator))) { 00151 value = wi_dictionary_data_for_key(fields, name); 00152 00153 [dictionary setObject:[NSString stringWithWiredString:value] forKey:[NSString stringWithWiredString:name]]; 00154 } 00155 00156 wi_release(pool); 00157 00158 return dictionary; 00159 } 00160 00161 00162 00163 #pragma mark - 00164 00165 - (void)setContextInfo:(void *)contextInfo { 00166 _contextInfo = contextInfo; 00167 } 00168 00169 00170 00171 - (void *)contextInfo { 00172 return _contextInfo; 00173 } 00174 00175 00176 00177 #pragma mark - 00178 00179 - (BOOL)getBool:(WIP7Bool *)value forName:(NSString *)name { 00180 return wi_p7_message_get_bool_for_name(_message, value, [_spec fieldNameForName:name]); 00181 } 00182 00183 00184 00185 - (BOOL)setBool:(WIP7Bool)value forName:(NSString *)name { 00186 return wi_p7_message_set_bool_for_name(_message, value, [_spec fieldNameForName:name]); 00187 } 00188 00189 00190 00191 - (BOOL)getEnum:(WIP7Enum *)value forName:(NSString *)name { 00192 return wi_p7_message_get_enum_for_name(_message, value, [_spec fieldNameForName:name]); 00193 } 00194 00195 00196 00197 - (BOOL)setEnum:(WIP7Enum)value forName:(NSString *)name { 00198 return wi_p7_message_set_enum_for_name(_message, value, [_spec fieldNameForName:name]); 00199 } 00200 00201 00202 00203 - (BOOL)getInt32:(WIP7Int32 *)value forName:(NSString *)name { 00204 return wi_p7_message_get_int32_for_name(_message, value, [_spec fieldNameForName:name]); 00205 } 00206 00207 00208 00209 - (BOOL)setInt32:(WIP7Int32)value forName:(NSString *)name { 00210 return wi_p7_message_set_int32_for_name(_message, value, [_spec fieldNameForName:name]); 00211 } 00212 00213 00214 00215 - (BOOL)getUInt32:(WIP7UInt32 *)value forName:(NSString *)name { 00216 return wi_p7_message_get_uint32_for_name(_message, value, [_spec fieldNameForName:name]); 00217 } 00218 00219 00220 00221 - (BOOL)setUInt32:(WIP7UInt32)value forName:(NSString *)name { 00222 return wi_p7_message_set_uint32_for_name(_message, value, [_spec fieldNameForName:name]); 00223 } 00224 00225 00226 00227 - (BOOL)getInt64:(WIP7Int64 *)value forName:(NSString *)name { 00228 return wi_p7_message_get_int64_for_name(_message, value, [_spec fieldNameForName:name]); 00229 } 00230 00231 00232 00233 - (BOOL)setInt64:(WIP7Int64)value forName:(NSString *)name { 00234 return wi_p7_message_set_int64_for_name(_message, value, [_spec fieldNameForName:name]); 00235 } 00236 00237 00238 00239 - (BOOL)getUInt64:(WIP7UInt64 *)value forName:(NSString *)name { 00240 return wi_p7_message_get_uint64_for_name(_message, value, [_spec fieldNameForName:name]); 00241 } 00242 00243 00244 00245 - (BOOL)setUInt64:(WIP7UInt64)value forName:(NSString *)name { 00246 return wi_p7_message_set_uint64_for_name(_message, value, [_spec fieldNameForName:name]); 00247 } 00248 00249 00250 00251 - (BOOL)getDouble:(WIP7Double *)value forName:(NSString *)name { 00252 return wi_p7_message_get_double_for_name(_message, value, [_spec fieldNameForName:name]); 00253 } 00254 00255 00256 00257 - (BOOL)setDouble:(WIP7Double)value forName:(NSString *)name { 00258 return wi_p7_message_set_double_for_name(_message, value, [_spec fieldNameForName:name]); 00259 } 00260 00261 00262 00263 - (BOOL)getOOBData:(WIP7OOBData *)value forName:(NSString *)name { 00264 return wi_p7_message_get_oobdata_for_name(_message, value, [_spec fieldNameForName:name]); 00265 } 00266 00267 00268 00269 - (BOOL)setOOBData:(WIP7OOBData)value forName:(NSString *)name { 00270 return wi_p7_message_set_oobdata_for_name(_message, value, [_spec fieldNameForName:name]); 00271 } 00272 00273 00274 00275 #pragma mark - 00276 00277 - (BOOL)setString:(NSString *)string forName:(NSString *)name { 00278 const char *buffer; 00279 NSUInteger length; 00280 00281 buffer = [string UTF8String]; 00282 length = strlen(buffer); 00283 00284 return wi_p7_message_write_binary(_message, buffer, length + 1, [_spec fieldIDForName:name]); 00285 } 00286 00287 00288 00289 - (NSString *)stringForName:(NSString *)name { 00290 unsigned char *binary; 00291 uint32_t length; 00292 00293 if(!wi_p7_message_read_binary(_message, &binary, &length, [_spec fieldIDForName:name])) 00294 return NULL; 00295 00296 return [NSString stringWithUTF8String:(char *) binary]; 00297 } 00298 00299 00300 00301 - (BOOL)setData:(NSData *)data forName:(NSString *)name { 00302 return wi_p7_message_write_binary(_message, [data bytes], [data length], [_spec fieldIDForName:name]); 00303 } 00304 00305 00306 00307 - (NSData *)dataForName:(NSString *)name { 00308 unsigned char *binary; 00309 uint32_t length; 00310 00311 if(!wi_p7_message_read_binary(_message, &binary, &length, [_spec fieldIDForName:name])) 00312 return NULL; 00313 00314 return [NSData dataWithBytes:binary length:length]; 00315 } 00316 00317 00318 00319 - (BOOL)setNumber:(NSNumber *)number forName:(NSString *)name { 00320 wi_pool_t *pool; 00321 BOOL result = NO; 00322 00323 pool = wi_pool_init(wi_pool_alloc()); 00324 00325 result = wi_p7_message_set_number_for_name(_message, [number wiredNumber], [name wiredString]); 00326 00327 wi_release(pool); 00328 00329 return result; 00330 } 00331 00332 00333 00334 - (NSNumber *)numberForName:(NSString *)name { 00335 NSNumber *number; 00336 wi_pool_t *pool; 00337 00338 pool = wi_pool_init(wi_pool_alloc()); 00339 00340 number = [NSNumber numberWithWiredNumber:wi_p7_message_number_for_name(_message, [_spec fieldNameForName:name])]; 00341 00342 wi_release(pool); 00343 00344 return number; 00345 } 00346 00347 00348 00349 - (BOOL)setEnumName:(NSString *)enumName forName:(NSString *)name { 00350 return wi_p7_message_set_enum_name_for_name(_message, [enumName wiredString], [_spec fieldNameForName:name]); 00351 } 00352 00353 00354 00355 - (NSString *)enumNameForName:(NSString *)name { 00356 NSString *enumName; 00357 wi_pool_t *pool; 00358 wi_string_t *string; 00359 00360 pool = wi_pool_init(wi_pool_alloc()); 00361 00362 string = wi_p7_message_enum_name_for_name(_message, [_spec fieldNameForName:name]); 00363 00364 if(string) 00365 enumName = [NSString stringWithWiredString:string]; 00366 else 00367 enumName = NULL; 00368 00369 wi_release(pool); 00370 00371 return enumName; 00372 } 00373 00374 00375 00376 - (BOOL)setDate:(NSDate *)date forName:(NSString *)name { 00377 return [self setDouble:[date timeIntervalSince1970] forName:name]; 00378 ; 00379 } 00380 00381 00382 00383 - (NSDate *)dateForName:(NSString *)name { 00384 NSTimeInterval interval; 00385 00386 if(![self getDouble:&interval forName:name]) 00387 return NULL; 00388 00389 return [NSDate dateWithTimeIntervalSince1970:interval]; 00390 } 00391 00392 00393 00394 - (BOOL)setUUID:(NSString *)string forName:(NSString *)name { 00395 wi_pool_t *pool; 00396 wi_uuid_t *uuid; 00397 BOOL result = NO; 00398 00399 pool = wi_pool_init(wi_pool_alloc()); 00400 00401 uuid = wi_uuid_with_string([string wiredString]); 00402 00403 if(uuid) 00404 result = wi_p7_message_set_uuid_for_name(_message, uuid, [name wiredString]); 00405 else 00406 result = NO; 00407 00408 wi_release(pool); 00409 00410 return result; 00411 } 00412 00413 00414 00415 - (NSString *)UUIDForName:(NSString *)name { 00416 NSString *string; 00417 wi_pool_t *pool; 00418 wi_uuid_t *uuid; 00419 00420 pool = wi_pool_init(wi_pool_alloc()); 00421 00422 uuid = wi_p7_message_uuid_for_name(_message, [name wiredString]); 00423 string = uuid ? [NSString stringWithWiredString:wi_uuid_string(uuid)] : NULL; 00424 00425 wi_release(pool); 00426 00427 return string; 00428 } 00429 00430 00431 00432 - (BOOL)setList:(NSArray *)list forName:(NSString *)name { 00433 NSEnumerator *enumerator; 00434 wi_pool_t *pool; 00435 wi_mutable_array_t *array; 00436 wi_runtime_instance_t *instance; 00437 id object; 00438 BOOL result; 00439 00440 pool = wi_pool_init(wi_pool_alloc()); 00441 array = wi_array_init(wi_mutable_array_alloc()); 00442 00443 enumerator = [list objectEnumerator]; 00444 00445 while((object = [enumerator nextObject])) { 00446 00447 if([object isKindOfClass:[NSString class]]) 00448 instance = [object wiredString]; 00449 else 00450 instance = NULL; 00451 00452 if(instance) 00453 wi_mutable_array_add_data(array, instance); 00454 } 00455 00456 result = wi_p7_message_set_list_for_name(_message, array, [_spec fieldNameForName:name]); 00457 00458 wi_release(array); 00459 wi_release(pool); 00460 00461 return result; 00462 } 00463 00464 00465 00466 - (NSArray *)listForName:(NSString *)name { 00467 NSMutableArray *list; 00468 wi_pool_t *pool; 00469 wi_array_t *array; 00470 wi_runtime_instance_t *instance; 00471 id object; 00472 wi_uinteger_t i, count; 00473 00474 pool = wi_pool_init(wi_pool_alloc()); 00475 array = wi_p7_message_list_for_name(_message, [_spec fieldNameForName:name]); 00476 00477 if(array) { 00478 list = [NSMutableArray array]; 00479 count = wi_array_count(array); 00480 00481 for(i = 0; i < count; i++) { 00482 instance = WI_ARRAY(array, i); 00483 00484 if(wi_runtime_id(instance) == wi_string_runtime_id()) 00485 object = [NSString stringWithWiredString:instance]; 00486 else 00487 object = NULL; 00488 00489 if(object) 00490 [list addObject:object]; 00491 } 00492 } else { 00493 list = NULL; 00494 } 00495 00496 wi_release(pool); 00497 00498 return list; 00499 } 00500 00501 @end