Packets: Add StringParser ostream operation
[senf.git] / PPI / Connectors.cc
index ff29901..4c5a3ea 100644 (file)
 
 // Custom includes
 #include "Route.hh"
+#include "Module.hh"
+#include "ModuleManager.hh"
 
 //#include "Connectors.mpp"
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
 ///////////////////////////////////////////////////////////////////////////
+// senf::ppi::connector::Connector
+
+prefix_ void senf::ppi::connector::Connector::connect(Connector & target)
+{
+    // The connector is not registered -> route() or noroute() statement missing
+    SENF_ASSERT( module_ && 
+                 "senf::ppi::connector::Connector::connect(): (source) "
+                 "Missing route() or noroute()" );
+    // The connector is already connected
+    SENF_ASSERT( ! peer_ &&
+                 "senf::ppi::connector::Connector::connect(): (source) "
+                 "duplicate connection" );
+    // The target connector is not registered -> route() or noroute() statement missing
+    SENF_ASSERT( target.module_ &&
+                 "senf::ppi::connector::Connector::connect(): (target) "
+                 "Missing route() or noroute()" );
+    // The target connector is already connected
+    SENF_ASSERT( ! target.peer_ &&
+                 "senf::ppi::connector::Connector::connect(): (target) "
+                 "duplicate connection" );
+    if (! (packetTypeID() == typeid(void) ||
+           target.packetTypeID() == typeid(void) || 
+           packetTypeID() == target.packetTypeID()) )
+        throw IncompatibleConnectorsException() 
+            << ": " << prettyName(packetTypeID()) 
+            << " [in module " << prettyName(typeid(*module_))  << "] "
+            << ", " << prettyName(target.packetTypeID())
+            << " [in module " << prettyName(typeid(*target.module_)) << "]";
+            
+    peer_ = & target;
+    target.peer_ = this;
+
+    if (! initializationScheduled())
+        enqueueInitializable();
+    if (! peer().initializationScheduled())
+        peer().enqueueInitializable();
+}
+
+prefix_ void senf::ppi::connector::Connector::disconnect()
+{
+    // Cannot disconnected a non-connected connector
+    SENF_ASSERT( peer_ &&
+                 "senf::ppi::connector::Connector::disconnect(): Not connected" );
+    Connector & peer (*peer_);
+    peer_ = 0;
+    peer.peer_ = 0;
+
+    if (! initializationScheduled())
+        enqueueInitializable();
+    if (! peer.initializationScheduled())
+        peer.enqueueInitializable();
+}
+
+prefix_ std::type_info const & senf::ppi::connector::Connector::packetTypeID()
+{
+    return typeid(void);
+}
+
+///////////////////////////////////////////////////////////////////////////
 // senf::ppi::connector::PassiveConnector
 
 ////////////////////////////////////////
 // private members
 
+prefix_ void senf::ppi::connector::PassiveConnector::v_init()
+{
+    Routes::const_iterator i (routes_.begin());
+    Routes::const_iterator const i_end (routes_.end());
+    for (; i != i_end; ++i)
+        if ((*i)->throttled())
+            break;
+    if (i == i_end)
+        remoteThrottled_ = false;
+    if (throttled())
+        emitThrottle();
+    else
+        emitUnthrottle();
+}
+
+prefix_ void senf::ppi::connector::PassiveConnector::v_unthrottleEvent()
+{}
+
 prefix_ void senf::ppi::connector::PassiveConnector::notifyUnthrottle()
 {
     if (throttled() && !nativeThrottled_) {
@@ -62,24 +141,36 @@ prefix_ void senf::ppi::connector::PassiveConnector::notifyUnthrottle()
 ////////////////////////////////////////
 // private members
 
+prefix_ void senf::ppi::connector::ActiveConnector::v_init()
+{
+    if (! connected())
+        notifyThrottle();
+}
+
 prefix_ void senf::ppi::connector::ActiveConnector::notifyThrottle()
 {
-    if (throttleCallback_)
-        throttleCallback_();
-    NotifyRoutes::const_iterator i (notifyRoutes_.begin());
-    NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
-    for (; i != i_end; ++i)
-        (*i)->notifyThrottle();
+    if (! throttled_) {
+        throttled_ = true;
+        if (throttleCallback_)
+            throttleCallback_();
+        NotifyRoutes::const_iterator i (notifyRoutes_.begin());
+        NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
+        for (; i != i_end; ++i)
+            (*i)->notifyThrottle();
+    }
 }
 
 prefix_ void senf::ppi::connector::ActiveConnector::notifyUnthrottle()
 {
-    if (unthrottleCallback_)
-        unthrottleCallback_();
-    NotifyRoutes::const_iterator i (notifyRoutes_.begin());
-    NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
-    for (; i != i_end; ++i)
-        (*i)->notifyUnthrottle();
+    if (throttled_) {
+        throttled_ = false;
+        if (unthrottleCallback_)
+            unthrottleCallback_();
+        NotifyRoutes::const_iterator i (notifyRoutes_.begin());
+        NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
+        for (; i != i_end; ++i)
+            (*i)->notifyUnthrottle();
+    }
 }
 
 prefix_ void senf::ppi::connector::ActiveConnector::registerRoute(ForwardingRoute & route)