RCF provides a built in publish/subscribe implementation.
Publish/subscribe is a messaging paradigm where publishers send out messsages to groups of subscribers. Rather than publishers being aware of individual subscribers and sending messages directly to them, publishers instead categorize their messages into topics, and subscribers choose which topics to receive messages from. When a publisher publishes a message on a particular topic, all subscribers subscribing to that topic will receive the message.
Publish/subscribe can be used in the presence of firewalls and NAT's. The only network topology requirement is that the subscriber must be able to initiate a network connection to the publisher. Publishers will never attempt to establish network connections to their subscribers.
To create a publisher, use RCF::RcfServer::createPublisher<>()
:
This will create a publisher with a default topic name. The default topic name is the runtime name of the RCF interface passed to RCF::RcfServer::createPublisher<>()
(in this case "I_PrintService"
).
The topic name can also be set explicitly. For example, to create two publishers, with different topic names, using the same RCF interface:
The RCF::Publisher<>
object returned by RCF::RcfServer::createPublisher<>()
is used to publish remote calls. Published remote calls always have one-way semantics, and are received by all subscribers currently subscribing to that publishing topic. To publish a call, use RCF::Publisher<>::publish()
:
The publishing topic is closed, and all of its subscribers disconnected, when the RCF::Publisher<>
object is destroyed, or when RCF::Publisher<>::close()
is called.
To subscribe to a publisher, use RCF::RcfServer::createSubscription<>()
:
As for publishers, the topic name defaults to the runtime name of the RCF interface (in this case "I_PrintService"
). The topic name can also be specified explicitly:
The first parameter passed to RCF::RcfServer::createSubscription<>()
is the object which will receive the published messages. It is the applications responsibility to make sure this object is not destroyed while the subscription is still connected.
A disconnect callback can be provided, which will be called if the subscriber is disconnected:
To terminate a subscription, destroy the Subscription
object, or call Subscription::close()
:
Access controls can be applied to publishers, in the form of an access control callback which will be called on the publishing server, for each subscriber attempting to set up a subscription. Similarly to servant binding access controls, publisher access controls can be used to inspect the RCF::RcfSession
of the subscriber connections for any relevant authentication information: