Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / PPI / MultiConnectorMixin.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
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 MultiConnectorMixin.test unit tests */
25
26 //#include "MultiConnectorMixin.test.hh"
27 //#include "MultiConnectorMixin.test.ih"
28
29 // Custom includes
30 #include "MultiConnectorMixin.hh"
31 #include "DebugModules.hh"
32 #include "Joins.hh"
33 #include "AnnotationRouter.hh"
34
35 #include <senf/Utils/auto_unit_test.hh>
36 #include <boost/test/test_tools.hpp>
37
38 #define prefix_
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40 namespace ppi = senf::ppi;
41 namespace connector = ppi::connector;
42 namespace module = ppi::module;
43 namespace debug = module::debug;
44
45 namespace {
46
47     // Primitive join
48     class SequenceContainerModule
49         : public module::Module,
50           public module::MultiConnectorMixin<SequenceContainerModule, connector::PassiveInput<> >
51     {
52         SENF_PPI_MODULE(SequenceContainerModule);
53         typedef module::MultiConnectorMixin<SequenceContainerModule, connector::PassiveInput<> > base;
54
55     public:
56         connector::ActiveOutput<> output;
57
58         using base::connectors;
59
60         SequenceContainerModule() : count (0u)
61         {
62             noroute(output);
63         }
64
65         unsigned count;
66
67     private:
68         void connectorSetup(connector::PassiveInput<> & c)
69         {
70             route(c, output);
71             c.onRequest(boost::bind(&SequenceContainerModule::request, this, boost::ref(c)));
72             ++ count;
73         }
74
75         void connectorDestroy(connector::PassiveInput<> const & c)
76         {
77             -- count;
78         }
79
80         void request(connector::PassiveInput<> & c)
81         {
82             output(c());
83         }
84
85         friend class module::MultiConnectorMixin<SequenceContainerModule, connector::PassiveInput<> >;
86     };
87
88     // Primitive duplicator
89     class UserContainerModule
90         : public module::Module,
91           public module::MultiConnectorMixin<UserContainerModule, connector::ActiveOutput<>, void, void>
92     {
93         SENF_PPI_MODULE(UserContainerModule);
94         typedef std::vector< boost::shared_ptr<UserContainerModule::ConnectorType> > Connectors;
95     public:
96         connector::PassiveInput<> input;
97
98         UserContainerModule()
99         {
100             noroute(input);
101             input.onRequest(&UserContainerModule::request);
102         }
103
104         Connectors const & connectors() const
105         { return connectors_; }
106
107     private:
108         void connectorSetup(std::auto_ptr<ConnectorType> c)
109         {
110             route(input, *c);
111             connectors_.push_back(boost::shared_ptr<ConnectorType>(c));
112         }
113
114         void connectorDestroy(ConnectorType const * c)
115         {
116             Connectors::iterator i (
117                 std::find_if(connectors_.begin(), connectors_.end(),
118                              boost::bind(&Connectors::value_type::get,_1) == c));
119             if (i != connectors_.end())
120                 connectors_.erase(i);
121         }
122
123         void request()
124         {
125             senf::Packet p (input());
126             for (Connectors::iterator i (connectors_.begin()), i_end (connectors_.end());
127                     i != i_end; ++i)
128                 (**i)(p);
129         }
130
131         Connectors connectors_;
132
133         friend class module::MultiConnectorMixin<UserContainerModule, connector::ActiveOutput<>, void, void>;
134     };
135
136     struct IntAnnotation {
137         int value;
138         bool operator<(IntAnnotation const & other) const { return value < other.value; }
139         IntAnnotation() {}
140         IntAnnotation(int v) : value(v) {}
141     };
142
143     std::ostream & operator<<(std::ostream & os, IntAnnotation const & value)
144     { os << value.value; return os; }
145 }
146
147 SENF_AUTO_UNIT_TEST(multiConnectorMixin_sequenceContainer)
148 {
149     debug::ActiveSource source1;
150     debug::ActiveSource source2;
151     SequenceContainerModule module;
152     debug::PassiveSink sink;
153
154     ppi::connect(source1, module);
155     ppi::connect(source2, module);
156     ppi::connect(module, sink);
157     ppi::init();
158
159     senf::Packet p (senf::DataPacket::create());
160
161     source1.submit(p);
162     BOOST_CHECK_EQUAL( sink.size(), 1u );
163     BOOST_CHECK( sink.pop_front() == p );
164     source2.submit(p);
165     BOOST_CHECK_EQUAL( sink.size(), 1u );
166     BOOST_CHECK( sink.pop_front() == p );
167
168     BOOST_CHECK_EQUAL( module.connectors().size(), 2u );
169     BOOST_CHECK_EQUAL( module.count, 2u );
170     source1.output.disconnect();
171     BOOST_CHECK_EQUAL( module.connectors().size(), 1u );
172     BOOST_CHECK_EQUAL( module.count, 1u );
173 }
174
175 SENF_AUTO_UNIT_TEST(multiConnectorMixin_userContainer)
176 {
177     debug::ActiveSource source;
178     UserContainerModule module;
179     debug::PassiveSink sink1;
180     debug::PassiveSink sink2;
181
182     ppi::connect(source, module);
183     ppi::connect(module, sink1);
184     ppi::connect(module, sink2);
185     ppi::init();
186
187     senf::Packet p (senf::DataPacket::create());
188
189     source.submit(p);
190     BOOST_CHECK_EQUAL( sink1.size(), 1u );
191     BOOST_CHECK_EQUAL( sink2.size(), 1u );
192     BOOST_CHECK( sink1.pop_front() == p );
193     BOOST_CHECK( sink2.pop_front() == p );
194
195     BOOST_CHECK_EQUAL( module.connectors().size(), 2u );
196     sink1.input.disconnect();
197     BOOST_CHECK_EQUAL( module.connectors().size(), 1u );
198 }
199
200 SENF_AUTO_UNIT_TEST(multiConnectorMixin_multipleModules)
201 {
202     debug::ActiveSource source;
203     debug::PassiveSink sink;
204     module::PassiveJoin join1;
205     module::PassiveJoin join2;
206     module::AnnotationRouter<IntAnnotation> router;
207     UserContainerModule module;
208
209     ppi::connect(source, join1);
210     ppi::connect(join1, router);
211     ppi::connect(router, 1, join2);
212     ppi::connect(join2, module);
213     ppi::connect(module, sink);
214
215     senf::Packet p (senf::DataPacket::create());
216     p.annotation<IntAnnotation>().value = 1;
217
218     source.submit(p);
219     BOOST_CHECK_EQUAL( sink.size(), 1u );
220     BOOST_CHECK( sink.pop_front() == p );
221 }
222
223 //-/////////////////////////////////////////////////////////////////////////////////////////////////
224 #undef prefix_
225
226 \f
227 // Local Variables:
228 // mode: c++
229 // fill-column: 100
230 // comment-column: 40
231 // c-file-style: "senf"
232 // indent-tabs-mode: nil
233 // ispell-local-dictionary: "american"
234 // compile-command: "scons -u test"
235 // End: