Extend DDS Example to use command interface

This commit is contained in:
Alexey Rybalchenko
2015-11-23 11:28:15 +01:00
parent 7744d8bc91
commit 90f0e3cb78
13 changed files with 181 additions and 238 deletions

View File

@@ -21,7 +21,6 @@
#include <boost/asio.hpp> // for DDS
#include "FairMQLogger.h"
#include "FairMQParser.h"
#include "FairMQProgOptions.h"
#include "FairMQExample3Sampler.h"
#include "FairMQTools.h"
@@ -32,8 +31,10 @@
#include "FairMQTransportFactoryZMQ.h"
#endif
#include "KeyValue.h" // DDS
#include "KeyValue.h" // DDS Key Value
#include "CustomCmd.h" // DDS Custom Commands
using namespace std;
using namespace boost::program_options;
int main(int argc, char** argv)
@@ -45,13 +46,11 @@ int main(int argc, char** argv)
try
{
std::string text; // text to be sent for processing.
std::string interfaceName; // name of the network interface to use for communication.
string interfaceName; // name of the network interface to use for communication.
options_description samplerOptions("Sampler options");
samplerOptions.add_options()
("text", value<std::string>(&text)->default_value("Hello"), "Text to send out")
("network-interface", value<std::string>(&interfaceName)->default_value("eth0"), "Name of the network interface to use (e.g. eth0, ib0, wlan0, en0...)");
("network-interface", value<string>(&interfaceName)->default_value("eth0"), "Name of the network interface to use (e.g. eth0, ib0, wlan0, en0...)");
config.AddToCmdLineOptions(samplerOptions);
@@ -60,12 +59,7 @@ int main(int argc, char** argv)
return 0;
}
std::string filename = config.GetValue<std::string>("config-json-file");
std::string id = config.GetValue<std::string>("id");
config.UserParser<FairMQParser::JSON>(filename, id);
sampler.fChannels = config.GetFairMQMap();
string id = config.GetValue<string>("id");
LOG(INFO) << "PID: " << getpid();
@@ -78,7 +72,11 @@ int main(int argc, char** argv)
sampler.SetTransport(transportFactory);
sampler.SetProperty(FairMQExample3Sampler::Id, id);
sampler.SetProperty(FairMQExample3Sampler::Text, text);
// configure data output channel
FairMQChannel dataOutChannel("push", "bind", "");
dataOutChannel.UpdateRateLogging(0);
sampler.fChannels["data-out"].push_back(dataOutChannel);
// Get the IP of the current host and store it for binding.
map<string,string> IPs;
@@ -114,6 +112,24 @@ int main(int argc, char** argv)
sampler.ChangeState("INIT_TASK");
sampler.WaitForEndOfState("INIT_TASK");
dds::custom_cmd::CCustomCmd ddsCustomCmd;
// Subscribe on custom commands
ddsCustomCmd.subscribeCmd([&](const string& command, const string& condition, uint64_t senderId)
{
LOG(INFO) << "Received custom command: " << command << " condition: " << condition << " senderId: " << senderId;
if (command == "check-state")
{
ddsCustomCmd.sendCmd(id + ": " + sampler.GetCurrentStateName(), to_string(senderId));
}
else
{
LOG(WARN) << "Received unknown command: " << command;
LOG(WARN) << "Origin: " << senderId;
LOG(WARN) << "Destination: " << condition;
}
});
sampler.ChangeState("RUN");
sampler.WaitForEndOfState("RUN");
@@ -125,7 +141,7 @@ int main(int argc, char** argv)
sampler.ChangeState("END");
}
catch (std::exception& e)
catch (exception& e)
{
LOG(ERROR) << e.what();
LOG(INFO) << "Command line options are the following: ";