1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/BLIP/BLIPDispatcher.m Fri May 23 17:37:36 2008 -0700
1.3 @@ -0,0 +1,108 @@
1.4 +//
1.5 +// BLIPDispatcher.m
1.6 +// MYNetwork
1.7 +//
1.8 +// Created by Jens Alfke on 5/15/08.
1.9 +// Copyright 2008 Jens Alfke. All rights reserved.
1.10 +//
1.11 +
1.12 +#import "BLIPDispatcher.h"
1.13 +#import "Target.h"
1.14 +#import "BLIPRequest.h"
1.15 +#import "BLIPProperties.h"
1.16 +
1.17 +
1.18 +@implementation BLIPDispatcher
1.19 +
1.20 +
1.21 +- (id) init
1.22 +{
1.23 + self = [super init];
1.24 + if (self != nil) {
1.25 + _targets = [[NSMutableArray alloc] init];
1.26 + _predicates = [[NSMutableArray alloc] init];
1.27 + }
1.28 + return self;
1.29 +}
1.30 +
1.31 +- (void) dealloc
1.32 +{
1.33 + [_targets release];
1.34 + [_predicates release];
1.35 + [_parent release];
1.36 + [super dealloc];
1.37 +}
1.38 +
1.39 +
1.40 +@synthesize parent=_parent;
1.41 +
1.42 +
1.43 +- (void) addTarget: (MYTarget*)target forPredicate: (NSPredicate*)predicate
1.44 +{
1.45 + [_targets addObject: target];
1.46 + [_predicates addObject: predicate];
1.47 +}
1.48 +
1.49 +
1.50 +- (void) removeTarget: (MYTarget*)target
1.51 +{
1.52 + NSUInteger i = [_targets indexOfObject: target];
1.53 + if( i != NSNotFound ) {
1.54 + [_targets removeObjectAtIndex: i];
1.55 + [_predicates removeObjectAtIndex: i];
1.56 + }
1.57 +}
1.58 +
1.59 +
1.60 +- (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key
1.61 +{
1.62 + [self addTarget: target
1.63 + forPredicate: [NSComparisonPredicate predicateWithLeftExpression: [NSExpression expressionForKeyPath: key]
1.64 + rightExpression: [NSExpression expressionForConstantValue: value]
1.65 + modifier: NSDirectPredicateModifier
1.66 + type: NSEqualToPredicateOperatorType
1.67 + options: 0]];
1.68 +}
1.69 +
1.70 +
1.71 +- (BOOL) dispatchMessage: (BLIPMessage*)message
1.72 +{
1.73 + NSDictionary *properties = message.properties.allProperties;
1.74 + NSUInteger n = _predicates.count;
1.75 + for( NSUInteger i=0; i<n; i++ ) {
1.76 + NSPredicate *p = [_predicates objectAtIndex: i];
1.77 + if( [p evaluateWithObject: properties] ) {
1.78 + MYTarget *target = [_targets objectAtIndex: i];
1.79 + LogTo(BLIP,@"Dispatcher matched %@ -- calling %@",p,target);
1.80 + [target invokeWithSender: message];
1.81 + return YES;
1.82 + }
1.83 + }
1.84 + return [_parent dispatchMessage: message];
1.85 +}
1.86 +
1.87 +
1.88 +@end
1.89 +
1.90 +
1.91 +/*
1.92 + Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
1.93 +
1.94 + Redistribution and use in source and binary forms, with or without modification, are permitted
1.95 + provided that the following conditions are met:
1.96 +
1.97 + * Redistributions of source code must retain the above copyright notice, this list of conditions
1.98 + and the following disclaimer.
1.99 + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
1.100 + and the following disclaimer in the documentation and/or other materials provided with the
1.101 + distribution.
1.102 +
1.103 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
1.104 + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
1.105 + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
1.106 + BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1.107 + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1.108 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1.109 + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
1.110 + THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.111 + */