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