4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief MultiMCLoop non-inline non-template implementation */
26 //#include "MultiMCLoop.hh"
27 //#include "MultiMCLoop.ih"
30 #include <senf/Scheduler/ClockService.hh>
31 #include <senf/Scheduler/Scheduler.hh>
32 #include <senf/Utils/membind.hh>
33 #include <senf/Socket/Protocols/INet.hh>
34 #include <boost/format.hpp>
36 //#include "MultiMCLoop.mpp"
38 ///////////////////////////////cc.p////////////////////////////////////////
40 typedef senf::UDPv4ClientSocketHandle UDPSocket;
46 senf::scheduler::FdEvent event;
48 void handler(int events);
51 MCReader(unsigned n, std::string const & name, UDPSocket::Address const & group);
54 prefix_ MCReader::MCReader(unsigned n, std::string const & name_,
55 UDPSocket::Address const & group)
56 : name (name_), socket (),
57 event (name, senf::membind(&MCReader::handler, this), socket,
58 senf::scheduler::FdEvent::EV_READ)
60 socket.protocol().reuseaddr(true);
62 socket.protocol().bindInterface("dummy0");
63 socket.protocol().mcAddMembership(group.address(), "dummy0");
66 prefix_ void MCReader::handler(int events)
68 std::cout << "I " << name << ": " << socket.read() << "\n";
71 ///////////////////////////////////////////////////////////////////////////
76 UDPSocket::Address group;
78 senf::ClockService::clock_type interval;
79 senf::scheduler::TimerEvent event;
85 MCWriter(std::string const & name, UDPSocket::Address const & group,
86 senf::ClockService::clock_type interval);
89 prefix_ MCWriter::MCWriter(std::string const & name_, UDPSocket::Address const & group_,
90 senf::ClockService::clock_type interval_)
91 : name (name_), group (group_), socket (), interval (interval_),
92 event (name, senf::membind(&MCWriter::handler, this), senf::ClockService::now() + interval),
96 prefix_ void MCWriter::handler()
99 ss << name << "-" << ++count;
100 std::cout << "O " << name << ": " << ss.str() << "\n";
101 socket.writeto(group, ss.str());
102 event.timeout(senf::scheduler::eventTime() + interval);
105 ///////////////////////////////////////////////////////////////////////////
112 IfSetup(std::string const & iface_);
115 void sys(std::string const & cmd);
117 struct SystemException : public senf::Exception
118 { SystemException() : senf::Exception("IfSetup::SystemException") {} };
122 prefix_ IfSetup::IfSetup(std::string const & iface_)
125 sys((boost::format("ifconfig %s up") % iface).str());
126 sys((boost::format("ifconfig %s 192.168.192.1") % iface).str());
127 sys((boost::format("ip route add 224.0.0.0/4 dev %s") % iface).str());
130 prefix_ IfSetup::~IfSetup()
133 sys((boost::format("ifconfig %s down") % iface).str());
135 catch (SystemException & ex) {
136 std::cerr << ex.what() << "\n";
140 prefix_ void IfSetup::sys(std::string const & cmd)
142 int rv (system(cmd.c_str()));
144 throw SystemException() << ": code " << rv << ": " << cmd;
147 ///////////////////////////////////////////////////////////////////////////
149 void sigintHandler(siginfo_t const &)
151 senf::scheduler::terminate();
154 ///////////////////////////////////////////////////////////////////////////
156 int main(int argc, char * argv[])
159 IfSetup setup ("dummy0");
161 senf::scheduler::SignalEvent sigint (SIGINT, &sigintHandler);
163 UDPSocket::Address g1 ("225.1:43434");
164 UDPSocket::Address g2 ("225.2:43434");
166 MCReader r1g1 (1u, "r1g1", g1);
167 MCReader r2g1 (2u, "r2g1", g1);
168 MCReader r1g2 (3u, "r1g2", g2);
169 MCReader r2g2 (4u, "r2g2", g2);
171 MCWriter w1g1 ("w1g1", g1, senf::ClockService::milliseconds(600));
172 MCWriter w2g1 ("w2g1", g1, senf::ClockService::milliseconds(800));
173 MCWriter w1g2 ("w1g2", g2, senf::ClockService::milliseconds(700));
174 MCWriter w2g2 ("w2g2", g2, senf::ClockService::milliseconds(900));
176 senf::scheduler::process();
178 catch (std::exception const & ex) {
179 std::cerr << ex.what() << "\n";
184 ///////////////////////////////cc.e////////////////////////////////////////
186 //#include "MultiMCLoop.mpp"
192 // comment-column: 40
193 // c-file-style: "senf"
194 // indent-tabs-mode: nil
195 // ispell-local-dictionary: "american"
196 // compile-command: "scons -u multimcloop"