Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Logger / simple-server / Logging_Handler.cpp
blob8b58ade2c5cb670cab1416d4f5d3312f153e8ff0
1 #include "ace/Log_Msg.h"
2 #include "ace/Message_Block.h"
3 #include "ace/Log_Record.h"
4 #include "ace/OS_NS_string.h"
5 #include "ace/CDR_Stream.h"
6 #include <memory>
8 #include "Logging_Handler.h"
9 #include "Reactor_Singleton.h"
11 Logging_Handler::~Logging_Handler ()
13 // Make sure there are no timers.
14 REACTOR::instance ()->cancel_timer (this);
16 this->cli_stream_.close ();
19 // Extract the underlying ACE_SOCK_Stream (e.g., for purposes of
20 // accept()).
21 ACE_SOCK_Stream &
22 Logging_Handler::peer ()
24 return this->cli_stream_;
27 int
28 Logging_Handler::handle_timeout (const ACE_Time_Value &,
29 const void *arg)
31 #if defined (ACE_NDEBUG)
32 ACE_UNUSED_ARG (arg);
33 #endif /* ACE_NDEBUG */
35 ACE_ASSERT (arg == this);
36 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) handling timeout from this = %@\n"),
37 this));
38 return 0;
41 // Perform the logging record receive.
43 int
44 Logging_Handler::handle_input (ACE_HANDLE)
46 ACE_Log_Record log_record;
48 // We need to use the old two-read trick here since TCP sockets
49 // don't support framing natively. Allocate a message block for the
50 // payload; initially at least large enough to hold the header, but
51 // needs some room for alignment.
52 ACE_Message_Block *payload_p = 0;
53 ACE_Message_Block *header_p = 0;
54 ACE_NEW_RETURN (header_p,
55 ACE_Message_Block (ACE_DEFAULT_CDR_BUFSIZE),
56 -1);
58 std::unique_ptr <ACE_Message_Block> header (header_p);
60 // Align the Message Block for a CDR stream
61 ACE_CDR::mb_align (header.get ());
63 ACE_CDR::Boolean byte_order;
64 ACE_CDR::ULong length;
66 ssize_t count = ACE::recv_n (this->peer ().get_handle (),
67 header->wr_ptr (),
68 8);
69 switch (count)
71 // Handle shutdown and error cases.
72 default:
73 case -1:
74 case 0:
76 ACE_DEBUG ((LM_DEBUG,
77 ACE_TEXT ("server logging daemon closing down\n")));
79 return -1;
80 /* NOTREACHED */
82 case 8:
83 // Just fall through in this case..
84 break;
87 header->wr_ptr (8); // Reflect addition of 8 bytes.
89 // Create a CDR stream to parse the 8-byte header.
90 ACE_InputCDR header_cdr (header.get ());
92 // Extract the byte-order and use helper methods to disambiguate
93 // octet, booleans, and chars.
94 header_cdr >> ACE_InputCDR::to_boolean (byte_order);
96 // Set the byte-order on the stream...
97 header_cdr.reset_byte_order (byte_order);
99 // Extract the length
100 header_cdr >> length;
102 ACE_NEW_RETURN (payload_p,
103 ACE_Message_Block (length),
104 -1);
105 std::unique_ptr <ACE_Message_Block> payload (payload_p);
107 // Ensure there's sufficient room for log record payload.
108 ACE_CDR::grow (payload.get (), 8 + ACE_CDR::MAX_ALIGNMENT + length);
110 // Use <recv_n> to obtain the contents.
111 if (ACE::recv_n (this->peer ().get_handle (),
112 payload->wr_ptr (),
113 length) <= 0)
115 ACE_ERROR ((LM_ERROR,
116 ACE_TEXT ("%p\n"),
117 ACE_TEXT ("recv_n()")));
118 return -1;
121 payload->wr_ptr (length); // Reflect additional bytes
123 ACE_InputCDR payload_cdr (payload.get ());
124 payload_cdr.reset_byte_order (byte_order);
125 payload_cdr >> log_record; // Finally extract the <ACE_log_record>.
127 log_record.length (length);
129 log_record.print (ACE_TEXT_CHAR_TO_TCHAR (this->host_name_), 1, stderr);
131 return 0;
134 // Extract underlying device descriptor.
136 ACE_HANDLE
137 Logging_Handler::get_handle () const
139 return this->cli_stream_.get_handle ();
143 Logging_Handler::open ()
145 ACE_INET_Addr addr;
147 if (this->cli_stream_.get_remote_addr (addr) == -1)
148 return -1;
149 else
151 ACE_OS::strncpy (this->host_name_,
152 addr.get_host_name (),
153 MAXHOSTNAMELEN + 1);
155 if (REACTOR::instance ()->register_handler (this, READ_MASK) == -1)
156 ACE_ERROR_RETURN ((LM_ERROR,
157 ACE_TEXT ("(%P|%t) can't register with reactor\n")),
158 -1);
159 else if (REACTOR::instance ()->schedule_timer
160 (this, (const void *) this,
161 ACE_Time_Value (2),
162 ACE_Time_Value (2)) == -1)
163 ACE_ERROR_RETURN ((LM_ERROR,
164 ACE_TEXT ("(%P|%t) can't register with reactor\n")),
165 -1);
166 else
167 ACE_DEBUG ((LM_DEBUG,
168 ACE_TEXT ("(%P|%t) connected with %C\n"),
169 this->host_name_));
170 return 0;
174 // Perform termination activities when deregistered from the
175 // ACE_Reactor.
177 Logging_Handler::handle_close (ACE_HANDLE, ACE_Reactor_Mask)
179 // Must have been allocated dynamically
180 delete this;
181 return 0;
184 // Perform termination activities when close fails.
186 Logging_Handler::close ()
188 return this->handle_close ();