planet.generic.commonapi.behaviours
Class BehavioursPoolImpl

java.lang.Object
  extended byplanet.generic.commonapi.behaviours.BehavioursPoolImpl
All Implemented Interfaces:
BehavioursPool, java.io.Serializable

public class BehavioursPoolImpl
extends java.lang.Object
implements BehavioursPool

The BehavioursPool's class is aimed at providing an internal scheduler of node's behaviuours. At startup, behaviours are registered to the pool from Behaviour.properties file, following the next syntax:

    
      Class = Type, Mode, Probability, When, Role 
 
Then, a message interceptor is build up and is ready to invoke behaviours when a message pattern matches to those provided by Behaviour.properties file.

Author:
Marc Sanchez, Jordi Pujol Date: 10/10/2004
See Also:
Serialized Form

Field Summary
protected  java.util.Vector[][] beh
          BehavioursPool properties: mapping from patterns to behaviours.
protected static int BEH_MAPPINGS
          Total number of Behaviour's Queue:
    Good and Local Behaviours Good and Remote Behaviours Bad and Local Behaviours Bad and Remote Behaviours
protected  java.util.Vector behaviours
          Auxiliar Vector to use in the onMessage() method.
protected  java.util.Iterator behIt
          Auxiliar Iterator to use in the onMessage() method.
protected  int behSlots
          Number of behaviour's slots: numberTypes * numberModes;
protected  BehavioursFilter filter
          Filter instance.
protected static int NULL_MASK
          Behaviour's Property: Lack Of Neutral and Always Mask.
protected static int ROLE_BAD
          Behaviour's Property: Bad modifier for Behaviour's role.
protected static int ROLE_GOOD
          Behaviour's Property: Good modifier for Behaviour's role.
protected static int ROLE_NEUTRAL_MASK
          Behaviour's Property: Neutral modifier applied to a behaviour ignores the distinction between local or remote traffic handled by a node.
protected static int TRAFFIC_ALWAYS_MASK
          Behaviour's Property: Always modifier applied to a behaviour ignores the distinction between local or remote traffic handled by a node.
protected static int TRAFFIC_LOCAL
          Behaviour's Property: Local modifier applied to a behaviour means it only will be scheduled when a RouteMessage has as destination the local node.
protected static int TRAFFIC_REMOTE
          Behaviour's Property: Remote modifier applied to a behaviour means it only will be scheduled when a RouteMessage has as destination a remote node and needs to be rerouted trough the overlay again.
 
Constructor Summary
BehavioursPoolImpl()
          Builds a non initialized pool.
 
Method Summary
protected  void build(java.util.Vector patterns)
          Given the patterns sorted from more-to-less specific as input, builds a mapping from RouteMessage's patterns to behaviours.
 void onMessage(RouteMessage msg, Node node)
          Given a RouteMessage and a Node as input, onMessage's method intends to invoke some behaviours only if RouteMessage's type and mode fields matches some behaviour's pattern.
protected  void prettyPrintAll()
           
protected  void printMap(int queue)
           
protected  void setPattern(int queue, int typePos, java.lang.String modeOf, BehavioursPatternImpl pattern)
          Given the initial position for type on mapping typePos and mode as input, sets the pattern on the mapping taking into account the grammar tree extracted from wildcards semantics.
protected  boolean[] whichQueues(int Mask, int RoleOf, int WhenTo)
          The WhichQueue's method selects for a given pattern what Queues must include a copy of the current behaviour.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

beh

protected java.util.Vector[][] beh
BehavioursPool properties: mapping from patterns to behaviours. By reflection, the behaviours pool given a set of patterns P do:
  	For each pattern on P do
  		Mode = pattern.getMode();
  		Type = pattern.getType();
  		beh[Type * NumberOfModes + Mode] = pattern; 
      done;
  
Moreover, Pattern's definiton provides additional specifiers:


ROLE_GOOD

protected static final int ROLE_GOOD
Behaviour's Property: Good modifier for Behaviour's role. This means the behaviour qualified by this modifier only will be scheduled by good peers.

See Also:
Constant Field Values

ROLE_BAD

protected static final int ROLE_BAD
Behaviour's Property: Bad modifier for Behaviour's role. This means the behaviour qualified by this modifier only will be scheduled by bad peers.

See Also:
Constant Field Values

TRAFFIC_LOCAL

protected static final int TRAFFIC_LOCAL
Behaviour's Property: Local modifier applied to a behaviour means it only will be scheduled when a RouteMessage has as destination the local node.

See Also:
Constant Field Values

TRAFFIC_REMOTE

protected static int TRAFFIC_REMOTE
Behaviour's Property: Remote modifier applied to a behaviour means it only will be scheduled when a RouteMessage has as destination a remote node and needs to be rerouted trough the overlay again.


filter

protected BehavioursFilter filter
Filter instance.


behSlots

protected int behSlots
Number of behaviour's slots: numberTypes * numberModes;


behaviours

protected java.util.Vector behaviours
Auxiliar Vector to use in the onMessage() method.


behIt

protected java.util.Iterator behIt
Auxiliar Iterator to use in the onMessage() method.


TRAFFIC_ALWAYS_MASK

protected static final int TRAFFIC_ALWAYS_MASK
Behaviour's Property: Always modifier applied to a behaviour ignores the distinction between local or remote traffic handled by a node.

See Also:
Constant Field Values

ROLE_NEUTRAL_MASK

protected static final int ROLE_NEUTRAL_MASK
Behaviour's Property: Neutral modifier applied to a behaviour ignores the distinction between local or remote traffic handled by a node.

See Also:
Constant Field Values

NULL_MASK

protected static final int NULL_MASK
Behaviour's Property: Lack Of Neutral and Always Mask.

See Also:
Constant Field Values

BEH_MAPPINGS

protected static final int BEH_MAPPINGS
Total number of Behaviour's Queue:
  1. Good and Local Behaviours
  2. Good and Remote Behaviours
  3. Bad and Local Behaviours
  4. Bad and Remote Behaviours

See Also:
Constant Field Values
Constructor Detail

BehavioursPoolImpl

public BehavioursPoolImpl()
                   throws InitializationException
Builds a non initialized pool. Requires the setValues(...) method invokation.

Method Detail

whichQueues

protected boolean[] whichQueues(int Mask,
                                int RoleOf,
                                int WhenTo)
The WhichQueue's method selects for a given pattern what Queues must include a copy of the current behaviour.

Parameters:
Mask - The Mask for Pattern.Always and Pattern.Neutral modifiers.
RoleOf - The Behaviour's Role for a Good or Bad peer.
WhenTo - Specifies Behaviour should be activated when incoming messages refer to the local node or either when refer to a remote.
Returns:
Which Behaviour's Queue must copy this behaviour.

build

protected void build(java.util.Vector patterns)
              throws InitializationException
Given the patterns sorted from more-to-less specific as input, builds a mapping from RouteMessage's patterns to behaviours.

Parameters:
patterns - The List of patterns.
Throws:
InitializationException

setPattern

protected void setPattern(int queue,
                          int typePos,
                          java.lang.String modeOf,
                          BehavioursPatternImpl pattern)
                   throws InitializationException
Given the initial position for type on mapping typePos and mode as input, sets the pattern on the mapping taking into account the grammar tree extracted from wildcards semantics. Hence, the maximum number of mappings goes from typePos to typePos + NumberOfModes. For example, suppose type is QUERY_JOIN and modes are REQUEST and REPLY the maximun number of mappings to set is 2;

Parameters:
queue - Identifies where to be queued the pattern.
typePos - Initial position on behaviours mapping array, i.e, [typePos..typePos + NumberOfModes]
modeOf - Mode of the pattern: Universal wildcard or '*', Complementary wildcard or '?' and Tag.
pattern - Pattern to be treat.
Throws:
InitializationException

prettyPrintAll

protected void prettyPrintAll()

printMap

protected void printMap(int queue)

onMessage

public void onMessage(RouteMessage msg,
                      Node node)
               throws NoSuchBehaviourException,
                      NoBehaviourDispatchedException
Given a RouteMessage and a Node as input, onMessage's method intends to invoke some behaviours only if RouteMessage's type and mode fields matches some behaviour's pattern.

Specified by:
onMessage in interface BehavioursPool
Parameters:
msg - RoteMessage taken as input.
node - Node taken as input.
Returns:
Returns either an array of RouteMessages or null when no messages need to transmit this node.
Throws:
NoSuchBehaviourException - when no behaviour matches RouteMessage's pattern.
NoBehaviourDispatchedException