4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief MultiMCLoop non-inline non-template implementation */
31 //#include "MultiMCLoop.hh"
32 //#include "MultiMCLoop.ih"
35 #include <senf/Scheduler/ClockService.hh>
36 #include <senf/Scheduler/Scheduler.hh>
37 #include <senf/Utils/membind.hh>
38 #include <senf/Socket/Protocols/INet.hh>
39 #include <boost/format.hpp>
40 #include <boost/scoped_ptr.hpp>
42 //#include "MultiMCLoop.mpp"
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 typedef senf::UDPv4ClientSocketHandle UDPSocket;
52 senf::scheduler::FdEvent event;
54 void handler(int events);
57 MCReader(unsigned n, std::string const & name, UDPSocket::Address const & group);
60 prefix_ MCReader::MCReader(unsigned n, std::string const & name_,
61 UDPSocket::Address const & group)
62 : name (name_), socket (),
63 event (name, senf::membind(&MCReader::handler, this), socket,
64 senf::scheduler::FdEvent::EV_READ)
66 socket.protocol().reuseaddr(true);
68 socket.protocol().bindInterface("dummy0");
69 socket.protocol().mcAddMembership(group.address(), "dummy0");
72 prefix_ void MCReader::handler(int events)
74 std::cout << "I " << name << ": " << socket.read() << "\n";
77 //-/////////////////////////////////////////////////////////////////////////////////////////////////
82 UDPSocket::Address group;
84 senf::ClockService::clock_type interval;
85 senf::scheduler::TimerEvent event;
91 MCWriter(std::string const & name, UDPSocket::Address const & group,
92 senf::ClockService::clock_type interval);
95 prefix_ MCWriter::MCWriter(std::string const & name_, UDPSocket::Address const & group_,
96 senf::ClockService::clock_type interval_)
97 : name (name_), group (group_), socket (), interval (interval_),
98 event (name, senf::membind(&MCWriter::handler, this), senf::ClockService::now() + interval),
102 prefix_ void MCWriter::handler()
104 std::stringstream ss;
105 ss << name << "-" << ++count;
106 std::cout << "O " << name << ": " << ss.str() << "\n";
107 socket.writeto(group, ss.str());
108 event.timeout(senf::scheduler::eventTime() + interval);
111 //-/////////////////////////////////////////////////////////////////////////////////////////////////
118 IfSetup(std::string const & iface_);
121 void sys(std::string const & cmd);
123 struct SystemException : public senf::Exception
124 { SystemException() : senf::Exception("IfSetup::SystemException") {} };
128 prefix_ IfSetup::IfSetup(std::string const & iface_)
131 sys((boost::format("ifconfig %s up") % iface).str());
132 sys((boost::format("ifconfig %s 192.168.192.1") % iface).str());
133 sys((boost::format("ip route add 224.0.0.0/4 dev %s") % iface).str());
136 prefix_ IfSetup::~IfSetup()
139 sys((boost::format("ifconfig %s down") % iface).str());
141 catch (SystemException & ex) {
142 std::cerr << ex.what() << "\n";
146 prefix_ void IfSetup::sys(std::string const & cmd)
148 int rv (system(cmd.c_str()));
150 throw SystemException() << ": code " << rv << ": " << cmd;
153 //-/////////////////////////////////////////////////////////////////////////////////////////////////
155 void sigintHandler(siginfo_t const &)
157 senf::scheduler::terminate();
160 //-/////////////////////////////////////////////////////////////////////////////////////////////////
162 int main(int argc, char * argv[])
165 boost::scoped_ptr<IfSetup> setup (
166 (argc != 2 || std::string(argv[1]) != "-n") ? new IfSetup("dummy0") : 0);
168 senf::scheduler::SignalEvent sigint (SIGINT, &sigintHandler);
170 UDPSocket::Address g1 ("225.1:43434");
171 UDPSocket::Address g2 ("225.2:43434");
173 MCReader r1g1 (1u, "r1g1", g1);
174 MCReader r2g1 (2u, "r2g1", g1);
175 MCReader r1g2 (3u, "r1g2", g2);
176 MCReader r2g2 (4u, "r2g2", g2);
178 MCWriter w1g1 ("w1g1", g1, senf::ClockService::milliseconds(600));
179 MCWriter w2g1 ("w2g1", g1, senf::ClockService::milliseconds(800));
180 MCWriter w1g2 ("w1g2", g2, senf::ClockService::milliseconds(700));
181 MCWriter w2g2 ("w2g2", g2, senf::ClockService::milliseconds(900));
183 senf::scheduler::process();
185 catch (std::exception const & ex) {
186 std::cerr << ex.what() << "\n";
191 //-/////////////////////////////////////////////////////////////////////////////////////////////////
193 //#include "MultiMCLoop.mpp"
199 // comment-column: 40
200 // c-file-style: "senf"
201 // indent-tabs-mode: nil
202 // ispell-local-dictionary: "american"
203 // compile-command: "scons -u multimcloop"