Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / C++NPv1 / Reactive_Logging_Server_Ex.h
blob29cfea005c58e52902c846443416501bb3694a5c
1 /*
2 ** Copyright 2001 Addison Wesley. All Rights Reserved.
3 */
5 #ifndef _REACTIVE_LOGGING_SERVER_EX_H
6 #define _REACTIVE_LOGGING_SERVER_EX_H
8 #include "ace/ACE.h"
9 #include "ace/FILE_IO.h"
10 #include "ace/Handle_Set.h"
11 #include "ace/Hash_Map_Manager.h"
12 #include "ace/INET_Addr.h"
13 #include "ace/Log_Record.h"
14 #include "ace/SOCK_Acceptor.h"
15 #include "ace/SOCK_Stream.h"
16 #include "Logging_Server.h"
17 #include "Logging_Handler.h"
18 #include "ace/Null_Mutex.h"
19 #include "ace/Truncate.h"
20 #include "ace/os_include/os_fcntl.h"
22 typedef ACE_Hash_Map_Manager<ACE_HANDLE,
23 ACE_FILE_IO *,
24 ACE_Null_Mutex> LOG_MAP;
26 class Reactive_Logging_Server_Ex : public Logging_Server
28 protected:
29 // Associate an active handle to an <ACE_FILE_IO> pointer.
30 LOG_MAP log_map_;
32 // Keep track of acceptor socket and all the connected
33 // stream socket handles.
34 ACE_Handle_Set master_handle_set_;
36 // Keep track of read handles marked as active by <select>.
37 ACE_Handle_Set active_read_handles_;
39 virtual int open (u_short port) {
40 Logging_Server::open (port);
41 master_handle_set_.set_bit (acceptor ().get_handle ());
42 acceptor ().enable (ACE_NONBLOCK);
43 return 0;
46 virtual int wait_for_multiple_events () {
47 active_read_handles_ = master_handle_set_;
48 int width = ACE_Utils::truncate_cast<int> ((intptr_t)active_read_handles_.max_set ()) + 1;
50 return ACE::select (width, active_read_handles_);
53 virtual int handle_connections () {
54 ACE_SOCK_Stream logging_peer;
56 while (acceptor ().accept (logging_peer) != -1) {
57 ACE_FILE_IO *log_file = new ACE_FILE_IO;
59 // Use the client's hostname as the logfile name.
60 make_log_file (*log_file, &logging_peer);
62 // Add the new <logging_peer>'s handle to the map and
63 // to the set of handles we <select> for input.
64 log_map_.bind (logging_peer.get_handle (), log_file);
65 master_handle_set_.set_bit (logging_peer.get_handle ());
67 active_read_handles_.clr_bit (acceptor ().get_handle ());
68 return 0;
71 virtual int handle_data (ACE_SOCK_Stream *) {
72 ACE_Handle_Set_Iterator peer_iterator (active_read_handles_);
74 for (ACE_HANDLE handle;
75 (handle = peer_iterator ()) != ACE_INVALID_HANDLE;
76 ) {
77 ACE_FILE_IO *log_file = 0;
78 log_map_.find (handle, log_file);
79 Logging_Handler logging_handler (*log_file, handle);
81 if (logging_handler.log_record () == -1) {
82 logging_handler.close ();
83 master_handle_set_.clr_bit (handle);
84 log_map_.unbind (handle);
85 log_file->close ();
86 delete log_file;
89 return 0;
93 #endif /* _REACTIVE_LOGGING_SERVER_EX_H */