Asynchronous Dispatching of Remote Calls
This sample demonstrates asynchronous server-side dispatching of remote calls.
#include <iostream>
#include <deque>
#include <RCF/RCF.hpp>
RCF_BEGIN(I_PrintService, "I_PrintService")
RCF_METHOD_R1(int, Print, const std::string &)
RCF_END(I_PrintService)
class PrintService
{
public:
int Print(const std::string & msg)
{
RCF::Lock lock(mPrintCallsMutex);
mPrintCalls.push_back(PrintCall(RCF::getCurrentRcfSession()));
return 0;
}
PrintService()
{
mStopFlag = false;
mPrinterThreadPtr.reset(new RCF::Thread(
[&]() { processPrintCalls(); }));
}
~PrintService()
{
mStopFlag = true;
mPrinterThreadPtr->join();
}
private:
RCF::Mutex mPrintCallsMutex;
std::deque<PrintCall> mPrintCalls;
RCF::ThreadPtr mPrinterThreadPtr;
volatile bool mStopFlag;
void processPrintCalls()
{
while ( !mStopFlag )
{
Sleep(100);
std::deque<PrintCall> printCalls;
{
RCF::Lock lock(mPrintCallsMutex);
printCalls.swap(mPrintCalls);
}
for ( std::size_t i = 0; i < printCalls.size(); ++i )
{
PrintCall & printCall = printCalls[i];
const std::string & stringToPrint = printCall.parameters().a1.get();
std::cout << "I_PrintService service: " << stringToPrint << std::endl;
printCall.parameters().r.set( (int) stringToPrint.size());
printCall.commit();
}
}
}
};
int main()
{
try
{
PrintService printService;
server.
bind<I_PrintService>(printService);
std::cout << "Press Enter to exit..." << std::endl;
std::cin.get();
}
{
}
return 0;
}
Asynchronous Invocation of Remote Calls
This sample demonstrates asynchronous client-side invocation of remote calls.
#include <iostream>
#include <RCF/RCF.hpp>
RCF_BEGIN(I_PrintService, "I_PrintService")
RCF_METHOD_R1(int, Print, const std::string &)
RCF_END(I_PrintService)
typedef std::shared_ptr< RcfClient<I_PrintService> > PrintServicePtr;
void onPrintCompleted(PrintServicePtr client,
RCF::Future<
int> fRet)
{
std::unique_ptr<RCF::Exception> ePtr = fRet.getAsyncException();
if ( ePtr.get() )
{
std::cout << "Print() returned an exception: " << ePtr->getErrorMessage() << std::endl;
}
else
{
std::cout << "Print() returned: " << *fRet << std::endl;
}
}
int main()
{
try
{
std::cout << std::endl;
std::cout << "Synchronous call:" << std::endl;
int ret = client.Print("Hello World");
std::cout << "Print() returned: " << ret << std::endl;
{
std::cout << std::endl;
std::cout << "Asynchronous call with polling:" << std::endl;
{
RCF::sleepMs(500);
}
std::unique_ptr<RCF::Exception> ePtr = fRet.getAsyncException();
if ( ePtr )
{
std::cout << "Print() returned an exception: " << ePtr->getErrorMessage() << std::endl;
}
else
{
std::cout << "Print() returned: " << *fRet << std::endl;
}
}
{
PrintServicePtr clientPtr(
new RcfClient<I_PrintService>(
RCF::TcpEndpoint(50001)));
std::cout << std::endl;
std::cout << "Asynchronous call with completion callback:" << std::endl;
auto onCompletion = [=]() { onPrintCompleted(clientPtr, fRet); };
fRet = clientPtr->Print(
RCF::AsyncTwoway(onCompletion),
"Hello World");
}
RCF::sleepMs(1000);
std::cout << std::endl;
std::cout << "Press Enter to exit..." << std::endl;
std::cin.get();
}
{
}
return 0;
}