Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / orbsvcs / tests / BiDir_CORBALOC / TimeServer.cpp
blobcbe94d65983043039ef6ca0a4b92d951337fe26e
1 #include "TimeModuleS.h"
2 #include "tao/BiDir_GIOP/BiDirGIOP.h"
3 #include "tao/AnyTypeCode/Any.h"
4 #include "tao/Utils/PolicyList_Destroyer.h"
5 #include "orbsvcs/CosNamingC.h"
6 #include "tao/AnyTypeCode/Any.h"
7 #include "ace/OS_NS_time.h"
9 class Time_impl :
10 public POA_TimeModule::Time
12 public:
13 virtual TimeModule::TimeOfDay
14 get_gmt ();
16 void Shutdown ();
20 TimeModule::TimeOfDay Time_impl::get_gmt ()
22 time_t time_now = ACE_OS::time(0);
23 struct tm *time_p = ACE_OS::gmtime(&time_now);
25 TimeModule::TimeOfDay tod;
27 tod.hour = time_p->tm_hour;
28 tod.minute = time_p->tm_min;
29 tod.second = time_p->tm_sec;
31 return tod;
34 void
35 Time_impl::Shutdown ()
37 ACE_OS::exit(0);
40 int
41 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
43 try
45 // Initialize orb
46 CORBA::ORB_var orb =
47 CORBA::ORB_init (argc, argv);
49 // Get reference to Root POA.
50 CORBA::Object_var obj =
51 orb->resolve_initial_references ("RootPOA");
53 PortableServer::POA_var rootpoa =
54 PortableServer::POA::_narrow (obj.in ());
56 // Activate POA manager
57 PortableServer::POAManager_var mgr =
58 rootpoa->the_POAManager ();
60 mgr->activate ();
62 PortableServer::POA_var poa;
64 TAO::Utils::PolicyList_Destroyer PolicyList (3);
65 PolicyList.length (3);
67 PolicyList [0] =
68 rootpoa->create_lifespan_policy (PortableServer::PERSISTENT);
70 PolicyList [1] =
71 rootpoa->create_id_assignment_policy (PortableServer::USER_ID);
73 CORBA::Any CallbackPolicy;
74 CallbackPolicy <<= BiDirPolicy::BOTH;
75 const char* sServerPoaName = "TelemetryServer";
77 PolicyList [2] =
78 orb->create_policy (BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE,
79 CallbackPolicy);
81 poa = rootpoa->create_POA (sServerPoaName,
82 mgr.in(),
83 PolicyList);
86 PortableServer::ObjectId_var ServerId =
87 PortableServer::string_to_ObjectId ("TimeServer");
89 // Create an object
90 Time_impl *time_servant = new Time_impl;
91 PortableServer::ServantBase_var self_manage (time_servant);
93 poa->activate_object_with_id (ServerId.in (),
94 time_servant);
96 // Get a reference after activating the object
97 CORBA::Object_var object = poa->id_to_reference (ServerId.in ());
98 TimeModule::Time_var tm = TimeModule::Time::_narrow (object.in ());
100 // Get reference to initial naming context
101 CORBA::Object_var name_obj =
102 orb->resolve_initial_references ("NameService");
104 CosNaming::NamingContext_var inc =
105 CosNaming::NamingContext::_narrow (name_obj.in ());
107 if (CORBA::is_nil (inc.in ()))
109 ACE_ERROR ((LM_ERROR,
110 "(%P|%t) Error fetching naming context\n"));
113 CosNaming::Name service_name;
114 service_name.length(1);
115 service_name[0].id =
116 CORBA::string_dup ("Time");
118 inc->rebind (service_name,
119 tm.in ());
121 // Run the event loop for fun
122 ACE_Time_Value tv (3, 0);
124 // Accept requests
125 orb->run (&tv);
127 rootpoa->destroy (0 , 0);
129 orb->destroy ();
131 catch (const CORBA::Exception& ex)
133 ex._tao_print_exception ("Caught an exception\n");
135 return -1;
138 return 0;