6e05ebea0aeefd05809ef497ff85af1c2f558564
[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 "../Utils/auto_unit_test.hh"
33 #include <boost/test/test_tools.hpp>
34
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 namespace {
39     // We only test the user-collection case, all other cases are already handled by
40     // existing modules
41
42     // Primitive duplicator
43     class MyModule
44         : public senf::ppi::module::Module,
45           public senf::ppi::module::MultiConnectorMixin<MyModule,
46                                                         senf::ppi::connector::ActiveOutput<>,
47                                                         void, void>
48     {
49         SENF_PPI_MODULE(MyModule);
50     public:
51         senf::ppi::connector::PassiveInput<> input;
52
53         MyModule()
54             {
55                 noroute(input); 
56                 input.onRequest(&MyModule::request);
57             }
58
59     private:
60         void connectorSetup(std::auto_ptr<ConnectorType> c)
61             {
62                 route(input, *c);
63                 connectors_.push_back(boost::shared_ptr<ConnectorType>(c));
64             }
65
66         void request()
67             {
68                 senf::Packet p (input());
69                 for (Connectors::iterator i (connectors_.begin()), i_end (connectors_.end());
70                      i != i_end; ++i)
71                     (**i)(p);
72             }
73
74         typedef std::vector< boost::shared_ptr<MyModule::ConnectorType> > Connectors;
75         Connectors connectors_;
76                 
77         friend class senf::ppi::module::MultiConnectorMixin<MyModule,
78                                                             senf::ppi::connector::ActiveOutput<>,
79                                                             void, void>;
80
81     };
82 }
83
84 BOOST_AUTO_UNIT_TEST(multiConnectorMixin_userContainer)
85 {
86     senf::ppi::module::debug::ActiveSource source;
87     MyModule module;
88     senf::ppi::module::debug::PassiveSink sink1;
89     senf::ppi::module::debug::PassiveSink sink2;
90
91     senf::ppi::connect(source, module);
92     senf::ppi::connect(module, sink1);
93     senf::ppi::connect(module, sink2);
94     senf::ppi::init();
95
96     senf::Packet p (senf::DataPacket::create());
97
98     source.submit(p);
99     BOOST_CHECK_EQUAL( sink1.size(), 1u );
100     BOOST_CHECK_EQUAL( sink2.size(), 1u );
101     BOOST_CHECK( sink1.pop_front() == p );
102     BOOST_CHECK( sink2.pop_front() == p );
103     
104 }
105
106 ///////////////////////////////cc.e////////////////////////////////////////
107 #undef prefix_
108
109 \f
110 // Local Variables:
111 // mode: c++
112 // fill-column: 100
113 // comment-column: 40
114 // c-file-style: "senf"
115 // indent-tabs-mode: nil
116 // ispell-local-dictionary: "american"
117 // compile-command: "scons -u test"
118 // End: