Line data Source code
1 : // Copyright (c) 2012-2019 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
17 : // along with this program; if not, write to the Free Software
18 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : /** \file
21 : * \brief Implementation of the Snap Communicator class.
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 : // self
43 : //
44 : #include "eventdispatcher/pipe_message_connection.h"
45 :
46 :
47 : // snaplogger lib
48 : //
49 : #include <snaplogger/message.h>
50 :
51 :
52 : // snapdev lib
53 : //
54 : #include <snapdev/not_reached.h>
55 :
56 :
57 : // last include
58 : //
59 : #include <snapdev/poison.h>
60 :
61 :
62 :
63 :
64 : namespace ed
65 : {
66 :
67 :
68 :
69 : /** \brief Send a message.
70 : *
71 : * This function sends a message to the process on the other side
72 : * of this pipe connection.
73 : *
74 : * \exception event_dispatcher_runtime_error
75 : * This function throws this exception if the write() to the pipe
76 : * fails to write the entire message. This should only happen if
77 : * the pipe gets severed.
78 : *
79 : * \param[in] msg The message to be sent.
80 : * \param[in] cache Whether to cache the message if there is no connection.
81 : * (Ignore because a pipe has to always be there until
82 : * closed and then it can't be reopened.)
83 : *
84 : * \return Always true, although if an error occurs the function throws.
85 : */
86 0 : bool pipe_message_connection::send_message(message const & msg, bool cache)
87 : {
88 0 : snap::NOTUSED(cache);
89 :
90 : // transform the message to a string and write to the socket
91 : // the writing is asynchronous so the message is saved in a cache
92 : // and transferred only later when the run() loop is hit again
93 : //
94 0 : std::string buf(msg.to_message());
95 0 : buf += '\n';
96 0 : return write(buf.c_str(), buf.length()) == static_cast<ssize_t>(buf.length());
97 : }
98 :
99 :
100 : /** \brief Process a line (string) just received.
101 : *
102 : * The function parses the line as a message (snap_communicator_message)
103 : * and then calls the process_message() function if the line was valid.
104 : *
105 : * \param[in] line The line of text that was just read.
106 : */
107 0 : void pipe_message_connection::process_line(std::string const & line)
108 : {
109 0 : if(line.empty())
110 : {
111 0 : return;
112 : }
113 :
114 0 : message msg;
115 0 : if(msg.from_message(line))
116 : {
117 0 : dispatch_message(msg);
118 : }
119 : else
120 : {
121 : // TODO: what to do here? This could be that the version changed
122 : // and the messages are not compatible anymore.
123 : //
124 : SNAP_LOG_ERROR
125 0 : << "snap_communicator::snap_pipe_message_connection::process_line() was asked to process an invalid message ("
126 0 : << line
127 0 : << ")";
128 : }
129 : }
130 :
131 :
132 :
133 6 : } // namespace ed
134 : // vim: ts=4 sw=4 et
|