Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / tests / ImplRepo / scale / server_i.cpp
blobe2b30ccbff46c5b7514acbfc671c4892f919164f
1 #include "server_i.h"
3 #include "tao/IORTable/IORTable.h"
4 #include "tao/PortableServer/Root_POA.h"
5 #include "tao/ImR_Client/ImR_Client.h"
7 #include "ace/Get_Opt.h"
8 #include "ace/Read_Buffer.h"
9 #include "ace/streams.h"
11 class test_i
12 : public virtual POA_test
14 int n_;
15 CORBA::ORB_var orb_;
16 public:
17 test_i (CORBA::ORB_ptr orb)
18 : n_(0)
19 , orb_(CORBA::ORB::_duplicate(orb))
22 virtual ~test_i () {
24 virtual CORBA::Long get (void)
26 ++n_;
27 CORBA::Object_var obj = orb_->resolve_initial_references("POACurrent");
28 PortableServer::Current_var cur = PortableServer::Current::_narrow(obj.in());
29 ACE_ASSERT(! CORBA::is_nil(cur.in()));
30 PortableServer::POA_var poa = cur->get_POA();
31 CORBA::String_var poaname = poa->the_name();
33 ACE_DEBUG((LM_DEBUG, "%s: get() %d\n", poaname.in(), n_));
34 return n_;
38 // The server name of the Aiprlane Server
39 static const char DEFAULT_SERVER_NAME[] = "TestObject";
41 Server_i::Server_i (void)
42 : server_name_(DEFAULT_SERVER_NAME)
43 , count_(1)
47 Server_i::~Server_i()
51 int
52 Server_i::parse_args (int argc, ACE_TCHAR* argv[])
54 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("p:c:h"));
55 int c;
57 while ((c = get_opts ()) != -1)
59 switch (c)
61 case 'p': // prefix for all created objects
62 this->server_name_ = ACE_TEXT_ALWAYS_CHAR(get_opts.opt_arg ());
63 break;
64 case 'c': // Number of imr-ified objects to create.
65 this->count_ = ACE_OS::atoi(get_opts.opt_arg());
66 break;
67 case '?': // display help for use of the server.
68 case 'h':
69 default:
70 ACE_ERROR_RETURN ((LM_ERROR,
71 "usage: %s"
72 " [-c] <count=1> ImR-ified objects."
73 " [-p] <prefix=%s> To all ImR-ified object names."
74 "\n",
75 argv[0],
76 DEFAULT_SERVER_NAME),1);
80 return 0;
83 namespace
85 ACE_CString toStr(int n)
87 char buf[20];
88 return ACE_OS::itoa(n, buf, 10);
92 int
93 Server_i::init (int argc, ACE_TCHAR** argv)
95 try
97 this->orb_ = CORBA::ORB_init (argc, argv);
99 int retval = this->parse_args (argc, argv);
100 if (retval != 0)
101 return retval;
103 CORBA::Object_var obj =
104 this->orb_->resolve_initial_references ("RootPOA");
105 this->root_poa_ =
106 PortableServer::POA::_narrow (obj.in ());
107 ACE_ASSERT(! CORBA::is_nil(this->root_poa_.in()));
109 PortableServer::POAManager_var poa_manager =
110 this->root_poa_->the_POAManager ();
112 obj = this->orb_->resolve_initial_references ("IORTable");
113 IORTable::Table_var ior_table =
114 IORTable::Table::_narrow (obj.in ());
115 ACE_ASSERT(! CORBA::is_nil(ior_table.in()));
117 // If -orbuseimr 1 is specified then all persistent poas will be
118 // registered with the imr.
119 CORBA::PolicyList policies (2);
120 policies.length (2);
121 policies[0] = this->root_poa_->create_id_assignment_policy (PortableServer::USER_ID);
122 policies[1] = this->root_poa_->create_lifespan_policy (PortableServer::PERSISTENT);
124 test_i* test_svt;
125 ACE_NEW_RETURN (test_svt, test_i(orb_.in()), -1);
126 PortableServer::ServantBase_var scoped_svt(test_svt);
128 PortableServer::ObjectId_var server_id =
129 PortableServer::string_to_ObjectId ("Test");
131 // Create count_ POAs, activate an object in each, and register the object with
132 // the IORTable.
133 for (int i = 0; i < this->count_; ++i)
135 ACE_CString name = this->server_name_ + "_" + toStr(i);
137 PortableServer::POA_var poa =
138 this->root_poa_->create_POA (name.c_str(),
139 poa_manager.in (),
140 policies);
142 poa->activate_object_with_id (server_id.in (), test_svt);
144 TAO_Root_POA* tmp_poa = dynamic_cast<TAO_Root_POA*>(poa.in());
145 obj = tmp_poa->id_to_reference_i (server_id.in (), false);
147 CORBA::String_var ior = this->orb_->object_to_string (obj.in ());
149 ior_table->bind (name.c_str(), ior.in ());
152 policies[0]->destroy();
153 policies[1]->destroy();
155 catch (const CORBA::Exception& ex)
157 ex._tao_print_exception ("Server_i::init");
158 throw;
162 return 0;
166 Server_i::run (void)
170 PortableServer::POAManager_var poa_manager =
171 this->root_poa_->the_POAManager ();
173 poa_manager->activate ();
175 // We have potentially lots of IORs, so just write out a simple text
176 // file that the run_test.pl can use to know we're done.
178 ACE_CString status = this->server_name_ + ACE_CString(".status");
179 ofstream out(status.c_str());
180 out << "started" << endl;
183 ACE_DEBUG ((LM_DEBUG,
184 "\n Started Server %s with %d imr-ified objects.\n\n",
185 this->server_name_.c_str(),
186 this->count_));
188 this->orb_->run ();
190 this->root_poa_->destroy(1, 1);
191 this->orb_->destroy();
193 catch (const CORBA::Exception& ex)
195 ex._tao_print_exception ("Server_i::run");
196 throw;
199 return 0;