Fixed a serious bug - a race condition where a data buffer in the writer's queue could be dealloced (not the NSData, but its bytes themselves) before the writer sent it, resulting in an EFAULT error.
5 // Created by Jens Alfke on 5/15/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import "BLIPDispatcher.h"
11 #import "BLIPRequest.h"
12 #import "BLIPProperties.h"
17 @implementation BLIPDispatcher
24 _targets = [[NSMutableArray alloc] init];
25 _predicates = [[NSMutableArray alloc] init];
33 [_predicates release];
39 @synthesize parent=_parent;
42 #if ! TARGET_OS_IPHONE
43 - (void) addTarget: (MYTarget*)target forPredicate: (NSPredicate*)predicate
45 [_targets addObject: target];
46 [_predicates addObject: predicate];
51 - (void) removeTarget: (MYTarget*)target
53 NSUInteger i = [_targets indexOfObject: target];
54 if( i != NSNotFound ) {
55 [_targets removeObjectAtIndex: i];
56 [_predicates removeObjectAtIndex: i];
61 - (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key
65 [_predicates addObject: $array(key,value)];
66 [_targets addObject: target];
68 [self addTarget: target
69 forPredicate: [NSComparisonPredicate predicateWithLeftExpression: [NSExpression expressionForKeyPath: key]
70 rightExpression: [NSExpression expressionForConstantValue: value]
71 modifier: NSDirectPredicateModifier
72 type: NSEqualToPredicateOperatorType
78 static BOOL testPredicate( id predicate, NSDictionary *properties ) {
80 NSString *key = [predicate objectAtIndex: 0];
81 NSString *value = [predicate objectAtIndex: 1];
82 return $equal( [properties objectForKey: key], value );
84 return [(NSPredicate*)predicate evaluateWithObject: properties];
89 - (BOOL) dispatchMessage: (BLIPMessage*)message
91 NSDictionary *properties = message.properties.allProperties;
92 NSUInteger n = _predicates.count;
93 for( NSUInteger i=0; i<n; i++ ) {
94 id p = [_predicates objectAtIndex: i];
95 if( testPredicate(p, properties) ) {
96 MYTarget *target = [_targets objectAtIndex: i];
97 LogTo(BLIP,@"Dispatcher matched %@ -- calling %@",p,target);
98 [target invokeWithSender: message];
102 return [_parent dispatchMessage: message];
106 - (MYTarget*) asTarget;
108 return $target(self,dispatchMessage:);
116 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
118 Redistribution and use in source and binary forms, with or without modification, are permitted
119 provided that the following conditions are met:
121 * Redistributions of source code must retain the above copyright notice, this list of conditions
122 and the following disclaimer.
123 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
124 and the following disclaimer in the documentation and/or other materials provided with the
127 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
128 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
129 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
130 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
131 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
132 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
133 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
134 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.