/******************************************************************************** * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence version 3 (LGPL) version 3, * * copied verbatim in the file "LICENSE" * ********************************************************************************/ /* * File: runSamplerRoot.cxx * Author: winckler */ // FairRoot - FairMQ #include "FairMQLogger.h" #include "FairMQProgOptions.h" #include "FairMQDevice.h" #include #include #include #include using namespace std; typedef unordered_map> FairMQMap; class MyDevice : public FairMQDevice { public: MyDevice() : fRate(0.5) {} virtual ~MyDevice() {} void SetRate(double r) { fRate = r; } double GetRate() { return fRate; } void Print() { LOG(INFO) << "[MyDevice] rate = " << fRate; } private: double fRate; }; void MyCallBack(MyDevice& d, double val) { d.SetRate(val); d.Print(); } void PrintMQParam(const FairMQMap& channels, const FairMQProgOptions& config) { for (const auto& p : channels) { int index = 0; for (const auto& channel : p.second) { string typeKey = p.first + "." + to_string(index) + ".type"; string methodKey = p.first + "." + to_string(index) + ".method"; string addressKey = p.first + "." + to_string(index) + ".address"; string propertyKey = p.first + "." + to_string(index) + ".property"; string sndBufSizeKey = p.first + "." + to_string(index) + ".sndBufSize"; string rcvBufSizeKey = p.first + "." + to_string(index) + ".rcvBufSize"; string rateLoggingKey = p.first + "." + to_string(index) + ".rateLogging"; LOG(INFO) << "Channel name = " << p.first; LOG(INFO) << "key = " << typeKey <<"\t value = " << config.GetValue(typeKey); LOG(INFO) << "key = " << methodKey <<"\t value = " << config.GetValue(methodKey); LOG(INFO) << "key = " << addressKey <<"\t value = " << config.GetValue(addressKey); LOG(INFO) << "key = " << propertyKey <<"\t value = " << config.GetValue(propertyKey); LOG(INFO) << "key = " << sndBufSizeKey << "\t value = " << config.GetValue(sndBufSizeKey); LOG(INFO) << "key = " << rcvBufSizeKey <<"\t value = " << config.GetValue(rcvBufSizeKey); LOG(INFO) << "key = " << rateLoggingKey <<"\t value = " << config.GetValue(rateLoggingKey); } } } int main(int argc, char** argv) { try { // create option manager object FairMQProgOptions config; // add key description to cmd line options config.GetCmdLineOptions().add_options() ("data-rate", po::value()->default_value(0.5), "Data rate"); // parse command lines, parse json file and init FairMQMap config.ParseAll(argc, argv); // // get FairMQMap // auto map1 = config.GetFairMQMap(); // // form keys from map1 and print the value stored in variable map // PrintMQParam(map1, config); // // update value in variable map, and propagate the update to the FairMQMap // config.UpdateValue("data.0.address","tcp://localhost:1234"); // // get the updated FairMQMap // auto map2 = config.GetFairMQMap(); // // modify one channel value // map2.at("data").at(0).UpdateSndBufSize(500); // // update the FairMQMap and propagate the change in variable map // config.UpdateChannelMap(map2); // // print values stored in variable map // PrintMQParam(map2, config); MyDevice device; device.CatchSignals(); device.SetConfig(config); // getting as string and conversion helpers // string dataRateStr = config.GetStringValue("data-rate"); // double dataRate = config.ConvertTo(dataRateStr); // LOG(INFO) << "dataRate: " << dataRate; LOG(INFO) << "Subscribing: (data.0.address)"; config.Subscribe("data.0.address", [&device](const string& key, const string& value) { LOG(INFO) << "[callback] Updating device parameter " << key << " = " << value; device.fChannels.at("data").at(0).UpdateAddress(value); }); LOG(INFO) << "Subscribing: (data.0.rcvBufSize)"; config.Subscribe("data.0.rcvBufSize", [&device](const string& key, int value) { LOG(INFO) << "[callback] Updating device parameter " << key << " = " << value; device.fChannels.at("data").at(0).UpdateRcvBufSize(value); }); LOG(INFO) << "Subscribing: (data-rate)"; config.Subscribe("data-rate", [&device](const string& key, double value) { LOG(INFO) << "[callback] Updating device parameter " << key << " = " << value; device.SetRate(value); }); LOG(INFO) << "Starting value updates...\n"; config.UpdateValue("data.0.address", "tcp://localhost:4321"); LOG(INFO) << "config: " << config.GetValue("data.0.address"); LOG(INFO) << "device: " << device.fChannels.at("data").at(0).GetAddress() << endl; config.UpdateValue("data.0.rcvBufSize", 100); LOG(INFO) << "config: " << config.GetValue("data.0.rcvBufSize"); LOG(INFO) << "device: " << device.fChannels.at("data").at(0).GetRcvBufSize() << endl; config.UpdateValue("data-rate", 0.9); LOG(INFO) << "config: " << config.GetValue("data-rate"); LOG(INFO) << "device: " << device.GetRate() << endl; // device.Print(); // advanced commands // LOG(INFO) << "-------------------- start custom 1"; // config.Connect("myNewKey", [](MyDevice& d, double val) // { // d.SetRate(val); // d.Print(); // }); // config.Emit("myNewKey", device, 0.123); // LOG(INFO) << "-------------------- start custom 2 with function"; // config.Connect("function example", &MyCallBack); // config.Emit("function example", device, 6.66); } catch (exception& e) { LOG(ERROR) << "Unhandled Exception reached the top of main: " << e.what() << ", application will now exit"; return 1; } return 0; }