Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / examples / C++NPv1 / Logging_Client.cpp
blobc752b33e28620e61232759bb68d9e246517c2a0e
1 /*
2 ** Copyright 2001 Addison Wesley. All Rights Reserved.
3 */
5 #include "ace/OS_NS_sys_time.h"
6 #include "ace/CDR_Stream.h"
7 #include "ace/INET_Addr.h"
8 #include "ace/SOCK_Connector.h"
9 #include "ace/SOCK_Stream.h"
10 #include "ace/Log_Msg.h"
11 #include "ace/Log_Record.h"
12 #include "ace/Truncate.h"
13 #include "ace/OS_NS_unistd.h"
14 #include "ace/OS_NS_stdlib.h"
16 // FUZZ: disable check_for_streams_include
17 #include "ace/streams.h"
19 #if defined (ACE_WIN32) && defined (ACE_USES_OLD_IOSTREAMS)
20 # include <stdio.h>
21 #else
22 # include <string>
23 #endif
25 class Logging_Client {
26 private:
27 ACE_SOCK_Stream logging_peer_;
29 public:
30 ACE_SOCK_Stream &peer () { return logging_peer_; }
32 //FUZZ: disable check_for_lack_ACE_OS
33 int send (const ACE_Log_Record &log_record) {
34 //FUZZ: enable check_for_lack_ACE_OS
36 // Serialize the log record using a CDR stream, allocate
37 // enough space for the complete <ACE_Log_Record>.
38 const size_t max_payload_size =
39 4 // type()
40 + 8 // timestamp
41 + 4 // process id
42 + 4 // data length
43 + ACE_Log_Record::MAXLOGMSGLEN // data
44 + ACE_CDR::MAX_ALIGNMENT; // padding;
46 // Insert contents of <log_record> into payload stream.
47 ACE_OutputCDR payload (max_payload_size);
48 payload << log_record;
50 // Get the number of bytes used by the CDR stream.
51 ACE_CDR::ULong length =
52 ACE_Utils::truncate_cast<ACE_CDR::ULong> (payload.total_length ());
54 // Send a header so the receiver can determine the byte
55 // order and size of the incoming CDR stream.
56 ACE_OutputCDR header (ACE_CDR::MAX_ALIGNMENT + 8);
57 header << ACE_OutputCDR::from_boolean (ACE_CDR_BYTE_ORDER);
59 // Store the size of the payload that follows
60 header << ACE_CDR::ULong (length);
61 // Use an iovec to send both buffer and payload simultaneously.
62 iovec iov[2];
63 iov[0].iov_base = header.begin ()->rd_ptr ();
64 iov[0].iov_len = 8;
65 iov[1].iov_base = payload.begin ()->rd_ptr ();
66 iov[1].iov_len = length;
68 // Send header and payload efficiently using "gather-write".
69 return logging_peer_.sendv_n (iov, 2);
72 ~Logging_Client () { logging_peer_.close (); }
76 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
78 u_short logger_port = argc > 1 ? ACE_OS::atoi (argv[1]) : 0;
79 const char *logger_host =
80 argc > 2 ? argv[2] : ACE_DEFAULT_SERVER_HOST;
81 int result;
83 ACE_INET_Addr server_addr;
85 if (logger_port != 0)
86 result = server_addr.set (logger_port, logger_host);
87 else
88 result = server_addr.set ("ace_logger", logger_host);
89 if (result == -1)
90 ACE_ERROR_RETURN ((LM_ERROR, "lookup %s, %p\n",
91 logger_port == 0 ? "ace_logger" : argv[1],
92 logger_host), 1);
94 ACE_SOCK_Connector connector;
95 Logging_Client logging_client;
97 if (connector.connect (logging_client.peer (), server_addr) < 0)
98 ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "connect()"), 1);
100 #if defined (ACE_WIN32) && defined (ACE_USES_OLD_IOSTREAMS)
101 for (;;) {
102 char user_input[ACE_Log_Record::MAXLOGMSGLEN];
103 if (!gets (user_input))
104 break;
106 ACE_Time_Value now (ACE_OS::gettimeofday ());
107 ACE_Log_Record log_record (LM_INFO, now, ACE_OS::getpid ());
108 log_record.msg_data (user_input);
109 if (logging_client.send (log_record) == -1)
110 ACE_ERROR_RETURN ((LM_ERROR,
111 "%p\n", "logging_client.send()"), 1);
113 #else
114 // Limit the number of characters read on each record
115 cin.width (ACE_Log_Record::MAXLOGMSGLEN);
117 for (;;) {
118 std::string user_input;
119 std::getline (cin, user_input, '\n');
121 if (!cin || cin.eof ()) break;
123 ACE_Time_Value now (ACE_OS::gettimeofday ());
124 ACE_Log_Record log_record (LM_INFO, now, ACE_OS::getpid ());
125 log_record.msg_data (user_input.c_str ());
126 if (logging_client.send (log_record) == -1)
127 ACE_ERROR_RETURN ((LM_ERROR,
128 "%p\n", "logging_client.send()"), 1);
130 #endif
132 return 0; // Logging_Client destructor closes TCP connection.