Handling Subscriptions

A robot needs to receive data from its sensors on a continuous basis. For example, a robot needs to be informed instantly if one of the bumper sensors has hit an object. To accommodate this need, DSSP allows services to receive event notifications from other services. A service that reads data from a bumper sensor will need to notify another service when the state of the bumper sensor has changed. The ability to handle subscriptions is a critical component of MSRS. Without this feature, it would be impossible to operate and monitor robotics applications in real time.

With a subscription involving two services (e.g., Service A and Service B). Service A could provide the state and serve as the publisher. Service B, the subscriber, requests to be notified whenever the state for service A changes. You will need to add subscription code to both services. Service A will need to support subscriptions, and Service B will need to initiate the subscribe request. Service B will then receive an event notification when the state for Service A changes (see Figure 2-6).

The relationship between a subscriber and publisher service.

Figure 2-6. The relationship between a subscriber and publisher service.

Configure the Publisher

MSRS provides a system service named SubscriptionManager. You can see this service listed in Control Panel. The SubscriptionManager service is responsible for forwarding notifications to the appropriate subscribers and maintaining a list of those subscribers. To make it convenient to call the SubscriptionManager class from the publisher, you can define a namespace alias as shown in the following code:

using submgr = Microsoft.Dss.Services.SubscriptionManager;

The SubscriptionManager service functions as a partner service for each service that needs to support subscriptions. The SubscriptionManager service is declared using the Partner attribute. For example, the following code is used to declare the SubscriptionManager as a partner for the publisher service:

[Partner("SubMgr", Contract = submgr.Contract.Identifier,
        CreationPolicy = PartnerCreationPolicy.CreateAlways)]
private submgr.SubscriptionManagerPort _submgrPort = new
        submgr.SubscriptionManagerPort();

For a service to allow subscriptions, it must provide a subscribe message that supports SubscribeRequestType and SubscribeResponseType, such as the following:

public class Subscribe : Subscribe<SubscribeRequestType,
             PortSet<SubscribeResponseType, Fault>>
{
}

Once the message has been created, it must be added to the PortSet for the main operations port. There will also need to be a service handler for the subscribe operation. This handler is responsible for adding subscribers to the list using the SubscriptionService. Assuming that your handler accepts a parameter named sub, the DsspServiceBase.SubscribeHelper method can be placed in the SubscribeHandler:

SubscribeHelper(_submgrPort, sub.Body, sub.ResponsePort);

The last thing the publisher needs to do is send notifications to subscribers whenever the state has changed. Code to initiate a send notification must be added anywhere the state is updated. For example, the following code could be added to a ReplaceHandler:

base.SendNotification(_submgrPort, replace);

As you are adding code to send notifications, keep in mind that not all operations will generate an event. For example, Get and Query do not generate events. The operations that do generate events are as follows:

  • Delete

  • Drop

  • Insert

  • Replace

  • Update

  • Upsert

Configure the Subscriber

After the publisher service is configured, you will need to add code to the subscriber service. The first thing to do is reference the proxy assembly file for the publisher service. This is because services communicate with each other through their proxy files. The proxy assembly is referenced by right-clicking References from Solution Explorer and selecting Add Reference. You will then browse to the bin subdirectory for the MSRS installation and locate the proxy.dll file for the publisher service.

After the reference is added, you will need to change certain properties for the assembly to prevent it from being copied to the output directory for the subscriber service. By doing this, you will ensure that you are always referencing the correct version of the assembly. To change the properties, locate the assembly in the References folder, right-click the assembly, and then select Properties. Change the Copy Local and Specific Version properties to false.

To access the publisher class easily, you may add a namespace alias declaration to the subscriber’s implementation class. You will need to add a Partner declaration, similar to the one created in the subscriber service. This will allow the subscriber service to access the Subscribe operation that was added to the publisher service. This is what allows the subscriber to receive notifications when the state for the publisher service changes. The last thing to do is to add code to the subscriber service that calls the Subscribe operation. To see a step-by-step example of configuring the publisher and subscriber, refer to Service Tutorials 4 and 5, available at http://msdn2.microsoft.com/library/bb905438. You can also take a look at the Atom/RSS Syndication service tutorial, available at http://msdn2.microsoft.com/library/bb648747.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.82.93