Neokernel Articles
Neokernel Demos
Sales and Licensing
Support
Company

Using the Service Manager for Agent Collaboration

This article discusses how to use the Service Manager for inter agent collaboration and communication. We will look at examples of 2 Agents that register themselves into the ServiceManager so that they can be accessed by other Agents. The first Agent in this example is a RequestAgent and will register itself as "example.agent". Since this is a RequestAgent and it's registered name ends with ".agent", you can access it via the url http://localhost/example.agent (or whatever the hostname that neokernel is running on).


On startup, the register() method is called so the Agent is registered in the ServiceManager under the name "example.agent". When the handleRequest() method is called, (because http://localhost/example.agent is requested by a browser), the Agent will attempt to fetch a reference to another Agent from the ServiceManager and call one of the other Agent's methods. In this example, the method is getExampleString(). This call returns a String which is sent to the AgentRequest output so that it appears in the web browser.


Example code for this RequestAgent:


public class Agent1:RequestAgent
{
  public override void initProps()
  {
    // This is the name that this agent will be registered as
    setDefault("service_name", "example.agent");

    // This is the name of other registered agent
    //in the ServiceManager
    setDefault("other_agent_name", "other_agent");
  }


  public override void start()
  {
    // Register with the ServiceManager
    register();
  }

  public override void handleRequest(AgentRequest agentRequest)
  {
  // Get a reference to the other agent from the ServiceManager.
  // If this Agent used a common interface like a
  // RequestAgentInterface, you could cast it to a
  // object of type RequestAgentInterface to access
  // interface methods.
    Agent2 agent = (Agent2)getServiceImpl(getString("other_agent_name"));

    // Access this agent's method to get a string to return
    agentRequest.println(agent.getExampleString());
  }
}

The other Agent that the first Agent accesses can be any kind of Object (like a Hashtable, for example), but in this example we'll use a subclass of an Agent. This Agent will register itself in the ServiceManager on startup as "other_agent", a name that the first Agent uses to obtain a reference. This Agent also provides the method which the other Agent calls, getExampleString().


public class Agent2:Agent
{
  public override void initProps()
  {
    // This agent's name in the ServiceManager
    setDefault("service_name","other_agent");
  }

  public override void start()
  {
    // Register with the ServiceManager
    register();
  }

  // This is the method that the first agent will call to get a String
  public string getExampleString()
  {
    return("This is a string that is generated by the other agent");
  }
}

Along with RequestAgent and Agent, there are also ScheduledAgents and ServiceAgents. ScheduledAgents run off of a Schedule while ServiceAgents are intended to provide some kind of service (RequestAgent is a subclass of ServiceAgent). Services can be grouped together using an interface, for example:


public interface RandomStringInterface
{
  // This method is supposed to provide a random String
  void getRandomString(int salt);
}

This interface can be implemented by any number of ServiceAgents that provide different kinds of Strings, and a calling Agent can access them as needed through the ServiceManager, casting them as a RandomStringInterface, and calling the getRandomString() method.

Updated: 7/1/11 1:55 PM