diff options
author | Steve Huston <shuston@riverace.com> | 2001-10-15 18:41:11 +0000 |
---|---|---|
committer | Steve Huston <shuston@riverace.com> | 2001-10-15 18:41:11 +0000 |
commit | 722a9a7614a703648210dcdee9d030eb0ea282a3 (patch) | |
tree | df232e520a0cd0a689692ad9728014b1b44caebe /examples/C++NPv2 | |
parent | 34439dea3babf2f879b676cef450e4ec7dc20919 (diff) | |
download | ATCD-722a9a7614a703648210dcdee9d030eb0ea282a3.tar.gz |
ChangeLogTag:Mon Oct 15 11:26:39 2001 Steve Huston <shuston@riverace.com>
Diffstat (limited to 'examples/C++NPv2')
-rw-r--r-- | examples/C++NPv2/Logging_Handler.cpp | 128 | ||||
-rw-r--r-- | examples/C++NPv2/Logging_Handler.h | 55 |
2 files changed, 183 insertions, 0 deletions
diff --git a/examples/C++NPv2/Logging_Handler.cpp b/examples/C++NPv2/Logging_Handler.cpp new file mode 100644 index 00000000000..6c7916be8dc --- /dev/null +++ b/examples/C++NPv2/Logging_Handler.cpp @@ -0,0 +1,128 @@ +/* +** $Id$ +** +** Copyright 2001 Addison Wesley. All Rights Reserved. +*/ + +#include "ace/ACE.h" +#include "ace/CDR_Stream.h" +#include "ace/INET_Addr.h" +#include "ace/Log_Record.h" +#include "ace/Message_Block.h" + +#include "Logging_Handler.h" + + +int operator>> (ACE_InputCDR &cdr, ACE_Log_Record &log_record) +{ + ACE_CDR::Long type; + ACE_CDR::Long pid; + ACE_CDR::Long sec, usec; + ACE_CDR::ULong buffer_len; + + // Extract each field from input CDR stream into <log_record>. + if ((cdr >> type) && (cdr >> pid) && (cdr >> sec) && (cdr >> usec) + && (cdr >> buffer_len)) { + ACE_TCHAR log_msg[ACE_Log_Record::MAXLOGMSGLEN+1]; + log_record.type (type); + log_record.pid (pid); + log_record.time_stamp (ACE_Time_Value (sec, usec)); + cdr.read_char_array (log_msg, buffer_len); + log_msg[buffer_len] = '\0'; + log_record.msg_data (log_msg); + } + return cdr.good_bit (); +} + + +int Logging_Handler::recv_log_record (ACE_Message_Block *&mblk) +{ + // Put <logging_peer>'s hostname in new message block. + ACE_INET_Addr peer_addr; + logging_peer_.get_remote_addr (peer_addr); + mblk = new ACE_Message_Block (MAXHOSTNAMELEN + 1); + peer_addr.get_host_name (mblk->wr_ptr (), MAXHOSTNAMELEN); + mblk->wr_ptr (strlen (mblk->wr_ptr ()) + 1); // Go past name + + // Allocate a message block for the payload; initially at least + // large enough to hold the header, but needs some room for + // alignment. + ACE_Message_Block *payload = + new ACE_Message_Block (ACE_DEFAULT_CDR_BUFSIZE); + // Align the Message Block for a CDR stream + ACE_CDR::mb_align (payload); + if (logging_peer_.recv_n (payload->wr_ptr (), 8) == 8) { + payload->wr_ptr (8); // Reflect addition of 8 bytes + + // Create a CDR stream to parse the 8-byte header. + ACE_InputCDR cdr (payload); + + // Extract the byte-order and use helper methods to + // disambiguate octet, booleans, and chars. + ACE_CDR::Boolean byte_order; + cdr >> ACE_InputCDR::to_boolean (byte_order); + + // Set the byte-order on the stream... + cdr.reset_byte_order (byte_order); + + // Extract the length + ACE_CDR::ULong length; + cdr >> length; + + // Ensure there's sufficient room for log record payload. + payload->size (length + 8); + + // Use <recv_n> to obtain the contents. + if (logging_peer_.recv_n (payload->wr_ptr (), length) > 0) { + payload->wr_ptr (length); // Reflect additional bytes + // Chain the payload to mblk via the contination field. + mblk->cont (payload); + return length; + } + } + // Error cases end up here, so we need to release the memory to + // prevent a leak. + payload->release (); + payload = 0; + mblk->release (); + mblk = 0; + return -1; +} + + +int Logging_Handler::write_log_record (ACE_Message_Block *mblk) +{ + // Peer hostname is in the <mblk> and the log record data + // is in its continuation. + if (log_file_->send_n (mblk) == -1) + return -1; + if (ACE::debug ()) { + // Build a CDR stream from the log record data. + ACE_InputCDR cdr (mblk->cont ()); + ACE_CDR::Boolean byte_order; + ACE_CDR::ULong length; + // Extract the byte-order and length, ending up at the start + // of the log record itself. Use the byte order to properly + // set the CDR stream for extracting the contents. + cdr >> ACE_InputCDR::to_boolean (byte_order); + cdr.reset_byte_order (byte_order); + cdr >> length; + ACE_Log_Record log_record; + cdr >> log_record; // Finally extract the <ACE_log_record>. + log_record.print (mblk->rd_ptr (), 1, cerr); + } + return mblk->total_length (); +} + + +int Logging_Handler::log_record () +{ + ACE_Message_Block *mblk = 0; + if (recv_log_record (mblk) == -1) + return -1; + else { + int result = write_log_record (mblk); + mblk->release (); // Free up the contents. + return result == -1 ? -1 : 0; + } +} diff --git a/examples/C++NPv2/Logging_Handler.h b/examples/C++NPv2/Logging_Handler.h new file mode 100644 index 00000000000..7e33da73c12 --- /dev/null +++ b/examples/C++NPv2/Logging_Handler.h @@ -0,0 +1,55 @@ +/* +** $Id$ +** +** Copyright 2001 Addison Wesley. All Rights Reserved. +*/ + +#ifndef _LOGGING_HANDLER_H +#define _LOGGING_HANDLER_H + +#include "ace/FILE_IO.h" +#include "ace/SOCK_Stream.h" + +class ACE_Message_Block; + +class Logging_Handler +{ +protected: + ACE_FILE_IO *log_file_; // Pointer to a log file. + + ACE_SOCK_Stream logging_peer_; // Connected to the client. + +public: + // Initialization and termination methods. + Logging_Handler (ACE_FILE_IO &log_file): log_file_ (&log_file) {} + Logging_Handler (ACE_FILE_IO &log_file, + ACE_HANDLE handle): log_file_ (&log_file) + { logging_peer_.set_handle (handle); } + Logging_Handler (ACE_FILE_IO &log_file, + const ACE_SOCK_Stream &logging_peer) + : log_file_ (&log_file), logging_peer_ (logging_peer) {} + Logging_Handler (const ACE_SOCK_Stream &logging_peer) + : log_file_ (0), logging_peer_ (logging_peer) {} + int close () { return logging_peer_.close (); } + + // Receive one log record from a connected client. Returns + // length of record on success and <mblk> contains the + // hostname, <mblk->cont()> contains the log record header + // (the byte order and the length) and the data. Returns -1 on + // failure or connection close. + int recv_log_record (ACE_Message_Block *&log_record); + + // Write one record to the log file. The <mblk> contains the + // hostname and the <mblk->cont> contains the log record. + // Returns length of record written on success, or -1 on failure. + int write_log_record (ACE_Message_Block *log_record); + + // Log one record by calling <recv_log_record> and + // <write_log_record>. Returns 0 on success and -1 on failure. + int log_record (); + + // Accessor method. + ACE_SOCK_Stream &peer () { return logging_peer_; } +}; + +#endif /* _LOGGING_HANDLER_H */ |