PPI: Add priority argument to PriorityJoin connect()
g0dil [Mon, 23 Mar 2009 14:32:13 +0000 (14:32 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1166 270642c3-0616-0410-b53a-bc976706d245

PPI/Joins.cc
PPI/Joins.cti
PPI/Joins.hh
PPI/Joins.test.cc

index 71428c6..b8ce2d6 100644 (file)
@@ -80,10 +80,19 @@ prefix_ void senf::ppi::module::PassiveJoin::onUnthrottle()
 ////////////////////////////////////////
 // private members
 
-prefix_ senf::ppi::connector::ActiveInput<> & senf::ppi::module::PriorityJoin::newInput()
+prefix_ senf::ppi::connector::ActiveInput<> &
+senf::ppi::module::PriorityJoin::newInput(int priority)
 {
-    inputs_.push_back(new connector::ActiveInput<>());
-    connector::ActiveInput<> & input (inputs_.back());
+    if (priority > int(inputs_.size()))
+        priority = inputs_.size();
+    else if (priority < 0) {
+        priority = inputs_.size() + priority + 1;
+        if (priority < 0) 
+            priority = 0;
+    }
+
+    connector::ActiveInput<> & input (
+        *inputs_.insert(inputs_.begin()+priority, new connector::ActiveInput<>()));
 
     noroute(input);
     input.onThrottle(&PriorityJoin::onThrottle);
index b2dedda..1e77d16 100644 (file)
@@ -48,9 +48,9 @@ senf::ppi::module::PassiveJoin::connect(Source & source)
 
 template <class Source>
 prefix_ senf::ppi::connector::GenericActiveInput &
-senf::ppi::module::PriorityJoin::connect(Source & source)
+senf::ppi::module::PriorityJoin::connect(Source & source, int priority)
 {
-    connector::GenericActiveInput & input (newInput());
+    connector::GenericActiveInput & input (newInput(priority));
     ppi::connect(source,input);
     return input;
 }
@@ -61,17 +61,17 @@ senf::ppi::module::PriorityJoin::connect(Source & source)
 #ifndef DOXYGEN
 
 template <class Source>
-prefix_ senf::ppi::connector::GenericPassiveInput & senf::ppi::connect(Source & source,
-                                                                module::PassiveJoin & target)
+prefix_ senf::ppi::connector::GenericPassiveInput &
+senf::ppi::connect(Source & source, module::PassiveJoin & target)
 {
     return target.connect(source);
 }
 
 template <class Source>
-prefix_ senf::ppi::connector::GenericActiveInput & senf::ppi::connect(Source & source,
-                                                               module::PriorityJoin & target)
+prefix_ senf::ppi::connector::GenericActiveInput &
+senf::ppi::connect(Source & source, module::PriorityJoin & target, int priority)
 {
-    return target.connect(source);
+    return target.connect(source, priority);
 }
 
 #endif
index 266e807..8960551 100644 (file)
@@ -44,7 +44,8 @@ namespace ppi {
     connector::GenericPassiveInput & connect(Source & source, module::PassiveJoin & target);
 
     template <class Source>
-    connector::GenericActiveInput & connect(Source & source, module::PriorityJoin & target);
+    connector::GenericActiveInput & connect(Source & source, module::PriorityJoin & target,
+                                            int priority = -1);
 
 #endif
 
@@ -133,13 +134,13 @@ namespace module {
         PriorityJoin();
 
     private:
-        connector::ActiveInput<> & newInput();
+        connector::ActiveInput<> & newInput(int priority);
 
 #ifndef DOXYGEN
     public:
 #endif
         template <class Source>
-        connector::GenericActiveInput & connect(Source & source);
+        connector::GenericActiveInput & connect(Source & source, int prioricty);
 
     private:
         void request();
index 7e14488..44e9759 100644 (file)
@@ -106,6 +106,25 @@ BOOST_AUTO_UNIT_TEST(priorityJoin)
     BOOST_CHECK( sink.request() == p1 );
     BOOST_CHECK( sink.request() == p2 );
     BOOST_CHECK( ! sink );
+
+    debug::PassiveSource source3;
+    debug::PassiveSource source4;
+    ppi::connect(source3, join, 0);
+    ppi::connect(source4, join, -2);
+    // Ordering now: source3, source1, source4, source2
+    senf::Packet p3 (senf::DataPacket::create());
+    senf::Packet p4 (senf::DataPacket::create());
+    source4.submit(p4);
+    source3.submit(p3);
+    source2.submit(p2);
+    source1.submit(p1);
+    BOOST_CHECK( sink.request() == p3 );
+    BOOST_CHECK( sink.request() == p1 );
+    BOOST_CHECK( sink.request() == p4 );
+    BOOST_CHECK( sink.request() == p2 );
+    BOOST_CHECK( ! sink );
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////