LCOV - code coverage report
Current view: top level - eventdispatcher - local_stream_client_message_connection.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 15 18 83.3 %
Date: 2021-09-19 09:06:58 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13