Merge pull request #2317 from jwillemsen/jwi-deleteop
[ACE_TAO.git] / ACE / examples / C++NPv2 / AIO_Client_Logging_Daemon.h
blob351cb467962cbd344a8c77a54369c38865a1ed5f
1 /*
2 ** Copyright 2002 Addison Wesley. All Rights Reserved.
3 */
5 #ifndef _AIO_CLIENT_LOGGING_DAEMON_H
6 #define _AIO_CLIENT_LOGGING_DAEMON_H
8 #include "ace/Asynch_Connector.h"
9 #include "ace/Asynch_IO.h"
10 #include "ace/Message_Block.h"
11 #include "ace/Null_Mutex.h"
12 #include "ace/Proactor.h"
13 #include "ace/Singleton.h"
14 #include "ace/Synch_Traits.h"
15 #include "ace/Task.h"
17 #include <openssl/ssl.h>
19 class AIO_CLD_Acceptor;
21 class AIO_Input_Handler : public ACE_Service_Handler {
22 public:
23 AIO_Input_Handler (AIO_CLD_Acceptor *acc = 0)
24 : acceptor_ (acc), mblk_ (0) {}
26 virtual ~AIO_Input_Handler ();
28 // Called by ACE_Asynch_Acceptor when a client connects.
29 virtual void open (ACE_HANDLE new_handle,
30 ACE_Message_Block &message_block);
32 protected:
33 enum { LOG_HEADER_SIZE = 8 }; // Length of CDR header
34 AIO_CLD_Acceptor *acceptor_; // Our creator
35 ACE_Message_Block *mblk_; // Block to receive log record
36 ACE_Asynch_Read_Stream reader_; // Async read factory
38 // Handle input from logging clients.
39 virtual void handle_read_stream
40 (const ACE_Asynch_Read_Stream::Result &result);
44 class AIO_Output_Handler
45 : public ACE_Task<ACE_NULL_SYNCH>,
46 public ACE_Service_Handler {
47 public:
48 AIO_Output_Handler () : can_write_ (0) {}
50 virtual ~AIO_Output_Handler ();
52 // Entry point into the <AIO_Output_Handler>.
53 virtual int put (ACE_Message_Block *, ACE_Time_Value * = 0);
55 // Hook method called when server connection is established.
56 virtual void open (ACE_HANDLE new_handle,
57 ACE_Message_Block &message_block);
59 // A trivial implementation that is needed to suppress compile
60 // warnings/errors.
61 virtual int open (void *args)
63 return ACE_Task<ACE_NULL_SYNCH>::open (args);
66 protected:
67 ACE_Asynch_Read_Stream reader_; // Detects connection loss
68 ACE_Asynch_Write_Stream writer_; // Sends to server
69 int can_write_; // Safe to begin send a log record?
71 // Initiate the send of a log record
72 void start_write (ACE_Message_Block *mblk = 0);
74 // Handle disconnects from the logging server.
75 virtual void handle_read_stream
76 (const ACE_Asynch_Read_Stream::Result &result);
78 // Handle completed write to logging server.
79 virtual void handle_write_stream
80 (const ACE_Asynch_Write_Stream::Result &result);
83 typedef ACE_Unmanaged_Singleton<AIO_Output_Handler, ACE_Null_Mutex>
84 OUTPUT_HANDLER;
87 class AIO_CLD_Connector
88 : public ACE_Asynch_Connector<AIO_Output_Handler> {
89 public:
90 enum { INITIAL_RETRY_DELAY = 3, MAX_RETRY_DELAY = 60 };
92 // Constructor.
93 AIO_CLD_Connector ()
94 : retry_delay_ (INITIAL_RETRY_DELAY), ssl_ctx_ (0), ssl_ (0)
95 { open (); }
97 // Destructor frees the SSL resources.
98 virtual ~AIO_CLD_Connector () {
99 SSL_free (ssl_);
100 SSL_CTX_free (ssl_ctx_);
101 proactor ()->cancel_timer (*this);
104 // Hook method to detect failure and validate peer before
105 // opening handler.
106 virtual int validate_connection
107 (const ACE_Asynch_Connect::Result& result,
108 const ACE_INET_Addr &remote, const ACE_INET_Addr& local);
110 // Re-establish a connection to the logging server.
111 int reconnect () { return connect (remote_addr_); }
113 protected:
114 // Hook method called on timer expiration - retry connect
115 virtual void handle_time_out
116 (const ACE_Time_Value&, const void *);
118 // Template method to create a new handler
119 virtual AIO_Output_Handler *make_handler ()
120 { return OUTPUT_HANDLER::instance (); }
122 // Address at which logging server listens for connections.
123 ACE_INET_Addr remote_addr_;
125 // Seconds to wait before trying the next connect
126 int retry_delay_;
128 // The SSL "context" data structure.
129 SSL_CTX *ssl_ctx_;
131 // The SSL data structure corresponding to authenticated SSL
132 // connections.
133 SSL *ssl_;
136 typedef ACE_Unmanaged_Singleton<AIO_CLD_Connector, ACE_Null_Mutex>
137 CLD_CONNECTOR;
139 #endif /* _AIO_CLIENT_LOGGING_DAEMON_H */