BLIP/BLIPDispatcher.h
author Jens Alfke <jens@mooseyard.com>
Sun Jul 13 10:52:48 2008 -0700 (2008-07-13)
changeset 20 02224e981209
parent 2 9fdd8dba529c
child 26 cb9cdf247239
permissions -rw-r--r--
* Fixed link error in BLIPClient target.
* Fixed *Ph*n* #ifdef issue (thanks, Jimmy)
jens@0
     1
//
jens@0
     2
//  BLIPDispatcher.h
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 <Foundation/Foundation.h>
jens@0
    10
@class MYTarget, BLIPMessage;
jens@0
    11
jens@0
    12
jens@8
    13
/** Routes BLIP messages to targets based on a series of rules.
jens@8
    14
 
jens@8
    15
    Every BLIPConnection has a BLIPDispatcher, which is initially empty, but you can add rules
jens@2
    16
    to it.
jens@2
    17
 
jens@2
    18
    Every BLIPListener also has a dispatcher, which is inherited as the parent by every
jens@2
    19
    connection that it accepts, so you can add rules to the listener's dispatcher to share them
jens@2
    20
    between all connections.
jens@2
    21
 
jens@2
    22
    It's not necessary to use a dispatcher. Any undispatched requests will be sent to the
jens@2
    23
    BLIPConnection's delegate's -connection:receivedRequest: method, which can do its own
jens@2
    24
    custom handling. But it's often easier to use the dispatcher to associate handlers with
jens@2
    25
    request based on property values. */
jens@0
    26
@interface BLIPDispatcher : NSObject 
jens@0
    27
{
jens@0
    28
    NSMutableArray *_predicates, *_targets;
jens@0
    29
    BLIPDispatcher *_parent;
jens@0
    30
}
jens@0
    31
jens@2
    32
/** The inherited parent dispatcher.
jens@2
    33
    If a message does not match any of this dispatcher's rules, it will next be passed to
jens@2
    34
    the parent, if there is one. */
jens@0
    35
@property (retain) BLIPDispatcher *parent;
jens@0
    36
jens@8
    37
/** Convenience method that adds a rule that compares a property against a string. */
jens@8
    38
- (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key;
jens@8
    39
jens@8
    40
#if ! TARGET_OS_IPHONE      /* NSPredicate is not available on iPhone, unfortunately */
jens@2
    41
/** Adds a new rule, to call a given target method if a given predicate matches the message. */
jens@0
    42
- (void) addTarget: (MYTarget*)target forPredicate: (NSPredicate*)predicate;
jens@8
    43
#endif
jens@2
    44
jens@2
    45
/** Removes all rules with the given target method. */
jens@0
    46
- (void) removeTarget: (MYTarget*)target;
jens@0
    47
jens@2
    48
/** Tests the message against all the rules, in the order they were added, and calls the
jens@2
    49
    target of the first matching rule.
jens@2
    50
    If no rule matches, the message is passed to the parent dispatcher's -dispatchMessage:,
jens@2
    51
    if there is a parent.
jens@2
    52
    If no rules at all match, NO is returned. */
jens@0
    53
- (BOOL) dispatchMessage: (BLIPMessage*)message;
jens@0
    54
jens@2
    55
/** Returns a target object that will call this dispatcher's -dispatchMessage: method.
jens@2
    56
    This can be used to make this dispatcher the target of another dispatcher's rule,
jens@2
    57
    stringing them together hierarchically. */
jens@2
    58
- (MYTarget*) asTarget;
jens@2
    59
jens@0
    60
@end