Angel \”Java\” Lopez on Blog

October 17, 2007

Agents using Concurrency and Coordination Runtime (CCR)

Filed under: .NET, AjAgents, Concurrency and Coordination Runtime — Tags: , — ajlopez @ 11:05 am

Yesterday, I was writing a minimal example, to explore some ideas using the Concurrency and Coordination Runtime, the Microsoft library introduced with its Robotics Studio. More about CCR at:

Concurrent Affairs: Concurrency and Coordination Runtime

The idea is to implement passing of message between components, in an asynchronous way. The example code can be download from AjAgentsCCR-0.1.zip

The Agent

In the example, the interface to implement for each component, loosely named agent, is:

public delegate void ReturnHandler<T>(T t);  

public interface IAgent
{
    void Post(Message msg);
    void Post(string action,
        params object[] parameters);
    void Post<T>(string action,
        ReturnHandler<T> handler,
        params object[] parameters);
}

The components can receive message (more about that class below). But it can be invoked using an action (like a method name) and a variable list of parameters.

The third Post method to implements has a delegate, a method to run when the post of the message returns a value, in the future. You can send a message to another agent, without waiting for a response, or you can write a delegate to process the returned value.

The Message

It is a class with

action: a name to identified the action associated with the message

body: the message payload

response port: where to send a return value, if needed

The code:

public class Message
{
    private string action;  

    public string Action
    {
        get { return action; }
        set { action = value; }
    }  

    private object body;  

    public object Body
    {
        get { return body; }
        set { body = value; }
    }  

    private Port<object> returnport;  

    internal Port<object> ReturnPort
    {
        get { return returnport; }
        set { returnport = value; }
    }  

    public Message()
    {
    }  

    public Message(string action)
    {
        this.action = action;
    }  

    public Message(string action, object body)
    {
        this.action = action;
        this.body = body;
    }  

    public Message(string action, object[] bodyvalues)
    {
        this.action = action;
        if (bodyvalues != null && bodyvalues.Length == 1)
            this.body = bodyvalues[0];
        else
            this.body = bodyvalues;
    }
}

Note that the message can have an array on body values, that are put in the body as an object.

The implementation

In this initial example, the IAgent interface has an implementation based on invoke, via reflection, a method in the agent, that has a name equals to the action in the message, and that can receive the parameters that are the body of the message.

The message, when received, is put on a CCR port, so it could be handled asynchronously. There is a Receive arbiter to attend the incoming messages, that routes them to corresponding method in the current object.

I think this is an interesting solution: you can write a normal class, with normal methods, and make it an agent, inheriting from Agent class.

You can send a message with

agent1.Post("Decrement",20);

You can “query” some value with:

agent1.Post<int>("GetCounter", PrintCounter);

Future improvements

I must work on:

  • Write an Enviroment, or Host class, where the agents “live”. The Host will be resposible of maintain a list of agents by name.
  • Write a configuration file where I can specify what agents to load and start in the Environment, creating and relating the agents using something like dependency injection. I envision hosts of agents that can load remote agent code from assemblies in other servers in the network.
  • Use these agents in a distributed way, with Windows Communication Foundation (WCF), or Decentralized System Services (DSS). In this latter case, a DSS service component could be an agent or it could be an entry point to an agent Environment/Host
  • Implement some Subscribe/Notify pattern between agents.

Angel “Java” Lopez
http://www.ajlopez.com/

6 Comments »

  1. [...] Agents using Concurrency and Coordination Runtime (CCR) [...]

    Pingback by Grid Computing Programming « Angel “Java” Lopez on Blog — December 3, 2007 @ 10:54 am

  2. [...] Agents using Concurrency and Coordination Runtime (CCR) [...]

    Pingback by Genetic Algorithms with AjAgents and Concurrency and Coordination Runtime (CCR) « Angel “Java” Lopez on Blog — April 10, 2008 @ 10:19 am

  3. [...] Agentes usando Concurrency and Coordination Runtime (CCR)Agents using Concurrency and Coordination Runtime (CCR) [...]

    Pingback by Algoritmos Genéticos con AjAgents y Concurrency and Coordination Runtime (CCR) - Angel "Java" Lopez — April 13, 2008 @ 3:03 pm

  4. [...] Agents using Concurrency and Coordination Runtime (CCR) [...]

    Pingback by Presentando Microsoft Robotics Developer Studio - Angel "Java" Lopez — September 14, 2008 @ 6:07 pm

  5. [...] Sydney: CCR Thoughts Whilst in Sydney I was asked an interesting question about my usage of the CCR. So let me start by first highlighting Angel “Java” Lopez blog which has a few postings on the CCR: AjMessages, Distributed Agents using DSS/VPL and Agents. [...]

    Pingback by Sydney: CCR Thoughts « Tales from a Trading Desk — September 28, 2010 @ 7:43 am

  6. Valuable info. Fortunate me I found your website by accident, and
    I’m surprised why this coincidence didn’t came about in advance!
    I bookmarked it.

    Comment by Full Report — February 7, 2013 @ 12:34 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 56 other followers

%d bloggers like this: