📄 behaviourspoolimpl.java
字号:
package planet.generic.commonapi.behaviours;
import java.util.Iterator;
import java.util.Vector;
import planet.commonapi.Node;
import planet.commonapi.RouteMessage;
import planet.commonapi.behaviours.Behaviour;
import planet.commonapi.behaviours.BehavioursFilter;
import planet.commonapi.behaviours.BehavioursInvoker;
import planet.commonapi.behaviours.BehavioursPool;
import planet.commonapi.behaviours.exception.NoBehaviourDispatchedException;
import planet.commonapi.behaviours.exception.NoSuchBehaviourException;
import planet.commonapi.behaviours.exception.OutOfRangeError;
import planet.commonapi.exception.InitializationException;
import planet.generic.commonapi.factory.GenericFactory;
import planet.util.Properties;
/**
* 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:
* <br><pre>
* Class = Type, Mode, Probability, When, Role
* </pre>
* Then, a message interceptor is build up and is ready to invoke behaviours
* when a message pattern matches to those provided by <b> Behaviour.properties </b>
* file.
* @author <a href="mailto: marc.sanchez@urv.net">Marc Sanchez</a>
* @author <a href="mailto: jordi.pujol@estudiants.urv.es">Jordi Pujol</a>
* Date: 10/10/2004
*/
public class BehavioursPoolImpl implements BehavioursPool {
/**
* BehavioursPool properties: mapping from <b>patterns</b> to <b>behaviours</b>.
* <ul>
* <li>Patterns are composed by: Type and Mode, for example:
* "QUERY_JOIN" and "REQUEST" related to the available
* RouteMessage types and modes. </li>
* <li>Types: The available RouteMessage types. Correct range in: 0..NumberOfTypes. </li>
* <li>Modes: The available RouteMessage mode. Correct range in: 0..NumberOfModes. </li>
* </ul>
* By reflection, the behaviours pool given a set of patterns P do:
* <pre>
* For each pattern on P do
* Mode = pattern.getMode();
* Type = pattern.getType();
* beh[Type * NumberOfModes + Mode] = pattern;
* done;
* </pre>
* Moreover, Pattern's definiton provides additional specifiers:
* <ul>
* <li><b>Incoming RouteMessage's destination</b>: Local, Remote and
* Always. Local qualifier affects iff incoming messages for
* the local node. Hence, only behaviours for the local node
* may be matched. Remote qualifier affects iff messages for
* remote nodes. Hence, programmer must code behaviours which
* alter the underlying overlay protocol when RouteMessages
* are destined to remote nodes. Always modifier is used as
* a wild card to ignore local or remote behaviour. </li>
* <li><b>Behaviours's role</b>: Bad, Good and Neutral. Bad qualifier
* alters only the behaviours from bad nodes. Good qualifier
* alters only the behaviours from good nodes and Neutral is
* a wild card used to ignore peer's behaviour.</li>
* </ul>
**/
protected java.util.Vector[][] beh;
/**
* Behaviour's Property: Good modifier for Behaviour's role. This means the behaviour
* qualified by this modifier only will be scheduled by good peers.
*/
protected static final int ROLE_GOOD = 0;
/**
* Behaviour's Property: Bad modifier for Behaviour's role. This means the behaviour
* qualified by this modifier only will be scheduled by bad peers.
*/
protected static final int ROLE_BAD = 1;
/**
* 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 final int TRAFFIC_LOCAL = 0;
/**
* 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.
*/
protected static int TRAFFIC_REMOTE = 1;
/**
* Filter instance.
*/
protected BehavioursFilter filter;
/**
* Number of behaviour's slots: numberTypes * numberModes;
*/
protected int behSlots;
/**
* Auxiliar Vector to use in the onMessage() method.
*/
protected Vector behaviours = null;
/**
* Auxiliar Iterator to use in the onMessage() method.
*/
protected Iterator behIt = null;
/**
* Behaviour's Property: Always modifier applied to a behaviour ignores the
* distinction between local or remote traffic handled by a node.
*/
protected static final int TRAFFIC_ALWAYS_MASK = 0x2;
/**
* Behaviour's Property: Neutral modifier applied to a behaviour ignores the
* distinction between local or remote traffic handled by a node.
*/
protected static final int ROLE_NEUTRAL_MASK = 0x1;
/**
* Behaviour's Property: Lack Of Neutral and Always Mask.
*/
protected static final int NULL_MASK = 0x0;
/**
* Total number of Behaviour's Queue:
* <ol type="1" start="0">
* <li>Good and Local Behaviours</li>
* <li>Good and Remote Behaviours</li>
* <li>Bad and Local Behaviours</li>
* <li>Bad and Remote Behaviours</li>
* </ol>
*/
protected static final int BEH_MAPPINGS = 4;
/**
* Name of whole behaviours queue.
*/
private static final String Queues[] = {
"Good and Local ",
"Good and Remote ",
"Bad and Local ",
"Bad and Remote " };
/**
* Specific properties for these behaviours.
*/
private BehavioursPropertiesImpl props = null;
/**
* Builds a non initialized pool. Requires the setValues(...) method
* invokation.
*/
public BehavioursPoolImpl() throws InitializationException {
filter = GenericFactory.buildBehavioursFilter();
behSlots = Properties.behavioursNumberOfTypes *
Properties.behavioursNumberOfModes;
props = (BehavioursPropertiesImpl)Properties.behavioursPropertiesInstance;
build(props.patterns);
if (props.debug) prettyPrintAll();
}
/**
* The WhichQueue's method selects for a given pattern what Queues must
* include a copy of the current behaviour.
* @param Mask The Mask for Pattern.Always and Pattern.Neutral modifiers.
* @param RoleOf The Behaviour's Role for a <b>Good</b> or <b>Bad</b> peer.
* @param WhenTo Specifies Behaviour should be activated when incoming messages
* refer to the local node or either when refer to a remote.
* @return Which Behaviour's Queue must copy this behaviour.
*/
protected boolean[] whichQueues(int Mask, int RoleOf, int WhenTo) {
boolean[] which = { false, false, false, false };
if (Mask == NULL_MASK) which[RoleOf * BEH_MAPPINGS / 2 + WhenTo] = true;
else if ((Mask & (ROLE_NEUTRAL_MASK|TRAFFIC_ALWAYS_MASK)) == (ROLE_NEUTRAL_MASK|TRAFFIC_ALWAYS_MASK))
which = new boolean[] { true, true, true, true };
else if ((Mask & ROLE_NEUTRAL_MASK) == ROLE_NEUTRAL_MASK) {
which[ROLE_GOOD * BEH_MAPPINGS / 2 + WhenTo] = true;
which[ROLE_BAD * BEH_MAPPINGS / 2 + WhenTo] = true;
} else if ((Mask & TRAFFIC_ALWAYS_MASK) == TRAFFIC_ALWAYS_MASK) {
which[RoleOf * BEH_MAPPINGS / 2 + TRAFFIC_LOCAL] = true;
which[RoleOf * BEH_MAPPINGS / 2 + TRAFFIC_REMOTE] = true;
}
return which;
}
/**
* Given the patterns sorted from more-to-less specific as input,
* builds a mapping from RouteMessage's patterns to behaviours.
* @param patterns The List of patterns.
*/
protected void build(Vector patterns) throws InitializationException {
beh = new Vector[BEH_MAPPINGS][behSlots];
for (int behQueue = 0; behQueue < BEH_MAPPINGS; behQueue++)
for(int slot = 0; slot < behSlots; slot++)
beh[behQueue][slot] = new Vector();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -