UDPServer.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "UDPServer.hh"
18 //#include "UDPServer.ih"
19 
20 // Custom includes
21 #include <boost/algorithm/string/trim.hpp>
22 #include <senf/Utils/membind.hh>
23 
24 //#include "UDPServer.mpp"
25 #define prefix_
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 
29  : replies_ (true), emptyReplies_ (true), target_ (),
30  handle_ (senf::UDPv4ClientSocketHandle(address)),
31  readevent_ ("senf::console::UDPServer::readevent",
32  senf::membind(&UDPServer::handleInput, this),
33  handle_,
34  senf::scheduler::FdEvent::EV_READ),
35  parser_ (), executor_ ()
36 {
37  if (address.address().multicast())
39  SENF_LOG(("UDP Console server started at " << address ));
40 }
41 
43  : replies_ (true), target_ (), handle_ (senf::UDPv6ClientSocketHandle(address)),
44  readevent_ ("senf::console::UDPServer::readevent",
45  senf::membind(&UDPServer::handleInput, this),
46  handle_,
47  senf::scheduler::FdEvent::EV_READ),
48  parser_ (), executor_ ()
49 {
50  if (address.address().multicast())
52  SENF_LOG(("UDP Console server started at " << address ));
53 }
54 
56 {
57  replies_ = enable;
58  return *this;
59 }
60 
63 {
65  "Internal failure: INet6 address on INet4 socket ??" );
66  target_ = address;
67  return *this;
68 }
69 
72 {
74  "Internal failure: INet4 address on INet6 socket ??" );
75  target_ = address;
76  return *this;
77 }
78 
80 {
81  emptyReplies_ = enable;
82  return *this;
83 }
84 
86  const
87 {
88  return executor_.chroot();
89 }
90 
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;
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 (ExceptionMixin & ex) {
120  stream << ex.message() << '\n';
121  SENF_LOG((senf::log::IMPORTANT)("Error: " << ex.message()));
123  }
124  catch (std::exception & ex) {
125  stream << ex.what() << '\n';
126  SENF_LOG((senf::log::IMPORTANT)("Error: " << ex.what()));
127  }
128  if (replies_ && (emptyReplies_ || ! stream.str().empty())) {
129  if (target_)
130  address = target_;
131  if (stream.str().empty())
132  stream << '\0';
133  handle_.writeto(address, stream.str());
134  }
135 }
136 
137 //-/////////////////////////////////////////////////////////////////////////////////////////////////
138 #undef prefix_
139 //#include "UDPServer.mpp"
140 
141 
142 // Local Variables:
143 // mode: c++
144 // fill-column: 100
145 // comment-column: 40
146 // c-file-style: "senf"
147 // indent-tabs-mode: nil
148 // ispell-local-dictionary: "american"
149 // compile-command: "scons -u test"
150 // End:
static short const addressFamily
void mcAddMembership(INet6Address const &mcAddr) const
UDPServer & replies(bool enable)
Enable or disable reply packets.
Definition: UDPServer.cc:55
UDPServer public header.
void mcAddMembership(INet4Address const &mcAddr) const
Config/console tree directory node.
Definition: Node.hh:406
u8 data[SPECTRAL_HT20_NUM_BINS]
std::string backtrace() const
void parse(std::string const &command, Callback cb)
Parse string.
Definition: Parse.cc:364
boost::function< R(Args)> membind(R(T::*fn)(Args), T *ob)
UDPServer(senf::INet4SocketAddress const &address)
Open UDP server on address.
Definition: UDPServer.cc:28
ProtocolClientSocketHandle< UDPv6SocketProtocol > UDPv6ClientSocketHandle
DirectoryNode & root() const
Get root node.
Definition: UDPServer.cc:85
Definition: Config.hh:28
boost::range_const_iterator< ForwardReadableRange const >::type writeto(AddressParam addr, ForwardReadableRange const &range)
Thrown by built-in &#39;exit&#39; command.
Definition: Executor.hh:58
bool multicast() const
Address local() const
DirectoryNode & cwd() const
Current working directory.
Definition: Executor.cc:49
#define SENF_ASSERT(x, comment)
static short const addressFamily
UDPServer & emptyReplies(bool enable)
Enable or disable empty reply packets.
Definition: UDPServer.cc:79
std::string message() const
INet6Address address() const
DirectoryNode & chroot() const
Get root node.
#define SENF_LOG(args)
bool multicast() const
std::pair< std::string, Address > readfrom(unsigned limit=0)
INet4Address address() const
#define prefix_
Definition: UDPServer.cc:25
ProtocolClientSocketHandle< UDPv4SocketProtocol > UDPv4ClientSocketHandle
UDP Console server.
Definition: UDPServer.hh:48