Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

UDPServer.cc

Go to the documentation of this file.
00001 // $Id: UDPServer.cc 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2009
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #include "UDPServer.hh"
00027 //#include "UDPServer.ih"
00028 
00029 // Custom includes
00030 #include <boost/algorithm/string/trim.hpp>
00031 #include <senf/Utils/membind.hh>
00032 
00033 //#include "UDPServer.mpp"
00034 #define prefix_
00035 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00036 
00037 prefix_ senf::console::UDPServer::UDPServer(senf::INet4SocketAddress const & address)
00038     : replies_ (true), emptyReplies_ (true), target_ (),
00039       handle_ (senf::UDPv4ClientSocketHandle(address)),
00040       readevent_ ("senf::console::UDPServer::readevent",
00041                   senf::membind(&UDPServer::handleInput, this),
00042                   handle_,
00043                   senf::scheduler::FdEvent::EV_READ),
00044       parser_ (), executor_ ()
00045 {
00046     if (address.address().multicast())
00047         handle_.facet<senf::INet4MulticastSocketProtocol>().mcAddMembership(address.address());
00048     SENF_LOG(("UDP Console server started at " << address ));
00049 }
00050 
00051 prefix_ senf::console::UDPServer::UDPServer(senf::INet6SocketAddress const & address)
00052     : replies_ (true), target_ (), handle_ (senf::UDPv6ClientSocketHandle(address)),
00053       readevent_ ("senf::console::UDPServer::readevent",
00054                   senf::membind(&UDPServer::handleInput, this),
00055                   handle_,
00056                   senf::scheduler::FdEvent::EV_READ),
00057       parser_ (), executor_ ()
00058 {
00059     if (address.address().multicast())
00060         handle_.facet<senf::INet6MulticastSocketProtocol>().mcAddMembership(address.address());
00061     SENF_LOG(("UDP Console server started at " << address ));
00062 }
00063 
00064 prefix_ senf::console::UDPServer & senf::console::UDPServer::replies(bool enable)
00065 {
00066     replies_ = enable;
00067     return *this;
00068 }
00069 
00070 prefix_ senf::console::UDPServer &
00071 senf::console::UDPServer::replies(senf::INet4SocketAddress const & address)
00072 {
00073     SENF_ASSERT( handle_.local().family() == senf::INet4SocketAddress::addressFamily,
00074                  "Internal failure: INet6 address on INet4 socket ??" );
00075     target_ = address;
00076     return *this;
00077 }
00078 
00079 prefix_ senf::console::UDPServer &
00080 senf::console::UDPServer::replies(senf::INet6SocketAddress const & address)
00081 {
00082     SENF_ASSERT( handle_.local().family() == senf::INet6SocketAddress::addressFamily,
00083                  "Internal failure: INet4 address on INet6 socket ??" );
00084     target_ = address;
00085     return *this;
00086 }
00087 
00088 prefix_ senf::console::UDPServer & senf::console::UDPServer::emptyReplies(bool enable)
00089 {
00090     emptyReplies_ = enable;
00091     return *this;
00092 }
00093 
00094 prefix_ senf::console::DirectoryNode & senf::console::UDPServer::root()
00095     const
00096 {
00097     return executor_.chroot();
00098 }
00099 
00100 prefix_ senf::console::UDPServer & senf::console::UDPServer::root(DirectoryNode & root)
00101 {
00102     executor_.chroot(root);
00103     return *this;
00104 }
00105 
00106 prefix_ void senf::console::UDPServer::handleInput(int events)
00107 {
00108     if (events != senf::scheduler::FdEvent::EV_READ) {
00109         SENF_LOG((senf::log::IMPORTANT)("Input handle read error. Closing socket."));
00110         readevent_.disable();
00111         handle_.close();
00112         return;
00113     }
00114 
00115     std::string data;
00116     senf::GenericBSDSocketAddress address;
00117     handle_.readfrom(data, address, 0u);
00118     boost::trim(data);
00119 
00120     executor_.cwd(executor_.chroot());
00121     std::stringstream stream;
00122     try {
00123         parser_.parse(data, boost::bind<void>( boost::ref(executor_), boost::ref(stream), _1));
00124     }
00125     catch (Executor::ExitException &) {
00126         // Ignored
00127     }
00128     catch (ExceptionMixin & ex) {
00129         stream << ex.message() << '\n';
00130         SENF_LOG((senf::log::IMPORTANT)("Error: " << ex.message()));
00131         SENF_LOG((senf::log::NOTICE)(ex.backtrace()));
00132     }
00133     catch (std::exception & ex) {
00134         stream << ex.what() << '\n';
00135         SENF_LOG((senf::log::IMPORTANT)("Error: " << ex.what()));
00136     }
00137     if (replies_ && (emptyReplies_ || ! stream.str().empty())) {
00138         if (target_)
00139             address = target_;
00140         if (stream.str().empty())
00141             stream << '\0';
00142         handle_.writeto(address, stream.str());
00143     }
00144 
00145 }
00146 
00147 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00148 #undef prefix_
00149 //#include "UDPServer.mpp"
00150 
00151 
00152 // Local Variables:
00153 // mode: c++
00154 // fill-column: 100
00155 // comment-column: 40
00156 // c-file-style: "senf"
00157 // indent-tabs-mode: nil
00158 // ispell-local-dictionary: "american"
00159 // compile-command: "scons -u test"
00160 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research