fr.dyade.aaa.agent

Class Driver

Known Direct Subclasses:
AgentDriver

public abstract class Driver
extends java.lang.Object

Abstract base class for drivers such as DriverIn used by ProxyAgent.

Multithreading in the agent server is special, as the code executed outside of the main engine thread is no longer part of a transaction. The class Driver has been designed to help programming in a separate thread in the agent server.

A key difference between the main thread and a driver thread is the semantics of sending a notification. When in the main thread of the agent server, a notification is sent as part of an agent reaction. It is then part of a transaction, and is actually sent only when the transaction commits. When in a separate thread, the directSendto function of class Channel is used instead, actually creating a new transaction for the single purpose of sending the notification. When the function returns the notification sending has been committed. The function sendTo in this class encapsulates the call to the special Channel function, and should be used by derived classes.

This class is designed to be derived. Derived classes should define the function run, providing the actual code to be executed in the separate thread, and may define the function end, providing finalizing code executed when the thread terminates. The end function is always called, even when the run function raises an exception.

Field Summary

boolean
canStop
protected static Hashtable
drivers
protected int
id
identifier local to the driver creator
boolean
isRunning
Boolean variable used to stop the driver properly.
protected Logger
logmon
protected String
name
protected Thread
thread
separate thread running the driver

Constructor Summary

Driver()
Constructor with default id.
Driver(int id)
Constructor.

Method Summary

void
close()
protected void
end()
Finalizes driver; called in finally clause of driver thread.
String
getName()
Returns name of driver, actually classname and driver id.
void
run()
Actually executes the driver code.
protected void
sendTo(AgentId to, Notification not)
Sends a notification to an agent.
void
start()
Starts the driver execution.
void
stop()
Stops the driver execution.
String
toString()
Provides a string image for this object.

Field Details

canStop

public boolean canStop


drivers

protected static Hashtable drivers


id

protected int id
identifier local to the driver creator


isRunning

public boolean isRunning
Boolean variable used to stop the driver properly. If this variable is true then it indicates that the driver is stopping.


logmon

protected Logger logmon


name

protected String name


thread

protected Thread thread
separate thread running the driver

Constructor Details

Driver

protected Driver()
Constructor with default id.


Driver

protected Driver(int id)
Constructor.

Parameters:
id - identifier local to the driver creator

Method Details

close

public void close()


end

protected void end()
Finalizes driver; called in finally clause of driver thread. May be overloaded in derived classes.


getName

public String getName()
Returns name of driver, actually classname and driver id. It should be overloaded in subclass to take in account the proxy name.


run

public void run()
            throws Exception
Actually executes the driver code. To be defined in derived classes.

Beware: this code is executed in a separate thread, outside from any transaction. Notifications may be sent using function sendTo, and they will actually be sent as soon as the function is called; there is no atomic treatment as there is in an agent reaction.


sendTo

protected final void sendTo(AgentId to,
                            Notification not)
            throws IOException
Sends a notification to an agent.

Provides a similar function as the sendTo function in Agent class, except that the notification is sent directly via a directSendTo method.

Parameters:
to - target agent
not - notification to send


start

public void start()
Starts the driver execution.


stop

public void stop()
Stops the driver execution.


toString

public String toString()
Provides a string image for this object.

Returns:
printable image of this object


Copyright B) 2004 Scalagent - All rights reserved