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