BLIP/BLIPDispatcher.m
author Jens Alfke <jens@mooseyard.com>
Wed Apr 29 13:57:10 2009 -0700 (2009-04-29)
changeset 32 b3254a2f6d6c
parent 2 9fdd8dba529c
permissions -rw-r--r--
Tweaked docs
     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 #if ! TARGET_OS_IPHONE
    43 - (void) addTarget: (MYTarget*)target forPredicate: (NSPredicate*)predicate
    44 {
    45     [_targets addObject: target];
    46     [_predicates addObject: predicate];
    47 }
    48 #endif
    49 
    50 
    51 - (void) removeTarget: (MYTarget*)target
    52 {
    53     NSUInteger i = [_targets indexOfObject: target];
    54     if( i != NSNotFound ) {
    55         [_targets removeObjectAtIndex: i];
    56         [_predicates removeObjectAtIndex: i];
    57     }
    58 }
    59 
    60 
    61 - (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key
    62 {
    63 #if TARGET_OS_IPHONE
    64     Assert(target);
    65     [_predicates addObject: $array(key,value)];
    66     [_targets addObject: target];
    67 #else
    68     [self addTarget: target 
    69        forPredicate: [NSComparisonPredicate predicateWithLeftExpression: [NSExpression expressionForKeyPath: key]
    70                                                         rightExpression: [NSExpression expressionForConstantValue: value]
    71                                                                modifier: NSDirectPredicateModifier
    72                                                                    type: NSEqualToPredicateOperatorType
    73                                                                 options: 0]];
    74 #endif
    75 }
    76 
    77 
    78 static BOOL testPredicate( id predicate, NSDictionary *properties ) {
    79 #if TARGET_OS_IPHONE
    80     NSString *key = [predicate objectAtIndex: 0];
    81     NSString *value = [predicate objectAtIndex: 1];
    82     return $equal( [properties objectForKey: key], value );
    83 #else
    84     return [(NSPredicate*)predicate evaluateWithObject: properties];
    85 #endif
    86 }
    87 
    88 
    89 - (BOOL) dispatchMessage: (BLIPMessage*)message
    90 {
    91     NSDictionary *properties = message.properties.allProperties;
    92     NSUInteger n = _predicates.count;
    93     for( NSUInteger i=0; i<n; i++ ) {
    94         id p = [_predicates objectAtIndex: i];
    95         if( testPredicate(p, properties) ) {
    96             MYTarget *target = [_targets objectAtIndex: i];
    97             LogTo(BLIP,@"Dispatcher matched %@ -- calling %@",p,target);
    98             [target invokeWithSender: message];
    99             return YES;
   100         }
   101     }
   102     return [_parent dispatchMessage: message];
   103 }
   104 
   105 
   106 - (MYTarget*) asTarget;
   107 {
   108     return $target(self,dispatchMessage:);
   109 }
   110 
   111 
   112 @end
   113 
   114 
   115 /*
   116  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
   117  
   118  Redistribution and use in source and binary forms, with or without modification, are permitted
   119  provided that the following conditions are met:
   120  
   121  * Redistributions of source code must retain the above copyright notice, this list of conditions
   122  and the following disclaimer.
   123  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
   124  and the following disclaimer in the documentation and/or other materials provided with the
   125  distribution.
   126  
   127  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
   128  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
   129  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
   130  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   131  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
   132   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   133  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
   134  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   135  */