QC example: add setting of the property, test if its successful

This commit is contained in:
Alexey Rybalchenko 2020-02-20 16:33:51 +01:00 committed by Dennis Klein
parent beff0af51b
commit 8123a6ecab
8 changed files with 47 additions and 83 deletions

View File

@ -15,7 +15,7 @@ add_subdirectory(multipart)
add_subdirectory(multiple-channels) add_subdirectory(multiple-channels)
if(BUILD_NANOMSG_TRANSPORT) if(BUILD_NANOMSG_TRANSPORT)
add_subdirectory(multiple-transports) add_subdirectory(multiple-transports)
endif() endif()
add_subdirectory(qc) add_subdirectory(qc)
add_subdirectory(readout) add_subdirectory(readout)
add_subdirectory(region) add_subdirectory(region)

View File

@ -52,9 +52,11 @@ fairmq-dds-command-ui -c b
fairmq-dds-command-ui -c x fairmq-dds-command-ui -c x
fairmq-dds-command-ui -c j fairmq-dds-command-ui -c j
fairmq-dds-command-ui -c r fairmq-dds-command-ui -c r
sampler_and_sink="main/(Sampler|Sink).*" qcconsumer="main/QCConsumer.*"
fairmq-dds-command-ui -w "RUNNING->READY" -p $sampler_and_sink qcproducer="main/QCProducer.*"
echo "...$sampler_and_sink are READY, sending shutdown..." fairmq-dds-command-ui -c p --property-key qc --property-value active -p $qcproducer
fairmq-dds-command-ui -w "RUNNING->READY" -p $qcconsumer
echo "...$qcconsumer received data and transitioned to READY, sending shutdown..."
fairmq-dds-command-ui -c s fairmq-dds-command-ui -c s
fairmq-dds-command-ui -c t fairmq-dds-command-ui -c t
fairmq-dds-command-ui -c d fairmq-dds-command-ui -c d

View File

@ -14,9 +14,9 @@ class QCConsumer : public FairMQDevice
public: public:
QCConsumer() QCConsumer()
{ {
OnData("qc", [](FairMQMessagePtr& /*msg*/, int){ OnData("qc", [](FairMQMessagePtr& /*msg*/, int) {
LOG(info) << "received data"; LOG(info) << "received data";
return true; return false;
}); });
} }
}; };

View File

@ -14,15 +14,13 @@ class QCProducer : public FairMQDevice
public: public:
QCProducer() QCProducer()
: fDoQC(false) : fDoQC(false)
, fCounter(0)
, fInterval(100)
{ {
OnData("data1", &QCProducer::HandleData); OnData("data1", &QCProducer::HandleData);
} }
void InitTask() override void InitTask() override
{ {
GetConfig()->Subscribe<std::string>("qc", [&](const std::string& key, std::string value) { GetConfig()->Subscribe<std::string>("qcdevice", [&](const std::string& key, std::string value) {
if (key == "qc") { if (key == "qc") {
if (value == "active") { if (value == "active") {
fDoQC.store(true); fDoQC.store(true);
@ -37,13 +35,10 @@ class QCProducer : public FairMQDevice
bool HandleData(FairMQMessagePtr& msg, int) bool HandleData(FairMQMessagePtr& msg, int)
{ {
if (fDoQC.load() == true) { if (fDoQC.load() == true) {
if (++fCounter == fInterval) { FairMQMessagePtr msgCopy(NewMessage());
fCounter = 0; msgCopy->Copy(*msg);
FairMQMessagePtr msgCopy(NewMessage()); if (Send(msg, "qc") < 0) {
msgCopy->Copy(*msg); return false;
if (Send(msg, "qc") < 0) {
return false;
}
} }
} }
@ -54,12 +49,10 @@ class QCProducer : public FairMQDevice
return true; return true;
} }
void ResetTask() override { GetConfig()->Unsubscribe<std::string>("qc"); } void ResetTask() override { GetConfig()->Unsubscribe<std::string>("qcdevice"); }
private: private:
std::atomic<bool> fDoQC; std::atomic<bool> fDoQC;
int fCounter;
int fInterval;
}; };
void addCustomOptions(boost::program_options::options_description& /*options*/) {} void addCustomOptions(boost::program_options::options_description& /*options*/) {}

View File

@ -15,41 +15,22 @@
class Sampler : public FairMQDevice class Sampler : public FairMQDevice
{ {
public: public:
Sampler() Sampler() {}
: fMaxIterations(0)
, fNumIterations(0)
{}
protected: protected:
uint64_t fMaxIterations;
uint64_t fNumIterations;
virtual void InitTask()
{
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
}
virtual bool ConditionalRun() virtual bool ConditionalRun()
{ {
FairMQMessagePtr msg(NewMessage(1000)); FairMQMessagePtr msg(NewMessage(1000));
if (Send(msg, "data1") < 0) { if (Send(msg, "data1") < 0) {
return false; return false;
} else if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) {
LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
return false;
} }
std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
return true; return true;
} }
}; };
namespace bpo = boost::program_options; namespace bpo = boost::program_options;
void addCustomOptions(bpo::options_description& options) void addCustomOptions(bpo::options_description& options) {}
{
options.add_options()
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
}
FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) { return new Sampler(); } FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) { return new Sampler(); }

View File

@ -14,38 +14,12 @@
class Sink : public FairMQDevice class Sink : public FairMQDevice
{ {
public: public:
Sink() Sink() { OnData("data2", &Sink::HandleData); }
: fMaxIterations(0)
, fNumIterations(0)
{
OnData("data2", &Sink::HandleData);
}
protected: protected:
virtual void InitTask() bool HandleData(FairMQMessagePtr& /*msg*/, int /*index*/) { return true; }
{
fMaxIterations = fConfig->GetProperty<uint64_t>("max-iterations");
}
bool HandleData(FairMQMessagePtr& /*msg*/, int /*index*/)
{
if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) {
LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
return false;
}
return true;
}
private:
uint64_t fMaxIterations;
uint64_t fNumIterations;
}; };
namespace bpo = boost::program_options; namespace bpo = boost::program_options;
void addCustomOptions(bpo::options_description& options) void addCustomOptions(bpo::options_description& options) {}
{
options.add_options()
("max-iterations", bpo::value<uint64_t>()->default_value(0), "Maximum number of iterations of Run/ConditionalRun/OnData (0 - infinite)");
}
FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) { return new Sink(); } FairMQDevicePtr getDevice(const fair::mq::ProgOptions& /*config*/) { return new Sink(); }

View File

@ -17,12 +17,12 @@ set_target_properties(${plugin} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/fairmq LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/fairmq
) )
set(exe1 fairmq-dds-command-ui) set(exe fairmq-dds-command-ui)
add_executable(${exe1} ${CMAKE_CURRENT_SOURCE_DIR}/runDDSCommandUI.cxx) add_executable(${exe} ${CMAKE_CURRENT_SOURCE_DIR}/runDDSCommandUI.cxx)
target_link_libraries(${exe1} FairMQ Commands SDK StateMachine DDS::dds_intercom_lib DDS::dds_protocol_lib) target_link_libraries(${exe} FairMQ Commands SDK StateMachine DDS::dds_intercom_lib DDS::dds_protocol_lib)
target_include_directories(${exe1} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(${exe} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
install(TARGETS ${plugin} ${exe1} install(TARGETS ${plugin} ${exe}
EXPORT ${PROJECT_EXPORT_SET} EXPORT ${PROJECT_EXPORT_SET}
LIBRARY DESTINATION ${PROJECT_INSTALL_LIBDIR} LIBRARY DESTINATION ${PROJECT_INSTALL_LIBDIR}
RUNTIME DESTINATION ${PROJECT_INSTALL_BINDIR} RUNTIME DESTINATION ${PROJECT_INSTALL_BINDIR}

View File

@ -52,11 +52,11 @@ struct TerminalConfig
void printControlsHelp() void printControlsHelp()
{ {
cout << "Use keys to control the devices:" << endl; cout << "Use keys to control the devices:" << endl;
cout << "[c] check states, [o] dump config, [h] help, [r] run, [s] stop, [t] reset task, [d] reset device, [q] end, [j] init task, [i] init device, [k] complete init, [b] bind, [x] connect" << endl; cout << "[c] check states, [o] dump config, [h] help, [r] run, [s] stop, [t] reset task, [d] reset device, [q] end, [j] init task, [i] init device, [k] complete init, [b] bind, [x] connect, [p] set property" << endl;
cout << "To quit press Ctrl+C" << endl; cout << "To quit press Ctrl+C" << endl;
} }
void handleCommand(const string& command, const string& path, unsigned int timeout, Topology& topo) void handleCommand(const string& command, const string& path, unsigned int timeout, Topology& topo, const string& pKey, const string& pVal)
{ {
if (command == "c") { if (command == "c") {
cout << "> checking state of the devices" << endl; cout << "> checking state of the devices" << endl;
@ -73,6 +73,16 @@ void handleCommand(const string& command, const string& path, unsigned int timeo
cout << d.first << ": " << p.first << " : " << p.second << endl; cout << d.first << ": " << p.first << " : " << p.second << endl;
} }
} }
} else if (command == "p") {
if (pKey == "" || pVal == "") {
cout << "cannot send property with empty key and/or value! given key: '" << pKey << "', value: '" << pVal << "'." << endl;
return;
}
const DeviceProperties props{{pKey, pVal}};
cout << "> sending property" << endl;
topo.SetProperties(props, path);
// give dds time to complete request
this_thread::sleep_for(chrono::milliseconds(100));
} else if (command == "i") { } else if (command == "i") {
cout << "> init devices" << endl; cout << "> init devices" << endl;
topo.ChangeState(TopologyTransition::InitDevice, std::chrono::milliseconds(timeout)); topo.ChangeState(TopologyTransition::InitDevice, std::chrono::milliseconds(timeout));
@ -112,10 +122,10 @@ void handleCommand(const string& command, const string& path, unsigned int timeo
} }
} }
void sendCommand(const string& commandIn, const string& path, unsigned int timeout, Topology& topo) void sendCommand(const string& commandIn, const string& path, unsigned int timeout, Topology& topo, const string& pKey, const string& pVal)
{ {
if (commandIn != "") { if (commandIn != "") {
handleCommand(commandIn, path, timeout, topo); handleCommand(commandIn, path, timeout, topo, pKey, pVal);
return; return;
} }
@ -128,7 +138,7 @@ void sendCommand(const string& commandIn, const string& path, unsigned int timeo
command = c; command = c;
while (true) { while (true) {
handleCommand(command, path, timeout, topo); handleCommand(command, path, timeout, topo, pKey, pVal);
cin >> c; cin >> c;
command = c; command = c;
} }
@ -142,6 +152,8 @@ try {
string command; string command;
string path; string path;
string targetState; string targetState;
string pKey;
string pVal;
unsigned int timeout; unsigned int timeout;
fair::Logger::SetConsoleSeverity("debug"); fair::Logger::SetConsoleSeverity("debug");
@ -166,6 +178,8 @@ try {
options.add_options() options.add_options()
("command,c", bpo::value<string>(&command)->default_value(""), "Command character") ("command,c", bpo::value<string>(&command)->default_value(""), "Command character")
("path,p", bpo::value<string>(&path)->default_value(""), "DDS Topology path to send command to (empty - send to all tasks)") ("path,p", bpo::value<string>(&path)->default_value(""), "DDS Topology path to send command to (empty - send to all tasks)")
("property-key", bpo::value<string>(&pKey)->default_value(""), "property key to be used with 'p' command")
("property-value", bpo::value<string>(&pVal)->default_value(""), "property value to be used with 'p' command")
("wait-for-state,w", bpo::value<string>(&targetState)->default_value(""), "Wait until targeted FairMQ devices reach the given state") ("wait-for-state,w", bpo::value<string>(&targetState)->default_value(""), "Wait until targeted FairMQ devices reach the given state")
("timeout,t", bpo::value<unsigned int>(&timeout)->default_value(0), "Timeout in milliseconds when waiting for a device state (0 - wait infinitely)") ("timeout,t", bpo::value<unsigned int>(&timeout)->default_value(0), "Timeout in milliseconds when waiting for a device state (0 - wait infinitely)")
("help,h", "Produce help message"); ("help,h", "Produce help message");
@ -175,7 +189,7 @@ try {
if (vm.count("help")) { if (vm.count("help")) {
cout << "FairMQ DDS Command UI" << endl << options << endl; cout << "FairMQ DDS Command UI" << endl << options << endl;
cout << "Commands: [c] check state, [o] dump config, [h] help, [r] run, [s] stop, [t] reset task, [d] reset device, [q] end, [j] init task, [i] init device, [k] complete init, [b] bind, [x] connect" << endl; cout << "Commands: [c] check state, [o] dump config, [h] help, [r] run, [s] stop, [t] reset task, [d] reset device, [q] end, [j] init task, [i] init device, [k] complete init, [b] bind, [x] connect, [p] set property" << endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -189,7 +203,7 @@ try {
if (targetState != "") { if (targetState != "") {
if (command != "") { if (command != "") {
sendCommand(command, path, timeout, topo); sendCommand(command, path, timeout, topo, pKey, pVal);
} }
size_t pos = targetState.find("->"); size_t pos = targetState.find("->");
if (pos == string::npos) { if (pos == string::npos) {
@ -200,7 +214,7 @@ try {
// cout << "WaitForState(" << targetState << ") result: " << ec.message() << endl; // cout << "WaitForState(" << targetState << ") result: " << ec.message() << endl;
} }
} else { } else {
sendCommand(command, path, timeout, topo); sendCommand(command, path, timeout, topo, pKey, pVal);
} }
return EXIT_SUCCESS; return EXIT_SUCCESS;