Thursday, July 31, 2008

Custom messages using the PortSet

I am not sure whether this is in the tutorials, but I thought I would highlight it anyway. Making custom messages in MRDS is really easy. Also you can pass state information through them. This post should also give you insight into how the request operations on hardware works.

To create a custom message, the first step is to create the request class. This request class can contain data that you want to send through. It is declared in the <dss_name>Types.cs file. Remember that all properties need to be Serializable. That means (a) the class needs to be a [DataContract] and (b) the properties need to be ints, bools, strings, etc ([DataMember]). I do not recommend customizable objects, since these services are actually designed to pass between computers, what good is a local definition? So create the message of the structure you want:
[DataContract]
public class NewMessageRequest
{
private bool _input;

[DataMember]
public bool Input
{
get { return _input; }
set { _input = value; }
}
}

Then create a class in the <dss_name>Types.cs that inherits from the Update portset:
public class NewMessage : 
Update<NewMessageRequest,
PortSet<ReturnType, Fault>>
{
public NewMessage() :
base(new NewMessageRequest()) { }
}

What is awesome is you can specify the return type. I kinda guessed this behaviour from navel gazing at the code. That means you can post state changes directly back or whatever information you wish. I went for the state, so that the DSS is still 'responsible' for the output it gives. Then you need to write the handler for the request. This occurs in the <dss_name>.cs file:
[ServiceHandler
(ServiceHandlerBehavior.Exclusive)]
public IEnumerator<ITask>
NewMessageHandler(NewMessage msg)
{
if (beh.Body.Input)
{
//perform certain changes
}
else
{
//perform other changes
}
msg.ResponsePort.Post
(/*ReturnType instance*/);
yield break;
}

In this manner, you can create customized messages and defer computation to other services, rather than focusing all computation in one service. This is really distributable computing, making use of DSS and CCR.

0 incoming messages (comments):