783a565f59f4dca0eb56a0fb22368a06729f7551
[senf.git] / senf / Utils / Console / UDPServer.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 UDPServer non-inline non-template implementation */
25
26 #include "UDPServer.hh"
27 //#include "UDPServer.ih"
28
29 // Custom includes
30 #include <boost/algorithm/string/trim.hpp>
31 #include <senf/Utils/membind.hh>
32
33 //#include "UDPServer.mpp"
34 #define prefix_
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36
37 prefix_ senf::console::UDPServer::UDPServer(senf::INet4SocketAddress const & address)
38     : replies_ (true), emptyReplies_ (true), target_ (),
39       handle_ (senf::UDPv4ClientSocketHandle(address)),
40       readevent_ ("senf::console::UDPServer::readevent",
41                   senf::membind(&UDPServer::handleInput, this),
42                   handle_,
43                   senf::scheduler::FdEvent::EV_READ),
44       parser_ (), executor_ ()
45 {
46     if (address.address().multicast())
47         handle_.facet<senf::INet4MulticastSocketProtocol>().mcAddMembership(address.address());
48     SENF_LOG(("UDP Console server started at " << address ));
49 }
50
51 prefix_ senf::console::UDPServer::UDPServer(senf::INet6SocketAddress const & address)
52     : replies_ (true), target_ (), handle_ (senf::UDPv6ClientSocketHandle(address)),
53       readevent_ ("senf::console::UDPServer::readevent",
54                   senf::membind(&UDPServer::handleInput, this),
55                   handle_,
56                   senf::scheduler::FdEvent::EV_READ),
57       parser_ (), executor_ ()
58 {
59     if (address.address().multicast())
60         handle_.facet<senf::INet6MulticastSocketProtocol>().mcAddMembership(address.address());
61     SENF_LOG(("UDP Console server started at " << address ));
62 }
63
64 prefix_ senf::console::UDPServer & senf::console::UDPServer::replies(bool enable)
65 {
66     replies_ = enable;
67     return *this;
68 }
69
70 prefix_ senf::console::UDPServer &
71 senf::console::UDPServer::replies(senf::INet4SocketAddress const & address)
72 {
73     SENF_ASSERT( handle_.local().family() == senf::INet4SocketAddress::addressFamily,
74                  "Internal failure: INet6 address on INet4 socket ??" );
75     target_ = address;
76     return *this;
77 }
78
79 prefix_ senf::console::UDPServer &
80 senf::console::UDPServer::replies(senf::INet6SocketAddress const & address)
81 {
82     SENF_ASSERT( handle_.local().family() == senf::INet6SocketAddress::addressFamily,
83                  "Internal failure: INet4 address on INet6 socket ??" );
84     target_ = address;
85     return *this;
86 }
87
88 prefix_ senf::console::UDPServer & senf::console::UDPServer::emptyReplies(bool enable)
89 {
90     emptyReplies_ = enable;
91     return *this;
92 }
93
94 prefix_ senf::console::DirectoryNode & senf::console::UDPServer::root()
95     const
96 {
97     return executor_.chroot();
98 }
99
100 prefix_ senf::console::UDPServer & senf::console::UDPServer::root(DirectoryNode & root)
101 {
102     executor_.chroot(root);
103     return *this;
104 }
105
106 prefix_ void senf::console::UDPServer::handleInput(int events)
107 {
108     if (events != senf::scheduler::FdEvent::EV_READ) {
109         SENF_LOG((senf::log::IMPORTANT)("Input handle read error. Closing socket."));
110         readevent_.disable();
111         handle_.close();
112         return;
113     }
114
115     std::string data;
116     senf::GenericBSDSocketAddress address;
117     handle_.readfrom(data, address, 0u);
118     boost::trim(data);
119
120     executor_.cwd(executor_.chroot());
121     std::stringstream stream;
122     try {
123         parser_.parse(data, boost::bind<void>( boost::ref(executor_), boost::ref(stream), _1));
124     }
125     catch (Executor::ExitException &) {
126         // Ignored
127     }
128     catch (ExceptionMixin & ex) {
129         stream << ex.message() << '\n';
130         SENF_LOG((senf::log::IMPORTANT)("Error: " << ex.message()));
131         SENF_LOG((senf::log::NOTICE)(ex.backtrace()));
132     }
133     catch (std::exception & ex) {
134         stream << ex.what() << '\n';
135         SENF_LOG((senf::log::IMPORTANT)("Error: " << ex.what()));
136     }
137     if (replies_ && (emptyReplies_ || ! stream.str().empty())) {
138         if (target_)
139             address = target_;
140         if (stream.str().empty())
141             stream << '\0';
142         handle_.writeto(address, stream.str());
143     }
144
145 }
146
147 //-/////////////////////////////////////////////////////////////////////////////////////////////////
148 #undef prefix_
149 //#include "UDPServer.mpp"
150
151 \f
152 // Local Variables:
153 // mode: c++
154 // fill-column: 100
155 // comment-column: 40
156 // c-file-style: "senf"
157 // indent-tabs-mode: nil
158 // ispell-local-dictionary: "american"
159 // compile-command: "scons -u test"
160 // End: