Socket: Add missing InetSocketProtocol baseclass to relevant socket protocols
[senf.git] / PPI / Connectors.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Connectors non-inline non-template implementation */
25
26 #include "Connectors.hh"
27 #include "Connectors.ih"
28
29 // Custom includes
30 #include "Route.hh"
31 #include "Module.hh"
32 #include "ModuleManager.hh"
33
34 //#include "Connectors.mpp"
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::ppi::connector::Connector
40
41 prefix_ void senf::ppi::connector::Connector::connect(Connector & target)
42 {
43     SENF_ASSERT( module_ && ! peer_ && target.module_ && ! target.peer_ );
44     if (! (packetTypeID() == typeid(void) ||
45            target.packetTypeID() == typeid(void) || 
46            packetTypeID() == target.packetTypeID()) )
47         throw IncompatibleConnectorsException() 
48             << ": " << prettyName(packetTypeID()) 
49             << " [in module " << prettyName(typeid(*module_))  << "] "
50             << ", " << prettyName(target.packetTypeID())
51             << " [in module " << prettyName(typeid(*target.module_)) << "]";
52             
53     peer_ = & target;
54     target.peer_ = this;
55
56     if (ModuleManager::instance().running())
57         v_init();
58 }
59
60 prefix_ std::type_info const & senf::ppi::connector::Connector::packetTypeID()
61 {
62     return typeid(void);
63 }
64
65 ///////////////////////////////////////////////////////////////////////////
66 // senf::ppi::connector::PassiveConnector
67
68 ////////////////////////////////////////
69 // private members
70
71 prefix_ void senf::ppi::connector::PassiveConnector::v_init()
72 {
73     Routes::const_iterator i (routes_.begin());
74     Routes::const_iterator const i_end (routes_.end());
75     for (; i != i_end; ++i)
76         if ((*i)->throttled())
77             break;
78     if (i == i_end)
79         remoteThrottled_ = false;
80     if (throttled())
81         emitThrottle();
82     else
83         emitUnthrottle();
84 }
85
86 prefix_ void senf::ppi::connector::PassiveConnector::v_unthrottleEvent()
87 {}
88
89 prefix_ void senf::ppi::connector::PassiveConnector::notifyUnthrottle()
90 {
91     if (throttled() && !nativeThrottled_) {
92         Routes::const_iterator i (routes_.begin());
93         Routes::const_iterator const i_end (routes_.end());
94         for (; i != i_end; ++i)
95             if ((*i)->throttled())
96                 break;
97         if (i == i_end) {
98             remoteThrottled_ = false;
99             emitUnthrottle();
100         }
101     } 
102     else
103         remoteThrottled_ = false;
104 }
105
106 ///////////////////////////////////////////////////////////////////////////
107 // senf::ppi::connector::ActiveConnector
108
109 ////////////////////////////////////////
110 // private members
111
112 prefix_ void senf::ppi::connector::ActiveConnector::v_init()
113 {
114     if (! connected())
115         notifyThrottle();
116 }
117
118 prefix_ void senf::ppi::connector::ActiveConnector::notifyThrottle()
119 {
120     if (! throttled_) {
121         throttled_ = true;
122         if (throttleCallback_)
123             throttleCallback_();
124         NotifyRoutes::const_iterator i (notifyRoutes_.begin());
125         NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
126         for (; i != i_end; ++i)
127             (*i)->notifyThrottle();
128     }
129 }
130
131 prefix_ void senf::ppi::connector::ActiveConnector::notifyUnthrottle()
132 {
133     if (throttled_) {
134         throttled_ = false;
135         if (unthrottleCallback_)
136             unthrottleCallback_();
137         NotifyRoutes::const_iterator i (notifyRoutes_.begin());
138         NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
139         for (; i != i_end; ++i)
140             (*i)->notifyUnthrottle();
141     }
142 }
143
144 prefix_ void senf::ppi::connector::ActiveConnector::registerRoute(ForwardingRoute & route)
145 {
146     notifyRoutes_.push_back(&route);
147 }
148
149 ///////////////////////////////////////////////////////////////////////////
150 // senf::ppi::connector::InputConnector
151
152 prefix_ senf::Packet senf::ppi::connector::InputConnector::operator()()
153 {
154     if (empty())
155         v_requestEvent();
156     Packet p;
157     if (! empty()) {
158         p = peek();
159         queue_.pop_back();
160         v_dequeueEvent();
161     }
162     return p;
163 }
164
165 ////////////////////////////////////////
166 // private members
167
168 prefix_ void senf::ppi::connector::InputConnector::v_requestEvent()
169 {}
170
171 prefix_ void senf::ppi::connector::InputConnector::v_enqueueEvent()
172 {}
173
174 prefix_ void senf::ppi::connector::InputConnector::v_dequeueEvent()
175 {}
176
177 ///////////////////////////////////////////////////////////////////////////
178 // senf::ppi::connector::GenericActiveInput
179
180 ////////////////////////////////////////
181 // private members
182
183 prefix_ void senf::ppi::connector::GenericActiveInput::v_requestEvent()
184 {
185     request();
186 }
187
188 ///////////////////////////////////////////////////////////////////////////
189 // senf::ppi::connector::GenericPassiveInput
190
191 ////////////////////////////////////////
192 // private members 
193
194 prefix_ void senf::ppi::connector::GenericPassiveInput::v_enqueueEvent()
195 {
196     emit();
197     qdisc_->update(*this, QueueingDiscipline::ENQUEUE);
198 }
199
200 prefix_ void senf::ppi::connector::GenericPassiveInput::v_dequeueEvent()
201 {
202     qdisc_->update(*this, QueueingDiscipline::DEQUEUE);
203 }
204
205 prefix_ void senf::ppi::connector::GenericPassiveInput::v_unthrottleEvent()
206 {
207     size_type n (queueSize());
208     while (n) {
209         emit();
210         size_type nn (queueSize());
211         if (n == nn)
212             break;
213         n = nn;
214     }
215 }
216
217 ///////////////////////////////cc.e////////////////////////////////////////
218 #undef prefix_
219 //#include "Connectors.mpp"
220
221 \f
222 // Local Variables:
223 // mode: c++
224 // fill-column: 100
225 // comment-column: 40
226 // c-file-style: "senf"
227 // indent-tabs-mode: nil
228 // ispell-local-dictionary: "american"
229 // compile-command: "scons -u test"
230 // End: