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 UDPServer non-inline non-template implementation */
26 #include "UDPServer.hh"
27 //#include "UDPServer.ih"
30 #include <boost/algorithm/string/trim.hpp>
31 #include "../membind.hh"
33 //#include "UDPServer.mpp"
35 ///////////////////////////////cc.p////////////////////////////////////////
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),
43 senf::scheduler::FdEvent::EV_READ),
44 parser_ (), executor_ ()
46 if (address.address().multicast())
47 handle_.facet<senf::INet4MulticastSocketProtocol>().mcAddMembership(address.address());
48 SENF_LOG(("UDP Console server started at " << address));
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),
56 senf::scheduler::FdEvent::EV_READ),
57 parser_ (), executor_ ()
59 if (address.address().multicast())
60 handle_.facet<senf::INet6MulticastSocketProtocol>().mcAddMembership(address.address());
61 SENF_LOG(("UDP Console server started at " << address));
64 prefix_ senf::console::UDPServer & senf::console::UDPServer::replies(bool enable)
70 prefix_ senf::console::UDPServer &
71 senf::console::UDPServer::replies(senf::INet4SocketAddress const & address)
73 SENF_ASSERT( handle_.local().family() == senf::INet4SocketAddress::addressFamily );
78 prefix_ senf::console::UDPServer &
79 senf::console::UDPServer::replies(senf::INet6SocketAddress const & address)
81 SENF_ASSERT( handle_.local().family() == senf::INet6SocketAddress::addressFamily );
86 prefix_ senf::console::UDPServer & senf::console::UDPServer::emptyReplies(bool enable)
88 emptyReplies_ = enable;
92 prefix_ senf::console::DirectoryNode & senf::console::UDPServer::root()
95 return executor_.chroot();
98 prefix_ senf::console::UDPServer & senf::console::UDPServer::root(DirectoryNode & root)
100 executor_.chroot(root);
104 prefix_ void senf::console::UDPServer::handleInput(int events)
106 if (events != senf::scheduler::FdEvent::EV_READ) {
107 SENF_LOG((senf::log::IMPORTANT)("Input handle read error. Closing socket."));
108 readevent_.disable();
114 senf::GenericBSDSocketAddress address;
115 handle_.readfrom(data, address, 0u);
118 executor_.cwd(executor_.chroot());
119 std::stringstream stream;
121 parser_.parse(data, boost::bind<void>( boost::ref(executor_), boost::ref(stream), _1));
123 catch (Executor::ExitException &) {
126 catch (std::exception & ex) {
127 std::string msg (ex.what());
128 std::string::size_type i (msg.find("-- \n"));
129 if (i != std::string::npos)
130 msg = msg.substr(i+4);
131 stream << msg << std::endl;
133 if (replies_ && (emptyReplies_ || ! stream.str().empty())) {
136 if (stream.str().empty())
138 handle_.writeto(address, stream.str());
143 ///////////////////////////////cc.e////////////////////////////////////////
145 //#include "UDPServer.mpp"
151 // comment-column: 40
152 // c-file-style: "senf"
153 // indent-tabs-mode: nil
154 // ispell-local-dictionary: "american"
155 // compile-command: "scons -u test"