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>
35 #include <boost/scoped_ptr.hpp>
37 //#include "MultiMCLoop.mpp"
39 ///////////////////////////////cc.p////////////////////////////////////////
41 typedef senf::UDPv4ClientSocketHandle UDPSocket;
47 senf::scheduler::FdEvent event;
49 void handler(int events);
52 MCReader(unsigned n, std::string const & name, UDPSocket::Address const & group);
55 prefix_ MCReader::MCReader(unsigned n, std::string const & name_,
56 UDPSocket::Address const & group)
57 : name (name_), socket (),
58 event (name, senf::membind(&MCReader::handler, this), socket,
59 senf::scheduler::FdEvent::EV_READ)
61 socket.protocol().reuseaddr(true);
63 socket.protocol().bindInterface("dummy0");
64 socket.protocol().mcAddMembership(group.address(), "dummy0");
67 prefix_ void MCReader::handler(int events)
69 std::cout << "I " << name << ": " << socket.read() << "\n";
72 ///////////////////////////////////////////////////////////////////////////
77 UDPSocket::Address group;
79 senf::ClockService::clock_type interval;
80 senf::scheduler::TimerEvent event;
86 MCWriter(std::string const & name, UDPSocket::Address const & group,
87 senf::ClockService::clock_type interval);
90 prefix_ MCWriter::MCWriter(std::string const & name_, UDPSocket::Address const & group_,
91 senf::ClockService::clock_type interval_)
92 : name (name_), group (group_), socket (), interval (interval_),
93 event (name, senf::membind(&MCWriter::handler, this), senf::ClockService::now() + interval),
97 prefix_ void MCWriter::handler()
100 ss << name << "-" << ++count;
101 std::cout << "O " << name << ": " << ss.str() << "\n";
102 socket.writeto(group, ss.str());
103 event.timeout(senf::scheduler::eventTime() + interval);
106 ///////////////////////////////////////////////////////////////////////////
113 IfSetup(std::string const & iface_);
116 void sys(std::string const & cmd);
118 struct SystemException : public senf::Exception
119 { SystemException() : senf::Exception("IfSetup::SystemException") {} };
123 prefix_ IfSetup::IfSetup(std::string const & iface_)
126 sys((boost::format("ifconfig %s up") % iface).str());
127 sys((boost::format("ifconfig %s 192.168.192.1") % iface).str());
128 sys((boost::format("ip route add 224.0.0.0/4 dev %s") % iface).str());
131 prefix_ IfSetup::~IfSetup()
134 sys((boost::format("ifconfig %s down") % iface).str());
136 catch (SystemException & ex) {
137 std::cerr << ex.what() << "\n";
141 prefix_ void IfSetup::sys(std::string const & cmd)
143 int rv (system(cmd.c_str()));
145 throw SystemException() << ": code " << rv << ": " << cmd;
148 ///////////////////////////////////////////////////////////////////////////
150 void sigintHandler(siginfo_t const &)
152 senf::scheduler::terminate();
155 ///////////////////////////////////////////////////////////////////////////
157 int main(int argc, char * argv[])
160 boost::scoped_ptr<IfSetup> setup (
161 (argc != 2 || std::string(argv[1]) != "-n") ? new IfSetup("dummy0") : 0);
163 senf::scheduler::SignalEvent sigint (SIGINT, &sigintHandler);
165 UDPSocket::Address g1 ("225.1:43434");
166 UDPSocket::Address g2 ("225.2:43434");
168 MCReader r1g1 (1u, "r1g1", g1);
169 MCReader r2g1 (2u, "r2g1", g1);
170 MCReader r1g2 (3u, "r1g2", g2);
171 MCReader r2g2 (4u, "r2g2", g2);
173 MCWriter w1g1 ("w1g1", g1, senf::ClockService::milliseconds(600));
174 MCWriter w2g1 ("w2g1", g1, senf::ClockService::milliseconds(800));
175 MCWriter w1g2 ("w1g2", g2, senf::ClockService::milliseconds(700));
176 MCWriter w2g2 ("w2g2", g2, senf::ClockService::milliseconds(900));
178 senf::scheduler::process();
180 catch (std::exception const & ex) {
181 std::cerr << ex.what() << "\n";
186 ///////////////////////////////cc.e////////////////////////////////////////
188 //#include "MultiMCLoop.mpp"
194 // comment-column: 40
195 // c-file-style: "senf"
196 // indent-tabs-mode: nil
197 // ispell-local-dictionary: "american"
198 // compile-command: "scons -u multimcloop"