CbmRoot
Loading...
Searching...
No Matches
ParameterMQServer.cxx
Go to the documentation of this file.
1/********************************************************************************
2 * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3 * *
4 * This software is distributed under the terms of the *
5 * GNU Lesser General Public Licence version 3 (LGPL) version 3, *
6 * copied verbatim in the file "LICENSE" *
7 ********************************************************************************/
15#include "ParameterMQServer.h"
16
17#include "CbmMQDefs.h"
18#include "CbmSetup.h"
19
20#include "FairMQLogger.h"
21#include "FairMQProgOptions.h"
22#include "FairParAsciiFileIo.h"
23#include "FairParGenericSet.h"
24#include "FairParRootFileIo.h"
25#include "FairRuntimeDb.h"
26
27#include "Rtypes.h"
28#include "TGeoManager.h"
29#include "TList.h"
30#include "TMessage.h"
31#include "TObjString.h"
32#include "TSystem.h"
33
34using namespace std;
35
37 : fRtdb(FairRuntimeDb::instance())
38{
39}
40
42{
43 string loadLibs = fConfig->GetValue<string>("libs-to-load");
44 if (loadLibs.length() > 0) {
45 LOG(info) << "There are libraries to load.";
46 if (loadLibs.find(";") != std::string::npos) {
47 LOG(info) << "There are several libraries to load";
48 istringstream f(loadLibs);
49 string s;
50 while (getline(f, s, ';')) {
51 LOG(info) << "Load library " << s;
52 gSystem->Load(s.c_str());
53 }
54 }
55 else {
56 LOG(info) << "Load library " << loadLibs;
57 gSystem->Load(loadLibs.c_str());
58 }
59 }
60 else {
61 LOG(info) << "There are no libraries to load.";
62 }
63
64 fFirstInputName = fConfig->GetValue<string>("first-input-name");
65 fFirstInputType = fConfig->GetValue<string>("first-input-type");
66 fSecondInputName = fConfig->GetValue<string>("second-input-name");
67 fSecondInputType = fConfig->GetValue<string>("second-input-type");
68 fOutputName = fConfig->GetValue<string>("output-name");
69 fOutputType = fConfig->GetValue<string>("output-type");
70 fChannelName = fConfig->GetValue<string>("channel-name");
71
72 fsSetupName = fConfig->GetValue<std::string>("setup");
73 LOG(info) << "Using setup: " << fsSetupName;
74
75
76 if (fRtdb != 0) {
77 // Set first input
78 if (fFirstInputType == "ROOT") {
79 FairParRootFileIo* par1R = new FairParRootFileIo();
80 par1R->open(fFirstInputName.data(), "UPDATE");
81 fRtdb->setFirstInput(par1R);
82 }
83 else if (fFirstInputType == "ASCII") {
84 FairParAsciiFileIo* par1A = new FairParAsciiFileIo();
85 if (fFirstInputName.find(";") != std::string::npos) {
86 LOG(info) << "File list found!";
87 TList* parFileList = new TList();
88 TObjString* parFile(NULL);
89 istringstream f(fFirstInputName);
90 string s;
91 while (getline(f, s, ';')) {
92 LOG(info) << "File: " << s;
93 parFile = new TObjString(s.c_str());
94 parFileList->Add(parFile);
95 par1A->open(parFileList, "in");
96 }
97 }
98 else {
99 LOG(info) << "Single input file found!";
100 par1A->open(fFirstInputName.data(), "in");
101 }
102 fRtdb->setFirstInput(par1A);
103 }
104
105 // Set second input
106 if (fSecondInputName != "") {
107 if (fSecondInputType == "ROOT") {
108 FairParRootFileIo* par2R = new FairParRootFileIo();
109 par2R->open(fSecondInputName.data(), "UPDATE");
110 fRtdb->setSecondInput(par2R);
111 }
112 else if (fSecondInputType == "ASCII") {
113 FairParAsciiFileIo* par2A = new FairParAsciiFileIo();
114 if (fSecondInputName.find(";") != std::string::npos) {
115 LOG(info) << "File list found!";
116 TList* parFileList = new TList();
117 TObjString* parFile(NULL);
118 istringstream f(fSecondInputName);
119 string s;
120 while (getline(f, s, ';')) {
121 LOG(info) << "File: " << s;
122 parFile = new TObjString(s.c_str());
123 parFileList->Add(parFile);
124 par2A->open(parFileList, "in");
125 }
126 }
127 else {
128 LOG(info) << "Single input file found!";
129 par2A->open(fFirstInputName.data(), "in");
130 }
131 fRtdb->setSecondInput(par2A);
132 }
133 }
134
135 // Set output
136 if (fOutputName != "") {
137 if (fOutputType == "ROOT") {
138 FairParRootFileIo* parOut = new FairParRootFileIo(kTRUE);
139 parOut->open(fOutputName.data());
140 fRtdb->setOutput(parOut);
141 }
142
143 fRtdb->saveOutput();
144 }
145 }
146 fRtdb->print();
147
148 // ----- CbmSetup -----------------------------------------------------
149 if ("" != fsSetupName) {
152 }
153 // ------------------------------------------------------------------------
154}
155
157{
158 string parameterName = "";
159 FairParGenericSet* par = nullptr;
160
162 FairMQMessagePtr req(NewMessage());
163
164 if (Receive(req, fChannelName, 0) > 0) {
165 string reqStr(static_cast<char*>(req->GetData()), req->GetSize());
166 LOG(info) << "Received parameter request from client: \"" << reqStr << "\"";
167
168 if ("setup" == reqStr) {
169 // TODO: support for multiple setups on Par Server? with request containing setup name?
170 if ("" != fsSetupName && fSetup) {
172 CbmSetupStorable exchangableSetup(fSetup);
173
174 TMessage* tmsg = new TMessage(kMESS_OBJECT);
175 tmsg->WriteObject(&exchangableSetup);
176
177 FairMQMessagePtr rep(NewMessage(
178 tmsg->Buffer(), tmsg->BufferSize(),
179 [](void* /*data*/, void* object) { delete static_cast<TMessage*>(object); }, tmsg));
180
181 if (Send(rep, fChannelName, 0) < 0) {
182 LOG(error) << "failed sending reply to Setup request";
183 break;
184 }
185 }
186 else {
187 LOG(error) << "CbmSetup uninitialized!";
188 // Send an empty message back to keep the REQ/REP cycle
189 FairMQMessagePtr rep(NewMessage());
190 if (Send(rep, fChannelName, 0) < 0) {
191 LOG(error) << "failed sending reply to Setup request";
192 break;
193 }
194 }
195 }
196 else {
197 size_t pos = reqStr.rfind(",");
198 string newParameterName = reqStr.substr(0, pos);
199 int runId = stoi(reqStr.substr(pos + 1));
200 LOG(info) << "Parameter name: " << newParameterName;
201 LOG(info) << "Run ID: " << runId;
202
203 LOG(info) << "Retrieving parameter...";
204 // Check if the parameter name has changed to avoid getting same container repeatedly
205 if (newParameterName != parameterName) {
206 parameterName = newParameterName;
207 par = static_cast<FairParGenericSet*>(fRtdb->getContainer(parameterName.c_str()));
208 }
209 LOG(info) << "Retrieving parameter...Done";
210
211 if (-1 != runId) { fRtdb->initContainers(runId); }
212
213 LOG(info) << "Sending following parameter to the client:";
214 if (par) {
215 par->print();
216
217 TMessage* tmsg = new TMessage(kMESS_OBJECT);
218 tmsg->WriteObject(par);
219
220 FairMQMessagePtr rep(NewMessage(
221 tmsg->Buffer(), tmsg->BufferSize(),
222 [](void* /*data*/, void* object) { delete static_cast<TMessage*>(object); }, tmsg));
223
224 if (Send(rep, fChannelName, 0) < 0) {
225 LOG(error) << "failed sending reply";
226 break;
227 }
228 }
229 else {
230 LOG(error) << "Parameter uninitialized!";
231 // Send an empty message back to keep the REQ/REP cycle
232 FairMQMessagePtr rep(NewMessage());
233 if (Send(rep, fChannelName, 0) < 0) {
234 LOG(error) << "failed sending reply";
235 break;
236 }
237 }
238 }
239 }
240 }
241}
242
244{
245 if (gGeoManager) {
246 gGeoManager->GetListOfVolumes()->Delete();
247 gGeoManager->GetListOfShapes()->Delete();
248 }
249 delete fRtdb;
250}
static CbmSetup * Instance()
Definition CbmSetup.cxx:160
void LoadSetup(const char *setupName)
Definition CbmSetup.h:64
FairRuntimeDb * fRtdb
std::string fSecondInputName
std::string fFirstInputName
std::string fFirstInputType
std::string fSecondInputType
virtual void InitTask()
bool CheckCurrentState(FairMQDevice *device, cbm::mq::State state)
Definition CbmMQDefs.h:52
Hash for CbmL1LinkKey.