REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestMessenger.h
1 /*************************************************************************
2  * This file is part of the REST software framework. *
3  * *
4  * Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
5  * For more information see http://gifna.unizar.es/trex *
6  * *
7  * REST is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * REST is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have a copy of the GNU General Public License along with *
18  * REST in $REST_PATH/LICENSE. *
19  * If not, see http://www.gnu.org/licenses/. *
20  * For the list of contributors see $REST_PATH/CREDITS. *
21  *************************************************************************/
22 
23 #ifndef RestCore_TRestMessenger
24 #define RestCore_TRestMessenger
25 
26 #include "TRestMetadata.h"
27 #include "TRestRun.h"
28 
29 class TRestMessenger : public TRestMetadata {
30  private:
32 #define Nmsg 100
33 #define MsgLength 256
34  struct messagepool_t {
35  void* owner;
36  char name[MsgLength];
37  struct message_t {
38  void* provider;
39  char content[MsgLength];
40  void Reset() {
41  provider = nullptr;
42  for (int i = 0; i < MsgLength; i++) {
43  content[i] = 0;
44  }
45  }
46  bool IsEmpty() { return provider == nullptr || content[0] == 0; }
47  };
48  message_t messages[Nmsg];
49  void Reset() {
50  owner = nullptr;
51  for (int i = 0; i < MsgLength; i++) {
52  name[i] = 0;
53  }
54  for (int i = 0; i < Nmsg; i++) {
55  messages[i].Reset();
56  }
57  }
58  int RequirePos() {
59  for (int i = 0; i < Nmsg; i++) {
60  if (messages[i].IsEmpty()) {
61  return i;
62  }
63  }
64  return -1;
65  }
66  };
67  int fShmId;
68  messagepool_t* fMessagePool;
69 
70  protected:
71  enum CommMode { MessagePool_Host, MessagePool_Client, MessagePool_TwoWay };
72 
73  TRestRun* fRun;
74 
75  CommMode fMode;
76  std::string fPoolToken; // to establish communication
77  std::string fPoolSource; // describes the source of message to be send. e.g. OUTPUTFILE, RUNNUMBER
78 
79  virtual void InitFromConfigFile() override;
80 
81  virtual void Initialize() override;
82 
83  virtual void AddPool(std::string message);
84 
85  bool lock(messagepool_t* pool, int timeoutMs = 1000);
86 
87  bool unlock(messagepool_t* pool, int timeoutMs = 1000);
88 
89  public:
90  virtual bool IsConnected() { return fMessagePool != nullptr; }
91 
92  virtual void SendMessage(std::string message = "");
93 
94  virtual std::vector<std::string> ShowMessagePool();
95 
96  virtual std::string ConsumeMessage();
97 
98  void PrintMetadata() override;
99  // Constructor
100  TRestMessenger();
101  // Constructor
102  TRestMessenger(int token, std::string mode = "TwoWay");
103  // Destructor
104  ~TRestMessenger();
105 
106  ClassDefOverride(TRestMessenger, 1); // Template for a REST "event process" class inherited from
107  // TRestEventProcess
108 };
109 #endif
void PrintMetadata() override
Implemented it in the derived metadata class to print out specific metadata information.
virtual void Initialize() override
Making default settings.
virtual void InitFromConfigFile() override
To make settings from rml file. This method must be implemented in the derived class.
A base class for any REST metadata class.
Definition: TRestMetadata.h:74
Data provider and manager in REST.
Definition: TRestRun.h:18
void Reset()
max 100 messages, each 256 char length