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"
15 @implementation BLIPDispatcher
22 _targets = [[NSMutableArray alloc] init];
23 _predicates = [[NSMutableArray alloc] init];
31 [_predicates release];
37 @synthesize parent=_parent;
40 - (void) addTarget: (MYTarget*)target forPredicate: (NSPredicate*)predicate
42 [_targets addObject: target];
43 [_predicates addObject: predicate];
47 - (void) removeTarget: (MYTarget*)target
49 NSUInteger i = [_targets indexOfObject: target];
50 if( i != NSNotFound ) {
51 [_targets removeObjectAtIndex: i];
52 [_predicates removeObjectAtIndex: i];
57 - (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key
59 [self addTarget: target
60 forPredicate: [NSComparisonPredicate predicateWithLeftExpression: [NSExpression expressionForKeyPath: key]
61 rightExpression: [NSExpression expressionForConstantValue: value]
62 modifier: NSDirectPredicateModifier
63 type: NSEqualToPredicateOperatorType
68 - (BOOL) dispatchMessage: (BLIPMessage*)message
70 NSDictionary *properties = message.properties.allProperties;
71 NSUInteger n = _predicates.count;
72 for( NSUInteger i=0; i<n; i++ ) {
73 NSPredicate *p = [_predicates objectAtIndex: i];
74 if( [p evaluateWithObject: properties] ) {
75 MYTarget *target = [_targets objectAtIndex: i];
76 LogTo(BLIP,@"Dispatcher matched %@ -- calling %@",p,target);
77 [target invokeWithSender: message];
81 return [_parent dispatchMessage: message];
89 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
91 Redistribution and use in source and binary forms, with or without modification, are permitted
92 provided that the following conditions are met:
94 * Redistributions of source code must retain the above copyright notice, this list of conditions
95 and the following disclaimer.
96 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
97 and the following disclaimer in the documentation and/or other materials provided with the
100 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
101 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
102 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
103 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
104 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
105 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
106 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
107 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.