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, AbstractServerAgent, AbstractShareAgent, AbstractShareAgent, BeanShellAgent, BeeLauncher, Broker, ChanInfoAgent, ChatAgent, Client, DesktopAgent, DocBrowserAgent, DynamicTwoChannelsCommunicator, ExplorerLauncher, FormalismAgent, GPongAgent, GraphicGroupObserver, GroupMessageTracer, GroupObserver, Hello, JessAgent, JessMonitor, Launcher, LineChartAgent, MadChatHello, MadkitNetworkAgent, MadkitOutput, ManagerAgent, MemoryMonitorAgent, OrganizationTracer, OutputRerouter, Pager, PingPong, PluginAgent, PluginDesignerAgent, PrivateAgent, PrivateAgent1, ProgressAgent, ProgressBar, ProgressBar, PropertyAgent, Provider, PythonAgent, Scheduler, SchemeAgent, SEditAgent, SmallSearchFileAgent, SmallSearchFileAgent, SmallSenderAgent, SmallSenderAgent, SmallShareAgent, SmallShareAgent, SmallUpdatePluginAgent, 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 lifecycle, messaging, graphical interface, group and roles management, agent information, ... The agent behavior intentionnaly is *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. after the activate() method has been called on this agent). That means that you should not use any of the agent methods in constructor

Author:
Olivier Gutknecht, Fabien Michel : MadKit 3.0 revizion
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()
          Due to the "Thread problem" (see http://java.sun.com/j2se/1.4/docs/api/index.html for more details) when an agent is killed, by another agent or interface (unvolontary end: whithout exiting of the live method) the Kernel will try a Thread.stop on it.
 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 for threaded agents.
 void pause(int t)
          Suspend the agent for a while.
 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, disposeMyGUI, end, foundGroup, getAddress, getAgentInformation, getAgentsWithRole, getAgentsWithRole, getAgentWithRole, getAgentWithRole, getAvailableCommunities, getBean, getController, getCurrentKernelAddress, getDebug, getExistingGroups, getExistingGroups, getExistingRoles, getExistingRoles, getGroups, getGUIObject, getMessageBoxSize, getMyGroups, getMyGroups, getMyRoles, getMyRoles, getName, getRoles, hasGUI, 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, windowClosing
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, 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 not the whole threadgroup) until a message has been received


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

receiveMessage

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

Overrides:
receiveMessage in class AbstractAgent

live

public void live()
This method defines the main behavior for 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()
Due to the "Thread problem" (see http://java.sun.com/j2se/1.4/docs/api/index.html for more details) when an agent is killed, by another agent or interface (unvolontary end: whithout 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 by 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 developper responsability to be sure that an agent cleanly exit its thread when dying. The exitImmediatlyOnKill method is made to easy this procedure. When a dead agent, that has been killed, calls this method, its thread immediatly exits if it is still running, avoiding to become a "zombie agent" 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++; } public void live() { int i=0; while(true) { exitImmediatlyOnKill(); //SURE CODE i++; } }



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