BLIP/BLIPDispatcher.h
author Jens Alfke <jens@mooseyard.com>
Tue Jul 21 10:04:10 2009 -0700 (2009-07-21)
changeset 61 981f9d604c88
parent 8 6f539dd9921c
permissions -rw-r--r--
Prevent crash if MYBonjourQuery is released during response handling
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@26
    28
    @private
jens@0
    29
    NSMutableArray *_predicates, *_targets;
jens@0
    30
    BLIPDispatcher *_parent;
jens@0
    31
}
jens@0
    32
jens@2
    33
/** The inherited parent dispatcher.
jens@2
    34
    If a message does not match any of this dispatcher's rules, it will next be passed to
jens@2
    35
    the parent, if there is one. */
jens@0
    36
@property (retain) BLIPDispatcher *parent;
jens@0
    37
jens@8
    38
/** Convenience method that adds a rule that compares a property against a string. */
jens@8
    39
- (void) addTarget: (MYTarget*)target forValueOfProperty: (NSString*)value forKey: (NSString*)key;
jens@8
    40
jens@8
    41
#if ! TARGET_OS_IPHONE      /* NSPredicate is not available on iPhone, unfortunately */
jens@2
    42
/** Adds a new rule, to call a given target method if a given predicate matches the message. */
jens@0
    43
- (void) addTarget: (MYTarget*)target forPredicate: (NSPredicate*)predicate;
jens@8
    44
#endif
jens@2
    45
jens@2
    46
/** Removes all rules with the given target method. */
jens@0
    47
- (void) removeTarget: (MYTarget*)target;
jens@0
    48
jens@2
    49
/** Tests the message against all the rules, in the order they were added, and calls the
jens@2
    50
    target of the first matching rule.
jens@2
    51
    If no rule matches, the message is passed to the parent dispatcher's -dispatchMessage:,
jens@2
    52
    if there is a parent.
jens@2
    53
    If no rules at all match, NO is returned. */
jens@0
    54
- (BOOL) dispatchMessage: (BLIPMessage*)message;
jens@0
    55
jens@2
    56
/** Returns a target object that will call this dispatcher's -dispatchMessage: method.
jens@2
    57
    This can be used to make this dispatcher the target of another dispatcher's rule,
jens@2
    58
    stringing them together hierarchically. */
jens@2
    59
- (MYTarget*) asTarget;
jens@2
    60
jens@0
    61
@end