PPI: Add simple connector type compatibility check
[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
33 //#include "Connectors.mpp"
34 #define prefix_
35 ///////////////////////////////cc.p////////////////////////////////////////
36
37 ///////////////////////////////////////////////////////////////////////////
38 // senf::ppi::connector::Connector
39
40 prefix_ void senf::ppi::connector::Connector::connect(Connector & target)
41 {
42     SENF_ASSERT( module_ && ! peer_ && target.module_ && ! target.peer_ );
43     if (! (packetTypeID() == typeid(void) ||
44            target.packetTypeID() == typeid(void) || 
45            packetTypeID() == target.packetTypeID()) )
46         throw IncompatibleConnectorsException() 
47             << ": " << prettyName(packetTypeID()) 
48             << " [in module " << prettyName(typeid(*module_))  << "] "
49             << ", " << prettyName(target.packetTypeID())
50             << " [in module " << prettyName(typeid(*target.module_)) << "]";
51             
52     peer_ = & target;
53     target.peer_ = this;
54 }
55
56 ///////////////////////////////////////////////////////////////////////////
57 // senf::ppi::connector::PassiveConnector
58
59 ////////////////////////////////////////
60 // private members
61
62 prefix_ void senf::ppi::connector::PassiveConnector::notifyUnthrottle()
63 {
64     if (throttled() && !nativeThrottled_) {
65         Routes::const_iterator i (routes_.begin());
66         Routes::const_iterator const i_end (routes_.end());
67         for (; i != i_end; ++i)
68             if ((*i)->throttled())
69                 break;
70         if (i == i_end) {
71             remoteThrottled_ = false;
72             emitUnthrottle();
73         }
74     } 
75     else
76         remoteThrottled_ = false;
77 }
78
79 ///////////////////////////////////////////////////////////////////////////
80 // senf::ppi::connector::ActiveConnector
81
82 ////////////////////////////////////////
83 // private members
84
85 prefix_ void senf::ppi::connector::ActiveConnector::notifyThrottle()
86 {
87     if (throttleCallback_)
88         throttleCallback_();
89     NotifyRoutes::const_iterator i (notifyRoutes_.begin());
90     NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
91     for (; i != i_end; ++i)
92         (*i)->notifyThrottle();
93 }
94
95 prefix_ void senf::ppi::connector::ActiveConnector::notifyUnthrottle()
96 {
97     if (unthrottleCallback_)
98         unthrottleCallback_();
99     NotifyRoutes::const_iterator i (notifyRoutes_.begin());
100     NotifyRoutes::const_iterator const i_end (notifyRoutes_.end());
101     for (; i != i_end; ++i)
102         (*i)->notifyUnthrottle();
103 }
104
105 prefix_ void senf::ppi::connector::ActiveConnector::registerRoute(ForwardingRoute & route)
106 {
107     notifyRoutes_.push_back(&route);
108 }
109
110 ///////////////////////////////////////////////////////////////////////////
111 // senf::ppi::connector::InputConnector
112
113 prefix_ senf::Packet senf::ppi::connector::InputConnector::operator()()
114 {
115     if (empty())
116         v_requestEvent();
117     Packet p;
118     if (! empty()) {
119         p = peek();
120         queue_.pop_back();
121         v_dequeueEvent();
122     }
123     return p;
124 }
125
126 ////////////////////////////////////////
127 // private members
128
129 prefix_ void senf::ppi::connector::InputConnector::v_requestEvent()
130 {}
131
132 prefix_ void senf::ppi::connector::InputConnector::v_enqueueEvent()
133 {}
134
135 prefix_ void senf::ppi::connector::InputConnector::v_dequeueEvent()
136 {}
137
138 ///////////////////////////////////////////////////////////////////////////
139 // senf::ppi::connector::GenericActiveInput
140
141 ////////////////////////////////////////
142 // private members
143
144 prefix_ void senf::ppi::connector::GenericActiveInput::v_requestEvent()
145 {
146     request();
147 }
148
149 ///////////////////////////////////////////////////////////////////////////
150 // senf::ppi::connector::GenericPassiveInput
151
152 ////////////////////////////////////////
153 // private members 
154
155 prefix_ void senf::ppi::connector::GenericPassiveInput::v_enqueueEvent()
156 {
157     emit();
158     qdisc_->update(*this, QueueingDiscipline::ENQUEUE);
159 }
160
161 prefix_ void senf::ppi::connector::GenericPassiveInput::v_dequeueEvent()
162 {
163     qdisc_->update(*this, QueueingDiscipline::DEQUEUE);
164 }
165
166 prefix_ void senf::ppi::connector::GenericPassiveInput::v_unthrottleEvent()
167 {
168     size_type n (queueSize());
169     while (n) {
170         emit();
171         size_type nn (queueSize());
172         if (n == nn)
173             break;
174         n = nn;
175     }
176 }
177
178 ///////////////////////////////cc.e////////////////////////////////////////
179 #undef prefix_
180 //#include "Connectors.mpp"
181
182 \f
183 // Local Variables:
184 // mode: c++
185 // fill-column: 100
186 // comment-column: 40
187 // c-file-style: "senf"
188 // indent-tabs-mode: nil
189 // ispell-local-dictionary: "american"
190 // compile-command: "scons -u test"
191 // End: