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
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@0
    13
jens@0
    14
jens@0
    15
@implementation BLIPDispatcher
jens@0
    16
jens@0
    17
jens@0
    18
- (id) init
jens@0
    19
{
jens@0
    20
    self = [super init];
jens@0
    21
    if (self != nil) {
jens@0
    22
        _targets = [[NSMutableArray alloc] init];
jens@0
    23
        _predicates = [[NSMutableArray alloc] init];
jens@0
    24
    }
jens@0
    25
    return self;
jens@0
    26
}
jens@0
    27
jens@0
    28
- (void) dealloc
jens@0
    29
{
jens@0
    30
    [_targets release];
jens@0
    31
    [_predicates release];
jens@0
    32
    [_parent release];
jens@0
    33
    [super dealloc];
jens@0
    34
}
jens@0
    35
jens@0
    36
jens@0
    37
@synthesize parent=_parent;
jens@0
    38
jens@0
    39
jens@0
    40
- (void) addTarget: (MYTarget*)target forPredicate: (NSPredicate*)predicate
jens@0
    41
{
jens@0
    42
    [_targets addObject: target];
jens@0
    43
    [_predicates addObject: predicate];
jens@0
    44
}
jens@0
    45
jens@0
    46
jens@0
    47
- (void) removeTarget: (MYTarget*)target
jens@0
    48
{
jens@0
    49
    NSUInteger i = [_targets indexOfObject: target];
jens@0
    50
    if( i != NSNotFound ) {
jens@0
    51
        [_targets removeObjectAtIndex: i];
jens@0
    52
        [_predicates removeObjectAtIndex: i];
jens@0
    53
    }
jens@0
    54
}
jens@0
    55
jens@0
    56
jens@0
    57
- (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key
jens@0
    58
{
jens@0
    59
    [self addTarget: target 
jens@0
    60
       forPredicate: [NSComparisonPredicate predicateWithLeftExpression: [NSExpression expressionForKeyPath: key]
jens@0
    61
                                                        rightExpression: [NSExpression expressionForConstantValue: value]
jens@0
    62
                                                               modifier: NSDirectPredicateModifier
jens@0
    63
                                                                   type: NSEqualToPredicateOperatorType
jens@0
    64
                                                                options: 0]];
jens@0
    65
}
jens@0
    66
jens@0
    67
jens@0
    68
- (BOOL) dispatchMessage: (BLIPMessage*)message
jens@0
    69
{
jens@0
    70
    NSDictionary *properties = message.properties.allProperties;
jens@0
    71
    NSUInteger n = _predicates.count;
jens@0
    72
    for( NSUInteger i=0; i<n; i++ ) {
jens@0
    73
        NSPredicate *p = [_predicates objectAtIndex: i];
jens@0
    74
        if( [p evaluateWithObject: properties] ) {
jens@0
    75
            MYTarget *target = [_targets objectAtIndex: i];
jens@0
    76
            LogTo(BLIP,@"Dispatcher matched %@ -- calling %@",p,target);
jens@0
    77
            [target invokeWithSender: message];
jens@0
    78
            return YES;
jens@0
    79
        }
jens@0
    80
    }
jens@0
    81
    return [_parent dispatchMessage: message];
jens@0
    82
}
jens@0
    83
jens@0
    84
jens@0
    85
@end
jens@0
    86
jens@0
    87
jens@0
    88
/*
jens@0
    89
 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
jens@0
    90
 
jens@0
    91
 Redistribution and use in source and binary forms, with or without modification, are permitted
jens@0
    92
 provided that the following conditions are met:
jens@0
    93
 
jens@0
    94
 * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@0
    95
 and the following disclaimer.
jens@0
    96
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
jens@0
    97
 and the following disclaimer in the documentation and/or other materials provided with the
jens@0
    98
 distribution.
jens@0
    99
 
jens@0
   100
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@0
   101
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@0
   102
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@0
   103
 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@0
   104
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@0
   105
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@0
   106
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@0
   107
 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@0
   108
 */