Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / link_poas / server.cpp
blobdc946f2142ce0629cd04fb37f214293161d2c609
1 // server.cpp
2 // This version uses the Implementation Repository.
4 #include "Test_i.h"
6 #include "tao/PortableServer/Root_POA.h"
7 #include "tao/ImR_Client/ImR_Client.h"
9 #include "ace/Get_Opt.h"
10 #include "ace/streams.h"
11 #include "ace/OS_NS_unistd.h"
13 PortableServer::POA_var root_poa;
14 PortableServer::POA_var poa_a;
15 PortableServer::POA_var poa_b;
16 PortableServer::POA_var poa_c;
18 void
19 createPOAs (ACE_CString &base)
21 PortableServer::LifespanPolicy_var life =
22 root_poa->create_lifespan_policy(PortableServer::PERSISTENT);
24 PortableServer::IdAssignmentPolicy_var assign =
25 root_poa->create_id_assignment_policy(PortableServer::USER_ID);
27 CORBA::PolicyList pols;
28 pols.length(2);
29 pols[0] = PortableServer::LifespanPolicy::_duplicate(life.in());
30 pols[1] = PortableServer::IdAssignmentPolicy::_duplicate(assign.in());
32 PortableServer::POAManager_var mgr = PortableServer::POAManager::_nil();
34 ACE_CString poa_name = base + ACE_CString ("_a");
35 poa_a = root_poa->create_POA(poa_name.c_str(), mgr.in(), pols);
36 poa_name = base + ACE_CString ("_b");
37 poa_b = root_poa->create_POA(poa_name.c_str(), mgr.in(), pols);
38 poa_name = base + ACE_CString ("_c");
39 poa_c = root_poa->create_POA(poa_name.c_str(), mgr.in(), pols);
42 void
43 activatePOAs()
45 PortableServer::POAManager_var mgr = root_poa->the_POAManager ();
46 mgr->activate ();
47 mgr = poa_a->the_POAManager ();
48 mgr->activate ();
49 mgr = poa_b->the_POAManager ();
50 mgr->activate ();
51 mgr = poa_c->the_POAManager ();
52 mgr->activate ();
55 int
56 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
58 try {
59 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
60 int delay = 0;
61 bool write_iors = false;
63 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("d:w"));
64 int c;
66 while ((c = get_opts ()) != -1)
67 switch (c)
69 case 'd':
70 delay = ACE_OS::atoi (get_opts.opt_arg ());
71 break;
72 case 'w':
73 write_iors = true;
74 break;
75 default:
76 ACE_DEBUG ((LM_DEBUG,
77 "usage: %s "
78 "-d <seconds to delay before initializing POA> "
79 "-w\n",
80 argv[0]));
81 return 1;
82 break;
85 CORBA::Object_var obj = orb->resolve_initial_references ("RootPOA");
86 root_poa = PortableServer::POA::_narrow (obj.in ());
88 ACE_CString base = ACE_CString ("TestObject");
90 if (!write_iors)
92 ACE_CString status_file = base + ACE_CString(".status");
93 ofstream out(status_file.c_str (), ios_base::app);
94 if (!out.good())
96 ACE_DEBUG ((LM_DEBUG, "server did not get good bit from %s\n", status_file.c_str()));
98 out << ACE_OS::getpid () << endl;
99 out.close ();
102 ACE_OS::sleep (delay);
104 createPOAs (base);
106 PortableServer::Servant_var<Test_i> test_servant = new Test_i;
108 PortableServer::ObjectId_var object_id =
109 PortableServer::string_to_ObjectId (base.c_str());
111 poa_a->activate_object_with_id (object_id.in(), test_servant.in ());
112 obj = poa_a->id_to_reference (object_id.in());
113 Test_var tva = Test::_narrow (obj.in());
115 poa_b->activate_object_with_id (object_id.in(), test_servant.in ());
116 obj = poa_b->id_to_reference (object_id.in());
117 Test_var tvb = Test::_narrow (obj.in());
119 poa_c->activate_object_with_id (object_id.in(), test_servant.in ());
120 obj = poa_c->id_to_reference (object_id.in());
121 Test_var tvc = Test::_narrow (obj.in());
123 if (write_iors)
125 ACE_DEBUG ((LM_DEBUG, "(%P) writing IOR files\n"));
126 CORBA::String_var iorstr = orb->object_to_string (tva.in ());
127 ACE_CString filename = base + "_a.ior";
128 FILE *iorf = ACE_OS::fopen (filename.c_str(), "w");
129 ACE_OS::fprintf (iorf, "%s", iorstr.in ());
130 ACE_OS::fclose (iorf);
132 iorstr = orb->object_to_string (tvb.in ());
133 filename = base + "_b.ior";
134 iorf = ACE_OS::fopen (filename.c_str(), "w");
135 ACE_OS::fprintf (iorf, "%s", iorstr.in ());
136 ACE_OS::fclose (iorf);
138 iorstr = orb->object_to_string (tvc.in ());
139 filename = base + "_c.ior";
140 iorf = ACE_OS::fopen (filename.c_str(), "w");
141 ACE_OS::fprintf (iorf, "%s", iorstr.in ());
142 ACE_OS::fclose (iorf);
145 activatePOAs ();
147 ACE_DEBUG ((LM_DEBUG, "Started Server %s \n", base.c_str()));
149 orb->run();
150 root_poa->destroy(1,1);
151 orb->destroy();
153 catch(const CORBA::Exception& ex) {
154 ex._tao_print_exception ("Server main()");
155 return 1;
158 return 0;