com.almende.eve.agent
Class Agent

java.lang.Object
  extended by com.almende.eve.agent.Agent
All Implemented Interfaces:
AgentInterface
Direct Known Subclasses:
CalcAgent, ChatAgent, EchoAgent, LogAgent, ManagementAgent, TestAgent

public abstract class Agent
extends Object
implements AgentInterface


Constructor Summary
Agent()
           
 
Method Summary
 void clear()
          Deprecated. 
 void create()
          This method is called once in the life time of an agent, at the moment the agent is being created by the AgentFactory.
<T> T
createAgentProxy(String url, Class<T> agentInterface)
          Create a proxy to an other agent.
 void delete()
          This method is called once in the life time of an agent, at the moment the agent is being deleted by the AgentFactory.
 void destroy()
          This method can is called when the agent is uninitialized, and is needed finalize the state of the agent.
protected  void finalize()
           
 AgentFactory getAgentFactory()
          Get the agent factory.
 State getContext()
          Deprecated. Use getState() instead
abstract  String getDescription()
          Retrieve a description of the agents functionality
 String getId()
          Get the Id of this agent
 List<Object> getMethods(Boolean asJSON)
          Get all available methods of this agent
 Scheduler getScheduler()
          Get a scheduler to schedule tasks for the agent to be executed later on.
 State getState()
          Get the agents state.
 String getType()
          Retrieve the type name of this agent, its class
 List<String> getUrls()
          Get the urls of this agent, for example "http://mysite.com/agents/key".
abstract  String getVersion()
          Retrieve the agents version number
 void init()
          This method is called directly after the agent and its state is initiated.
 String onSubscribe(String event, String callbackUrl, String callbackMethod)
          Let an other agent subscribe to one of this agents events When the event is triggered, a callback will be send to the provided callbackUrl.
 void onTrigger(String url, String method, com.fasterxml.jackson.databind.node.ObjectNode params)
          Asynchronously trigger an event.
 void onUnsubscribe(String subscriptionId, String event, String callbackUrl, String callbackMethod)
          Let an other agent unsubscribe from one of this agents events - If subscriptionId is provided, the subscription with this id will be deleted - If the parameter callbackUrl and optionally event and/or callbackMethod, all subscriptions with matching parameters will be deleted.
 void send(String url, String method)
          Send a request to an agent in JSON-RPC format
<T> T
send(String url, String method, Class<T> type)
          Send a request to an agent in JSON-RPC format
 void send(String url, String method, Object params)
          Send a request to an agent in JSON-RPC format
<T> T
send(String url, String method, Object params, Class<T> type)
          Send a request to an agent in JSON-RPC format
<T> void
sendAsync(String url, JSONRequest request, AsyncCallback<T> callback, Class<T> type)
          Send an asynchronous JSON-RPC request to an agent sendAsync is not supported on Google App Engine
<T> void
sendAsync(String url, String method, com.fasterxml.jackson.databind.node.ObjectNode params, AsyncCallback<T> callback, Class<T> type)
          Send an asynchronous JSON-RPC request to an agent sendAsync is not supported on Google App Engine
 void setAgentFactory(AgentFactory agentFactory)
           
 void setState(State state)
          Set the state of the agent instance.
protected  String subscribe(String url, String event, String callbackMethod)
          Subscribe to an other agents event
 String toString()
           
 void trigger(String event, Object params)
          Trigger an event
protected  void unsubscribe(String url, String subscriptionId)
          Unsubscribe from an other agents event
protected  void unsubscribe(String url, String event, String callbackMethod)
          Unsubscribe from an other agents event
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Agent

public Agent()
Method Detail

getDescription

public abstract String getDescription()
Description copied from interface: AgentInterface
Retrieve a description of the agents functionality

Specified by:
getDescription in interface AgentInterface
Returns:
description

getVersion

public abstract String getVersion()
Description copied from interface: AgentInterface
Retrieve the agents version number

Specified by:
getVersion in interface AgentInterface
Returns:
version

create

public void create()
This method is called once in the life time of an agent, at the moment the agent is being created by the AgentFactory. It can be overridden and used to perform some action when the agent is create, in that case super.create() should be called in the overridden create().


delete

public void delete()
This method is called once in the life time of an agent, at the moment the agent is being deleted by the AgentFactory. It can be overridden and used to perform some action when the agent is deleted, in that case super.delete() should be called in the overridden delete().


init

public void init()
This method is called directly after the agent and its state is initiated. It can be overridden and used to perform some action when the agent is initialized, in that case super.init() should be called in the overridden init().


destroy

public void destroy()
This method can is called when the agent is uninitialized, and is needed finalize the state of the agent. It can be overridden and used to perform some action when the agent is uninitialized, in that case super.destroy() should be called in the overridden destroy().


finalize

protected void finalize()
Overrides:
finalize in class Object

setState

public final void setState(State state)
Set the state of the agent instance. This method is used by the AgentFactory.

Parameters:
state -

getState

public final State getState()
Get the agents state. The state contains methods get, put, etc. to write properties into a persistent state.

Parameters:
state -

getContext

@Deprecated
public final State getContext()
Deprecated. Use getState() instead

Get the agents state. The state contains methods get, put, etc. to write properties into a persistent state.

Parameters:
state -

getScheduler

public final Scheduler getScheduler()
Get a scheduler to schedule tasks for the agent to be executed later on.

Parameters:
state -

setAgentFactory

public final void setAgentFactory(AgentFactory agentFactory)

getAgentFactory

public final AgentFactory getAgentFactory()
Get the agent factory. The agent factory can create/delete agents.

Returns:

clear

@Deprecated
public void clear()
           throws Exception
Deprecated. 

Clear the agents state, unsubscribe from all subscribed events, cancel all running tasks

Throws:
Exception

onSubscribe

public final String onSubscribe(String event,
                                String callbackUrl,
                                String callbackMethod)
Let an other agent subscribe to one of this agents events When the event is triggered, a callback will be send to the provided callbackUrl.

Specified by:
onSubscribe in interface AgentInterface
Parameters:
event -
callbackUrl -
callbackMethod -
Returns:
subscriptionId

onUnsubscribe

public final void onUnsubscribe(String subscriptionId,
                                String event,
                                String callbackUrl,
                                String callbackMethod)
Let an other agent unsubscribe from one of this agents events - If subscriptionId is provided, the subscription with this id will be deleted - If the parameter callbackUrl and optionally event and/or callbackMethod, all subscriptions with matching parameters will be deleted. (if only callbackUrl is provided, all subscriptions from this agent will be deleted).

Specified by:
onUnsubscribe in interface AgentInterface
Parameters:
subscriptionId -
event -
callbackUrl -
callbackMethod -

onTrigger

public final void onTrigger(String url,
                            String method,
                            com.fasterxml.jackson.databind.node.ObjectNode params)
                     throws Exception
Asynchronously trigger an event. the onTrigger method is called from a scheduled task, initiated in the method trigger

Parameters:
url -
method -
params -
Throws:
Exception

subscribe

protected String subscribe(String url,
                           String event,
                           String callbackMethod)
                    throws Exception
Subscribe to an other agents event

Parameters:
url -
event -
callbackMethod -
Returns:
subscriptionId
Throws:
Exception

unsubscribe

protected void unsubscribe(String url,
                           String subscriptionId)
                    throws Exception
Unsubscribe from an other agents event

Parameters:
url -
subscriptionId -
Throws:
Exception

unsubscribe

protected void unsubscribe(String url,
                           String event,
                           String callbackMethod)
                    throws Exception
Unsubscribe from an other agents event

Parameters:
url -
event -
callbackMethod -
Throws:
Exception

trigger

public final void trigger(String event,
                          Object params)
                   throws Exception
Trigger an event

Parameters:
event -
params - An ObjectNode, Map, or POJO
Throws:
Exception
JSONRPCException

getMethods

public final List<Object> getMethods(Boolean asJSON)
Get all available methods of this agent

Specified by:
getMethods in interface AgentInterface
Parameters:
asJSON - If true, result is in a JSON format easily parsable by a machine. If false (default), the returned list contains human readable strings.
Returns:

send

public final <T> T send(String url,
                        String method,
                        Object params,
                        Class<T> type)
             throws Exception
Send a request to an agent in JSON-RPC format

Parameters:
url - The url of the agent
method - The name of the method
params - A Object containing the parameter values of the method. This can be an ObjectNode, Map, or POJO.
type - The return type of the method
Returns:
Throws:
Exception

send

public final <T> T send(String url,
                        String method,
                        Class<T> type)
             throws Exception
Send a request to an agent in JSON-RPC format

Parameters:
url - The url of the agent
method - The name of the method
Returns:
Throws:
Exception

send

public final void send(String url,
                       String method,
                       Object params)
                throws Exception
Send a request to an agent in JSON-RPC format

Parameters:
url - The url of the agent
method - The name of the method
params - A Object containing the parameter values of the method. This can be an ObjectNode, Map, or POJO.
Throws:
Exception

createAgentProxy

public final <T> T createAgentProxy(String url,
                                    Class<T> agentInterface)
Create a proxy to an other agent. Invoked methods will be send to the actual agent via the AgentFactory.

Parameters:
url -
agentInterface - A Java Interface, extending AgentInterface
Returns:
agentProxy

send

public final void send(String url,
                       String method)
                throws Exception
Send a request to an agent in JSON-RPC format

Parameters:
url - The url of the agent
method - The name of the method
Throws:
Exception

sendAsync

public final <T> void sendAsync(String url,
                                String method,
                                com.fasterxml.jackson.databind.node.ObjectNode params,
                                AsyncCallback<T> callback,
                                Class<T> type)
                     throws Exception
Send an asynchronous JSON-RPC request to an agent sendAsync is not supported on Google App Engine

Parameters:
url - The url of the agent to be called
method - The name of the method
params - A JSONObject containing the parameter values of the method
callback - An AsyncCallback of which the onSuccess or onFailure method will be executed on callback.
type - The type of result coming from the callback.
Throws:
Exception
JSONException

sendAsync

public final <T> void sendAsync(String url,
                                JSONRequest request,
                                AsyncCallback<T> callback,
                                Class<T> type)
                     throws Exception
Send an asynchronous JSON-RPC request to an agent sendAsync is not supported on Google App Engine

Parameters:
url - The url of the agent to be called
request - JSON-RPC request containing method and params
callback - An AsyncCallback of which the onSuccess or onFailure method will be executed on callback.
type - The type of result coming from the callback.
Throws:
Exception
JSONException

getUrls

public final List<String> getUrls()
Get the urls of this agent, for example "http://mysite.com/agents/key". An agent can have multiple urls for different configured communication services, such as HTTP and XMPP.

Specified by:
getUrls in interface AgentInterface
Returns:
urls

getId

public final String getId()
Get the Id of this agent

Specified by:
getId in interface AgentInterface
Returns:

getType

public final String getType()
Retrieve the type name of this agent, its class

Specified by:
getType in interface AgentInterface
Returns:
classname

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2013 Almende BV. All Rights Reserved.