Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Oneway_Send_Timeouts / main.cpp
blobd8ff3f1ede16689ce5bee57fca63449e1b53561c
1 #include "Client_Task.h"
2 #include "Server_Task.h"
4 #include "ace/High_Res_Timer.h"
5 #include "ace/Log_Msg.h"
6 #include "ace/OS_NS_string.h"
7 #include "ace/OS_NS_strings.h"
9 #include <memory>
11 class MyMain
13 public:
14 MyMain (int argc, ACE_TCHAR* argv[]);
15 ~MyMain ();
17 void run ();
19 private:
20 void print_usage ();
21 bool init_server (const ACE_TCHAR* args);
22 bool init_client (const ACE_TCHAR* args);
24 std::unique_ptr<Server_Task> server_task_;
25 std::unique_ptr<Client_Task> client_task_;
27 bool s_init_;
28 bool shutdown_;
31 void MyMain::print_usage ()
34 bool MyMain::init_server (const ACE_TCHAR* args)
36 std::string my_args (ACE_TEXT_ALWAYS_CHAR (args));
37 // main thread and extra thread for backdoor operations
38 int thread_pool = 2;
40 #ifdef ACE_HAS_CPP14
41 server_task_ = std::make_unique<Server_Task> (my_args);
42 #else
43 server_task_.reset (new Server_Task (my_args));
44 #endif
46 ACE_ASSERT (server_task_);
48 server_task_->activate (THR_NEW_LWP | THR_JOINABLE | THR_INHERIT_SCHED, thread_pool);
50 int duration = 4; // wait 3 seconds for initialization
51 ACE_Time_Value current = ACE_High_Res_Timer::gettimeofday_hr ();
52 ACE_Time_Value timeout = current + ACE_Time_Value (duration);
54 while (current < timeout)
56 if (server_task_->ready ())
58 break;
60 ACE_Time_Value sleep_time;
61 sleep_time.msec (10);
62 ACE_OS::sleep (sleep_time);
63 current += sleep_time;
66 if (!server_task_->ready ())
68 server_task_->force_shutdown ();
69 server_task_->wait ();
70 server_task_.reset ();
71 return false;
74 return true;
77 bool MyMain::init_client (const ACE_TCHAR* args)
79 std::string my_args (ACE_TEXT_ALWAYS_CHAR (args));
80 int thread_pool = 1;
82 #ifdef ACE_HAS_CPP14
83 client_task_ = std::make_unique<Client_Task> (my_args);
84 #else
85 client_task_.reset (new Client_Task (my_args));
86 #endif
88 ACE_ASSERT (client_task_);
90 client_task_->activate (THR_NEW_LWP | THR_JOINABLE | THR_INHERIT_SCHED, thread_pool);
92 return true;
95 MyMain::MyMain (int argc, ACE_TCHAR* argv[])
96 : s_init_ (false)
97 , shutdown_ (false)
99 argc--;
100 for (int p = 1; p <= argc; p++)
102 if (ACE_OS::strcmp (argv[p], ACE_TEXT ("-?")) == 0)
104 print_usage ();
105 return;
108 if (ACE_OS::strcasecmp (argv[p], ACE_TEXT ("-s")) == 0)
110 const ACE_TCHAR* s_args = (((p + 1) <= argc) ? argv[p + 1] : 0);
111 s_init_ = this->init_server (s_args);
112 p++;
114 else if (ACE_OS::strcasecmp (argv[p], ACE_TEXT ("-c")) == 0)
116 const ACE_TCHAR* s_args = (((p + 1) <= argc) ? argv[p + 1] : 0);
117 if (s_init_)
119 this->init_client (s_args);
121 p++;
126 void MyMain::run ()
128 if (server_task_)
130 server_task_->wait ();
133 if (client_task_)
135 client_task_->wait ();
138 shutdown_ = true;
141 MyMain::~MyMain ()
143 if (!shutdown_)
145 this->run ();
149 int ACE_TMAIN (int argc, ACE_TCHAR* argv[])
151 MyMain my_main (argc, argv);
153 my_main.run ();
155 return 0;