ToolDAQFramework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
Utilities.h
Go to the documentation of this file.
1 #ifndef UTILITIES_H
2 #define UTILITIES_H
3 
4 #include "TimeDelta.h"
5 #include <iostream>
6 #include <zmq.hpp>
7 #include <sstream>
8 #include <pthread.h>
9 #include <map>
10 #include <Store.h>
11 #include <boost/uuid/uuid.hpp>
12 #include <boost/uuid/uuid_generators.hpp> // generators
13 #include <boost/uuid/uuid_io.hpp> // streaming operators etc.
14 #include <unistd.h>
15 
29 struct Thread_args{
30 
32  kill=false;
33  }
34 
35  Thread_args(zmq::context_t* contextin, std::string threadname, void (*funcin)(std::string)){
36 
37  context=contextin;
38  ThreadName=threadname;
39  func_with_string=funcin;
40  kill=false;
41  }
42 
43  Thread_args(zmq::context_t* contextin, std::string threadname, void (*funcin)(Thread_args*)){
44 
45  context=contextin;
46  ThreadName=threadname;
47  func=funcin;
48  kill=false;
49  }
50 
51  virtual ~Thread_args(){
52  running =false;
53  kill=true;
54  delete sock;
55  sock=0;
56  }
57 
58  zmq::context_t *context;
59  std::string ThreadName;
60  void (*func_with_string)(std::string);
61  void (*func)(Thread_args*);
62  pthread_t thread;
63  zmq::socket_t* sock;
64  bool running;
65  bool kill;
66 
67 };
68 
69 
82 class Utilities{
83 
84  public:
85 
86  Utilities(zmq::context_t* zmqcontext);
87  bool AddService(std::string ServiceName, unsigned int port, bool StatusQuery=false);
88  bool RemoveService(std::string ServiceName);
89  int UpdateConnections(std::string ServiceName, zmq::socket_t* sock, std::map<std::string,Store*> &connections);
90  Thread_args* CreateThread(std::string ThreadName, void (*func)(std::string)); //func = &my_int_func; ///< Create a simple thread that has string exchange with main thread
91  Thread_args* CreateThread(std::string ThreadName, void (*func)(Thread_args*), Thread_args* args);
92  bool MessageThread(Thread_args* args, std::string Message, bool block=true);
93  bool MessageThread(std::string ThreadName, std::string Message, bool block=true);
94  bool KillThread(Thread_args* &args);
95  bool KillThread(std::string ThreadName);
96 
97  template <typename T> bool KillThread(T* pointer){
98 
99  Thread_args* tmp=pointer;
100  return KillThread(tmp);
101 
102  }
103 
104  template <typename T> bool SendPointer(zmq::socket_t* sock, T* pointer){
105 
106  std::stringstream tmp;
107  tmp<<pointer;
108 
109  zmq::message_t message(tmp.str().length()+1);
110  snprintf ((char *) message.data(), tmp.str().length()+1 , "%s" , tmp.str().c_str()) ;
111 
112  return sock->send(message);
113 
114  }
115 
116  template<typename T> bool ReceivePointer(zmq::socket_t* sock, T*& pointer){
117 
118  zmq::message_t message;
119 
120  if(sock->recv(&message)){
121 
122  std::istringstream iss(static_cast<char*>(message.data()));
123 
124  // long long unsigned int tmpP;
125  unsigned long tmpP;
126  iss>>std::hex>>tmpP;
127 
128  pointer=reinterpret_cast<T*>(tmpP);
129 
130  return true;
131  }
132 
133  else {
134  pointer=0;
135  return false;
136  }
137 
138  }
139 
140 
141 
142 
143  private:
144 
145  zmq::context_t *context;
146  static void* String_Thread(void *arg);
147  static void* Thread(void *arg);
148  std::map<std::string, Thread_args*> Threads;
149 
150 
151 };
152 
153 
154 
155 namespace util {
157  struct Window {
161  Window() {}
162  Window(int trigger_num, TimeDelta start, TimeDelta end) :
163  m_trigger_num(trigger_num), m_start(start), m_end(end) {}
164  };
165 
167  static bool WindowSorter(const Window & lhs,
168  const Window & rhs) {
169  return lhs.m_start < rhs.m_start;
170  }
171 
173  bool FileExists(std::string pathname, std::string filename);
174 
176  void Log(const std::string & message, const int message_level);
177 
179  void Log(std::stringstream & message, const int message_level);
180 
182  enum LogLevel {FATAL=-1, ERROR=0, WARN=1, INFO=2, DEBUG1=3, DEBUG2=4, DEBUG3=5};
183 
184 } //namespace util
185 
186 #endif
187 
Contains the start and end of a time window, along with an ID (nominally trigger number) ...
Definition: Utilities.h:157
bool MessageThread(Thread_args *args, std::string Message, bool block=true)
Send simple string to String thread.
Definition: Utilities.cpp:202
bool running
Bool flag to tell the thread to run (if not set thread goes into wait cycle.
Definition: Utilities.h:64
TimeDelta m_end
Definition: Utilities.h:160
TimeDelta m_start
Definition: Utilities.h:159
bool AddService(std::string ServiceName, unsigned int port, bool StatusQuery=false)
Broadcasts an available service (only in remote mode)
Definition: Utilities.cpp:8
int m_trigger_num
Definition: Utilities.h:158
Thread_args(zmq::context_t *contextin, std::string threadname, void(*funcin)(Thread_args *))
Definition: Utilities.h:43
bool kill
Bool flay used to kill the thread.
Definition: Utilities.h:65
std::string ThreadName
name of thread (deffined at creation)
Definition: Utilities.h:59
zmq::socket_t * sock
ZMQ socket pointer is assigned in string thread,but can be sued otherwise.
Definition: Utilities.h:63
Thread_args(zmq::context_t *contextin, std::string threadname, void(*funcin)(std::string))
Definition: Utilities.h:35
static void * String_Thread(void *arg)
Simpe string thread.
Definition: Utilities.cpp:145
static void * Thread(void *arg)
Thread with args.
Definition: Utilities.cpp:187
void(* func_with_string)(std::string)
function pointer to string thread
Definition: Utilities.h:60
static bool WindowSorter(const Window &lhs, const Window &rhs)
When sorting Window structs, sort by the start time.
Definition: Utilities.h:167
void(* func)(Thread_args *)
function pointer to thread with args
Definition: Utilities.h:61
bool KillThread(T *pointer)
Kill a thread with args that inheirt form base Thread_args.
Definition: Utilities.h:97
LogLevel
Log level enumerations.
Definition: Utilities.h:182
Thread_args * CreateThread(std::string ThreadName, void(*func)(std::string))
Definition: Utilities.cpp:130
bool ReceivePointer(zmq::socket_t *sock, T *&pointer)
Receive a pointer over a ZMQ socket.
Definition: Utilities.h:116
Utilities(zmq::context_t *zmqcontext)
Simple constructor.
Definition: Utilities.cpp:3
std::map< std::string, Thread_args * > Threads
Map of threads managed by the utilities class.
Definition: Utilities.h:148
cudaEvent_t start
Definition: library_daq.h:171
virtual ~Thread_args()
Definition: Utilities.h:51
pthread_t thread
Simple constructor underlying thread that interface is built ontop of.
Definition: Utilities.h:62
zmq::context_t * context
ZMQ context used for ZMQ socket creation.
Definition: Utilities.h:58
void Log(const std::string &message, const int message_level)
Format messages in the same way as for tools.
Definition: Utilities.cpp:276
bool KillThread(Thread_args *&args)
Kill a thread assosiated to args.
Definition: Utilities.cpp:234
bool SendPointer(zmq::socket_t *sock, T *pointer)
Send a pointer over a ZMQ socket.
Definition: Utilities.h:104
Window(int trigger_num, TimeDelta start, TimeDelta end)
Definition: Utilities.h:162
bool RemoveService(std::string ServiceName)
Removes service broadcasts for a service.
Definition: Utilities.cpp:28
bool FileExists(std::string pathname, std::string filename)
Check if a file exists.
Definition: Utilities.cpp:264
zmq::context_t * context
ZMQ context pointer.
Definition: Utilities.h:145
int UpdateConnections(std::string ServiceName, zmq::socket_t *sock, std::map< std::string, Store * > &connections)
Dynamically connects a socket tp services broadcast with a specific name.
Definition: Utilities.cpp:42