Merge pull request #2317 from jwillemsen/jwi-deleteop
[ACE_TAO.git] / ACE / examples / C++NPv2 / TP_Reactor_Logging_Server.cpp
blobab9c42ccc13082f17660ad5cb14ccff9ced2eb61
1 /*
2 ** Copyright 2002 Addison Wesley. All Rights Reserved.
3 */
5 // FUZZ: disable check_for_streams_include
6 #include "ace/streams.h"
8 #include "ace/Reactor.h"
9 #include "ace/TP_Reactor.h"
10 #include "ace/Thread_Manager.h"
12 #if defined (ACE_WIN32) && defined (ACE_USES_OLD_IOSTREAMS)
13 # include <stdio.h>
14 #else
15 # include <string>
16 #endif
18 #include <memory>
19 #include "Reactor_Logging_Server_T.h"
20 #include "Logging_Acceptor_Ex.h"
22 typedef Reactor_Logging_Server<Logging_Acceptor_Ex>
23 Server_Logging_Daemon;
26 class Quit_Handler : public ACE_Event_Handler {
27 public:
28 Quit_Handler (ACE_Reactor *r) : ACE_Event_Handler (r) {}
30 virtual int handle_exception (ACE_HANDLE) {
31 reactor ()->end_reactor_event_loop ();
32 return -1; // Trigger call to handle_close() method.
35 virtual int handle_close (ACE_HANDLE, ACE_Reactor_Mask)
36 { delete this; return 0; }
38 protected:
39 // Protected destructor ensures dynamic allocation.
40 virtual ~Quit_Handler () {}
44 static ACE_THR_FUNC_RETURN event_loop (void *arg) {
45 ACE_Reactor *reactor = static_cast<ACE_Reactor *> (arg);
47 reactor->owner (ACE_OS::thr_self ());
48 reactor->run_reactor_event_loop ();
49 return 0;
53 static ACE_THR_FUNC_RETURN controller (void *arg) {
54 ACE_Reactor *reactor = static_cast<ACE_Reactor *> (arg);
56 Quit_Handler *quit_handler = 0;
57 ACE_NEW_RETURN (quit_handler, Quit_Handler (reactor), 0);
59 #if defined (ACE_WIN32) && defined (ACE_USES_OLD_IOSTREAMS)
60 for (;;) {
61 char user_input[80];
62 ACE_OS::fgets (user_input, sizeof (user_input), stdin);
63 if (ACE_OS::strcmp (user_input, "quit") == 0) {
64 reactor->notify (quit_handler);
65 break;
68 #else
69 for (;;) {
70 std::string user_input;
71 std::getline (cin, user_input, '\n');
73 if (user_input == "quit") {
74 reactor->notify (quit_handler);
75 break;
78 #endif
80 return 0;
84 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
86 const size_t N_THREADS = 4;
87 ACE_TP_Reactor tp_reactor;
88 ACE_Reactor reactor (&tp_reactor);
89 std::unique_ptr<ACE_Reactor> delete_instance
90 (ACE_Reactor::instance (&reactor));
92 Server_Logging_Daemon *server;
93 // Ignore argv[0]...
94 --argc; ++argv;
95 ACE_NEW_RETURN (server,
96 Server_Logging_Daemon (argc, argv,
97 ACE_Reactor::instance ()),
98 1);
99 ACE_Thread_Manager::instance ()->spawn_n
100 (N_THREADS, event_loop, ACE_Reactor::instance ());
101 ACE_Thread_Manager::instance ()->spawn
102 (controller, ACE_Reactor::instance ());
103 return ACE_Thread_Manager::instance ()->wait ();