Remote Call Framework 3.3
Publish/subscribe

Publish/subscribe - Publisher

This sample demonstrates a server publishing calls on a RCF interface.

#include <iostream>
#include <RCF/RCF.hpp>
// Define RCF interface.
RCF_BEGIN(I_PrintService, "I_PrintService")
RCF_METHOD_V1(void, Print, const std::string &)
RCF_END(I_PrintService)
int main()
{
try
{
RCF::RcfInit rcfInit;
RCF::RcfServer server(RCF::TcpEndpoint("127.0.0.1", 50001));
server.start();
// Configure a publisher.
typedef RCF::Publisher<I_PrintService> PrintServicePublisher;
typedef std::shared_ptr< PrintServicePublisher > PrintServicePublisherPtr;
PrintServicePublisherPtr publisherPtr = server.createPublisher<I_PrintService>();
// Publish a message once every second.
while ( true )
{
publisherPtr->publish().Print("Hello World");
RCF::sleepMs(1000);
}
}
catch ( const RCF::Exception & e )
{
std::cout << "Error: " << e.getErrorMessage() << std::endl;
}
return 0;
}

Publish/subscribe - Subscriber

This sample demonstrates a server receiving calls from a publisher.

#include <iostream>
#include <RCF/RCF.hpp>
// Define RCF interface.
RCF_BEGIN(I_PrintService, "I_PrintService")
RCF_METHOD_V1(void, Print, const std::string &)
RCF_END(I_PrintService)
// This class will receive the published messages.
class PrintService
{
public:
void Print(const std::string & s)
{
std::cout << "I_PrintService service: " << s << std::endl;
}
};
int main()
{
try
{
RCF::RcfInit rcfInit;
PrintService printService;
server.start();
// Configure a subscription.
subParms.setPublisherEndpoint(RCF::TcpEndpoint("127.0.0.1", 50001));
RCF::SubscriptionPtr subscriptionPtr = server.createSubscription<I_PrintService>(
printService,
subParms);
// At this point, should be receiving published messages from the publisher.
// ...
std::cout << "Press Enter to exit..." << std::endl;
std::cin.get();
}
catch ( const RCF::Exception & e )
{
std::cout << "Error: " << e.getErrorMessage() << std::endl;
}
return 0;
}