FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairMQExParamsClient.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 (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
15 #include "FairMQExParamsClient.h"
16 
17 #include "FairMQExParamsParOne.h"
18 #include "RootSerializer.h"
19 
20 #include <FairMQLogger.h>
21 #include <chrono>
22 #include <thread> // this_thread::sleep_for
23 
24 using namespace std;
25 
27  : fRunId(0)
28  , fParameterName()
29  , fMaxIterations(0)
30  , fNumIterations(0)
31 {}
32 
34 
36 {
37  fParameterName = fConfig->GetValue<string>("parameter-name");
38  fMaxIterations = fConfig->GetValue<uint64_t>("max-iterations");
39  fRunId = 2000;
40 }
41 
43 {
44  LOG(info) << "Requesting parameter \"" << fParameterName << "\" for Run ID " << fRunId << ".";
45 
46  // NewSimpleMessage creates a copy of the data and takes care of its destruction (after the transfer takes place).
47  // Should only be used for small data because of the cost of an additional copy
48  FairMQMessagePtr req(NewSimpleMessage(fParameterName + "," + to_string(fRunId)));
49  FairMQMessagePtr rep(NewMessage());
50 
51  if (Send(req, "data") > 0) {
52  if (Receive(rep, "data") >= 0) {
53  if (rep->GetSize() != 0) {
54  FairMQExParamsParOne* par = nullptr;
55  Deserialize<RootSerializer>(*rep, par);
56  LOG(info) << "Received parameter from the server:";
57  par->print();
58  delete par;
59  } else {
60  LOG(error) << "Received empty reply. Parameter not available";
61  }
62  }
63  }
64 
65  fRunId == 2099 ? fRunId = 2000 : fRunId++;
66 
67  if (fMaxIterations > 0 && ++fNumIterations >= fMaxIterations) {
68  LOG(info) << "Configured maximum number of iterations reached. Leaving RUNNING state.";
69  return false;
70  }
71 
72  this_thread::sleep_for(chrono::seconds(1));
73 
74  return true;
75 }
FairMQExParamsParOne * par