Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / ACE / examples / C++NPv2 / TP_Logging_Server.h
blobd1a43b9b8626bff785e1c3f2d033d68471d79d00
1 /*
2 ** Copyright 2002 Addison Wesley. All Rights Reserved.
3 */
5 #ifndef _TP_LOGGING_SERVER_H
6 #define _TP_LOGGING_SERVER_H
8 #include "ace/Singleton.h"
9 #include "ace/Synch.h"
10 #include "ace/Task.h"
11 #include "Logging_Acceptor.h"
12 #include "Logging_Event_Handler.h"
13 #include "Reactor_Logging_Server_T.h"
14 #include "TPLS_export.h"
15 #include <memory>
17 class TP_Logging_Task : public ACE_Task<ACE_SYNCH> {
18 // Instantiated with an MT synchronization trait.
19 public:
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); }
29 virtual int svc ();
32 typedef ACE_Unmanaged_Singleton<TP_Logging_Task, ACE_Null_Mutex>
33 TP_LOGGING_TASK;
35 /*******************************************************/
37 class TP_Logging_Acceptor;
39 class TP_Logging_Handler : public Logging_Event_Handler {
40 friend class TP_Logging_Acceptor;
41 protected:
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.
46 int queued_count_;
48 // Indicates whether <Logging_Event_Handler::handle_close()>
49 // must be called to cleanup and delete this object.
50 int deferred_close_;
52 // Serialize access to <queued_count_> and <deferred_close_>.
53 ACE_SYNCH_MUTEX lock_;
55 public:
56 TP_Logging_Handler (ACE_Reactor *reactor)
57 : Logging_Event_Handler (reactor),
58 queued_count_ (0),
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 {
72 public:
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) {
81 delete peer_handler;
82 return -1;
83 } else if (peer_handler->open () == -1)
84 peer_handler->handle_close (ACE_INVALID_HANDLE, 0);
85 return 0;
89 /*******************************************************/
91 class TP_Logging_Server : public ACE_Service_Object {
92 protected:
93 // Contains the reactor, acceptor, and handlers.
94 typedef Reactor_Logging_Server<TP_Logging_Acceptor>
95 LOGGING_DISPATCHER;
96 LOGGING_DISPATCHER *logging_dispatcher_;
98 public:
99 TP_Logging_Server (): logging_dispatcher_ (0) {}
101 virtual int init (int argc, ACE_TCHAR *argv[]) {
102 int i;
103 char **array = 0;
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]));
109 ACE_NEW_NORETURN
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_;
123 return 0;
127 ACE_FACTORY_DECLARE (TPLS, TP_Logging_Handler)
129 #endif /* _TP_LOGGING_SERVER_H */