DDS  ver. 2.0
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 "dds_intercom.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(true)
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  if (!m_channel.expired())
78  {
79  auto pUI = m_channel.lock();
80  pUI->template pushMsg<protocol_api::cmdSIMPLE_MSG>(
82 
83  // measure time to activate
84  std::chrono::steady_clock::time_point curTime = std::chrono::steady_clock::now();
85 
86  pUI->template pushMsg<protocol_api::cmdPROGRESS>(protocol_api::SProgressCmd(
91  std::chrono::duration_cast<std::chrono::milliseconds>(curTime - m_startTime).count()));
92  }
93 
94  checkAllReceived();
95  }
96  catch (std::bad_weak_ptr& e)
97  {
98  // TODO: Do we need to log something here?
99  }
100 
101  return true;
102  }
103 
104  template <class A>
106  const A& _cmd,
108  {
109  try
110  {
111  std::lock_guard<std::mutex> lock(m_mutexReceive);
112 
114 
115  T* pThis = static_cast<T*>(this);
116  std::string userMessage = pThis->getErrorMessage(_sender, _cmd, _channel);
117 
118  if (!m_channel.expired())
119  {
120  auto pUI = m_channel.lock();
121  pUI->template pushMsg<protocol_api::cmdSIMPLE_MSG>(
123 
124  // measure time to activate
125  std::chrono::steady_clock::time_point curTime = std::chrono::steady_clock::now();
126 
127  pUI->template pushMsg<protocol_api::cmdPROGRESS>(protocol_api::SProgressCmd(
128  m_srcCommand,
132  std::chrono::duration_cast<std::chrono::milliseconds>(curTime - m_startTime).count()));
133  }
134 
135  checkAllReceived();
136  }
137  catch (std::bad_weak_ptr& e)
138  {
139  // TODO: Do we need to log something here?
140  }
141 
142  return true;
143  }
144 
145  private:
146  void checkAllReceived()
147  {
148  try
149  {
150  if (allReceived())
151  {
152  T* pThis = static_cast<T*>(this);
153  std::string userMessage = pThis->getAllReceivedMessage();
154 
155  if (!m_channel.expired())
156  {
157  auto pUI = m_channel.lock();
158  pUI->template pushMsg<protocol_api::cmdSIMPLE_MSG>(
160 
162  {
163  pUI->template pushMsg<protocol_api::cmdSHUTDOWN>();
164  }
165  m_channel.reset();
166  }
167  }
168  }
169  catch (std::bad_weak_ptr& e)
170  {
171  // TODO: Do we need to log something here?
172  }
173  }
174 
175  public:
180  std::mutex m_mutexStart;
181  std::mutex m_mutexReceive;
183  uint16_t m_srcCommand;
184 
185  private:
186  std::chrono::steady_clock::time_point m_startTime;
187  };
188 
189  class CGetLogChannelInfo : public CUIChannelInfo<CGetLogChannelInfo>
190  {
191  public:
194  {
196  }
197 
198  std::string getMessage(const dds::protocol_api::SSenderInfo& _sender,
200  CAgentChannel::weakConnectionPtr_t _channel) const
201  {
202  std::stringstream ss;
203  auto p = _channel.lock();
204  ss << nofReceived() << "/" << m_nofRequests << " [" << p->getId(_sender) << "] -> "
205  << _cmd.m_requestedFileName;
206  return ss.str();
207  }
208 
210  const protocol_api::SSimpleMsgCmd& _cmd,
211  CAgentChannel::weakConnectionPtr_t _channel) const
212  {
213  std::stringstream ss;
214  auto p = _channel.lock();
215  ss << nofReceived() << "/" << m_nofRequests << " Error [" << p->getId(_sender) << "]: " << _cmd.m_sMsg;
216  return ss.str();
217  }
218 
219  std::string getAllReceivedMessage() const
220  {
221  std::stringstream ss;
222  ss << "total: " << m_nofRequests << ", received: " << nofReceived()
223  << ", errors: " << m_nofReceivedErrors;
224  return ss.str();
225  }
226  };
227 
228  class CTestChannelInfo : public CUIChannelInfo<CTestChannelInfo>
229  {
230  public:
233  , m_totalReceived(0)
234  , m_totalTime(0)
235  {
237  }
238 
239  std::string getMessage(const dds::protocol_api::SSenderInfo& _sender,
241  CAgentChannel::weakConnectionPtr_t _channel) const
242  {
243  std::stringstream ss;
244  auto p = _channel.lock();
245  float downloadTime = 0.000001 * _cmd.m_downloadTime; // micros->s
246  float speed = (downloadTime != 0.) ? 0.001 * _cmd.m_receivedFileSize / downloadTime : 0;
247  ss << nofReceived() << "/" << m_nofRequests << " [" << p->getId(_sender)
248  << "]: " << _cmd.m_receivedFileSize << " bytes in " << downloadTime << " s (" << speed << " KB/s)";
249  return ss.str();
250  }
251 
253  const protocol_api::SSimpleMsgCmd& _cmd,
254  CAgentChannel::weakConnectionPtr_t _channel) const
255  {
256  std::stringstream ss;
257  auto p = _channel.lock();
258  ss << nofReceived() << "/" << m_nofRequests << " Error [" << p->getId(_sender) << "]: " << _cmd.m_sMsg;
259  return ss.str();
260  }
261 
262  std::string getAllReceivedMessage() const
263  {
264  std::stringstream ss;
265  ss << "received: " << nofReceived() << ", total: " << m_nofRequests
266  << ", errors: " << m_nofReceivedErrors << " | ";
267 
268  float downloadTime = 0.000001 * m_totalTime; // micros->s
269  float speed = (downloadTime != 0.) ? 0.001 * m_totalReceived / downloadTime : 0;
270  ss << "download " << m_totalReceived << " bytes in " << downloadTime << " s (" << speed << " KB/s)";
271  return ss.str();
272  }
273 
274  size_t m_totalReceived; // [bytes]
275  size_t m_totalTime; // [ms]
276  };
277 
278  class CUpdateTopologyChannelInfo : public CUIChannelInfo<CUpdateTopologyChannelInfo>
279  {
280  public:
283  {
285  }
286 
287  std::string getMessage(const dds::protocol_api::SSenderInfo& _sender,
288  const protocol_api::SSimpleMsgCmd& _cmd,
289  CAgentChannel::weakConnectionPtr_t _channel) const
290  {
291  std::stringstream ss;
292  auto p = _channel.lock();
293  std::string str = (m_srcCommand == protocol_api::cmdACTIVATE_AGENT) ? "Activated" : "Stopped";
294  ss << nofReceived() << "/" << m_nofRequests << " [" << p->getId(_sender) << "] -> " << str;
295  return ss.str();
296  }
297 
299  const protocol_api::SSimpleMsgCmd& _cmd,
300  CAgentChannel::weakConnectionPtr_t _channel) const
301  {
302  std::stringstream ss;
303  auto p = _channel.lock();
304  ss << nofReceived() << "/" << m_nofRequests << " Error [" << p->getId(_sender) << "]: " << _cmd.m_sMsg;
305  return ss.str();
306  }
307 
308  std::string getAllReceivedMessage() const
309  {
310  std::stringstream ss;
311  std::string str = (m_srcCommand == protocol_api::cmdACTIVATE_AGENT) ? "activations" : "stopped";
312  ss << "total: " << m_nofRequests << ", " << str << ": " << nofReceived()
313  << ", errors: " << m_nofReceivedErrors << "\n";
314 
315  return ss.str();
316  }
317  };
318 
319  class CSubmitAgentsChannelInfo : public CUIChannelInfo<CSubmitAgentsChannelInfo>
320  {
321  public:
323 
324  public:
325  std::string getMessage(const protocol_api::SSimpleMsgCmd& _cmd,
326  CAgentChannel::weakConnectionPtr_t _channel) const;
327  std::string getErrorMessage(const protocol_api::SSimpleMsgCmd& _cmd,
328  CAgentChannel::weakConnectionPtr_t _channel) const;
329  std::string getAllReceivedMessage() const;
330  bool processCustomCommandMessage(const protocol_api::SCustomCmdCmd& _cmd,
332  bool isPluginOnline();
333  // This function can be called to check whether plug-in failed to start.
334  // If this is the case, the function will alert user and close connection to UI.
335  void checkPluginFailedToStart();
336  void shutdown();
337  void initPlugin();
338 
339  public:
342  std::chrono::system_clock::duration m_PluginStartTime;
343  bool m_bInit;
344  };
345  }
346 }
347 
348 #endif /* defined(__DDS__UIChannelInfo__) */
Definition: ProtocolCommands.h:53
Definition: BaseEventHandlersImpl.h:48
bool processErrorMessage(const dds::protocol_api::SSenderInfo &_sender, const A &_cmd, CAgentChannel::weakConnectionPtr_t _channel)
Definition: UIChannelInfo.h:105
std::string getMessage(const dds::protocol_api::SSenderInfo &_sender, const protocol_api::SSimpleMsgCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:287
CGetLogChannelInfo()
Definition: UIChannelInfo.h:192
std::string getMessage(const dds::protocol_api::SSenderInfo &_sender, const protocol_api::SBinaryAttachmentReceivedCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:239
std::string getErrorMessage(const dds::protocol_api::SSenderInfo &_sender, const protocol_api::SSimpleMsgCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:209
size_t nofReceived() const
Definition: UIChannelInfo.h:43
Definition: BinaryAttachmentReceivedCmd.h:15
size_t m_totalTime
Definition: UIChannelInfo.h:275
uint32_t m_receivedFileSize
Number of recieved bytes.
Definition: BinaryAttachmentReceivedCmd.h:26
Definition: UIChannelInfo.h:189
bool m_shutdownOnComplete
Definition: UIChannelInfo.h:182
Definition: UIChannelInfo.h:228
size_t m_nofReceivedErrors
Definition: UIChannelInfo.h:178
std::string m_strInitialSubmitRequest
Definition: UIChannelInfo.h:341
std::string getAllReceivedMessage() const
Definition: UIChannelInfo.h:262
CAgentChannel::weakConnectionPtr_t m_channelSubmitPlugin
Definition: UIChannelInfo.h:340
std::mutex m_mutexStart
Definition: UIChannelInfo.h:180
std::weak_ptr< CAgentChannel > weakConnectionPtr_t
Definition: BaseChannelImpl.h:217
Definition: SimpleMsgCmd.h:16
std::string m_sMsg
Definition: SimpleMsgCmd.h:27
void zeroCounters()
Definition: UIChannelInfo.h:54
CUpdateTopologyChannelInfo()
Definition: UIChannelInfo.h:281
std::string m_requestedFileName
Requested name of the file.
Definition: BinaryAttachmentReceivedCmd.h:24
Definition: UIChannelInfo.h:278
size_t m_totalReceived
Definition: UIChannelInfo.h:274
Definition: dds-agent/src/AgentConnectionManager.h:18
CUIChannelInfo()
Definition: UIChannelInfo.h:30
std::string getMessage(const dds::protocol_api::SSenderInfo &_sender, const protocol_api::SBinaryAttachmentReceivedCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:198
std::chrono::system_clock::duration m_PluginStartTime
Definition: UIChannelInfo.h:342
Definition: UIChannelInfo.h:26
std::string getErrorMessage(const dds::protocol_api::SSenderInfo &_sender, const protocol_api::SSimpleMsgCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:252
std::string getErrorMessage(const dds::protocol_api::SSenderInfo &_sender, const protocol_api::SSimpleMsgCmd &_cmd, CAgentChannel::weakConnectionPtr_t _channel) const
Definition: UIChannelInfo.h:298
CTestChannelInfo()
Definition: UIChannelInfo.h:231
Definition: ProtocolCommands.h:50
bool allReceived() const
Definition: UIChannelInfo.h:48
CAgentChannel::weakConnectionPtr_t m_channel
Definition: UIChannelInfo.h:179
Definition: CustomCmdCmd.h:14
uint32_t m_downloadTime
Time spent to download file [microseconds].
Definition: BinaryAttachmentReceivedCmd.h:27
Definition: def.h:152
Definition: ProtocolCommands.h:46
uint16_t m_srcCommand
Definition: UIChannelInfo.h:183
bool m_bInit
Definition: UIChannelInfo.h:343
std::string getAllReceivedMessage() const
Definition: UIChannelInfo.h:308
std::mutex m_mutexReceive
Definition: UIChannelInfo.h:181
size_t m_nofRequests
Definition: UIChannelInfo.h:176
bool processMessage(const dds::protocol_api::SSenderInfo &_sender, const A &_cmd, CAgentChannel::weakConnectionPtr_t _channel)
Definition: UIChannelInfo.h:64
std::string getAllReceivedMessage() const
Definition: UIChannelInfo.h:219
Definition: ProgressCmd.h:19
Definition: def.h:150
size_t m_nofReceived
Definition: UIChannelInfo.h:177
Definition: UIChannelInfo.h:319