added stream name to stream receiver interface callbacks
Stefan Schuermans

Stefan Schuermans commited on 2011-11-16 18:35:11
Showing 7 changed files, with 161 additions and 92 deletions.

... ...
@@ -55,19 +55,25 @@ void Printer::updateConfig()
55 55
 
56 56
 /**
57 57
  * @brief set current frame
58
+ * @param[in] stream stream name
58 59
  * @param[in] pFrame current frame
59 60
  */
60
-void Printer::setFrame(stBlinkenFrame *pFrame)
61
+void Printer::setFrame(const std::string &stream, stBlinkenFrame *pFrame)
61 62
 {
62 63
   char *str = BlinkenFrameToString(pFrame);
63 64
   std::cout << str;
64 65
   free(str);
66
+  (void)stream; // unused
65 67
 }
66 68
 
67
-/// set current frame to none
68
-void Printer::setNoFrame()
69
+/**
70
+ * @brief set current frame to none
71
+ * @param[in] stream stream name
72
+ */
73
+void Printer::setNoFrame(const std::string &stream)
69 74
 {
70 75
   std::cout << "no frame" << std::endl;
76
+  (void)stream; // unused
71 77
 }
72 78
 
73 79
 /// get input stream and attach to it
... ...
@@ -48,12 +48,16 @@ public:
48 48
 
49 49
   /**
50 50
    * @brief set current frame
51
+   * @param[in] stream stream name
51 52
    * @param[in] pFrame current frame
52 53
    */
53
-  virtual void setFrame(stBlinkenFrame *pFrame);
54
+  virtual void setFrame(const std::string &stream, stBlinkenFrame *pFrame);
54 55
 
55
-  /// set current frame to none
56
-  virtual void setNoFrame();
56
+  /**
57
+   * @brief set current frame to none
58
+   * @param[in] stream stream name
59
+   */
60
+  virtual void setNoFrame(const std::string &stream);
57 61
 
58 62
 protected:
59 63
   /// get input stream and attach to it
... ...
@@ -72,12 +72,16 @@ public:
72 72
 
73 73
   /**
74 74
    * @brief set current frame
75
+   * @param[in] stream stream name
75 76
    * @param[in] pFrame current frame
76 77
    */
77
-  virtual void setFrame(stBlinkenFrame *pFrame);
78
+  virtual void setFrame(const std::string &stream, stBlinkenFrame *pFrame);
78 79
 
79
-  /// set current frame to none
80
-  virtual void setNoFrame();
80
+  /**
81
+   * @brief set current frame to none
82
+   * @param[in] stream stream name
83
+   */
84
+  virtual void setNoFrame(const std::string &stream);
81 85
 
82 86
   /// callback when requsted time reached
83 87
   virtual void timeCall();
... ...
@@ -128,6 +132,24 @@ protected:
128 132
   /// remove timed-out dynamic destinations
129 133
   void removeTimedOutDynDests();
130 134
 
135
+  /**
136
+   * @brief send frame to all destinations
137
+   * @param[in] pFrame frame to send
138
+   */
139
+  void sendAllFrame(stBlinkenFrame *pFrame);
140
+
141
+  /// send "no frame" to all destinations
142
+  void sendAllNoFrame();
143
+
144
+  /**
145
+   * @brief send data to static/dynamic destinations
146
+   * @param[in] data data to send
147
+   * @param[in] dests static destinations
148
+   * @param[in] dynDests dynamic destinations
149
+   */
150
+  void sendDests(const std::string &data, const Dests dests,
151
+                 const DynDests dynDests);
152
+
131 153
   /**
132 154
    * @brief send current frame to address
133 155
    * @param[in] addr address to send to
... ...
@@ -222,8 +244,8 @@ Sender<ADDR, SOCK>::Sender(CallMgr &callMgr, StreamMgr &streamMgr,
222 244
 template<typename ADDR, typename SOCK>
223 245
 Sender<ADDR, SOCK>::~Sender()
224 246
 {
225
-  // send no frame to all destinations
226
-  setNoFrame();
247
+  // send "no frame" to all destinations
248
+  sendAllNoFrame();
227 249
 
228 250
   // destroy socket
229 251
   destroySock();
... ...
@@ -267,83 +289,30 @@ void Sender<ADDR, SOCK>::updateConfig()
267 289
 
268 290
 /**
269 291
  * @brief set current frame
292
+ * @param[in] stream stream name
270 293
  * @param[in] pFrame current frame
271 294
  */
272 295
 template<typename ADDR, typename SOCK>
273
-void Sender<ADDR, SOCK>::setFrame(stBlinkenFrame *pFrame)
296
+void Sender<ADDR, SOCK>::setFrame(const std::string &stream,
297
+                                  stBlinkenFrame *pFrame)
274 298
 {
275
-  std::string blp, eblp, mcuf;
299
+  // send frame to all destinations
300
+  sendAllFrame(pFrame);
276 301
 
277
-  // remove timed-out dynamic destinations
278
-  removeTimedOutDynDests();
279
-
280
-  // convert frame to protocol data
281
-  if (!m_destsBlp.empty() || !m_dynDestsBlp.empty())
282
-    frame2data(pFrame, BlinkenProtoBlp, blp);
283
-  if (!m_destsEblp.empty() || !m_dynDestsEblp.empty())
284
-    frame2data(pFrame, BlinkenProtoEblp, eblp);
285
-  if (!m_destsMcuf.empty() || !m_dynDestsMcuf.empty())
286
-    frame2data(pFrame, BlinkenProtoMcuf, mcuf);
287
-
288
-  // send frame to all static destinations
289
-  typename Dests::const_iterator itDest;
290
-  for (itDest = m_destsBlp.begin(); itDest != m_destsBlp.end(); ++itDest)
291
-    sendFrame(blp, itDest->m_addr);
292
-  for (itDest = m_destsEblp.begin(); itDest != m_destsEblp.end(); ++itDest)
293
-    sendFrame(eblp, itDest->m_addr);
294
-  for (itDest = m_destsMcuf.begin(); itDest != m_destsMcuf.end(); ++itDest)
295
-    sendFrame(mcuf, itDest->m_addr);
296
-
297
-  // send frame to all dynamic destinations
298
-  typename DynDests::const_iterator itDyn;
299
-  for (itDyn = m_dynDestsBlp.begin(); itDyn != m_dynDestsBlp.end(); ++itDyn)
300
-    sendFrame(blp, itDyn->first);
301
-  for (itDyn = m_dynDestsEblp.begin(); itDyn != m_dynDestsEblp.end(); ++itDyn)
302
-    sendFrame(eblp, itDyn->first);
303
-  for (itDyn = m_dynDestsMcuf.begin(); itDyn != m_dynDestsMcuf.end(); ++itDyn)
304
-    sendFrame(mcuf, itDyn->first);
305
-
306
-  // request time callback in one second
307
-  m_callMgr.requestTimeCall(this, Time::now() + Time(1));
302
+  (void)stream; // unused
308 303
 }
309 304
 
310
-/// set current frame to none
305
+/**
306
+ * @brief set current frame to none
307
+ * @param[in] stream stream name
308
+ */
311 309
 template<typename ADDR, typename SOCK>
312
-void Sender<ADDR, SOCK>::setNoFrame()
310
+void Sender<ADDR, SOCK>::setNoFrame(const std::string &stream)
313 311
 {
314
-  std::string blp, eblp, mcuf;
315
-
316
-  // remove timed-out dynamic destinations
317
-  removeTimedOutDynDests();
318
-
319
-  // get "no frame" protocol data
320
-  if (!m_destsBlp.empty() || !m_dynDestsBlp.empty())
321
-    noFrame2data(BlinkenProtoBlp, blp);
322
-  if (!m_destsEblp.empty() || !m_dynDestsEblp.empty())
323
-    noFrame2data(BlinkenProtoEblp, eblp);
324
-  if (!m_destsMcuf.empty() || !m_dynDestsMcuf.empty())
325
-    noFrame2data(BlinkenProtoMcuf, mcuf);
326
-
327
-  // send "no frame" to all staticdestinations
328
-  typename Dests::const_iterator itDest;
329
-  for (itDest = m_destsBlp.begin(); itDest != m_destsBlp.end(); ++itDest)
330
-    sendFrame(blp, itDest->m_addr);
331
-  for (itDest = m_destsEblp.begin(); itDest != m_destsEblp.end(); ++itDest)
332
-    sendFrame(eblp, itDest->m_addr);
333
-  for (itDest = m_destsMcuf.begin(); itDest != m_destsMcuf.end(); ++itDest)
334
-    sendFrame(mcuf, itDest->m_addr);
335
-
336
-  // send frame to all dynamic destinations
337
-  typename DynDests::const_iterator itDyn;
338
-  for (itDyn = m_dynDestsBlp.begin(); itDyn != m_dynDestsBlp.end(); ++itDyn)
339
-    sendFrame(blp, itDyn->first);
340
-  for (itDyn = m_dynDestsEblp.begin(); itDyn != m_dynDestsEblp.end(); ++itDyn)
341
-    sendFrame(eblp, itDyn->first);
342
-  for (itDyn = m_dynDestsMcuf.begin(); itDyn != m_dynDestsMcuf.end(); ++itDyn)
343
-    sendFrame(mcuf, itDyn->first);
312
+  // send "no frame" to all destinations
313
+  sendAllNoFrame();
344 314
 
345
-  // request time callback in one second
346
-  m_callMgr.requestTimeCall(this, Time::now() + Time(1));
315
+  (void)stream; // unused
347 316
 }
348 317
 
349 318
 /// callback when requsted time reached
... ...
@@ -355,13 +324,13 @@ void Sender<ADDR, SOCK>::timeCall()
355 324
 
356 325
   // get current frame from stream
357 326
   if (m_pInStream && m_pInStream->getCurFrame(pFrame))
358
-    // repeat frame - behave exactly like this frame had been set
359
-    setFrame(pFrame);
327
+    // repeat frame to all destinations
328
+    sendAllFrame(pFrame);
360 329
 
361 330
   // no stream of no current frame
362 331
   else
363
-    // repeat "no frame" - behave exactly like "no frame" had been set
364
-    setNoFrame();
332
+    // repeat "no frame" to all destinations
333
+    sendAllNoFrame();
365 334
 }
366 335
 
367 336
 /**
... ...
@@ -448,9 +417,9 @@ void Sender<ADDR, SOCK>::createSock()
448 417
 template<typename ADDR, typename SOCK>
449 418
 void Sender<ADDR, SOCK>::destroySock()
450 419
 {
451
-  // send no frame to all destinations
420
+  // send "no frame" to all destinations
452 421
   // (stream from this socket will stop now)
453
-  setNoFrame();
422
+  sendAllNoFrame();
454 423
 
455 424
   // clear dynamic destinations
456 425
   // (they registered with this socket adn this socket is gone)
... ...
@@ -586,6 +555,84 @@ void Sender<ADDR, SOCK>::removeTimedOutDynDests()
586 555
       ++itDyn;
587 556
 }
588 557
 
558
+/**
559
+ * @brief send frame to all destinations
560
+ * @param[in] pFrame frame to send
561
+ */
562
+template<typename ADDR, typename SOCK>
563
+void Sender<ADDR, SOCK>::sendAllFrame(stBlinkenFrame *pFrame)
564
+{
565
+  std::string data;
566
+
567
+  // remove timed-out dynamic destinations
568
+  removeTimedOutDynDests();
569
+
570
+  // convert frame to protocol data and send to all static/dynamic destinations
571
+  if (!m_destsBlp.empty() || !m_dynDestsBlp.empty()) {
572
+    frame2data(pFrame, BlinkenProtoBlp, data);
573
+    sendDests(data, m_destsBlp, m_dynDestsBlp);
574
+  }
575
+  if (!m_destsEblp.empty() || !m_dynDestsEblp.empty()) {
576
+    frame2data(pFrame, BlinkenProtoEblp, data);
577
+    sendDests(data, m_destsEblp, m_dynDestsEblp);
578
+  }
579
+  if (!m_destsMcuf.empty() || !m_dynDestsMcuf.empty()) {
580
+    frame2data(pFrame, BlinkenProtoMcuf, data);
581
+    sendDests(data, m_destsMcuf, m_dynDestsMcuf);
582
+  }
583
+
584
+  // request time callback in one second
585
+  m_callMgr.requestTimeCall(this, Time::now() + Time(1));
586
+}
587
+
588
+/// send "no frame" to all destinations
589
+template<typename ADDR, typename SOCK>
590
+void Sender<ADDR, SOCK>::sendAllNoFrame()
591
+{
592
+  std::string data;
593
+
594
+  // remove timed-out dynamic destinations
595
+  removeTimedOutDynDests();
596
+
597
+  // get "no frame" protocol data and send to all static/dynamic destinations
598
+  if (!m_destsBlp.empty() || !m_dynDestsBlp.empty()) {
599
+    noFrame2data(BlinkenProtoBlp, data);
600
+    sendDests(data, m_destsBlp, m_dynDestsBlp);
601
+  }
602
+  if (!m_destsEblp.empty() || !m_dynDestsEblp.empty()) {
603
+    noFrame2data(BlinkenProtoEblp, data);
604
+    sendDests(data, m_destsEblp, m_dynDestsEblp);
605
+  }
606
+  if (!m_destsMcuf.empty() || !m_dynDestsMcuf.empty()) {
607
+    noFrame2data(BlinkenProtoMcuf, data);
608
+    sendDests(data, m_destsMcuf, m_dynDestsMcuf);
609
+  }
610
+
611
+  // request time callback in one second
612
+  m_callMgr.requestTimeCall(this, Time::now() + Time(1));
613
+}
614
+
615
+/**
616
+ * @brief send data to static/dynamic destinations
617
+ * @param[in] data data to send
618
+ * @param[in] dests static destinations
619
+ * @param[in] dynDests dynamic destinations
620
+ */
621
+template<typename ADDR, typename SOCK>
622
+void Sender<ADDR, SOCK>::sendDests(const std::string &data, const Dests dests,
623
+                                   const DynDests dynDests)
624
+{
625
+  // send data to static destinations
626
+  typename Dests::const_iterator itDest;
627
+  for (itDest = dests.begin(); itDest != dests.end(); ++itDest)
628
+    sendFrame(data, itDest->m_addr);
629
+
630
+  // send data to all dynamic destinations
631
+  typename DynDests::const_iterator itDyn;
632
+  for (itDyn = dynDests.begin(); itDyn != dynDests.end(); ++itDyn)
633
+    sendFrame(data, itDyn->first);
634
+}
635
+
589 636
 /**
590 637
  * @brief send current frame to address
591 638
  * @param[in] addr address to send to
... ...
@@ -36,9 +36,9 @@ void Stream::attach(StreamRecv *recv)
36 36
 
37 37
   // send current frame to receiver
38 38
   if (m_pFrame)
39
-    recv->setFrame(m_pFrame);
39
+    recv->setFrame(m_name, m_pFrame);
40 40
   else
41
-    recv->setNoFrame();
41
+    recv->setNoFrame(m_name);
42 42
 }
43 43
 
44 44
 /**
... ...
@@ -66,7 +66,7 @@ void Stream::setFrame(stBlinkenFrame *pFrame)
66 66
   // pass frame to all receivers
67 67
   Recvs::iterator it;
68 68
   for (it = m_recvs.begin(); it != m_recvs.end(); ++it)
69
-    (*it)->setFrame(m_pFrame);
69
+    (*it)->setFrame(m_name, m_pFrame);
70 70
 }
71 71
 
72 72
 /// set current frame to none
... ...
@@ -81,7 +81,7 @@ void Stream::setNoFrame()
81 81
   // pass "no frame" to all receivers
82 82
   Recvs::iterator it;
83 83
   for (it = m_recvs.begin(); it != m_recvs.end(); ++it)
84
-    (*it)->setNoFrame();
84
+    (*it)->setNoFrame(m_name);
85 85
 }
86 86
 
87 87
 /**
... ...
@@ -7,6 +7,7 @@
7 7
 #define STREAM_H
8 8
 
9 9
 #include <set>
10
+#include <string>
10 11
 
11 12
 #include <BlinkenLib/BlinkenFrame.h>
12 13
 
... ...
@@ -15,7 +16,7 @@
15 16
 namespace Blinker {
16 17
 
17 18
 /// a video stream
18
-class Stream: public StreamRecv
19
+class Stream
19 20
 {
20 21
 protected:
21 22
   /// set of receivers of this stream
... ...
@@ -58,10 +59,14 @@ public:
58 59
   bool getCurFrame(stBlinkenFrame *&pFrame);
59 60
 
60 61
 protected:
62
+  /// stream name
63
+  std::string m_name;
61 64
   /// current frame (or NULL if no frame)
62 65
   stBlinkenFrame *m_pFrame;
63 66
   /// receivers of this stream
64 67
   Recvs m_recvs;
68
+
69
+friend class StreamMgr;
65 70
 }; // class Stream
66 71
 
67 72
 } // namespace Blinker
... ...
@@ -32,6 +32,7 @@ StreamMgr::~StreamMgr()
32 32
 Stream & StreamMgr::refStream(const std::string &name)
33 33
 {
34 34
   Entry &entry = m_streams[name];
35
+  entry.m_stream.m_name = name;
35 36
   entry.m_refCnt++;
36 37
   return entry.m_stream;
37 38
 }
... ...
@@ -6,6 +6,8 @@
6 6
 #ifndef STREAMRECV_H
7 7
 #define STREAMRECV_H
8 8
 
9
+#include <string>
10
+
9 11
 #include <BlinkenLib/BlinkenFrame.h>
10 12
 
11 13
 namespace Blinker {
... ...
@@ -23,12 +25,16 @@ public:
23 25
 public:
24 26
   /**
25 27
    * @brief set current frame
28
+   * @param[in] stream stream name
26 29
    * @param[in] pFrame current frame
27 30
    */
28
-  virtual void setFrame(stBlinkenFrame *pFrame) = 0;
31
+  virtual void setFrame(const std::string &stream, stBlinkenFrame *pFrame) = 0;
29 32
 
30
-  /// set current frame to none
31
-  virtual void setNoFrame() = 0;
33
+  /**
34
+   * @brief set current frame to none
35
+   * @param[in] stream stream name
36
+   */
37
+  virtual void setNoFrame(const std::string &stream) = 0;
32 38
 }; // class StreamRecv
33 39
 
34 40
 } // namespace Blinker
35 41