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