Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / Bug_4152_Regression / server.cpp
blobd07865512f6f61622d81072055e28cbc42d9ec94
1 // server.cpp
2 // This version uses the Implementation Repository.
4 #include "Test_i.h"
6 #include "tao/IORTable/IORTable.h"
7 #include "tao/PortableServer/Root_POA.h"
8 #include "tao/ImR_Client/ImR_Client.h"
10 #include "ace/Get_Opt.h"
11 #include "ace/Task.h"
12 #include "ace/streams.h"
13 #include "ace/OS_NS_unistd.h"
15 class ORB_Runner : public ACE_Task_Base
17 public:
18 explicit ORB_Runner (CORBA::ORB_ptr orb) : orb_(CORBA::ORB::_duplicate(orb)) {}
19 int svc ()
21 this->orb_->run ();
22 return 0;
25 private:
26 CORBA::ORB_var orb_;
29 PortableServer::POA_var root_poa;
30 PortableServer::POA_var poa_a;
32 void
33 createPOAs(ACE_CString &base)
35 PortableServer::LifespanPolicy_var life =
36 root_poa->create_lifespan_policy(PortableServer::PERSISTENT);
38 PortableServer::IdAssignmentPolicy_var assign =
39 root_poa->create_id_assignment_policy(PortableServer::USER_ID);
41 CORBA::PolicyList pols;
42 pols.length(2);
43 pols[0] = PortableServer::LifespanPolicy::_duplicate(life.in());
44 pols[1] = PortableServer::IdAssignmentPolicy::_duplicate(assign.in());
46 PortableServer::POAManager_var mgr = PortableServer::POAManager::_nil();
47 ACE_CString poa_name = base + ACE_CString ("_a");
48 poa_a = root_poa->create_POA(poa_name.c_str(), mgr.in(), pols);
51 void
52 activatePOAs ()
54 PortableServer::POAManager_var mgr = root_poa->the_POAManager ();
55 mgr->activate ();
56 mgr = poa_a->the_POAManager ();
57 mgr->activate ();
60 int
61 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
63 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
64 ORB_Runner *runner = new ORB_Runner (orb.in ());
65 int poa_delay = 10;
66 int shutdown_delay = 0;
68 ACE_DEBUG ((LM_DEBUG, "(%P|%t) Start server main\n"));
70 try
72 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("p:s:?"));
73 int c;
75 while ((c = get_opts ()) != -1)
76 switch (c)
78 case 'p':
79 poa_delay = ACE_OS::atoi (get_opts.opt_arg ());
80 break;
81 case 's':
82 shutdown_delay = ACE_OS::atoi (get_opts.opt_arg ());
83 break;
84 case '?':
85 ACE_DEBUG ((LM_DEBUG,
86 "usage: %s "
87 "-d <seconds to delay before initializing POA> "
88 "-s <seconds to delay before exiting main after the ORB destroy>\n",
89 argv[0]));
90 return 1;
91 break;
94 ACE_OS::sleep (poa_delay);
95 CORBA::Object_var obj = orb->resolve_initial_references ("RootPOA");
96 root_poa = PortableServer::POA::_narrow (obj.in ());
98 runner->activate ();
100 ACE_CString base = ACE_CString ("TestObject");
101 createPOAs (base);
103 PortableServer::Servant_var<Test_i> test_servant = new Test_i (orb.in ());
105 PortableServer::ObjectId_var object_id =
106 PortableServer::string_to_ObjectId (base.c_str());
108 poa_a->activate_object_with_id (object_id.in(), test_servant.in ());
109 obj = poa_a->id_to_reference (object_id.in());
111 Test_var tva = Test::_narrow (obj.in());
113 ACE_DEBUG ((LM_DEBUG, "(%P|%t) Started Server pid <%P> poa delay <%d> shutdown delay <%d>\n", poa_delay, shutdown_delay));
116 ACE_CString status_file = base + ACE_CString(".status");
117 ofstream out(status_file.c_str (), ios_base::app);
118 if (!out.good())
120 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server did not get good bit from %s\n", status_file.c_str()));
122 out << ACE_OS::getpid () << endl;
123 out.close ();
125 ACE_OS::sleep (poa_delay);
126 activatePOAs ();
128 ACE_DEBUG ((LM_DEBUG, "(%P|%t) Activated POA pid <%P>\n"));
130 TAO_Root_POA* tpoa = dynamic_cast<TAO_Root_POA*> (poa_a.in ());
131 ACE_ASSERT (tpoa != 0);
133 obj = tpoa->id_to_reference_i (object_id.in (), false);
134 CORBA::String_var test_ior = orb->object_to_string (obj.in ());
135 obj = orb->resolve_initial_references("IORTable");
136 IORTable::Table_var table = IORTable::Table::_narrow (obj.in ());
137 table->bind(base.c_str (), test_ior.in ());
139 test_ior = orb->object_to_string (tva.in());
140 base += "_a";
141 ACE_DEBUG ((LM_DEBUG, "(%P|%t) %C:\n%C\n", base.c_str(), test_ior.in()));
142 table->bind (base.c_str (), test_ior.in ());
144 runner->wait ();
146 ACE_DEBUG ((LM_DEBUG, "(%P|%t) Destroying POA pid <%P>\n"));
148 root_poa->destroy(1,1);
149 orb->destroy();
151 catch(const CORBA::Exception& ex)
153 ex._tao_print_exception ("Server main()");
154 return 1;
157 delete runner;
158 orb = CORBA::ORB::_nil ();
160 ACE_OS::sleep (shutdown_delay);
162 ACE_DEBUG ((LM_DEBUG, "(%P|%t) Exiting Server pid <%P>\n"));
164 return 0;