Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / Time_Policy_Custom / main.cpp
bloba1e6e1c0a4b67f106610c9923d2ec02ca90a4ed3
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/Service_Config.h"
21 #include "ace/ARGV.h"
23 #if (TAO_HAS_TIME_POLICY == 1)
25 #include "Custom_Time_Policy_Strategy.h"
27 class TestHandler
28 : public ACE_Event_Handler
30 public:
31 TestHandler (CORBA::ORB_ptr orb)
32 : orb_ (CORBA::ORB::_duplicate (orb)),
33 timeout_triggered_ (false)
36 virtual int handle_timeout (const ACE_Time_Value &tv,
37 const void *arg);
39 bool trigger_in(const ACE_Time_Value &delay);
41 bool timeout_triggered () { return this->timeout_triggered_; }
43 private:
44 CORBA::ORB_var orb_;
45 bool timeout_triggered_;
48 int TestHandler::handle_timeout (const ACE_Time_Value &,
49 const void *)
51 ACE_DEBUG ((LM_DEBUG, "TestHandler::handle_timeout - timeout triggered\n"));
52 this->timeout_triggered_ = true;
53 this->orb_->shutdown (false);
54 return 0;
57 bool TestHandler::trigger_in(const ACE_Time_Value &delay)
59 return -1 != this->orb_->orb_core ()->reactor ()->schedule_timer (this, 0, delay, ACE_Time_Value (0));
62 int
63 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
65 try
67 ACE_ARGV my_argv;
68 if (argc>1 && argv[1][0]=='-' && argv[1][1]=='s')
70 ACE_DEBUG ((LM_DEBUG, "loading static service\n"));
72 ACE_Service_Config::process_directive (ace_svc_desc_Custom_Time_Policy_Strategy);
73 my_argv.add(argv[0]);
74 for (int i=2; i<argc ;++i)
75 my_argv.add (argv[i]);
77 my_argv.add (ACE_TEXT ("-ORBSvcConfDirective"));
78 my_argv.add (ACE_TEXT ("static Time_Policy_Manager \"-ORBTimePolicyStrategy CUSTOM_TIME_POLICY\""), true);
80 else
82 for (int i=0; i<argc ;++i)
83 my_argv.add (argv[i]);
86 int my_argc = my_argv.argc ();
87 CORBA::ORB_var orb =
88 CORBA::ORB_init (my_argc, my_argv.argv ());
90 // check if custom policy installed in ORB
91 ACE_Time_Value tv_hr = ACE_High_Res_Timer::gettimeofday_hr ();
92 ACE_Time_Value tv_orb = orb->orb_core ()->reactor ()->timer_queue ()->gettimeofday ();
93 ACE_Time_Value tv_diff = tv_orb - tv_hr;
94 // The custom policy gives an offset of +10s
95 if (tv_diff.sec () != 10)
97 ACE_ERROR_RETURN ((LM_ERROR,
98 "Incorrect time offset (%us). Time policy doesn't seem to be loaded\n", tv_diff.sec ()),
99 1);
102 CORBA::Object_var poa_object =
103 orb->resolve_initial_references("RootPOA");
105 if (CORBA::is_nil (poa_object.in ()))
106 ACE_ERROR_RETURN ((LM_ERROR,
107 "Unable to initialize the POA.\n"),
110 PortableServer::POA_var root_poa =
111 PortableServer::POA::_narrow (poa_object.in ());
113 PortableServer::POAManager_var poa_manager =
114 root_poa->the_POAManager ();
116 poa_manager->activate ();
118 TestHandler test_handler (orb.in ());
120 // trigger in 2 seconds
121 if (!test_handler.trigger_in (ACE_Time_Value (2, 0)))
122 ACE_ERROR_RETURN ((LM_ERROR,
123 "Unable to schedule trigger.\n"),
126 // run for max. 4 seconds
127 ACE_Time_Value timeout (4, 0);
128 orb->run (timeout);
130 root_poa->destroy (true, // ethernalize objects
131 false); // wait for completion
133 orb->destroy ();
135 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
137 if (!test_handler.timeout_triggered ())
139 ACE_DEBUG ((LM_DEBUG, "timer handler did not trigger\n"));
140 return 1;
143 catch (const CORBA::Exception& ex)
145 ex._tao_print_exception ("Caught exception:");
146 return 1;
149 return 0;
152 #else
154 ACE_TMAIN(int , ACE_TCHAR * [])
156 ACE_DEBUG ((LM_INFO, "TAO built without Time Policy support\n"));
157 return 0;
159 #endif /* TAO_HAS_TIME_POLICY != 1 */