examples.communication
Class Agent

java.lang.Object
  extended byexamples.communication.Agent
All Implemented Interfaces:
AgentService, Resumable, Runnable, Serializable

public class Agent
extends Object
implements Resumable, Serializable, AgentService

This class represents three types of agents which jump subseqeuently to remote hosts, and communicate with each other:

SENDER - the sender agent only sends messages

RECEWIVER - the receiver agent only receives messages

TRANSCEIVER - the transceiver agent sends and receives messages

Every agent publishes its own AgentService which makes possible for the Launcher to send this agent the implicit names of all other "friend" agents or to signal for termination when an error occurs.

The agent (no matter what type) goes through the following states:

STATE_INIT_VARIABLES: in this state the agent initialises the varibles, set from the Launcher, its implicit name, etc..

STATE_REPORT_FEEDBACK: the agent reports to the Launcher errors(if they have occurred) and its implict name, nickname, the key of the service it has published and its type.

STATE_COMMUNICATE: if the agent is from type SENDER or TRANSCEIVER it tries to send message to one of the RECEIVER agents; if the agent is from type RECEIVER it checks constantly for a new message in the time between each migration.

STATE_TERMINATE: the agent terminates because of error or it has gone through its "lifecycle". Before the termination each agent should "report" the information about how many messages it has received/ sent and how many of them have benn received/not received.

This class implements the interface Resumable. This means if there was an error, which appeared during the program execution, and the agent is not in STATE_TERMINATE, it will try to migrate to the host where it was first launched and report the information it has gathered.

Author:
Zaharina Velikova, Jan Peters
See Also:
Serialized Form

Field Summary
static String COMM_TYPE_PROP
          String representation of TYPE property in agent's properties.
static String COMM_TYPE_RECEIVER
          String representation of the agent's type RECEIVER.
static String COMM_TYPE_SENDER
          String representation of the agent's type SENDER.
static String COMM_TYPE_TRANSCEIVER
          String representation of the agent's type TRANSCEIVER.
static int CT_RECEIVER
          int representation of the agent's type RECEIVER.
static int CT_SENDER
          int representation of the agent's type SENDER.
static int CT_TRANSCEIVER
          int representation of the agent's type TRANSCEIVER.
private  URL home_
          The de.fhg.igd.util.URL of the "home" of the agent(where the agent was first started).
private  String id_
          The id of the agent used for log messages.
private  String implicitName_
          The implicit name of the agent.
private  ArrayList implicitNames_
          The list with the names of the "receiver" agents.
static String JUMP_COUNT_PROP
          String representation of the property "count of jumps".
private  int jumpCount_
          Current number of successfull jumps..
private  int jumps_
          The number of times which the agent must migrate.
private  Object lock_
          Lock object for internal synchronization.
private  int messagesNotDelivered_
          The number of the messages which were not delivered.
private  int messagesReceived_
          The number of the received messages.
private  int messagesSent_
          The number of the sent messages.
private  String nickName_
          The nickname of the agent.
static String NICKNAME_PREFIX
          This is the prefix of each agent's nickname.
private  HashMap notDeliveredNotices_
          The HashMap object which contains the messages from the exceptions of not delivered messages.
private static long serialVersionUID
          Use serialVersionUID for interoperability.
static String SERVER_PREFIX_PROP
          String representation of the prefix of all urls in the agent's properties.
private  int state_
          The state of the agent.
static int STATE_COMMUNICATE
          Represents the third(main) state of the agent's lifecycle.
static int STATE_INIT_VARIABLES
          Represents the first state of the agent's lifecycle.
static int STATE_REPORT_FEEDBACK
          Represents the second state of the agent's lifecycle.
static int STATE_TERMINATE
          Represents the last state(termination) of the agent's lifecycle.
private  ArrayList targets_
          The ArrayList with the urls where the agent can migrate.
private  boolean terminate_
          The state of the agent.
private  long time_
          The time(in milliseconds) which the agent has before each migration.
private  int type_
          The type of this agent.
static String WAIT_TIME_PROP
          String representation of the "waiting time before each migration" property.
 
Fields inherited from interface examples.communication.AgentService
PUBLISH_KEY_PREFIX
 
Constructor Summary
Agent()
          Default constructor.
 
Method Summary
protected  ArrayList findValidTargets()
          Initializes the URL array from the Vicinity service.
protected  URL getLocalHost()
          Resolves the localhost url.
protected  URL getNextTarget()
          Finds a random reachable target for the next agent's migration.
 void initImplicitNames(ArrayList implicitNames)
          Initialises the implicit name of the agent's "friends"
protected  void migrate()
          Sets the Ticket according to the random choice of examples.communication.Agent#getNextTarget().
protected  void migrateHome()
          Sets the next migration target the host whrere the agent was first started.
 void resume(ErrorCode error)
          See class description.
 void run()
          See class description.
 void signalTermination()
          Sends a terminating signal to the agent.
protected  boolean stateCommunicate()
          Describes the state: STATE_COMMUNICATE.
protected  boolean stateInitVariables()
          Describes the state: STATE_INIT_VARIABLES.
protected  boolean stateReportFeedback()
          Describes the state: STATE_REPORT_FEEDBACK.
protected  void terminate()
          The agent reports the information about how many messages it has received/sent and how many of them have been received/not received and terminates.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Use serialVersionUID for interoperability.

See Also:
Constant Field Values

STATE_INIT_VARIABLES

public static final int STATE_INIT_VARIABLES
Represents the first state of the agent's lifecycle.

See Also:
description, Constant Field Values

STATE_REPORT_FEEDBACK

public static final int STATE_REPORT_FEEDBACK
Represents the second state of the agent's lifecycle.

See Also:
description, Constant Field Values

STATE_COMMUNICATE

public static final int STATE_COMMUNICATE
Represents the third(main) state of the agent's lifecycle.

See Also:
description, Constant Field Values

STATE_TERMINATE

public static final int STATE_TERMINATE
Represents the last state(termination) of the agent's lifecycle.

See Also:
description, Constant Field Values

NICKNAME_PREFIX

public static final String NICKNAME_PREFIX
This is the prefix of each agent's nickname. If there were no errors during launching an agent, it should get a nickname of type: "Agent-index:type" where index is the index under which it was launched by the Launcher and "type" is a string representation of the agent's type.

See Also:
Constant Field Values

CT_SENDER

public static final int CT_SENDER
int representation of the agent's type SENDER.

See Also:
Constant Field Values

CT_RECEIVER

public static final int CT_RECEIVER
int representation of the agent's type RECEIVER.

See Also:
Constant Field Values

CT_TRANSCEIVER

public static final int CT_TRANSCEIVER
int representation of the agent's type TRANSCEIVER.

See Also:
Constant Field Values

COMM_TYPE_SENDER

public static final String COMM_TYPE_SENDER
String representation of the agent's type SENDER.

See Also:
Constant Field Values

COMM_TYPE_RECEIVER

public static final String COMM_TYPE_RECEIVER
String representation of the agent's type RECEIVER.

See Also:
Constant Field Values

COMM_TYPE_TRANSCEIVER

public static final String COMM_TYPE_TRANSCEIVER
String representation of the agent's type TRANSCEIVER.

See Also:
Constant Field Values

COMM_TYPE_PROP

public static final String COMM_TYPE_PROP
String representation of TYPE property in agent's properties.

See Also:
Constant Field Values

SERVER_PREFIX_PROP

public static final String SERVER_PREFIX_PROP
String representation of the prefix of all urls in the agent's properties.

See Also:
Constant Field Values

WAIT_TIME_PROP

public static final String WAIT_TIME_PROP
String representation of the "waiting time before each migration" property.

See Also:
Constant Field Values

JUMP_COUNT_PROP

public static final String JUMP_COUNT_PROP
String representation of the property "count of jumps".

See Also:
Constant Field Values

implicitNames_

private ArrayList implicitNames_
The list with the names of the "receiver" agents.


targets_

private ArrayList targets_
The ArrayList with the urls where the agent can migrate.


notDeliveredNotices_

private HashMap notDeliveredNotices_
The HashMap object which contains the messages from the exceptions of not delivered messages.


nickName_

private String nickName_
The nickname of the agent.


implicitName_

private String implicitName_
The implicit name of the agent.


time_

private long time_
The time(in milliseconds) which the agent has before each migration.


jumps_

private int jumps_
The number of times which the agent must migrate. This number will be considered only if the targets for migration of the agent are more than 1.


home_

private URL home_
The de.fhg.igd.util.URL of the "home" of the agent(where the agent was first started).


type_

private int type_
The type of this agent.

See Also:
description

jumpCount_

private int jumpCount_
Current number of successfull jumps..


id_

private String id_
The id of the agent used for log messages.


lock_

private transient Object lock_
Lock object for internal synchronization.


messagesNotDelivered_

private int messagesNotDelivered_
The number of the messages which were not delivered.


messagesReceived_

private int messagesReceived_
The number of the received messages.


messagesSent_

private int messagesSent_
The number of the sent messages.


state_

private int state_
The state of the agent.


terminate_

private boolean terminate_
The state of the agent.

Constructor Detail

Agent

public Agent()
Default constructor.

Method Detail

getLocalHost

protected URL getLocalHost()
Resolves the localhost url.

Returns:
the url of the local host or null if the localhost cannot be found.

findValidTargets

protected ArrayList findValidTargets()
Initializes the URL array from the Vicinity service.

Returns:
The ArrayList of found targets or null, if Vicinity is not installed.

getNextTarget

protected URL getNextTarget()
Finds a random reachable target for the next agent's migration.

Returns:
A reachable url.

resume

public void resume(ErrorCode error)
            throws Exception
See class description.

Specified by:
resume in interface Resumable
Parameters:
error - DOCUMENT ME!
Throws:
Exception - DOCUMENT ME!

run

public void run()
See class description.

Specified by:
run in interface Runnable

migrate

protected void migrate()
Sets the Ticket according to the random choice of examples.communication.Agent#getNextTarget(). If no valid destination was found the agent terminates.

See Also:
terminate()

migrateHome

protected void migrateHome()
Sets the next migration target the host whrere the agent was first started.


terminate

protected void terminate()
The agent reports the information about how many messages it has received/sent and how many of them have been received/not received and terminates.


stateInitVariables

protected boolean stateInitVariables()
Describes the state: STATE_INIT_VARIABLES. See class description.

Returns:
DOCUMENT ME!

stateReportFeedback

protected boolean stateReportFeedback()
Describes the state: STATE_REPORT_FEEDBACK. See class description.

Returns:
DOCUMENT ME!

stateCommunicate

protected boolean stateCommunicate()
Describes the state: STATE_COMMUNICATE. See class description.

Returns:
DOCUMENT ME!

initImplicitNames

public void initImplicitNames(ArrayList implicitNames)
Description copied from interface: AgentService
Initialises the implicit name of the agent's "friends"

Specified by:
initImplicitNames in interface AgentService
Parameters:
implicitNames - DOCUMENT ME!
See Also:
AgentService.initImplicitNames(ArrayList)

signalTermination

public void signalTermination()
Description copied from interface: AgentService
Sends a terminating signal to the agent.

Specified by:
signalTermination in interface AgentService
See Also:
AgentService.signalTermination()


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.