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 UDPServer non-inline non-template implementation */
31 #include "UDPServer.hh"
32 //#include "UDPServer.ih"
35 #include <boost/algorithm/string/trim.hpp>
36 #include <senf/Utils/membind.hh>
38 //#include "UDPServer.mpp"
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 prefix_ senf::console::UDPServer::UDPServer(senf::INet4SocketAddress const & address)
43 : replies_ (true), emptyReplies_ (true), target_ (),
44 handle_ (senf::UDPv4ClientSocketHandle(address)),
45 readevent_ ("senf::console::UDPServer::readevent",
46 senf::membind(&UDPServer::handleInput, this),
48 senf::scheduler::FdEvent::EV_READ),
49 parser_ (), executor_ ()
51 if (address.address().multicast())
52 handle_.facet<senf::INet4MulticastSocketProtocol>().mcAddMembership(address.address());
53 SENF_LOG(("UDP Console server started at " << address ));
56 prefix_ senf::console::UDPServer::UDPServer(senf::INet6SocketAddress const & address)
57 : replies_ (true), target_ (), handle_ (senf::UDPv6ClientSocketHandle(address)),
58 readevent_ ("senf::console::UDPServer::readevent",
59 senf::membind(&UDPServer::handleInput, this),
61 senf::scheduler::FdEvent::EV_READ),
62 parser_ (), executor_ ()
64 if (address.address().multicast())
65 handle_.facet<senf::INet6MulticastSocketProtocol>().mcAddMembership(address.address());
66 SENF_LOG(("UDP Console server started at " << address ));
69 prefix_ senf::console::UDPServer & senf::console::UDPServer::replies(bool enable)
75 prefix_ senf::console::UDPServer &
76 senf::console::UDPServer::replies(senf::INet4SocketAddress const & address)
78 SENF_ASSERT( handle_.local().family() == senf::INet4SocketAddress::addressFamily,
79 "Internal failure: INet6 address on INet4 socket ??" );
84 prefix_ senf::console::UDPServer &
85 senf::console::UDPServer::replies(senf::INet6SocketAddress const & address)
87 SENF_ASSERT( handle_.local().family() == senf::INet6SocketAddress::addressFamily,
88 "Internal failure: INet4 address on INet6 socket ??" );
93 prefix_ senf::console::UDPServer & senf::console::UDPServer::emptyReplies(bool enable)
95 emptyReplies_ = enable;
99 prefix_ senf::console::DirectoryNode & senf::console::UDPServer::root()
102 return executor_.chroot();
105 prefix_ senf::console::UDPServer & senf::console::UDPServer::root(DirectoryNode & root)
107 executor_.chroot(root);
111 prefix_ void senf::console::UDPServer::handleInput(int events)
113 if (events != senf::scheduler::FdEvent::EV_READ) {
114 SENF_LOG((senf::log::IMPORTANT)("Input handle read error. Closing socket."));
115 readevent_.disable();
121 senf::GenericBSDSocketAddress address;
122 handle_.readfrom(data, address, 0u);
125 executor_.cwd(executor_.chroot());
126 std::stringstream stream;
128 parser_.parse(data, boost::bind<void>( boost::ref(executor_), boost::ref(stream), _1));
130 catch (Executor::ExitException &) {
133 catch (ExceptionMixin & ex) {
134 stream << ex.message() << '\n';
135 SENF_LOG((senf::log::IMPORTANT)("Error: " << ex.message()));
136 SENF_LOG((senf::log::NOTICE)(ex.backtrace()));
138 catch (std::exception & ex) {
139 stream << ex.what() << '\n';
140 SENF_LOG((senf::log::IMPORTANT)("Error: " << ex.what()));
142 if (replies_ && (emptyReplies_ || ! stream.str().empty())) {
145 if (stream.str().empty())
147 handle_.writeto(address, stream.str());
152 //-/////////////////////////////////////////////////////////////////////////////////////////////////
154 //#include "UDPServer.mpp"
160 // comment-column: 40
161 // c-file-style: "senf"
162 // indent-tabs-mode: nil
163 // ispell-local-dictionary: "american"
164 // compile-command: "scons -u test"