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