Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Oneway_Send_Timeouts / main.cpp
blobc1153ecbbf8b1dbf57ec27dcba7529a0d244ec63
1 #include "Server_Task.h"
2 #include "Client_Task.h"
4 #include "ace/Log_Msg.h"
5 #include "ace/OS_NS_string.h"
6 #include "ace/OS_NS_strings.h"
7 #include "ace/High_Res_Timer.h"
8 #include "ace/Auto_Ptr.h"
10 class MyMain
12 public:
13 MyMain (int argc, ACE_TCHAR* argv[]);
14 ~MyMain ();
16 void run ();
18 private:
19 void print_usage ();
20 bool init_server (const ACE_TCHAR* args);
21 bool init_client (const ACE_TCHAR* args);
23 std::auto_ptr<Server_Task> server_task_;
24 std::auto_ptr<Client_Task> client_task_;
26 bool s_init_;
27 bool shutdown_;
30 void
31 MyMain::print_usage ()
35 bool
36 MyMain::init_server (const ACE_TCHAR* args)
38 std::string my_args (ACE_TEXT_ALWAYS_CHAR(args));
39 // main thread and extra thread for backdoor operations
40 int thread_pool = 2;
42 ACE_auto_ptr_reset (server_task_, new Server_Task (my_args));
43 ACE_ASSERT (server_task_.get() != 0);
45 server_task_->activate (THR_NEW_LWP | THR_JOINABLE |THR_INHERIT_SCHED
46 , thread_pool);
48 int duration = 4; //wait 3 seconds for initialization
49 ACE_Time_Value current = ACE_High_Res_Timer::gettimeofday_hr ();
50 ACE_Time_Value timeout = current + ACE_Time_Value(duration);
52 while (current < timeout) {
53 if (server_task_->ready()) {
54 break;
56 ACE_Time_Value sleep_time;
57 sleep_time.msec (10);
58 ACE_OS::sleep (sleep_time);
59 current += sleep_time;
62 if (!server_task_->ready()) {
63 server_task_->force_shutdown ();
64 server_task_->wait ();
65 ACE_auto_ptr_reset (server_task_, (Server_Task*)0);
66 return false;
69 return true;
72 bool
73 MyMain::init_client (const ACE_TCHAR* args)
75 std::string my_args (ACE_TEXT_ALWAYS_CHAR(args));
76 int thread_pool = 1;
78 ACE_auto_ptr_reset (client_task_, new Client_Task (my_args));
79 ACE_ASSERT (client_task_.get() != 0);
81 client_task_->activate (THR_NEW_LWP | THR_JOINABLE |THR_INHERIT_SCHED
82 , thread_pool);
84 return true;
87 MyMain::MyMain (int argc, ACE_TCHAR* argv[])
88 : s_init_ (false), shutdown_ (false)
90 argc--;
91 for (int p = 1; p <= argc; p++)
93 if (ACE_OS::strcmp (argv[p], ACE_TEXT("-?")) == 0) {
94 print_usage ();
95 return;
98 if (ACE_OS::strcasecmp (argv[p], ACE_TEXT("-s")) == 0) {
99 const ACE_TCHAR* s_args = (((p+1) <= argc) ? argv[p+1] : 0);
100 s_init_ = this->init_server (s_args);
101 p++;
103 else if (ACE_OS::strcasecmp (argv[p], ACE_TEXT("-c")) == 0) {
104 const ACE_TCHAR* s_args = (((p+1) <= argc) ? argv[p+1] : 0);
105 if (s_init_) {
106 this->init_client (s_args);
108 p++;
113 void
114 MyMain::run ()
116 if (server_task_.get() != 0) {
117 server_task_->wait ();
120 if (client_task_.get() != 0) {
121 client_task_->wait ();
124 shutdown_ = true;
127 MyMain::~MyMain ()
129 if (!shutdown_) {
130 this->run ();
134 int ACE_TMAIN (int argc, ACE_TCHAR* argv[])
136 MyMain my_main (argc, argv);
138 my_main.run ();
140 return 0;