BLIP/BLIPDispatcher.m
author Jens Alfke <jens@mooseyard.com>
Fri May 23 17:37:36 2008 -0700 (2008-05-23)
changeset 0 9d67172bb323
child 1 8267d5c429c4
permissions -rw-r--r--
First checkin after breaking out of Cloudy
     1 //
     2 //  BLIPDispatcher.m
     3 //  MYNetwork
     4 //
     5 //  Created by Jens Alfke on 5/15/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "BLIPDispatcher.h"
    10 #import "Target.h"
    11 #import "BLIPRequest.h"
    12 #import "BLIPProperties.h"
    13 
    14 
    15 @implementation BLIPDispatcher
    16 
    17 
    18 - (id) init
    19 {
    20     self = [super init];
    21     if (self != nil) {
    22         _targets = [[NSMutableArray alloc] init];
    23         _predicates = [[NSMutableArray alloc] init];
    24     }
    25     return self;
    26 }
    27 
    28 - (void) dealloc
    29 {
    30     [_targets release];
    31     [_predicates release];
    32     [_parent release];
    33     [super dealloc];
    34 }
    35 
    36 
    37 @synthesize parent=_parent;
    38 
    39 
    40 - (void) addTarget: (MYTarget*)target forPredicate: (NSPredicate*)predicate
    41 {
    42     [_targets addObject: target];
    43     [_predicates addObject: predicate];
    44 }
    45 
    46 
    47 - (void) removeTarget: (MYTarget*)target
    48 {
    49     NSUInteger i = [_targets indexOfObject: target];
    50     if( i != NSNotFound ) {
    51         [_targets removeObjectAtIndex: i];
    52         [_predicates removeObjectAtIndex: i];
    53     }
    54 }
    55 
    56 
    57 - (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key
    58 {
    59     [self addTarget: target 
    60        forPredicate: [NSComparisonPredicate predicateWithLeftExpression: [NSExpression expressionForKeyPath: key]
    61                                                         rightExpression: [NSExpression expressionForConstantValue: value]
    62                                                                modifier: NSDirectPredicateModifier
    63                                                                    type: NSEqualToPredicateOperatorType
    64                                                                 options: 0]];
    65 }
    66 
    67 
    68 - (BOOL) dispatchMessage: (BLIPMessage*)message
    69 {
    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];
    78             return YES;
    79         }
    80     }
    81     return [_parent dispatchMessage: message];
    82 }
    83 
    84 
    85 @end
    86 
    87 
    88 /*
    89  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
    90  
    91  Redistribution and use in source and binary forms, with or without modification, are permitted
    92  provided that the following conditions are met:
    93  
    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
    98  distribution.
    99  
   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.
   108  */