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