DDS  ver. 3.6
UIChannelInfo.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 #ifndef __DDS__UIChannelInfo__
6 #define __DDS__UIChannelInfo__
7 
8 // DDS
9 #include "AgentChannel.h"
10 #include "CustomCmdCmd.h"
11 #include "ProtocolCommands.h"
12 #include "ToolsProtocol.h"
13 // STD
14 #include <chrono>
15 #include <mutex>
16 #include <sstream>
17 #include <string>
18 // BOOST
19 #include <boost/property_tree/json_parser.hpp>
20 
21 namespace dds
22 {
23  namespace commander_cmd
24  {
25  template <class T>
27  {
28 
29  public:
31  : m_nofRequests(0)
32  , m_nofReceived(0)
34  , m_mutexStart()
35  , m_mutexReceive()
36  , m_shutdownOnComplete(false)
37  , m_srcCommand(0)
38  , m_startTime(std::chrono::steady_clock::now())
39  {
40  }
41 
42  public:
43  size_t nofReceived() const
44  {
46  }
47 
48  bool allReceived() const
49  {
50  return (nofReceived() == m_nofRequests);
51  }
52 
53  public:
54  void zeroCounters()
55  {
56  m_nofRequests = 0;
57  m_nofReceived = 0;
59  m_startTime = std::chrono::steady_clock::now();
60  }
61 
62  public:
63  template <class A>
65  const A& _cmd,
67  {
68  try
69  {
70  std::lock_guard<std::mutex> lock(m_mutexReceive);
71 
72  ++m_nofReceived;
73 
74  T* pThis = static_cast<T*>(this);
75  std::string userMessage = pThis->getMessage(_sender, _cmd, _channel);
76 
77  sendUIMessage(userMessage);
78 
79  // measure time to activate
80  std::chrono::steady_clock::time_point curTime = std::chrono::steady_clock::now();
81 
87  std::chrono::duration_cast<std::chrono::milliseconds>(curTime - m_startTime).count()));
88 
89  checkAllReceived();
90  }
91  catch (std::bad_weak_ptr& e)
92  {
93  // TODO: Do we need to log something here?
94  }
95 
96  return true;
97  }
98 
99  template <class A>
101  const A& _cmd,
103  {
104  try
105  {
106  std::lock_guard<std::mutex> lock(m_mutexReceive);
107 
109 
110  T* pThis = static_cast<T*>(this);
111  std::string userMessage = pThis->getErrorMessage(_sender, _cmd, _channel);
112 
114 
115  // measure time to activate
116  std::chrono::steady_clock::time_point curTime = std::chrono::steady_clock::now();
118  m_srcCommand,
122  std::chrono::duration_cast<std::chrono::milliseconds>(curTime - m_startTime).count()));
123 
124  checkAllReceived();
125  }
126  catch (std::bad_weak_ptr& e)
127  {
128  // TODO: Do we need to log something here?
129  }
130 
131  return true;
132  }
133 
134  void sendUIMessage(const std::string& _msg,
136  {
137  try
138  {
139  if (m_channel.expired())
140  return;
141 
142  auto pUI = m_channel.lock();
143  if (pUI)
144  {
145  dds::tools_api::SMessageResponseData msg;
146  msg.m_requestID = m_requestID;
147  msg.m_msg = _msg;
148  msg.m_severity = _severity;
149 
151  cmd.m_sCmd = msg.toJSON();
152  cmd.m_sCondition = "";
153  pUI->template pushMsg<protocol_api::cmdCUSTOM_CMD>(cmd);
154  }
155  }
156  catch (...)
157  {
158  }
159  }
160 
162  {
163  try
164  {
165  if (m_channel.expired())
166  return;
167 
168  auto pUI = m_channel.lock();
169  if (pUI)
170  {
172  dds::tools_api::SProgressResponseData progress(_progress);
173  progress.m_requestID = m_requestID;
174  cmd.m_sCmd = progress.toJSON();
175  cmd.m_sCondition = "";
176  pUI->template pushMsg<protocol_api::cmdCUSTOM_CMD>(cmd);
177  }
178  }
179  catch (...)
180  {
181  }
182  }
183 
184  void doneWithUI()
185  {
186  try
187  {
188  if (m_channel.expired())
189  return;
190 
191  auto pUI = m_channel.lock();
192  if (pUI)
193  {
194  dds::tools_api::SDoneResponseData done;
195  done.m_requestID = m_requestID;
197  cmd.m_sCmd = done.toJSON();
198  cmd.m_sCondition = "";
199  pUI->template pushMsg<protocol_api::cmdCUSTOM_CMD>(cmd);
200  }
201  }
202  catch (...)
203  {
204  }
205  }
206 
207  private:
208  void checkAllReceived()
209  {
210  try
211  {
212  if (allReceived())
213  {
214  if (!m_channel.expired())
215  {
216  T* pThis = static_cast<T*>(this);
217  std::string userMessage = pThis->getAllReceivedMessage();
218 
219  sendUIMessage(userMessage);
221  {
222  doneWithUI();
223  m_channel.reset();
224  }
225  }
226  }
227  }
228  catch (std::bad_weak_ptr& e)
229  {
230  // TODO: Do we need to log something here?
231  }
232  }
233 
234  public:
239  std::mutex m_mutexStart;
240  std::mutex m_mutexReceive;
242  uint16_t m_srcCommand;
244 
245  private:
246  std::chrono::steady_clock::time_point m_startTime;
247  };
248 
249  class CGetLogChannelInfo : public CUIChannelInfo<CGetLogChannelInfo>
250  {
251  public:
254  {
256  }
257 
258  std::string getMessage(const dds::protocol_api::SSenderInfo& /*_sender*/,
260  CAgentChannel::weakConnectionPtr_t _channel) const
261  {
262  std::stringstream ss;
263  auto p = _channel.lock();
264  ss << nofReceived() << "/" << m_nofRequests << " [" << p->getId() << "] -> "
265  << _cmd.m_requestedFileName;
266  return ss.str();
267  }
268 
269  std::string getErrorMessage(const dds::protocol_api::SSenderInfo& /*_sender*/,
270  const protocol_api::SReplyCmd& _cmd,
271  CAgentChannel::weakConnectionPtr_t _channel) const
272  {
273  std::stringstream ss;
274  auto p = _channel.lock();
275  ss << nofReceived() << "/" << m_nofRequests << " Error [" << p->getId() << "]: " << _cmd.m_sMsg;
276  return ss.str();
277  }
278 
279  std::string getAllReceivedMessage() const
280  {
281  std::stringstream ss;
282  ss << "total: " << m_nofRequests << ", received: " << nofReceived()
283  << ", errors: " << m_nofReceivedErrors;
284  return ss.str();
285  }
286  };
287 
288  class CTestChannelInfo : public CUIChannelInfo<CTestChannelInfo>
289  {
290  public:
293  , m_totalReceived(0)
294  , m_totalTime(0)
295  {
297  }
298 
299  std::string getMessage(const dds::protocol_api::SSenderInfo& /*_sender*/,
301  CAgentChannel::weakConnectionPtr_t _channel) const
302  {
303  std::stringstream ss;
304  auto p = _channel.lock();
305  float downloadTime = 0.000001 * _cmd.m_downloadTime; // micros->s
306  float speed = (downloadTime != 0.) ? 0.001 * _cmd.m_receivedFileSize / downloadTime : 0;
307  ss << nofReceived() << "/" << m_nofRequests << " [" << p->getId() << "]: " << _cmd.m_receivedFileSize
308  << " bytes in " << downloadTime << " s (" << speed << " KB/s)";
309  return ss.str();
310  }
311 
312  std::string getErrorMessage(const dds::protocol_api::SSenderInfo& /*_sender*/,
313  const protocol_api::SReplyCmd& _cmd,
314  CAgentChannel::weakConnectionPtr_t _channel) const
315  {
316  std::stringstream ss;
317  auto p = _channel.lock();
318  ss << nofReceived() << "/" << m_nofRequests << " Error [" << p->getId() << "]: " << _cmd.m_sMsg;
319  return ss.str();
320  }
321 
322  std::string getAllReceivedMessage() const
323  {
324  std::stringstream ss;
325  ss << "received: " << nofReceived() << ", total: " << m_nofRequests
326  << ", errors: " << m_nofReceivedErrors << " | ";
327 
328  float downloadTime = 0.000001 * m_totalTime; // micros->s
329  float speed = (downloadTime != 0.) ? 0.001 * m_totalReceived / downloadTime : 0;
330  ss << "download " << m_totalReceived << " bytes in " << downloadTime << " s (" << speed << " KB/s)";
331  return ss.str();
332  }
333 
334  size_t m_totalReceived; // [bytes]
335  size_t m_totalTime; // [ms]
336  };
337 
338  class CUpdateTopologyChannelInfo : public CUIChannelInfo<CUpdateTopologyChannelInfo>
339  {
340  public:
343  {
345  }
346 
347  std::string getMessage(const dds::protocol_api::SSenderInfo& /*_sender*/,
348  const protocol_api::SReplyCmd& /*_cmd*/,
349  CAgentChannel::weakConnectionPtr_t _channel) const
350  {
351  std::stringstream ss;
352  auto p = _channel.lock();
353  std::string str;
355  str = "Activated";
357  str = "Stopped";
359  str = "Topology updated";
360  ss << nofReceived() << "/" << m_nofRequests << " [" << p->getId() << "] -> " << str;
361  return ss.str();
362  }
363 
364  std::string getErrorMessage(const dds::protocol_api::SSenderInfo& /*_sender*/,
365  const protocol_api::SReplyCmd& _cmd,
366  CAgentChannel::weakConnectionPtr_t _channel) const
367  {
368  std::stringstream ss;
369  auto p = _channel.lock();
370  ss << nofReceived() << "/" << m_nofRequests << " Error [" << p->getId() << "]: " << _cmd.m_sMsg;
371  return ss.str();
372  }
373 
374  std::string getAllReceivedMessage() const
375  {
376  std::stringstream ss;
377  std::string str;
379  str = "activated";
381  str = "stopped";
383  str = "topology updates";
384  ss << "total: " << m_nofRequests << ", " << str << ": " << nofReceived()
385  << ", errors: " << m_nofReceivedErrors << "\n";
386 
387  return ss.str();
388  }
389  };
390 
391  class CSubmitAgentsChannelInfo : public CUIChannelInfo<CSubmitAgentsChannelInfo>
392  {
393  public:
395 
396  public:
397  std::string getMessage(const protocol_api::SSimpleMsgCmd& _cmd,
398  CAgentChannel::weakConnectionPtr_t _channel) const;
399  std::string getErrorMessage(const protocol_api::SSimpleMsgCmd& _cmd,
400  CAgentChannel::weakConnectionPtr_t _channel) const;
401  std::string getAllReceivedMessage() const;
404  bool isPluginOnline();
405  // This function can be called to check whether plug-in failed to start.
406  // If this is the case, the function will alert user and close connection to UI.
408  void shutdown();
409  void initPlugin();
410 
411  public:
414  std::chrono::system_clock::duration m_PluginStartTime;
415  bool m_bInit;
416  };
417  } // namespace commander_cmd
418 } // namespace dds
419 
420 #endif /* defined(__DDS__UIChannelInfo__) */
requestID_t m_requestID
Definition: ToolsProtocolCore.h:141
Definition: ProtocolCommands.h:54
Definition: BaseEventHandlersImpl.h:48
bool processErrorMessage(const dds::protocol_api::SSenderInfo &_sender, const A &_cmd, CAgentChannel::weakConnectionPtr_t _channel)
Definition: UIChannelInfo.h:100
Definition: ProtocolCommands.h:61
CGetLogChannelInfo()
Definition: UIChannelInfo.h:252
void sendUIProgress(const dds::tools_api::SProgressResponseData &_progress)
Definition: UIChannelInfo.h:161
void checkPluginFailedToStart()
Definition: UIChannelInfo.cpp:169
size_t nofReceived() const
Definition: UIChannelInfo.h:43
std::string getErrorMessage(const protocol_api::SSimpleMsgCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.cpp:32
Definition: BinaryAttachmentReceivedCmd.h:15
bool isPluginOnline()
Definition: UIChannelInfo.cpp:162
size_t m_totalTime
Definition: UIChannelInfo.h:335
std::string getMessage(const protocol_api::SSimpleMsgCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.cpp:23
uint32_t m_receivedFileSize
Number of recieved bytes.
Definition: BinaryAttachmentReceivedCmd.h:26
std::string getErrorMessage(const dds::protocol_api::SSenderInfo &, const protocol_api::SReplyCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:312
Definition: UIChannelInfo.h:249
bool m_shutdownOnComplete
Definition: UIChannelInfo.h:241
Definition: UIChannelInfo.h:288
std::string getMessage(const dds::protocol_api::SSenderInfo &, const protocol_api::SReplyCmd &, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:347
size_t m_nofReceivedErrors
Definition: UIChannelInfo.h:237
Definition: ReplyCmd.h:15
std::string m_strInitialSubmitRequest
Definition: UIChannelInfo.h:413
std::string getAllReceivedMessage() const
Definition: UIChannelInfo.h:322
std::string m_sCondition
Definition: CustomCmdCmd.h:25
CAgentChannel::weakConnectionPtr_t m_channelSubmitPlugin
Definition: UIChannelInfo.h:412
std::mutex m_mutexStart
Definition: UIChannelInfo.h:239
std::weak_ptr< CAgentChannel > weakConnectionPtr_t
Definition: BaseChannelImpl.h:230
dds::tools_api::requestID_t m_requestID
Definition: UIChannelInfo.h:243
std::string m_sCmd
Definition: CustomCmdCmd.h:24
Definition: SimpleMsgCmd.h:16
void zeroCounters()
Definition: UIChannelInfo.h:54
CUpdateTopologyChannelInfo()
Definition: UIChannelInfo.h:341
std::string getAllReceivedMessage() const
Definition: UIChannelInfo.cpp:41
std::string m_sMsg
Definition: ReplyCmd.h:33
void doneWithUI()
Definition: UIChannelInfo.h:184
uint64_t requestID_t
Definition: ToolsProtocolCore.h:136
std::string toJSON() const
Fill structure into JSON.
Definition: ToolsProtocolCore.h:145
std::string m_requestedFileName
Requested name of the file.
Definition: BinaryAttachmentReceivedCmd.h:24
Definition: UIChannelInfo.h:338
size_t m_totalReceived
Definition: UIChannelInfo.h:334
Miscellaneous functions and helpers are located here.
Definition: AgentConnectionManager.h:13
CUIChannelInfo()
Definition: UIChannelInfo.h:30
std::chrono::system_clock::duration m_PluginStartTime
Definition: UIChannelInfo.h:414
std::string getErrorMessage(const dds::protocol_api::SSenderInfo &, const protocol_api::SReplyCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:364
Definition: UIChannelInfo.h:26
Definition: ProtocolCommands.h:52
Structure holds information of a done response.
Definition: ToolsProtocol.h:50
bool processCustomCommandMessage(const protocol_api::SCustomCmdCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel)
Definition: UIChannelInfo.cpp:50
CSubmitAgentsChannelInfo()
Definition: UIChannelInfo.cpp:15
Definition: ProtocolCommands.h:51
std::string getMessage(const dds::protocol_api::SSenderInfo &, const protocol_api::SBinaryAttachmentReceivedCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:299
CTestChannelInfo()
Definition: UIChannelInfo.h:291
bool allReceived() const
Definition: UIChannelInfo.h:48
CAgentChannel::weakConnectionPtr_t m_channel
Definition: UIChannelInfo.h:238
Definition: CustomCmdCmd.h:14
uint32_t m_downloadTime
Time spent to download file [microseconds].
Definition: BinaryAttachmentReceivedCmd.h:27
Definition: ProtocolCommands.h:46
uint16_t m_srcCommand
Definition: UIChannelInfo.h:242
bool m_bInit
Definition: UIChannelInfo.h:415
std::string getAllReceivedMessage() const
Definition: UIChannelInfo.h:374
EMsgSeverity
Enumeration with message severity.
Definition: Intercom.h:126
std::mutex m_mutexReceive
Definition: UIChannelInfo.h:240
std::string getErrorMessage(const dds::protocol_api::SSenderInfo &, const protocol_api::SReplyCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:269
size_t m_nofRequests
Definition: UIChannelInfo.h:235
bool processMessage(const dds::protocol_api::SSenderInfo &_sender, const A &_cmd, CAgentChannel::weakConnectionPtr_t _channel)
Definition: UIChannelInfo.h:64
void sendUIMessage(const std::string &_msg, dds::intercom_api::EMsgSeverity _severity=dds::intercom_api::EMsgSeverity::info)
Definition: UIChannelInfo.h:134
std::string getAllReceivedMessage() const
Definition: UIChannelInfo.h:279
void initPlugin()
Definition: UIChannelInfo.cpp:248
void shutdown()
Definition: UIChannelInfo.cpp:218
std::string getMessage(const dds::protocol_api::SSenderInfo &, const protocol_api::SBinaryAttachmentReceivedCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:258
size_t m_nofReceived
Definition: UIChannelInfo.h:236
Definition: UIChannelInfo.h:391