LCOV - code coverage report
Current view: top level - eventdispatcher - pipe_message_connection.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 15 6.7 %
Date: 2019-08-08 02:52:36 Functions: 2 4 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2012-2019  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // This program is free software; you can redistribute it and/or modify
       4             : // it under the terms of the GNU General Public License as published by
       5             : // the Free Software Foundation; either version 2 of the License, or
       6             : // (at your option) any later version.
       7             : //
       8             : // This program is distributed in the hope that it will be useful,
       9             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      10             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      11             : // GNU General Public License for more details.
      12             : //
      13             : // You should have received a copy of the GNU General Public License
      14             : // along with this program; if not, write to the Free Software
      15             : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      16             : 
      17             : /** \file
      18             :  * \brief Implementation of the Snap Communicator class.
      19             :  *
      20             :  * This class wraps the C poll() interface in a C++ object with many types
      21             :  * of objects:
      22             :  *
      23             :  * \li Server Connections; for software that want to offer a port to
      24             :  *     which clients can connect to; the server will call accept()
      25             :  *     once a new client connection is ready; this results in a
      26             :  *     Server/Client connection object
      27             :  * \li Client Connections; for software that want to connect to
      28             :  *     a server; these expect the IP address and port to connect to
      29             :  * \li Server/Client Connections; for the server when it accepts a new
      30             :  *     connection; in this case the server gets a socket from accept()
      31             :  *     and creates one of these objects to handle the connection
      32             :  *
      33             :  * Using the poll() function is the easiest and allows us to listen
      34             :  * on pretty much any number of sockets (on my server it is limited
      35             :  * at 16,768 and frankly over 1,000 we probably will start to have
      36             :  * real slowness issues on small VPN servers.)
      37             :  */
      38             : 
      39             : // self
      40             : //
      41             : #include "eventdispatcher/pipe_message_connection.h"
      42             : 
      43             : //#include "eventdispatcher/snap_communicator_dispatcher.h"
      44             : 
      45             : 
      46             : // snaplogger lib
      47             : //
      48             : #include "snaplogger/message.h"
      49             : 
      50             : 
      51             : // snapdev lib
      52             : //
      53             : #include "snapdev/not_reached.h"
      54             : //#include "snapdev/not_used.h"
      55             : //#include "snapdev/string_replace_many.h"
      56             : //
      57             : //
      58             : //// libaddr lib
      59             : ////
      60             : //#include "libaddr/addr_parser.h"
      61             : //
      62             : //
      63             : //// C++ lib
      64             : ////
      65             : //#include <sstream>
      66             : //#include <limits>
      67             : //#include <atomic>
      68             : //
      69             : //
      70             : //// C lib
      71             : ////
      72             : //#include <fcntl.h>
      73             : //#include <poll.h>
      74             : //#include <unistd.h>
      75             : //#include <sys/eventfd.h>
      76             : //#include <sys/inotify.h>
      77             : //#include <sys/ioctl.h>
      78             : //#include <sys/resource.h>
      79             : //#include <sys/syscall.h>
      80             : //#include <sys/time.h>
      81             : 
      82             : 
      83             : // last include
      84             : //
      85             : #include <snapdev/poison.h>
      86             : 
      87             : 
      88             : 
      89             : 
      90             : namespace ed
      91             : {
      92             : 
      93             : 
      94             : 
      95             : /** \brief Send a message.
      96             :  *
      97             :  * This function sends a message to the process on the other side
      98             :  * of this pipe connection.
      99             :  *
     100             :  * \exception event_dispatcher_runtime_error
     101             :  * This function throws this exception if the write() to the pipe
     102             :  * fails to write the entire message. This should only happen if
     103             :  * the pipe gets severed.
     104             :  *
     105             :  * \param[in] msg  The message to be sent.
     106             :  * \param[in] cache  Whether to cache the message if there is no connection.
     107             :  *                   (Ignore because a pipe has to always be there until
     108             :  *                   closed and then it can't be reopened.)
     109             :  *
     110             :  * \return Always true, although if an error occurs the function throws.
     111             :  */
     112           0 : bool pipe_message_connection::send_message(message const & msg, bool cache)
     113             : {
     114           0 :     snap::NOTUSED(cache);
     115             : 
     116             :     // transform the message to a string and write to the socket
     117             :     // the writing is asynchronous so the message is saved in a cache
     118             :     // and transferred only later when the run() loop is hit again
     119             :     //
     120           0 :     std::string buf(msg.to_message());
     121           0 :     buf += '\n';
     122           0 :     return write(buf.c_str(), buf.length()) == static_cast<ssize_t>(buf.length());
     123             : }
     124             : 
     125             : 
     126             : /** \brief Process a line (string) just received.
     127             :  *
     128             :  * The function parses the line as a message (snap_communicator_message)
     129             :  * and then calls the process_message() function if the line was valid.
     130             :  *
     131             :  * \param[in] line  The line of text that was just read.
     132             :  */
     133           0 : void pipe_message_connection::process_line(std::string const & line)
     134             : {
     135           0 :     if(line.empty())
     136             :     {
     137           0 :         return;
     138             :     }
     139             : 
     140           0 :     message msg;
     141           0 :     if(msg.from_message(line))
     142             :     {
     143           0 :         dispatch_message(msg);
     144             :     }
     145             :     else
     146             :     {
     147             :         // TODO: what to do here? This could be that the version changed
     148             :         //       and the messages are not compatible anymore.
     149             :         //
     150             :         SNAP_LOG_ERROR
     151           0 :             << "snap_communicator::snap_pipe_message_connection::process_line() was asked to process an invalid message ("
     152           0 :             << line
     153           0 :             << ")";
     154             :     }
     155             : }
     156             : 
     157             : 
     158             : 
     159           6 : } // namespace ed
     160             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12