Agents using Concurrency and Coordination Runtime (CCR)

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

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];
            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


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

6 thoughts on “Agents using Concurrency and Coordination Runtime (CCR)

  1. Pingback: Grid Computing Programming « Angel “Java” Lopez on Blog

  2. Pingback: Genetic Algorithms with AjAgents and Concurrency and Coordination Runtime (CCR) « Angel “Java” Lopez on Blog

  3. Pingback: Algoritmos Genéticos con AjAgents y Concurrency and Coordination Runtime (CCR) - Angel "Java" Lopez

  4. Pingback: Presentando Microsoft Robotics Developer Studio - Angel "Java" Lopez

  5. Pingback: Sydney: CCR Thoughts « Tales from a Trading Desk

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s