switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / Examples / MultiMCLoop / MultiMCLoop.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
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
10 //
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.
14 //
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.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
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.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief MultiMCLoop non-inline non-template implementation */
30
31 //#include "MultiMCLoop.hh"
32 //#include "MultiMCLoop.ih"
33
34 // Custom includes
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>
41
42 //#include "MultiMCLoop.mpp"
43 #define prefix_
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45
46 typedef senf::UDPv4ClientSocketHandle UDPSocket;
47
48 class MCReader
49 {
50     std::string name;
51     UDPSocket socket;
52     senf::scheduler::FdEvent event;
53
54     void handler(int events);
55
56 public:
57     MCReader(unsigned n, std::string const & name, UDPSocket::Address const & group);
58 };
59
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)
65 {
66     socket.protocol().reuseaddr(true);
67     socket.bind(group);
68     socket.protocol().bindInterface("dummy0");
69     socket.protocol().mcAddMembership(group.address(), "dummy0");
70 }
71
72 prefix_ void MCReader::handler(int events)
73 {
74     std::cout << "I " << name << ": " << socket.read() << "\n";
75 }
76
77 //-/////////////////////////////////////////////////////////////////////////////////////////////////
78
79 class MCWriter
80 {
81     std::string name;
82     UDPSocket::Address group;
83     UDPSocket socket;
84     senf::ClockService::clock_type interval;
85     senf::scheduler::TimerEvent event;
86     unsigned count;
87
88     void handler();
89
90 public:
91     MCWriter(std::string const & name, UDPSocket::Address const & group,
92              senf::ClockService::clock_type interval);
93 };
94
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),
99       count (0)
100 {}
101
102 prefix_ void MCWriter::handler()
103 {
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);
109 }
110
111 //-/////////////////////////////////////////////////////////////////////////////////////////////////
112
113 class IfSetup
114 {
115     std::string iface;
116
117 public:
118     IfSetup(std::string const & iface_);
119     ~IfSetup();
120
121     void sys(std::string const & cmd);
122
123     struct SystemException : public senf::Exception
124     { SystemException() : senf::Exception("IfSetup::SystemException") {} };
125
126 };
127
128 prefix_ IfSetup::IfSetup(std::string const & iface_)
129     : iface (iface_)
130 {
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());
134 }
135
136 prefix_ IfSetup::~IfSetup()
137 {
138     try {
139         sys((boost::format("ifconfig %s down") % iface).str());
140     }
141     catch (SystemException & ex) {
142         std::cerr << ex.what() << "\n";
143     }
144 }
145
146 prefix_ void IfSetup::sys(std::string const & cmd)
147 {
148     int rv (system(cmd.c_str()));
149     if (rv != 0)
150         throw SystemException() << ": code " << rv << ": " << cmd;
151 }
152
153 //-/////////////////////////////////////////////////////////////////////////////////////////////////
154
155 void sigintHandler(siginfo_t const &)
156 {
157     senf::scheduler::terminate();
158 }
159
160 //-/////////////////////////////////////////////////////////////////////////////////////////////////
161
162 int main(int argc, char * argv[])
163 {
164     try {
165         boost::scoped_ptr<IfSetup> setup (
166             (argc != 2 || std::string(argv[1]) != "-n") ? new IfSetup("dummy0") : 0);
167
168         senf::scheduler::SignalEvent sigint (SIGINT, &sigintHandler);
169
170         UDPSocket::Address g1 ("225.1:43434");
171         UDPSocket::Address g2 ("225.2:43434");
172
173         MCReader r1g1 (1u, "r1g1", g1);
174         MCReader r2g1 (2u, "r2g1", g1);
175         MCReader r1g2 (3u, "r1g2", g2);
176         MCReader r2g2 (4u, "r2g2", g2);
177
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));
182
183         senf::scheduler::process();
184     }
185     catch (std::exception const & ex) {
186         std::cerr << ex.what() << "\n";
187         return 1;
188     }
189 };
190
191 //-/////////////////////////////////////////////////////////////////////////////////////////////////
192 #undef prefix_
193 //#include "MultiMCLoop.mpp"
194
195 \f
196 // Local Variables:
197 // mode: c++
198 // fill-column: 100
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"
204 // End: