|
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/WIP7Message.h> 00030 #import <WiredNetworking/WIP7NotificationCenter.h> 00031 00032 @interface WIP7Notification : WIObject { 00033 @public 00034 id _observer; 00035 NSString *_messageName; 00036 WIP7UInt32 _transaction; 00037 SEL _selector; 00038 } 00039 00040 @end 00041 00042 00043 @implementation WIP7Notification 00044 00045 - (void)dealloc { 00046 [_messageName release]; 00047 00048 [super dealloc]; 00049 } 00050 00051 @end 00052 00053 00054 00055 @implementation WIP7NotificationCenter 00056 00057 - (id)init { 00058 self = [super init]; 00059 00060 _messageNameObservers = [[NSMutableArray alloc] init]; 00061 _transactionObservers = [[NSMutableArray alloc] init]; 00062 00063 return self; 00064 } 00065 00066 00067 00068 - (void)dealloc { 00069 [_messageNameObservers release]; 00070 [_transactionObservers release]; 00071 00072 [super dealloc]; 00073 } 00074 00075 00076 00077 #pragma mark - 00078 00079 - (void)setTransactionFieldName:(NSString *)transactionFieldName { 00080 [transactionFieldName retain]; 00081 [_transactionFieldName release]; 00082 00083 _transactionFieldName = transactionFieldName; 00084 } 00085 00086 00087 00088 - (NSString *)transactionFieldName { 00089 return _transactionFieldName; 00090 } 00091 00092 00093 00094 #pragma mark - 00095 00096 - (void)addObserver:(id)observer selector:(SEL)selector messageName:(NSString *)messageName { 00097 WIP7Notification *notification; 00098 00099 notification = [[WIP7Notification alloc] init]; 00100 notification->_observer = observer; 00101 notification->_messageName = [messageName retain]; 00102 notification->_selector = selector; 00103 [_messageNameObservers addObject:notification]; 00104 [notification release]; 00105 } 00106 00107 00108 00109 - (void)addObserver:(id)observer selector:(SEL)selector message:(WIP7Message *)message { 00110 WIP7Notification *notification; 00111 WIP7UInt32 transaction; 00112 00113 if(_transactionFieldName && [message getUInt32:&transaction forName:_transactionFieldName]) { 00114 notification = [[WIP7Notification alloc] init]; 00115 notification->_observer = observer; 00116 notification->_transaction = transaction; 00117 notification->_selector = selector; 00118 [_transactionObservers addObject:notification]; 00119 [notification release]; 00120 } 00121 } 00122 00123 00124 00125 - (void)removeObserver:(id)observer messageName:(NSString *)messageName { 00126 WIP7Notification *notification; 00127 NSUInteger i, count; 00128 00129 count = [_messageNameObservers count]; 00130 00131 for(i = 0; i < count; i++) { 00132 notification = [_messageNameObservers objectAtIndex:i]; 00133 00134 if(notification->_observer == observer && 00135 [notification->_messageName isEqualToString:messageName]) { 00136 [_messageNameObservers removeObjectAtIndex:i]; 00137 00138 i--; 00139 count--; 00140 } 00141 } 00142 } 00143 00144 00145 00146 - (void)removeObserver:(id)observer message:(WIP7Message *)message { 00147 WIP7Notification *notification; 00148 WIP7UInt32 transaction; 00149 NSUInteger i, count; 00150 00151 if(_transactionFieldName && [message getUInt32:&transaction forName:_transactionFieldName]) { 00152 count = [_transactionObservers count]; 00153 00154 for(i = 0; i < count; i++) { 00155 notification = [_transactionObservers objectAtIndex:i]; 00156 00157 if(notification->_observer == observer && 00158 notification->_transaction == transaction) { 00159 [_transactionObservers removeObjectAtIndex:i]; 00160 00161 i--; 00162 count--; 00163 } 00164 } 00165 } 00166 } 00167 00168 00169 00170 - (void)removeObserver:(id)observer { 00171 WIP7Notification *notification; 00172 NSUInteger i, count; 00173 00174 count = [_messageNameObservers count]; 00175 00176 for(i = 0; i < count; i++) { 00177 notification = [_messageNameObservers objectAtIndex:i]; 00178 00179 if(notification->_observer == observer) { 00180 [_messageNameObservers removeObjectAtIndex:i]; 00181 00182 i--; 00183 count--; 00184 } 00185 } 00186 00187 count = [_transactionObservers count]; 00188 00189 for(i = 0; i < count; i++) { 00190 notification = [_transactionObservers objectAtIndex:i]; 00191 00192 if(notification->_observer == observer) { 00193 [_transactionObservers removeObjectAtIndex:i]; 00194 00195 i--; 00196 count--; 00197 } 00198 } 00199 } 00200 00201 00202 00203 - (void)postMessage:(WIP7Message *)message { 00204 NSString *messageName; 00205 WIP7Notification *notification; 00206 NSUInteger i; 00207 WIP7UInt32 transaction; 00208 BOOL posted = NO; 00209 00210 if(_transactionFieldName && [message getUInt32:&transaction forName:_transactionFieldName]) { 00211 for(i = 0; i < [_transactionObservers count]; i++) { 00212 notification = [_transactionObservers objectAtIndex:i]; 00213 00214 if(notification->_transaction == transaction) { 00215 [notification->_observer performSelector:notification->_selector withObject:message]; 00216 00217 posted = YES; 00218 } 00219 } 00220 } 00221 00222 if(!posted) { 00223 messageName = [message name]; 00224 00225 if(messageName) { 00226 for(i = 0; i < [_messageNameObservers count]; i++) { 00227 notification = [_messageNameObservers objectAtIndex:i]; 00228 00229 if([notification->_messageName isEqualToString:messageName]) 00230 [notification->_observer performSelector:notification->_selector withObject:message]; 00231 } 00232 } 00233 } 00234 } 00235 00236 @end