now repeating frames after 1 s idle prepared sender to support multiple protocols
Stefan Schuermans

Stefan Schuermans commited on 2011-11-14 21:07:06
Showing 1 changed files, with 148 additions and 69 deletions.

... ...
@@ -18,12 +18,13 @@
18 18
 #include "SettingFile.h"
19 19
 #include "StreamMgr.h"
20 20
 #include "StreamRecv.h"
21
+#include "TimeCallee.h"
21 22
 
22 23
 namespace Blinker {
23 24
 
24 25
 /// stream sender
25 26
 template<typename ADDR, typename SOCK>
26
-class Sender: public Module, public StreamRecv
27
+class Sender: public Module, public StreamRecv, public TimeCallee
27 28
 {
28 29
 protected:
29 30
   /// static destination
... ...
@@ -70,6 +71,9 @@ public:
70 71
   /// set current frame to none
71 72
   virtual void setNoFrame();
72 73
 
74
+  /// callback when requsted time reached
75
+  virtual void timeCall();
76
+
73 77
 protected:
74 78
   /// get input stream and attach to it
75 79
   void getInStream();
... ...
@@ -86,49 +90,66 @@ protected:
86 90
   /**
87 91
    * @brief light update of static destinations,
88 92
    *        i.e. stat all files in current static destination directory
93
+   * @param[in] dests static destinations for protocol
94
+   * @param[in] proto Blinken protocol identifier
89 95
    */
90
-  void updateDestsLight();
96
+  void updateDestsLight(Dests &dests, etBlinkenProto proto);
91 97
 
92 98
   /**
93 99
    * @brief full update of static destinations,
94 100
    *        i.e. scan files in playlist directory
101
+   * @param[in] dirDests static destinations directory for protocol
102
+   * @param[in] dests static destinations for protocol
103
+   * @param[in] proto Blinken protocol identifier
95 104
    */
96
-  void updateDestsFull();
105
+  void updateDestsFull(Directory &dirDests, Dests &dests,
106
+                       etBlinkenProto proto);
97 107
 
98 108
   /**
99 109
    * @brief send current frame to address
100 110
    * @param[in] addr address to send to
111
+   * @param[in] proto Blinken protocol identifier
101 112
    */
102
-  void sendCurFrame(const ADDR &addr);
113
+  void sendCurFrame(const ADDR &addr, etBlinkenProto proto);
103 114
 
104 115
   /**
105
-   * @brief send frame to address
106
-   * @param[in] mcuf MCUF data of frame
116
+   * @brief send "no frame" to address
107 117
    * @param[in] addr address to send to
118
+   * @param[in] proto Blinken protocol identifier
108 119
    */
109
-  void sendFrame(const std::string &mcuf, const ADDR &addr);
120
+  void sendNoFrame(const ADDR &addr, etBlinkenProto proto);
110 121
 
111 122
   /**
112
-   * @brief send "no frame" to address
123
+   * @brief send frame to address
124
+   * @param[in] data protcol data of frame
113 125
    * @param[in] addr address to send to
114 126
    */
115
-  void sendNoFrame(const ADDR &addr);
127
+  void sendFrame(const std::string &data, const ADDR &addr);
116 128
 
117 129
   /**
118
-   * @brief convert frame to MCUF data
130
+   * @brief convert frame to protocol data
119 131
    * @param[in] pFrame frame
120
-   * @param[out] mcuf MCUF data
132
+   * @param[in] proto Blinken protocol identifier
133
+   * @param[out] data protcol data
121 134
    */
122
-  void frame2mcuf(stBlinkenFrame *pFrame, std::string &mcuf);
135
+  void frame2data(stBlinkenFrame *pFrame, etBlinkenProto proto,
136
+                  std::string &data);
137
+
138
+  /**
139
+   * @brief get "no frame" protocol data
140
+   * @param[in] proto Blinken protocol identifier
141
+   * @param[out] data protcol data
142
+   */
143
+  void noFrame2data(etBlinkenProto proto, std::string &data);
123 144
 
124 145
 protected:
125 146
   SettingFile m_fileInStream; ///< input stream name file
126 147
   SettingFile m_fileBind;     ///< bind address file
127
-  Directory   m_dirDests;     ///< static destinations directory
148
+  Directory   m_dirDestsMcuf; ///< static MCUF destinations directory
128 149
   std::string m_nameInStream; ///< name of input stream
129 150
   Stream      *m_pInStream;   ///< input stream
130 151
   SOCK        *m_pSock;       ///< socket to use for sending streams
131
-  Dests        m_dests;        ///< current static destinations
152
+  Dests        m_destsMcuf;   ///< current static MCUF destinations
132 153
 }; // class Sender
133 154
 
134 155
 /* ##########
... ...
@@ -147,7 +168,7 @@ Sender<ADDR, SOCK>::Sender(CallMgr &callMgr, StreamMgr &streamMgr,
147 168
   Module(callMgr, streamMgr, dirBase),
148 169
   m_fileInStream(dirBase.getFile("instream")),
149 170
   m_fileBind(dirBase.getFile("bind")),
150
-  m_dirDests(dirBase.getSubdir("destinations")),
171
+  m_dirDestsMcuf(dirBase.getSubdir("mcuf")),
151 172
   m_pInStream(NULL),
152 173
   m_pSock(NULL)
153 174
 {
... ...
@@ -157,22 +178,22 @@ Sender<ADDR, SOCK>::Sender(CallMgr &callMgr, StreamMgr &streamMgr,
157 178
   createSock();
158 179
 
159 180
   // load static destinations
160
-  updateDestsFull();
181
+  updateDestsFull(m_dirDestsMcuf, m_destsMcuf, BlinkenProtoMcuf);
161 182
 }
162 183
 
163 184
 /// virtual destructor
164 185
 template<typename ADDR, typename SOCK>
165 186
 Sender<ADDR, SOCK>::~Sender()
166 187
 {
167
-  // send no frame to all static destinations
168
-  typename Dests::const_iterator itDest;
169
-  for (itDest = m_dests.begin(); itDest != m_dests.end(); ++itDest)
170
-    sendNoFrame(itDest->m_addr);
188
+  // send no frame to all destinations
189
+  setNoFrame();
171 190
 
172 191
   // destroy socket
173 192
   destroySock();
174 193
   // detach from input stream and release it
175 194
   releaseInStream();
195
+  // cancel time callback
196
+  m_callMgr.cancelTimeCall(this);
176 197
 }
177 198
 
178 199
 /// check for update of configuration
... ...
@@ -193,10 +214,10 @@ void Sender<ADDR, SOCK>::updateConfig()
193 214
 
194 215
   // static destinations update
195 216
   // (directory modified -> full, otherwise -> light)
196
-  if (m_dirDests.checkModified())
197
-    updateDestsFull();
217
+  if (m_dirDestsMcuf.checkModified())
218
+    updateDestsFull(m_dirDestsMcuf, m_destsMcuf, BlinkenProtoMcuf);
198 219
   else
199
-    updateDestsLight();
220
+    updateDestsLight(m_destsMcuf, BlinkenProtoMcuf);
200 221
 }
201 222
 
202 223
 /**
... ...
@@ -208,23 +229,54 @@ void Sender<ADDR, SOCK>::setFrame(stBlinkenFrame *pFrame)
208 229
 {
209 230
   std::string mcuf;
210 231
 
211
-  // convert frame to MCUF packet
212
-  frame2mcuf(pFrame, mcuf);
232
+  // convert frame to protocol data
233
+  if (!m_destsMcuf.empty())
234
+    frame2data(pFrame, BlinkenProtoMcuf, mcuf);
213 235
 
214
-  // send frame to all static destinations
236
+  // send frame to all static MCUF destinations
215 237
   typename Dests::const_iterator itDest;
216
-  for (itDest = m_dests.begin(); itDest != m_dests.end(); ++itDest)
238
+  for (itDest = m_destsMcuf.begin(); itDest != m_destsMcuf.end(); ++itDest)
217 239
     sendFrame(mcuf, itDest->m_addr);
240
+
241
+  // request time callback in one second
242
+  m_callMgr.requestTimeCall(this, Time::now() + Time(1));
218 243
 }
219 244
 
220 245
 /// set current frame to none
221 246
 template<typename ADDR, typename SOCK>
222 247
 void Sender<ADDR, SOCK>::setNoFrame()
223 248
 {
224
-  // send "no frame" to all static destinations
249
+  std::string mcuf;
250
+
251
+  // get "no frame" protocol data
252
+  if (!m_destsMcuf.empty())
253
+    noFrame2data(BlinkenProtoMcuf, mcuf);
254
+
255
+  // send "no frame" to all static MCUF destinations
225 256
   typename Dests::const_iterator itDest;
226
-  for (itDest = m_dests.begin(); itDest != m_dests.end(); ++itDest)
227
-    sendNoFrame(itDest->m_addr);
257
+  for (itDest = m_destsMcuf.begin(); itDest != m_destsMcuf.end(); ++itDest)
258
+    sendFrame(mcuf, itDest->m_addr);
259
+
260
+  // request time callback in one second
261
+  m_callMgr.requestTimeCall(this, Time::now() + Time(1));
262
+}
263
+
264
+/// callback when requsted time reached
265
+template<typename ADDR, typename SOCK>
266
+void Sender<ADDR, SOCK>::timeCall()
267
+{
268
+  stBlinkenFrame *pFrame;
269
+  std::string data;
270
+
271
+  // get current frame from stream
272
+  if (m_pInStream && m_pInStream->getCurFrame(pFrame))
273
+    // repeat frame - behave exactly like this frame had been set
274
+    setFrame(pFrame);
275
+
276
+  // no stream of no current frame
277
+  else
278
+    // repeat "no frame" - behave exactly like "no frame" had been set
279
+    setNoFrame();
228 280
 }
229 281
 
230 282
 /// get input stream and attach to it
... ...
@@ -296,27 +348,29 @@ void Sender<ADDR, SOCK>::destroySock()
296 348
 /**
297 349
  * @brief light update of static destinations,
298 350
  *        i.e. stat all files in current static destination directory
351
+ * @param[in] dests static destinations for one protocol
352
+ * @param[in] proto Blinken protocol identifier
299 353
  */
300 354
 template<typename ADDR, typename SOCK>
301
-void Sender<ADDR, SOCK>::updateDestsLight()
355
+void Sender<ADDR, SOCK>::updateDestsLight(Dests &dests, etBlinkenProto proto)
302 356
 {
303 357
   // walk through all files in static dest dir and check for modification
304
-  typename Dests::iterator itDest = m_dests.begin();
305
-  while (itDest != m_dests.end()) {
358
+  typename Dests::iterator itDest = dests.begin();
359
+  while (itDest != dests.end()) {
306 360
 
307 361
     // address changed
308 362
     if (itDest->m_file.checkModified()) {
309 363
       // send "no frame" to old address
310
-      sendNoFrame(itDest->m_addr);
364
+      sendNoFrame(itDest->m_addr, proto);
311 365
       // re-load address
312 366
       if (!itDest->parse()) {
313 367
         // parsing address failed -> remove destination
314
-        itDest = m_dests.erase(itDest);
368
+        itDest = dests.erase(itDest);
315 369
         // do not advance to next destination
316 370
         continue;
317 371
       }
318 372
       // send current frame to new address
319
-      sendCurFrame(itDest->m_addr);
373
+      sendCurFrame(itDest->m_addr, proto);
320 374
     }
321 375
 
322 376
     // advance to next destination
... ...
@@ -328,30 +382,34 @@ void Sender<ADDR, SOCK>::updateDestsLight()
328 382
 /**
329 383
  * @brief full update of static destinations,
330 384
  *        i.e. scan files in playlist directory
385
+ * @param[in] dirDests static destinations directory for protocol
386
+ * @param[in] dests static destinations for protocol
387
+ * @param[in] proto Blinken protocol identifier
331 388
  */
332 389
 template<typename ADDR, typename SOCK>
333
-void Sender<ADDR, SOCK>::updateDestsFull()
390
+void Sender<ADDR, SOCK>::updateDestsFull(Directory &dirDests, Dests &dests,
391
+                                         etBlinkenProto proto)
334 392
 {
335 393
   // get list of files in static destinations directory
336 394
   typedef std::list<std::string> Filelist;
337 395
   Filelist curFiles;
338
-  m_dirDests.getEntries(Directory::TypeFile, curFiles);
396
+  dirDests.getEntries(Directory::TypeFile, curFiles);
339 397
 
340 398
   // walk through current static destinations and file list simultaneously
341 399
   Filelist::const_iterator itFile = curFiles.begin();
342
-  typename Dests::iterator itDest = m_dests.begin();
343
-  while (itFile != curFiles.end() || itDest != m_dests.end()) {
400
+  typename Dests::iterator itDest = dests.begin();
401
+  while (itFile != curFiles.end() || itDest != dests.end()) {
344 402
 
345 403
     // new static destination inserted
346
-    if (itDest == m_dests.end() ||
404
+    if (itDest == dests.end() ||
347 405
         (itFile != curFiles.end() && *itFile < itDest->m_name)) {
348 406
       // parse new address
349
-      Dest dest(*itFile, m_dirDests.getFile(*itFile));
407
+      Dest dest(*itFile, dirDests.getFile(*itFile));
350 408
       if (dest.parse()) {
351 409
         // insert new static destination
352
-        m_dests.insert(itDest, dest);
410
+        dests.insert(itDest, dest);
353 411
         // send current frame to new static destination
354
-        sendCurFrame(itDest->m_addr);
412
+        sendCurFrame(itDest->m_addr, proto);
355 413
       }
356 414
       // advance to next file
357 415
       ++itFile;
... ...
@@ -362,9 +420,9 @@ void Sender<ADDR, SOCK>::updateDestsFull()
362 420
     else if (itFile == curFiles.end() || *itFile > itDest->m_name ||
363 421
              itDest->m_file.checkModified()) {
364 422
       // send "no frame" to old static destination
365
-      sendNoFrame(itDest->m_addr);
423
+      sendNoFrame(itDest->m_addr, proto);
366 424
       // remove static destination
367
-      itDest = m_dests.erase(itDest);
425
+      itDest = dests.erase(itDest);
368 426
       // do not advance to next file
369 427
     }
370 428
 
... ...
@@ -381,69 +439,90 @@ void Sender<ADDR, SOCK>::updateDestsFull()
381 439
 /**
382 440
  * @brief send current frame to address
383 441
  * @param[in] addr address to send to
442
+ * @param[in] proto Blinken protocol identifier
384 443
  */
385 444
 template<typename ADDR, typename SOCK>
386
-void Sender<ADDR, SOCK>::sendCurFrame(const ADDR &addr)
445
+void Sender<ADDR, SOCK>::sendCurFrame(const ADDR &addr, etBlinkenProto proto)
387 446
 {
388 447
   stBlinkenFrame *pFrame;
389
-  std::string mcuf;
448
+  std::string data;
390 449
 
391 450
   // get current frame from stream
392 451
   if (m_pInStream && m_pInStream->getCurFrame(pFrame)) {
393
-    // convert frame to MCUF packet
394
-    frame2mcuf(pFrame, mcuf);
452
+    // convert frame to protocal data
453
+    frame2data(pFrame, proto, data);
395 454
     // send frame to address
396
-    sendFrame(mcuf, addr);
455
+    sendFrame(data, addr);
397 456
   }
398 457
 
399 458
   // no stream of no current frame
400 459
   else {
460
+    // get "no frame" ad protocal data
461
+    noFrame2data(proto, data);
401 462
     // send "no frame" to address
402
-    sendNoFrame(addr);
463
+    sendFrame(data, addr);
403 464
   }
404 465
 }
405 466
 
406 467
 /**
407
- * @brief send frame to address
408
- * @param[in] mcuf MCUF data of frame
468
+ * @brief send "no frame" to address
409 469
  * @param[in] addr address to send to
470
+ * @param[in] proto Blinken protocol identifier
410 471
  */
411 472
 template<typename ADDR, typename SOCK>
412
-void Sender<ADDR, SOCK>::sendFrame(const std::string &mcuf, const ADDR &addr)
473
+void Sender<ADDR, SOCK>::sendNoFrame(const ADDR &addr, etBlinkenProto proto)
413 474
 {
414
-  if (m_pSock) {
415
-    m_pSock->send(mcuf, addr);
416
-  }
475
+  std::string data;
476
+
477
+  // get "no frame" ad protocal data
478
+  noFrame2data(proto, data);
479
+
480
+  // send "no frame" to address
481
+  sendFrame(data, addr);
417 482
 }
418 483
 
419 484
 /**
420
- * @brief send "no frame" to address
485
+ * @brief send frame to address
486
+ * @param[in] data protocol data of frame
421 487
  * @param[in] addr address to send to
422 488
  */
423 489
 template<typename ADDR, typename SOCK>
424
-void Sender<ADDR, SOCK>::sendNoFrame(const ADDR &addr)
490
+void Sender<ADDR, SOCK>::sendFrame(const std::string &data, const ADDR &addr)
425 491
 {
426
-  std::string noframe; // FIXME
427
-
428 492
   if (m_pSock) {
429
-    m_pSock->send(noframe, addr);
493
+    m_pSock->send(data, addr);
430 494
   }
431 495
 }
432 496
 
433 497
 /**
434
- * @brief convert frame to MCUF data
498
+ * @brief convert frame to protocol data
435 499
  * @param[in] pFrame frame
436
- * @param[out] mcuf MCUF data
500
+ * @param[in] proto Blinken protocol identifier
501
+ * @param[out] data protocol data
437 502
  */
438 503
 template<typename ADDR, typename SOCK>
439
-void Sender<ADDR, SOCK>::frame2mcuf(stBlinkenFrame *pFrame, std::string &mcuf)
504
+void Sender<ADDR, SOCK>::frame2data(stBlinkenFrame *pFrame,
505
+                                    etBlinkenProto proto, std::string &data)
440 506
 {
441 507
   char buf[65536];
442 508
   int len;
443 509
 
444
-  // convert frame to MCUF packet
445
-  len = BlinkenFrameToNetwork(pFrame, BlinkenProtoMcuf, buf, sizeof(buf));
446
-  mcuf.assign(buf, len);
510
+  // convert frame to protcol data
511
+  len = BlinkenFrameToNetwork(pFrame, proto, buf, sizeof(buf));
512
+  data.assign(buf, len);
513
+}
514
+
515
+/**
516
+ * @brief get "no frame" protocol data
517
+ * @param[in] proto Blinken protocol identifier
518
+ * @param[out] data protcol data
519
+ */
520
+template<typename ADDR, typename SOCK>
521
+void Sender<ADDR, SOCK>::noFrame2data(etBlinkenProto proto, std::string &data)
522
+{
523
+  // obtain "no frame" protcol data
524
+  (void)proto; // FIXME
525
+  data.clear(); // FIXME
447 526
 }
448 527
 
449 528
 /* ################
450 529