FACT++  1.0
DimState.h
Go to the documentation of this file.
1 #ifndef FACT_DimState
2 #define FACT_DimState
3 
4 #include <set>
5 #include <string>
6 #include <functional>
7 
8 #include "State.h"
9 #include "Service.h"
10 #include "EventImp.h"
11 #include "WindowLog.h"
12 #include "Description.h"
13 #include "StateMachineImp.h"
14 
15 class DimState
16 {
17 public:
18  enum
19  {
20  kOffline = -256,
21  kNotAvailable = -257,
22  };
23 
24 protected:
25  typedef std::function<int(const EventImp &)> callback;
26 
27  callback fCallback;
28 
29  void HandlerImp(const EventImp &evt)
30  {
31  const bool disconnected = evt.GetSize()==0;
32 
33  last = cur;
34  cur = std::make_pair(evt.GetTime(), disconnected ? kOffline : evt.GetQoS());
35 
36  msg = disconnected ? "" : evt.GetString();
37  }
38 
39  int Callback(const EventImp &evt)
40  {
41  return fCallback ? fCallback(evt) : StateMachineImp::kSM_KeepState;
42  }
43 
44  virtual int Handler(const EventImp &evt)
45  {
46  HandlerImp(evt);
47  return Callback(evt);
48  }
49 
50 public:
51  DimState() { }
52  DimState(const std::string &n, const std::string s="STATE") : server(n),
53  service(n+"/"+s),
54  last(std::make_pair(Time(), kOffline)), cur(std::make_pair(Time(), kOffline))
55  {
56  }
57  virtual ~DimState()
58  {
59  }
60 
61  /*const*/ std::string server;
62  /*const*/ std::string service;
63 
64  std::pair<Time, int32_t> last;
65  std::pair<Time, int32_t> cur;
66  std::string msg;
67 
68  virtual void Subscribe(StateMachineImp &imp)
69  {
70  imp.Subscribe(service)
71  (imp.Wrap(std::bind(&DimState::Handler, this, std::placeholders::_1)));
72  }
73 
74  void SetCallback(const callback &cb)
75  {
76  fCallback = cb;
77  }
78 
79  const Time &time() const { return cur.first; }
80  const int32_t &state() const { return cur.second; }
81 
82  bool online() const { return state()>kOffline; }
83 
84  virtual State description() const { return State(kNotAvailable, ""); }
85 };
86 
87 inline std::ostream &operator<<(std::ostream& out, const DimState &s)
88 {
89  const State rc = s.description();
90 
91  out << s.time().GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
92  out << kBold << s.server;
93 
94  if (s.state()==DimState::kOffline)
95  return out << ": Offline";
96 
98  return out;
99 
100  out << ": ";
101 
102 // if (rc.index==-2)
103 // out << s.state();
104 // else
105  out << rc.name << "[" << rc.index << "]";
106 
107  if (!rc.comment.empty())
108  out << " - " << kBlue << rc.comment;
109 
110  return out;
111 }
112 
113 
115 {
116  typedef std::function<void()> callback_desc;
117 
118  callback_desc fCallbackStates;
119 
120  virtual void CallbackStates()
121  {
122  if (fCallbackStates)
123  fCallbackStates();
124  }
125 
126 
127 public:
128  DimDescribedState(const std::string &n) : DimState(n)
129  {
130  }
131 
132  std::vector<State> states;
133 
134  virtual void Subscribe(StateMachineImp &imp)
135  {
136  imp.Subscribe(server+"/STATE_LIST")
137  (imp.Wrap(std::bind(&DimDescribedState::HandleDesc, this, std::placeholders::_1)));
138 
139  DimState::Subscribe(imp);
140  }
141 
142  void SetCallbackStates(const callback_desc &cb)
143  {
144  fCallbackStates = cb;
145  }
146 
147  int HandleDesc(const EventImp &evt)
148  {
149  if (evt.GetSize()>0)
150  {
151  states = State::SplitStates(evt.GetString());
152  states.emplace_back(kOffline, "Offline");
153 
154  CallbackStates();
155  }
156 
158  }
159 
161  {
162  for (auto it=states.begin(); it!=states.end(); it++)
163  if (it->index==state())
164  return State(it->index, it->name, it->comment, time());
165 
166  return State(kNotAvailable, "n/a");
167  }
168 };
169 
171 {
172  typedef std::function<void()> callback_desc;
173 
174  callback_desc fCallbackDescriptions;
175 
176  virtual void CallbackDescriptions()
177  {
178  if (fCallbackDescriptions)
179  fCallbackDescriptions();
180  }
181 
182 public:
183  DimDescriptions(const std::string &n) : DimDescribedState(n)
184  {
185  }
186 
187  std::vector<std::vector<Description>> descriptions;
188 
189  virtual void Subscribe(StateMachineImp &imp)
190  {
191  imp.Subscribe(server+"/SERVICE_DESC")
192  (imp.Wrap(std::bind(&DimDescriptions::HandleServiceDesc, this, std::placeholders::_1)));
193 
195  }
196 
197  void SetCallbackDescriptions(const callback_desc &cb)
198  {
199  fCallbackDescriptions = cb;
200  }
201 
202 
203  int HandleServiceDesc(const EventImp &evt)
204  {
205  descriptions.clear();
206  if (evt.GetSize()>0)
207  {
208  std::string buf;
209  std::stringstream stream(evt.GetString());
210  while (getline(stream, buf, '\n'))
211  descriptions.push_back(Description::SplitDescription(buf));
212  }
213 
214  CallbackDescriptions();
215 
217  }
218 };
219 
220 class DimVersion : public DimState
221 {
222  int Handler(const EventImp &evt)
223  {
224  HandlerImp(evt);
225 
226  cur.second = evt.GetSize()==4 ? evt.GetInt() : kOffline;
227  if (cur.second==0)
228  cur.second=kOffline;
229 
230  return Callback(evt);
231  }
232 
233 public:
234  DimVersion() : DimState("DIS_DNS", "VERSION_NUMBER") { }
235 
236  std::string version() const
237  {
238  if (!online())
239  return "Offline";
240 
241  std::ostringstream out;
242  out << "V" << state()/100 << 'r' << state()%100;
243  return out.str();
244  }
245 
247  {
248  return State(state(), version(), "", time());
249  }
250 };
251 
252 class DimControl : public DimState
253 {
254  std::map<std::string, callback> fCallbacks;
255 
256  int Handler(const EventImp &evt)
257  {
258  HandlerImp(evt);
259 
260  shortmsg = msg;
261  file = "";
262  scriptdepth = -1;
263 
264  // Find begining of descriptor
265  const size_t p0 = msg.find_first_of(' ');
266  if (p0==std::string::npos)
268 
269  // Find begining of filename
270  const size_t p1 = msg.find_first_of(':');
271  if (p1==std::string::npos)
273 
274  // Find end of filename
275  const size_t p2 = msg.find_last_of('[');
276  if (p2==std::string::npos)
278 
279  scriptdepth = atoi(msg.c_str()+p0+1);
280  file = msg.substr(p1+1, p2-p1-1);
281 
282  shortmsg.insert(0, msg.substr(p0+1, p1-p0));
283  shortmsg.erase(p1+1,p2-p0-1);
284 
285  const int rc = Callback(evt);
286 
287  const auto func = fCallbacks.find(file);
288  if (func==fCallbacks.end())
289  return rc;
290 
291  // Call callback
292  return func->second(evt);
293  }
294 
295 
296 public:
297  DimControl() : DimState("DIM_CONTROL") { }
298 
299  std::string file;
300  std::string shortmsg;
302 
303  void AddCallback(const std::string &script, const callback &cb)
304  {
305  fCallbacks[script] = cb;
306  }
307 
309  {
310  return State(state(), "Current label", "", time());
311  }
312 };
313 
315 {
316 protected:
317  typedef std::function<void(const std::string &)> callback_srv;
318  typedef std::function<void(const EventImp &)> callback_evt;
319 
320  callback_srv fCallbackServerAdd;
321  callback_srv fCallbackServerRemove;
322  callback_evt fCallbackServerEvent;
323 
324  std::set<std::string> fServerList;
325 
326  void HandlerServerImp(const EventImp &evt);
327 
328  virtual void CallbackServerAdd(const std::string &str)
329  {
330  if (fCallbackServerAdd)
331  fCallbackServerAdd(str);
332  }
333  virtual void CallbackServerRemove(const std::string &str)
334  {
335  if (fCallbackServerRemove)
336  fCallbackServerRemove(str);
337  }
338  virtual void CallbackServerEvent(const EventImp &evt)
339  {
340  if (fCallbackServerEvent)
341  fCallbackServerEvent(evt);
342  }
343  virtual int HandlerServer(const EventImp &evt)
344  {
345  HandlerServerImp(evt);
346  CallbackServerEvent(evt);
347 
349  }
350 
351 public:
353  {
354  }
356  {
357  }
358 
360  std::string msg;
361 
362  virtual void Subscribe(StateMachineImp &imp)
363  {
364  imp.Subscribe("DIS_DNS/SERVER_LIST")
365  (imp.Wrap(std::bind(&DimDnsServerList::HandlerServer, this, std::placeholders::_1)));
366  }
367 
368  void SetCallbackServerAdd(const callback_srv &cb)
369  {
370  fCallbackServerAdd = cb;
371  }
372 
373  void SetCallbackServerRemove(const callback_srv &cb)
374  {
375  fCallbackServerRemove = cb;
376  }
377 
378  void SetCallbackServerEvent(const callback_evt &cb)
379  {
380  fCallbackServerEvent = cb;
381  }
382 
383  //const Time &time() const { return time; }
384  //const std::string &msg() const { return msg; }
385 };
386 
388 {
390 
391  typedef std::function<void(const Service &)> callback_svc;
392 
393  callback_svc fCallbackServiceAdd;
394  //callback_evt fCallbackServiceEvt;
395 
396  std::vector<std::string> fServiceList;
397 
398  std::set<std::string> fServers;
399 
400  void CallbackServerAdd(const std::string &server)
401  {
403 
404  if (fServers.find(server)!=fServers.end())
405  return;
406 
407  fStateMachine->Subscribe(server+"/SERVICE_LIST")
408  (fStateMachine->Wrap(std::bind(&DimDnsServiceList::HandlerServiceList, this, std::placeholders::_1)));
409 
410  fServers.insert(server);
411  }
412 
413  void HandlerServiceListImp(const EventImp &evt);
414 
415 /*
416  virtual void CallbackServiceEvt(const EventImp &evt)
417  {
418  if (fCallbackServiceEvt)
419  fCallbackServiceEvt(evt);
420  }
421 */
422  virtual int HandlerServiceList(const EventImp &evt)
423  {
424  HandlerServiceListImp(evt);
425  //CallbackServiceEvent(evt);
426 
428  }
429 
430 
431  virtual void CallbackServiceAdd(const Service &service)
432  {
433  if (fCallbackServiceAdd)
434  fCallbackServiceAdd(service);
435  }
436 
437 public:
438  DimDnsServiceList() : fStateMachine(0)
439  {
440  }
441 
443  {
444  fStateMachine = &imp;
446  }
447 
448  void SetCallbackServiceAdd(const callback_svc &cb)
449  {
450  fCallbackServiceAdd = cb;
451  }
452 /*
453  void SetCallbackServiceEvt(const callback_svc &cb)
454  {
455  fCallbackServiceEvt = cb;
456  }
457 */
458 };
459 
460 #endif
virtual void CallbackServerEvent(const EventImp &evt)
Definition: DimState.h:338
std::vector< std::string > fServiceList
Definition: DimState.h:396
virtual State description() const
Definition: DimState.h:84
virtual void Subscribe(StateMachineImp &imp)
Definition: DimState.h:134
DimDescribedState(const std::string &n)
Definition: DimState.h:128
int HandleServiceDesc(const EventImp &evt)
Definition: DimState.h:203
A general base-class describing events issues in a state machine.
Definition: EventImp.h:11
Base class for a state machine implementation.
callback_desc fCallbackStates
Definition: DimState.h:118
void CallbackServerAdd(const std::string &server)
Definition: DimState.h:400
DimDescriptions(const std::string &n)
Definition: DimState.h:183
std::string comment
Name (e.g. &#39;Connected&#39;)
Definition: State.h:13
virtual void CallbackServerRemove(const std::string &str)
Definition: DimState.h:333
int Handler(const EventImp &evt)
Definition: DimState.h:222
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
char str[80]
Definition: test_client.c:7
std::ostream & operator<<(std::ostream &out, const DimState &s)
Definition: DimState.h:87
int scriptdepth
Definition: DimState.h:301
std::set< std::string > fServers
Definition: DimState.h:398
STL namespace.
virtual void CallbackDescriptions()
Definition: DimState.h:176
std::vector< State > states
Definition: DimState.h:132
const int32_t & state() const
Definition: DimState.h:80
int32_t GetInt() const
Definition: EventImp.h:93
std::pair< Time, int32_t > last
Definition: DimState.h:64
std::string GetString() const
Definition: EventImp.cc:194
DimState()
Definition: DimState.h:51
void HandlerImp(const EventImp &evt)
Definition: DimState.h:29
State description() const
Definition: DimState.h:160
std::string server
Definition: DimState.h:61
int index
Definition: State.h:11
virtual void CallbackServerAdd(const std::string &str)
Definition: DimState.h:328
std::map< std::string, callback > fCallbacks
Definition: DimState.h:254
virtual ~DimState()
Definition: DimState.h:57
std::string version() const
Definition: DimState.h:236
std::string msg
Definition: DimState.h:360
DimVersion()
Definition: DimState.h:234
std::function< int(const EventImp &)> callback
Definition: DimState.h:25
void SetCallbackStates(const callback_desc &cb)
Definition: DimState.h:142
std::string shortmsg
Definition: DimState.h:300
virtual int HandlerServiceList(const EventImp &evt)
Definition: DimState.h:422
void SetCallbackServerEvent(const callback_evt &cb)
Definition: DimState.h:378
void AddCallback(const std::string &script, const callback &cb)
Definition: DimState.h:303
virtual Time GetTime() const
Definition: EventImp.h:57
std::function< void()> callback_desc
Definition: DimState.h:116
callback_evt fCallbackServerEvent
Definition: DimState.h:322
Set color Blue.
Definition: WindowLog.h:20
virtual int GetQoS() const
Definition: EventImp.h:58
void SetCallbackServiceAdd(const callback_svc &cb)
Definition: DimState.h:448
virtual void CallbackStates()
Definition: DimState.h:120
static std::vector< Description > SplitDescription(const std::string &buffer)
Definition: Description.cc:82
callback_srv fCallbackServerRemove
Definition: DimState.h:321
int Handler(const EventImp &evt)
Definition: DimState.h:256
DimControl()
Definition: DimState.h:297
virtual void Subscribe(StateMachineImp &imp)
Definition: DimState.h:362
std::function< void(const Service &)> callback_svc
Definition: DimState.h:391
void SetCallbackServerRemove(const callback_srv &cb)
Definition: DimState.h:373
virtual int HandlerServer(const EventImp &evt)
Definition: DimState.h:343
std::vector< std::vector< Description > > descriptions
Definition: DimState.h:187
std::string service
Definition: DimState.h:62
virtual ~DimDnsServerList()
Definition: DimState.h:355
callback_srv fCallbackServerAdd
Definition: DimState.h:320
virtual void Subscribe(StateMachineImp &imp)
Definition: DimState.h:68
EventImp & Subscribe(const std::string &name)
std::set< std::string > fServerList
Definition: DimState.h:324
int HandleDesc(const EventImp &evt)
Definition: DimState.h:147
callback_svc fCallbackServiceAdd
Definition: DimState.h:393
virtual int Handler(const EventImp &evt)
Definition: DimState.h:44
callback_desc fCallbackDescriptions
Definition: DimState.h:174
void SetCallbackDescriptions(const callback_desc &cb)
Definition: DimState.h:197
State description() const
Definition: DimState.h:308
bool online() const
Definition: DimState.h:82
std::function< void()> callback_desc
Definition: DimState.h:172
State description() const
Definition: DimState.h:246
std::string name
Index (e.g. 1)
Definition: State.h:12
std::string file
Definition: DimState.h:299
std::pair< Time, int32_t > cur
Definition: DimState.h:65
DimState(const std::string &n, const std::string s="STATE")
Definition: DimState.h:52
callback fCallback
Definition: DimState.h:27
std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const
Definition: Time.cc:240
const Time & time() const
Definition: DimState.h:79
int Callback(const EventImp &evt)
Definition: DimState.h:39
virtual void Subscribe(StateMachineImp &imp)
Definition: DimState.h:189
void SetCallbackServerAdd(const callback_srv &cb)
Definition: DimState.h:368
virtual void CallbackServiceAdd(const Service &service)
Definition: DimState.h:431
void SetCallback(const callback &cb)
Definition: DimState.h:74
static std::vector< State > SplitStates(const std::string &buffer)
Time of state change.
Definition: State.cc:58
std::function< void(const std::string &)> callback_srv
Definition: DimState.h:317
Set attribute Bold.
Definition: WindowLog.h:36
std::string msg
Definition: DimState.h:66
void Subscribe(StateMachineImp &imp)
Definition: DimState.h:442
std::function< int(const EventImp &)> Wrap(const std::function< int(const EventImp &)> &func)
std::function< void(const EventImp &)> callback_evt
Definition: DimState.h:318
virtual size_t GetSize() const
Definition: EventImp.h:55
Changes for version
Definition: README_v11.txt:85
StateMachineImp * fStateMachine
Definition: DimState.h:389