Added a BLIP port of Apple's "CocoaEcho" sample code.
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 - (void) addTarget: (MYTarget*)target forPredicate: (NSPredicate*)predicate
44 [_targets addObject: target];
45 [_predicates addObject: predicate];
49 - (void) removeTarget: (MYTarget*)target
51 NSUInteger i = [_targets indexOfObject: target];
52 if( i != NSNotFound ) {
53 [_targets removeObjectAtIndex: i];
54 [_predicates removeObjectAtIndex: i];
59 - (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key
61 [self addTarget: target
62 forPredicate: [NSComparisonPredicate predicateWithLeftExpression: [NSExpression expressionForKeyPath: key]
63 rightExpression: [NSExpression expressionForConstantValue: value]
64 modifier: NSDirectPredicateModifier
65 type: NSEqualToPredicateOperatorType
70 - (BOOL) dispatchMessage: (BLIPMessage*)message
72 NSDictionary *properties = message.properties.allProperties;
73 NSUInteger n = _predicates.count;
74 for( NSUInteger i=0; i<n; i++ ) {
75 NSPredicate *p = [_predicates objectAtIndex: i];
76 if( [p evaluateWithObject: properties] ) {
77 MYTarget *target = [_targets objectAtIndex: i];
78 LogTo(BLIP,@"Dispatcher matched %@ -- calling %@",p,target);
79 [target invokeWithSender: message];
83 return [_parent dispatchMessage: message];
87 - (MYTarget*) asTarget;
89 return $target(self,dispatchMessage:);
97 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
99 Redistribution and use in source and binary forms, with or without modification, are permitted
100 provided that the following conditions are met:
102 * Redistributions of source code must retain the above copyright notice, this list of conditions
103 and the following disclaimer.
104 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
105 and the following disclaimer in the documentation and/or other materials provided with the
108 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
109 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
110 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
111 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
112 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
113 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
114 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
115 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.