Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / Time_Policy / main.cpp
blobcf4a666c134b2b80003e2cea74ea500096e04e66
1 //=============================================================================
2 /**
3 * @file main.cpp
5 * Implementation of the server.
7 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
8 * @author Michael Kircher <Michael.Kircher@mchp.siemens.de>
9 */
10 //=============================================================================
12 #include "tao/ORB.h"
13 #include "tao/ORB_Core.h"
14 #include "tao/PortableServer/PortableServer.h"
15 #include "ace/Reactor.h"
16 #include "ace/Timer_Queue.h"
17 #include "ace/Time_Value.h"
18 #include "ace/OS_NS_sys_time.h"
19 #include "ace/High_Res_Timer.h"
20 #include "ace/Get_Opt.h"
22 #if (TAO_HAS_TIME_POLICY == 1) && \
23 (defined (ACE_HAS_HI_RES_TIMER) || \
24 defined (ACE_WIN32) || \
25 (defined (ghs) && defined (ACE_HAS_PENTIUM)) || \
26 ((defined (__GNUG__) || defined (__INTEL_COMPILER)) && !defined(ACE_VXWORKS) && defined (ACE_HAS_PENTIUM)) || \
27 (defined (ACE_HAS_POWERPC_TIMER) && (defined (ghs) || defined (__GNUG__))) || \
28 (defined (ACE_HAS_CLOCK_GETTIME) && defined (ACE_HAS_CLOCK_GETTIME_MONOTONIC)))
30 #if defined(TAO_USE_HR_TIME_POLICY_STRATEGY)
31 bool uses_hr_time = true;
32 #else
33 bool uses_hr_time = false;
34 #endif
36 class TestHandler
37 : public ACE_Event_Handler
39 public:
40 TestHandler (CORBA::ORB_ptr orb)
41 : orb_ (CORBA::ORB::_duplicate (orb)),
42 timeout_triggered_ (false)
45 virtual int handle_timeout (const ACE_Time_Value &tv,
46 const void *arg);
48 bool trigger_in(const ACE_Time_Value &delay);
50 bool timeout_triggered () { return this->timeout_triggered_; }
52 private:
53 CORBA::ORB_var orb_;
54 bool timeout_triggered_;
57 int TestHandler::handle_timeout (const ACE_Time_Value &,
58 const void *)
60 ACE_DEBUG ((LM_DEBUG, "TestHandler::handle_timeout - timeout triggered\n"));
61 this->timeout_triggered_ = true;
62 this->orb_->shutdown (false);
63 return 0;
66 bool TestHandler::trigger_in(const ACE_Time_Value &delay)
68 return -1 != this->orb_->orb_core ()->reactor ()->schedule_timer (this, 0, delay, ACE_Time_Value (0));
71 int
72 parse_args (int argc, ACE_TCHAR *argv[])
74 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("h"));
75 int c;
77 while ((c = get_opts ()) != -1)
78 switch (c)
80 case 'h':
81 uses_hr_time = true;
82 break;
83 case '?':
84 default:
85 ACE_ERROR_RETURN ((LM_ERROR,
86 "usage: %s "
87 "-h "
88 "\n"
89 "\t-h\t: uses highres time policy\n",
90 argv [0]),
91 -1);
92 break;
95 // Indicates successful parsing of the command line
96 return 0;
99 int
100 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
104 CORBA::ORB_var orb =
105 CORBA::ORB_init (argc, argv);
107 CORBA::Object_var poa_object =
108 orb->resolve_initial_references("RootPOA");
110 if (CORBA::is_nil (poa_object.in ()))
111 ACE_ERROR_RETURN ((LM_ERROR,
112 "Unable to initialize the POA.\n"),
115 PortableServer::POA_var root_poa =
116 PortableServer::POA::_narrow (poa_object.in ());
118 PortableServer::POAManager_var poa_manager =
119 root_poa->the_POAManager ();
121 if (parse_args (argc, argv) != 0)
122 return 1;
124 poa_manager->activate ();
126 TestHandler test_handler (orb.in ());
128 // trigger in 2 seconds
129 if (!test_handler.trigger_in (ACE_Time_Value (2, 0)))
130 ACE_ERROR_RETURN ((LM_ERROR,
131 "Unable to schedule trigger.\n"),
134 // reset system clock 4 seconds backwards
135 timespec_t curts;
136 ACE_Time_Value curtime = ACE_OS::gettimeofday ();
137 curtime -= ACE_Time_Value (4, 0);
138 curts = curtime;
139 if (ACE_OS::clock_settime (CLOCK_REALTIME, &curts) != 0)
141 ACE_DEBUG((LM_INFO,
142 "Unable to reset OS time. Insufficient privileges or not supported.\n"));
144 root_poa->destroy (true, // ethernalize objects
145 false); // wait for completion
147 orb->destroy ();
149 return 0;
151 else
153 // run for max. 4 seconds
154 ACE_Time_Value timeout (4, 0);
155 orb->run (timeout);
157 root_poa->destroy (true, // ethernalize objects
158 false); // wait for completion
160 orb->destroy ();
162 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
164 // reset system clock to correct time
165 curtime = ACE_OS::gettimeofday ();
166 curtime += ACE_Time_Value (4, 0);
167 curts = curtime;
168 ACE_OS::clock_settime (CLOCK_REALTIME, &curts);
170 if (!test_handler.timeout_triggered ())
172 ACE_DEBUG ((LM_DEBUG, "timer handler did not trigger\n"));
173 return uses_hr_time ? 1 : 0;
175 else
177 return uses_hr_time ? 0 : 1;
181 catch (const CORBA::Exception& ex)
183 ex._tao_print_exception ("Caught exception:");
184 return 1;
188 #else
190 ACE_TMAIN(int , ACE_TCHAR * [])
192 ACE_DEBUG ((LM_INFO, "TAO built without Time Policy support\n"));
193 return 0;
195 #endif /* TAO_HAS_TIME_POLICY != 1 */