Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/********************************************************************************
* 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" *
********************************************************************************/
/**
* ParameterMQServer.cxx
*
* @since 2015-10-26
* @author M. Al-Turany, A. Rybalchenko
*/
#include "CbmMQDefs.h"
#include "TMessage.h"
#include "Rtypes.h"
#include "FairRuntimeDb.h"
#include "FairParAsciiFileIo.h"
#include "FairParRootFileIo.h"
#include "FairParGenericSet.h"
#include "ParameterMQServer.h"
#include "FairMQLogger.h"
#include "FairMQProgOptions.h"
#include "TList.h"
#include "TObjString.h"
#include "TSystem.h"
#include "TGeoManager.h"
using namespace std;
ParameterMQServer::ParameterMQServer() :
fRtdb(FairRuntimeDb::instance()),
fFirstInputName("first_input.root"),
fFirstInputType("ROOT"),
fSecondInputName(""),
fSecondInputType("ROOT"),
fOutputName(""),
fOutputType("ROOT"),
fChannelName("data")
{
}
void ParameterMQServer::InitTask()
{
string loadLibs = fConfig->GetValue<string>("libs-to-load");
if ( loadLibs.length() > 0 ) {
LOG(info) << "There are libraries to load.";
if (loadLibs.find(";") != std::string::npos) {
LOG(info) << "There are several libraries to load";
istringstream f(loadLibs);
string s;
while (getline(f, s, ';')) {
LOG(info)<< "Load library " << s;
gSystem->Load(s.c_str());
}
} else {
LOG(info)<< "Load library " << loadLibs;
gSystem->Load(loadLibs.c_str());
}
} else {
LOG(info) << "There are no libraries to load.";
}
fFirstInputName = fConfig->GetValue<string>("first-input-name");
fFirstInputType = fConfig->GetValue<string>("first-input-type");
fSecondInputName = fConfig->GetValue<string>("second-input-name");
fSecondInputType = fConfig->GetValue<string>("second-input-type");
fOutputName = fConfig->GetValue<string>("output-name");
fOutputType = fConfig->GetValue<string>("output-type");
fChannelName = fConfig->GetValue<string>("channel-name");
if (fRtdb != 0)
{
// Set first input
if (fFirstInputType == "ROOT")
{
FairParRootFileIo* par1R = new FairParRootFileIo();
par1R->open(fFirstInputName.data(), "UPDATE");
fRtdb->setFirstInput(par1R);
}
else if (fFirstInputType == "ASCII")
{
FairParAsciiFileIo* par1A = new FairParAsciiFileIo();
if (fFirstInputName.find(";") != std::string::npos) {
LOG(info) << "File list found!";
TList *parFileList = new TList();
TObjString* parFile(NULL);
istringstream f(fFirstInputName);
string s;
while (getline(f, s, ';')) {
LOG(info)<< "File: " << s;
parFile = new TObjString(s.c_str());
parFileList->Add(parFile);
par1A->open(parFileList, "in");
}
} else {
LOG(info) << "Single input file found!";
par1A->open(fFirstInputName.data(), "in");
}
fRtdb->setFirstInput(par1A);
}
// Set second input
if (fSecondInputName != "")
{
if (fSecondInputType == "ROOT")
{
FairParRootFileIo* par2R = new FairParRootFileIo();
par2R->open(fSecondInputName.data(), "UPDATE");
fRtdb->setSecondInput(par2R);
}
else if (fSecondInputType == "ASCII")
{
FairParAsciiFileIo* par2A = new FairParAsciiFileIo();
if (fSecondInputName.find(";") != std::string::npos) {
LOG(info) << "File list found!";
TList *parFileList = new TList();
TObjString* parFile(NULL);
istringstream f(fSecondInputName);
string s;
while (getline(f, s, ';')) {
LOG(info)<< "File: " << s;
parFile = new TObjString(s.c_str());
parFileList->Add(parFile);
par2A->open(parFileList, "in");
}
} else {
LOG(info) << "Single input file found!";
par2A->open(fFirstInputName.data(), "in");
}
fRtdb->setSecondInput(par2A);
}
}
// Set output
if (fOutputName != "")
{
if (fOutputType == "ROOT")
{
FairParRootFileIo* parOut = new FairParRootFileIo(kTRUE);
parOut->open(fOutputName.data());
fRtdb->setOutput(parOut);
}
fRtdb->saveOutput();
}
}
fRtdb->print();
}
void ParameterMQServer::Run()
{
string parameterName = "";
FairParGenericSet* par = nullptr;
while (cbm::mq::CheckCurrentState(this, cbm::mq::State::Running))
{
FairMQMessagePtr req(NewMessage());
if (Receive(req, fChannelName, 0) > 0)
{
string reqStr(static_cast<char*>(req->GetData()), req->GetSize());
LOG(info) << "Received parameter request from client: \"" << reqStr << "\"";
size_t pos = reqStr.rfind(",");
string newParameterName = reqStr.substr(0, pos);
int runId = stoi(reqStr.substr(pos + 1));
LOG(info) << "Parameter name: " << newParameterName;
LOG(info) << "Run ID: " << runId;
LOG(info) << "Retrieving parameter...";
// Check if the parameter name has changed to avoid getting same container repeatedly
if (newParameterName != parameterName)
{
parameterName = newParameterName;
par = static_cast<FairParGenericSet*>(fRtdb->getContainer(parameterName.c_str()));
}
LOG(info) << "Retrieving parameter...Done";
if (-1 != runId) {
fRtdb->initContainers(runId);
}
LOG(info) << "Sending following parameter to the client:";
if (par)
{
par->print();
TMessage* tmsg = new TMessage(kMESS_OBJECT);
tmsg->WriteObject(par);
FairMQMessagePtr rep(NewMessage(tmsg->Buffer(),
tmsg->BufferSize(),
[](void* /*data*/, void* object){ delete static_cast<TMessage*>(object); },
tmsg));
if (Send(rep, fChannelName, 0) < 0)
{
LOG(error) << "failed sending reply";
break;
}
}
else
{
LOG(error) << "Parameter uninitialized!";
// Send an empty message back to keep the REQ/REP cycle
FairMQMessagePtr rep(NewMessage());
if (Send(rep, fChannelName, 0) < 0)
{
LOG(error) << "failed sending reply";
break;
}
}
}
}
}
ParameterMQServer::~ParameterMQServer()
{
if (gGeoManager) {
gGeoManager->GetListOfVolumes()->Delete();
gGeoManager->GetListOfShapes()->Delete();
}
delete fRtdb;
}