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