PPI: Allow connecting two MultiConnectorMixin modules
[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 "PPI.hh"
31
32 #include <senf/Utils/auto_unit_test.hh>
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 namespace ppi = senf::ppi;
39 namespace connector = ppi::connector;
40 namespace module = ppi::module;
41 namespace debug = module::debug;
42
43 namespace {
44     // We only test the user-collection case, all other cases are already handled by
45     // existing modules
46
47     // Primitive duplicator
48     class MyModule
49         : public module::Module,
50           public module::MultiConnectorMixin<MyModule, connector::ActiveOutput<>, void, void>
51     {
52         SENF_PPI_MODULE(MyModule);
53     public:
54         connector::PassiveInput<> input;
55
56         MyModule()
57         {
58             noroute(input); 
59             input.onRequest(&MyModule::request);
60         }
61
62     private:
63         void connectorSetup(std::auto_ptr<ConnectorType> c)
64         {
65             route(input, *c);
66             connectors_.push_back(boost::shared_ptr<ConnectorType>(c));
67         }
68
69         void request()
70         {
71             senf::Packet p (input());
72             for (Connectors::iterator i (connectors_.begin()), i_end (connectors_.end());
73                     i != i_end; ++i)
74                 (**i)(p);
75         }
76
77         typedef std::vector< boost::shared_ptr<MyModule::ConnectorType> > Connectors;
78         Connectors connectors_;
79                 
80         friend class module::MultiConnectorMixin<MyModule, connector::ActiveOutput<>, void, void>;
81     };
82         
83     struct IntAnnotation {
84         int value;
85         bool operator<(IntAnnotation const & other) const { return value < other.value; }
86         IntAnnotation() {}
87         IntAnnotation(int v) : value(v) {}
88     };
89
90     std::ostream & operator<<(std::ostream & os, IntAnnotation const & value)
91     { os << value.value; return os; }
92 }
93
94 BOOST_AUTO_UNIT_TEST(multiConnectorTraits)
95 {
96     BOOST_STATIC_ASSERT( senf::ppi::module::detail::is_multiconnector_source<MyModule>::value );
97     BOOST_STATIC_ASSERT( ! senf::ppi::module::detail::is_multiconnector_target<MyModule>::value );
98 }
99
100
101 BOOST_AUTO_UNIT_TEST(multiConnectorMixin_userContainer)
102 {
103     debug::ActiveSource source;
104     MyModule module;
105     debug::PassiveSink sink1;
106     debug::PassiveSink sink2;
107
108     ppi::connect(source, module);
109     ppi::connect(module, sink1);
110     ppi::connect(module, sink2);
111     ppi::init();
112
113     senf::Packet p (senf::DataPacket::create());
114
115     source.submit(p);
116     BOOST_CHECK_EQUAL( sink1.size(), 1u );
117     BOOST_CHECK_EQUAL( sink2.size(), 1u );
118     BOOST_CHECK( sink1.pop_front() == p );
119     BOOST_CHECK( sink2.pop_front() == p );
120 }
121
122 BOOST_AUTO_UNIT_TEST(multiConnectorMixin_multipleModules)
123 {
124     debug::ActiveSource source;
125     debug::PassiveSink sink;
126     module::PassiveJoin join1;
127     module::PassiveJoin join2;
128     module::AnnotationRouter<IntAnnotation> router;
129     MyModule module;
130     
131     ppi::connect(source, join1);
132     ppi::connect(join1, router);
133     ppi::connect(router, join2, 1);
134     ppi::connect(join2, module);
135     ppi::connect(module, sink);
136     
137     senf::Packet p (senf::DataPacket::create());
138     p.annotation<IntAnnotation>().value = 1;
139
140     source.submit(p);
141     BOOST_CHECK_EQUAL( sink.size(), 1u );
142     BOOST_CHECK( sink.pop_front() == p );
143 }
144
145 ///////////////////////////////cc.e////////////////////////////////////////
146 #undef prefix_
147
148 \f
149 // Local Variables:
150 // mode: c++
151 // fill-column: 100
152 // comment-column: 40
153 // c-file-style: "senf"
154 // indent-tabs-mode: nil
155 // ispell-local-dictionary: "american"
156 // compile-command: "scons -u test"
157 // End: