madkit.kernel
Class Agent

java.lang.Object
  extended by madkit.kernel.AbstractAgent
      extended by madkit.kernel.Agent
All Implemented Interfaces:
java.io.Serializable, java.lang.Runnable
Direct Known Subclasses:
AbstractEditorAgent, AbstractServerAgent, AbstractShareAgent, BeanShellAgent, BeeLauncher, Broker, ChatAgent, Client, DesktopAgent, DocBrowserAgent, DynamicTwoChannelsCommunicator, ExplorerLauncher, FormalismAgent, GPongAgent, GraphicGroupObserver, GroupMessageTracer, GroupObserver, Hello, JessAgent, JessMonitor, Launcher, LineChartAgent, MadkitNetworkAgent, MadkitOutput, ManagerAgent, MemoryMonitorAgent, MultiPong, OrganizationTracer, OutputRerouter, Pager, PingPong, PluginAgent, PluginDesignerAgent, PrivateAgent, PrivateAgent1, ProgressAgent, ProgressBar, PropertyAgent, Provider, PythonAgent, Scheduler, SchemeAgent, SEditAgent, SmallSearchFileAgent, SmallSenderAgent, SmallShareAgent, SmallUpdatePluginAgent, StructureAgent, TicTacToeAgent, WebBrowserAgent

public abstract class Agent
extends AbstractAgent
implements java.lang.Runnable, java.io.Serializable

The main MadKit AbstractAgent class. Provides support for non threaded agent life cycle, messaging, graphical interface, group and roles management, agent information, ...

The agent behavior is intentionally *not* defined. It is up to the agent developer to choose an agent model or to develop his specific agent library on top of the facilities provided by MadKit. However, all agent share the same organizational view, and the basic messaging code, so integration of agent coming from different developer and having heterogeneous models is quite easy.

Agent-related methods (almost everything here) can invoked only after registration in the kernel (i.e. starting from the activate() method call on this agent). That means that you should not use any of the agent methods in constructor

Version:
4.3
Author:
Olivier Gutknecht, Fabien Michel : MadKit 3.0 revision
See Also:
Serialized Form

Constructor Summary
Agent()
           
 
Method Summary
 boolean connectAgencyToAgency(java.lang.String host, int port)
           
 void createPlace(java.lang.String place)
           
 void createPlace(java.lang.String place, java.lang.String informations)
           
 void enableMobility(java.lang.String name)
           
 void enableMobility(java.lang.String name, int port)
           
protected  void exitImmediatlyOnKill()
          Ensures that an agent will be cleanly killed.
 KernelAddress getAgencyNamed(java.lang.String name)
           
 int joinPlace(java.lang.String place)
           
 int joinPlace(java.lang.String place, java.lang.String pwd)
           
 int joinPlace(java.lang.String Community, java.lang.String place, java.lang.String pwd)
           
 void live()
          This method defines the main behavior of threaded agents.
 void pause(int t)
          Suspend the agent for a while.
protected  void receiveMessage(Message m)
          this method is dramatically private.
 void run()
          Only useful to the agent micro-kernel
 Message waitNextMessage()
          This method is the blocking version of nextMessage().
 Message waitNextMessage(long timeout)
          This method is the blocking version of nextMessage(), with time-out.
 
Methods inherited from class madkit.kernel.AbstractAgent
activate, broadcastMessage, broadcastMessage, connectedWithCommunity, createGroup, createGroup, debug, destroyGroup, disposeMyGUI, end, foundGroup, getAddress, getAgentsWithRole, getAgentsWithRole, getAgentWithRole, getAgentWithRole, getAvailableCommunities, getBean, getController, getCurrentKernelAddress, getDebug, getExistingGroups, getExistingGroups, getExistingRoles, getExistingRoles, getGroups, getGUIObject, getMessageBoxSize, getMyGroups, getMyGroups, getMyRoles, getMyRoles, getName, getRoles, hasGUI, hashCode, hideMyGUI, initGUI, isBelongingToGroup, isBelongingToGroup, isCommunity, isGroup, isGroup, isMemberOf, isMemberOf, isMessageBoxEmpty, isRole, isRole, joinGroup, killAgent, launchAgent, launchAgent, leaveGroup, leaveGroup, leaveRole, leaveRole, nextMessage, print, println, redisplayMyGUI, requestRole, requestRole, requestRole, restoreAgent, sendMessage, sendMessage, sendMessage, setBean, setController, setDebug, setGUIObject, setName, setOutput, setOutputWriter, toString, windowClosing
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Agent

public Agent()
Method Detail

pause

public final void pause(int t)
Suspend the agent for a while. The main agent thread is put to sleep for the specified duration. If the agent developer creates additional threads, they will not be suspended

Parameters:
t - pause duration in milliseconds

waitNextMessage

public final Message waitNextMessage()
This method is the blocking version of nextMessage(). It suspends the main agent thread (but the children of the agent) until a message has been received

Returns:
the message

waitNextMessage

public final Message waitNextMessage(long timeout)
This method is the blocking version of nextMessage(), with time-out. It suspends the main agent thread until a message has been received or the time out delay elapsed (and returns null in that case)

Parameters:
timeout - the maximum time to wait, in milliseconds
Returns:
the message

receiveMessage

protected void receiveMessage(Message m)
this method is dramatically private. Nyark ;-)

Overrides:
receiveMessage in class AbstractAgent
Parameters:
m - the message

live

public void live()
This method defines the main behavior of threaded agents.


run

public final void run()
Only useful to the agent micro-kernel

Specified by:
run in interface java.lang.Runnable

createPlace

public void createPlace(java.lang.String place,
                        java.lang.String informations)

createPlace

public void createPlace(java.lang.String place)

enableMobility

public void enableMobility(java.lang.String name)

enableMobility

public void enableMobility(java.lang.String name,
                           int port)

connectAgencyToAgency

public boolean connectAgencyToAgency(java.lang.String host,
                                     int port)

joinPlace

public int joinPlace(java.lang.String place)

joinPlace

public int joinPlace(java.lang.String Community,
                     java.lang.String place,
                     java.lang.String pwd)

joinPlace

public int joinPlace(java.lang.String place,
                     java.lang.String pwd)

getAgencyNamed

public KernelAddress getAgencyNamed(java.lang.String name)

exitImmediatlyOnKill

protected final void exitImmediatlyOnKill()
Ensures that an agent will be cleanly killed. Due to the "Thread problem" (see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?. for more details) when an agent is killed, by another agent or interface (i.e. without exiting of the live method) the Kernel will try a Thread.stop on it. And the Thread problem is that this method is currently unsafe and does not work every time.

The result is that the agent has been killed, that is to say removed from organizations and invisible to others (no more messages), but its thread is still alive with unpredictable behaviors, wasting cpu time.

The sun API says : Most uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running. (This is the approach that JavaSoft's Tutorial has always recommended.)

In MadKit, agents are autonomous :)

So it is the developer responsibility to be sure that an agent cleanly exits its thread when killed.

The exitImmediatlyOnKill method is made to ease this procedure.

When a dead agent, which has been killed, calls this method, its thread immediately exits if it is still running, avoiding unpredictable behavior

For example this kind of agent may not be able to be cleanly killed by another due to its simplicity (no wait, no pause).


 public void live() {
        int i=0;
        while(true)     //UNSAFE
                i++;
}
 

instead of that, use the following :


public void live() {
        int i=0;
        while(true)     {
                exitImmediatlyOnKill();         //SAFE
                i++;
        }
}
 



Copyright © Madkit Team (O. Gutknecht, J. Ferber, F. Michel et al.) All Rights Reserved.