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