net.hivecell.hive.agent
abstract public class: AgentImpl [javadoc |
source]
java.lang.Object
java.rmi.server.RemoteObject
java.rmi.server.RemoteServer
java.rmi.server.UnicastRemoteObject
net.hivecell.hive.agent.AgentImpl
All Implemented Interfaces:
Agent, Serializable, Describable, Remote
Direct Known Subclasses:
EventSendingAgentImpl, CreditsAgentImpl, CameraDemoAgentImpl, GenericFilterAgentImpl, MovingAverageAgentImpl, BaseAWTGUIAgentImpl, StringSendingAgentImpl, AudioClipAgentImpl, AgentMessagingShadowAgentImpl, LocustToLocationTranslatingAgentImpl, BaseSwingGUIAgentImpl, MotionColorFilterAgentImpl, AutoMonitoringAgentImpl, PocketServerAgentImpl, CameraFinderAgentImpl, LocustShadowAgentImpl, ImageTakingDisplayingAgentImpl, LocationServiceRequestAgent, GenericResourceFinderAgentImpl, CellControlAgentImpl, AgentMonitoringAgentImpl, RandomDotAgentImpl, BaseGUIAgentImpl, CricketToLocationTranslatingAgentImpl, ClickAgentImpl, ThroughputTestingAgentImpl, CreditsControllingHuntingAgentImpl, IconicUIAgentImpl, EventTransceivingAgentImpl, CellListAgentImpl, EchoAgentImpl, ImageDisplayAgentImpl, RunningGraphAgentImpl, HTTPAccessTranslatingAgentImpl, MetronomeAgentImpl, AutoUIAgentImpl, SpokeCellEventAgentImpl, FileGrowthInfoAgentImpl, SpokeAgentImpl, WheresBradAgentImpl, EventTranslatingAgentImpl, EventReceivingAgentImpl, ContrastFilterAgentImpl, BaseUIAgentImpl, ImageSourceAgentImpl, CricketAgentImpl, EventNumberTranslatingAgentImpl, CreditsHuntingAgentImpl, HoneyIShrunkTheCDs, TreeUIAgentImpl, MusicUIAgentImpl, StringDisplayAgentImpl, CameraAgentImpl, WebImageAgentImpl, GenericSignalPassthroughAgentImpl, ChatGroupServerAgentImpl, FakeCameraAgentImpl, TwoWayConnectionAgentImpl, CricketShadowAgentImpl, MotionDetectorAgentImpl, FlashingDotAgentImpl, ExternalInterfaceAgentImpl, GrayscaleFilterAgentImpl, DJFinderAgentImpl, LatencyTestingAgentImpl, ChatAgentImpl, ThroughputCalculatingAgentImpl, ImageCreationAgentImpl, DJAgentImpl, SignalPassthrough
Base class for Hive agent implementations.
An agent has a life cycle.
- Once in its lifetime, the agent is constructed by the no
argument constructor.
- The agent arrives on a server; its
doLocalSetup()
method is called. This happens whenever it first comes to a server,
whether created by Cell.createNewAgent(Class) or when
it moves to the server.
- The agent's
doBehavior() method is called. This
method is the agent's main loop, where it's behavior is
implemented. The agent is free to do what it wants, but should
respect timeToStop. The agent sould never exit this
method, a convienience method waitUntilDeath is
provided for this purpose.
- When an agent is being killed, the notification
onDying method is called. The onMoving
is called for the notification for moving
onLocalCleanup is called to ask the agent to free
all its local resources. The reason for agent death can be
discovered by looking at the value of stopCode if
cleanup is dependant on the "type" of agent death
An agent might travel to another server, either by moving itself
or being moved by someone authorized. The
moveTo()
method initiates the move: when the agent arrives at the remote
server,
arriveAt() and
doBehaviour()
will be called again.
All agents have a notion of a list of "connections", which agents
they talk to. It is up to the agent to define the semantics of
what a connect means. Agents also have an icon that represents
them: by default this icon is loaded from
icons/AgentClassName.ppm.
Agents contain something similar to "popup menus" called
ActionCommands. These can be rendered by a UI if so wished, but
this is also so things can issue a string comamnd to the agent and
it will perform the command. this is just another way to ask the
agent to perform an action
- version:
$ - Revision: 1.2 $
| Field Summary |
|---|
| protected transient Cell | myCell | Pointer to the server I am currently on. |
| protected transient CellAddress | myAddress | the address this agent is currently sitting at |
| protected transient ThreadGroup | agentThreadGroup | Pointer to the thread group for this agent -- ell behaved
agents, if they need to spawn separate threads, will make those
threads members of this agents thread group. There is no way,
unfortunately, for Hive to enforce this though |
| protected Description | description | Pointer to my XML description. |
| protected transient boolean | readyFlag | Just describing whether or not this agent is ready for
operation or not |
| protected transient boolean | timeToStop | Variable indicating this agent should die soon. This is set
asynchronously, typically by the server. Agents should inspect
this in their main loop. |
| protected transient int | stopCode | Variable describing the reason why an agent was asked to stop |
| public static final int | AGENTKILLED | the stop code which means the agent is being killed |
| public static final int | AGENTMOVED | the stop code which means the agent is being moved |
| protected SerializableImage | icon | The icon that represents this agent. Should be loaded at create time. |
| protected String | iconName | the name of the icon file |
| protected Hashtable | commands | Hashtable and list for the popup menu items or their
non-graphical equivalents. |
| protected Vector | commandList | Hashtable of CommandObjects |
| Constructor: |
public AgentImpl() throws RemoteException {
super();
loadIcon();
description = new Description( this, this.getName() );
}
Basic constructor for agents. This only gets called once in an
agent's lifetime, when it is first created. It is not
called when the agent arrives at a new host. Also see:
- doLocalSetup Note - because of Java syntax rules, all subclasses must explicitly declare a constructor, even if all it does is call the superclass constructor.
|
| Method from net.hivecell.hive.agent.AgentImpl Summary: |
|---|
|
addActionCommand, blockUntilReady, configure, connectTo, diePlease, disconnectFrom, disconnectFromAll, doBehavior, doLocalCleanup, doLocalSetup, getActionCommands, getAddress, getCell, getComponentManagerShadow, getDescription, getIcon, getIconName, getName, getThreadGroup, invokeActionCommand, isReady, isTimeToStop, listAllIncomingConnections, listAllOutgoingConnections, loadIcon, moveTo, onDying, onMoving, setCell, setDescription, setIconName, setIsReady, setThreadGroup, setTimeToStop, waitUntilDeath |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from net.hivecell.hive.agent.AgentImpl Detail: |
public void addActionCommand(ActionCommand com) {
commandList.addElement( com.getName() );
commands.put( com.getName(), com );
}
Add a popup menu item or non-graphical equivalent to the
agent's behavior. |
public void blockUntilReady() {
while( !isReady() ) {
try {
synchronized( this ) {
wait();
}
} catch( InterruptedException error ) { }
}
}
this method should block until the agent is ready -- it just
sits in the wait method until it gets notified from the
setIsReady method |
public void configure(Object o,
Description desc) {
}
Override this function to let an agent configure itself from a Description.
This method is called by Hive if the agent was created through
The user's Global.CellDescription file. If you want to load a
configuration by hand, see Global.getConfigAsXMLDocument(). |
public boolean connectTo(Agent otherAgent) {
Debug.notice( "there is no default connection for an agent." );
return false;
}
Tell this agent to connect to some other agent. It is up to
the agent to define the semantics of what this means. By
default, AgentImpl just prints an error message and returns
false. |
public void diePlease() {
myCell.killAgent( (AgentImpl)this );
}
Ask the agent to politely die -- by default, the agent just
relays the call to the server and asks it tkill itself |
public void disconnectFrom(Agent otherAgent) {
Debug.notice( "there is no default disconnection for an agent." );
}
Disconnects this agent from the specified other agent. It is up
to the agent to define the semantics of what this means. By
default, AgentImpl just prints an error message and returns
false. |
public void disconnectFromAll() {
Debug.notice( "there is no default disconnection for an agent." );
}
Disconnects this agent from all agents. It is up to the agent
to define the semantics of what this means. By default,
AgentImpl just prints an error message and returns false. |
abstract public void doBehavior()
Do the basic behavior for an agent, the agent's main loop.
Override this to provide your particular agent's behavior.
Note: the doBehavior loop should not exit. Please call
waitUntilDeath() at the end of this method, this will prevent
the Cell from believing this agent prematurely exited. |
public void doLocalCleanup() {
}
Command the agent to free its local resources here. It may or
may not equal what happens when the agent is supposed to die --
but here would be the appropriate place to free File
descriptors, and stop threads, but do not do anything related
to the agent community -- do not notify them that this agent is
about to die. When this method terminates, this agent should
be ready to be killed |
public void doLocalSetup() throws AgentInitializationException {
}
Handle the setup when an agent arrives on a host -- it is
appropriate to allocate system resources (file descriptors,
etc) here and even to locate other agents that this agent needs
to communicate with. |
public Vector getActionCommands() {
return commandList;
}
Get the names of the ActionCommands this agent uses in the
order in which they were entered. |
public CellAddress getAddress() {
return myAddress;
}
return the address that i'm at |
public RemoteCell getCell() {
return (RemoteCell)myCell;
}
Return the cell I'm living on. |
public ComponentManagerShadow getComponentManagerShadow() throws ShadowNotFoundException {
return (ComponentManagerShadow)myCell.getShadow( (ComponentManagerShadowImpl.class).getName() );
}
A convenience method to return the ComponentManagerShadow or
throws an exception if one is not present. |
public Description getDescription() {
return description;
}
Return the semantic description of this agent. |
public SerializableImage getIcon() {
return icon;
}
Return the SerializableImage object that is the agent's icon. |
public String getIconName() {
return iconName;
}
Get the value of iconName. |
public String getName() {
if( description != null ) {
Description desc = description.semanticContext();
if( desc != null )
if( description.selectFirst( "nickname" ) ) // NOTE: OK for now, but should probably make this "name" later.
return description.getMatch().getText();
}
return Global.shortClassName(this);
}
get a "name" for this agent -- if the description contains a
nickname, then we should return that, else we return the short
string of the name |
public ThreadGroup getThreadGroup() {
return agentThreadGroup;
}
Return the thread group that we are in |
public boolean invokeActionCommand(String com) {
if( commands.containsKey( com ) ) {
((ActionCommand)commands.get( com )).invoke();
return true;
}
return false;
}
Invoke an action command by name. Currently, this will only be
called in a baseUICanvas or its non-graphical equivalent. |
public boolean isReady() {
return readyFlag;
}
this flag tells another object whether this agent is ready to
do its stuff. the semantics of being ready are left up to the
agent's author, however the only restriction is that the agent
should -not- report that it is ready until the server calls
setIsReady( true ) on this agent |
public boolean isTimeToStop() {
return timeToStop;
}
Tell if the agent been told to stop. |
public Vector listAllIncomingConnections() {
Debug.warning( "there is no default connection list for an agent." );
return new Vector();
}
Return a vector of all the agents that are connected to me. It
is up to the agent to define the semantics of what this means.
By default, AgentImpl just prints an error message and returns
false. |
public Vector listAllOutgoingConnections() {
Debug.warning( "there is no default connection list for an agent." );
return new Vector();
}
Return a vector of all that agents that i am connected to. It is up
to the agent to define the semantics of what this means. By
default, AgentImpl just prints an error message and returns
false. |
protected void loadIcon() {
InputStream is = null;
if( iconName.equals( "" ) ) {
icon = IconLoader.loadIcon( this.getClass() );
return;
} else {
is = Global.getConfigAsInputStream( iconName ); // Tries to load it from the user's home directory
if( is == null ) {
Debug.notice( "trying to get user-specified icon from directory in CLASSPATH" );
is = getClass().getResourceAsStream( iconName ); // Tries to load it from the current directory
}
}
if( is == null ) {
// Note: if these warnings are ever thrown, something has
// gone very wrong... (UnknownAgent.ppm not in
// hive/agent/icons)
Debug.warning( "no icon found for " + this.getClass().toString() + ", substituting " + IconLoader.defaultIconName );
is = getClass().getResourceAsStream( IconLoader.defaultIconName );
if( is == null ) {
Debug.critical( IconLoader.defaultIconName + " doesn't exist therefore agent icons are broken -- using a blank icon." );
icon = new SerializableImage( new int[]{ 1 }, new java.awt.Dimension( 1, 1 ) );
return;
}
}
// OK, got the file as a stream, let's parse the PPM out and cast it into SerializableImage.
try {
icon = new PPM( is );
} catch( Exception ex ) {
Debug.critical( "error loading agent's icon: " + ex );
}
}
Load the icon for an agent off of disk. If the agent is class
net.hivecell.hive.FooAgent then this looks for
net.hivecell.hive/icons/FooAgent.ppm in the class
path or Jar file. If that doesn't exist, then a default is
substituted. |
public void moveTo(CellAddress address) throws AgentDeliveryException {
// Debug.notice( Global.shortString( this ) + ".moveTo(" + address + ")" );
myCell.moveAgent( this, address );
}
Move this agent to a new host. this basically moves the call
up to the cell who handles it |
public void onDying() {
}
Used to notify the agent that it is about to be killed |
public void onMoving() {
}
Used to notify the agent that it is about to be moved |
public void setCell(Cell c) {
if( myCell != null )
Debug.warning( "setCell() was called on " + Global.shortString( this ) + " when it had a cell set!" );
myCell = c;
myAddress = myCell.getAddress();
}
Set the cell flag on the agent. Normally user code should not
call this - creation code in the cell should handle this |
public void setDescription(Description desc) {
desc.noContext();
this.description = new Description( this, desc.getNode() );
}
Set the semantic deescription of this agent. we take the xml
description, rip it apart and make sure the description of the
xml description is the right thing |
public void setIconName(String v) {
this.iconName = v;
loadIcon();
}
Set the value of iconName. |
public final void setIsReady(boolean flag) {
readyFlag = flag;
synchronized( this ) {
notifyAll();
}
}
This method tells the world that this agent is ready for
operation. Deciding when an agent is ready is up to the
agent's author, however the agent should -not- report that it
is ready until it can be connected to, receive and send
events, and perform any other special duties it needs to.
setIsReady(true) is called automatically when the agent's
doLocalSetup is done in Cell.java runAgentThread(). |
public void setThreadGroup(ThreadGroup tg) {
this.agentThreadGroup = tg;
}
Set the thread group of this agent. Normally user code should
not call this -- the creation methods in the server will set
this properly |
public final void setTimeToStop(int flag) {
timeToStop = true;
stopCode = flag;
synchronized( this ) {
notifyAll();
}
}
Sets the time to stop flag |
protected void waitUntilDeath() {
while( !timeToStop )
synchronized( this ) {
try {
wait();
} catch( InterruptedException error ) { }
}
}
Used at the end of the doBehavior method. Simply wait until
the timeToStop flag has been set and the notify method has been
called so we can exit properly, This should be done, otherwise
the agnet will fall out of its doBehavior loop, and the Cell
will "clean" the agnet up |