2 ** Copyright 2002 Addison Wesley. All Rights Reserved.
5 #ifndef _TP_LOGGING_SERVER_H
6 #define _TP_LOGGING_SERVER_H
8 #include "ace/Singleton.h"
11 #include "Logging_Acceptor.h"
12 #include "Logging_Event_Handler.h"
13 #include "Reactor_Logging_Server_T.h"
14 #include "TPLS_export.h"
17 class TP_Logging_Task
: public ACE_Task
<ACE_SYNCH
> {
18 // Instantiated with an MT synchronization trait.
20 enum { MAX_THREADS
= 4 };
22 virtual int open (void * = 0)
23 { return activate (THR_NEW_LWP
, MAX_THREADS
); }
25 virtual int put (ACE_Message_Block
*mblk
,
26 ACE_Time_Value
*timeout
= 0)
27 { return putq (mblk
, timeout
); }
32 typedef ACE_Unmanaged_Singleton
<TP_Logging_Task
, ACE_Null_Mutex
>
35 /*******************************************************/
37 class TP_Logging_Acceptor
;
39 class TP_Logging_Handler
: public Logging_Event_Handler
{
40 friend class TP_Logging_Acceptor
;
42 virtual ~TP_Logging_Handler () {} // No-op destructor.
44 // Number of pointers to this class instance that currently
45 // reside in the <TP_LOGGING_TASK> singleton's message queue.
48 // Indicates whether <Logging_Event_Handler::handle_close()>
49 // must be called to cleanup and delete this object.
52 // Serialize access to <queued_count_> and <deferred_close_>.
53 ACE_SYNCH_MUTEX lock_
;
56 TP_Logging_Handler (ACE_Reactor
*reactor
)
57 : Logging_Event_Handler (reactor
),
59 deferred_close_ (0) {}
61 // Called when input events occur, e.g., connection or data.
62 virtual int handle_input (ACE_HANDLE
);
64 // Called when this object is destroyed, e.g., when it's
65 // removed from a reactor.
66 virtual int handle_close (ACE_HANDLE
, ACE_Reactor_Mask
);
69 /*******************************************************/
71 class TP_Logging_Acceptor
: public Logging_Acceptor
{
73 TP_Logging_Acceptor (ACE_Reactor
*r
= ACE_Reactor::instance ())
74 : Logging_Acceptor (r
) {}
76 virtual int handle_input (ACE_HANDLE
) {
77 TP_Logging_Handler
*peer_handler
= 0;
78 ACE_NEW_RETURN (peer_handler
,
79 TP_Logging_Handler (reactor ()), -1);
80 if (acceptor_
.accept (peer_handler
->peer ()) == -1) {
83 } else if (peer_handler
->open () == -1)
84 peer_handler
->handle_close (ACE_INVALID_HANDLE
, 0);
89 /*******************************************************/
91 class TP_Logging_Server
: public ACE_Service_Object
{
93 // Contains the reactor, acceptor, and handlers.
94 typedef Reactor_Logging_Server
<TP_Logging_Acceptor
>
96 LOGGING_DISPATCHER
*logging_dispatcher_
;
99 TP_Logging_Server (): logging_dispatcher_ (0) {}
101 virtual int init (int argc
, ACE_TCHAR
*argv
[]) {
104 ACE_NEW_RETURN (array
, char*[argc
], -1);
105 std::unique_ptr
<char *[]> char_argv (array
);
107 for (i
= 0; i
< argc
; ++i
)
108 char_argv
[i
] = ACE::strnew (ACE_TEXT_ALWAYS_CHAR (argv
[i
]));
110 (logging_dispatcher_
,
111 TP_Logging_Server::LOGGING_DISPATCHER
112 (i
, char_argv
.get (), ACE_Reactor::instance ()));
113 for (i
= 0; i
< argc
; ++i
) ACE::strdelete (char_argv
[i
]);
114 if (logging_dispatcher_
== 0) return -1;
115 else return TP_LOGGING_TASK::instance ()->open ();
118 virtual int fini () {
119 TP_LOGGING_TASK::instance ()->flush ();
120 TP_LOGGING_TASK::instance ()->wait ();
121 TP_LOGGING_TASK::close ();
122 delete logging_dispatcher_
;
127 ACE_FACTORY_DECLARE (TPLS
, TP_Logging_Handler
)
129 #endif /* _TP_LOGGING_SERVER_H */