Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Servant_To_Reference_Test / server.cpp
blob47308260ac28d15e2f6f4290be6dd8d8cf9b7888
1 #include "Test_i.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/Task.h"
6 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
8 class MT_Task : public ACE_Task_Base
10 public:
11 MT_Task (PortableServer::POA_ptr p,
12 PortableServer::Servant one,
13 PortableServer::Servant two,
14 PortableServer::Servant three)
15 : p_ (PortableServer::POA::_duplicate (p))
16 , one_ (one)
17 , two_ (two)
18 , three_ (three)
22 int svc (void);
24 private:
25 PortableServer::POA_var p_;
26 PortableServer::Servant one_;
27 PortableServer::Servant two_;
28 PortableServer::Servant three_;
31 int
32 MT_Task::svc (void)
35 try
37 for (CORBA::Long i = 0;
38 i != 2000;
39 ++i)
41 CORBA::Object_var one_ref =
42 this->p_->servant_to_reference (this->one_);
44 CORBA::Object_var two_ref =
45 this->p_->servant_to_reference (this->two_);
47 CORBA::Object_var three_ref =
48 this->p_->servant_to_reference (this->three_);
51 catch (const CORBA::Exception& ex)
53 ex._tao_print_exception ("(%P|%t) Caugh exception\n");
56 return 0;
59 int
60 parse_args (int argc, ACE_TCHAR *argv[])
62 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
63 int c;
65 while ((c = get_opts ()) != -1)
66 switch (c)
68 case 'o':
69 ior_output_file = get_opts.opt_arg ();
70 break;
72 case '?':
73 default:
74 ACE_ERROR_RETURN ((LM_ERROR,
75 "usage: %s "
76 "-o <iorfile>"
77 "\n",
78 argv [0]),
79 -1);
81 // Indicates successful parsing of the command line
82 return 0;
85 int
86 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
88 try
90 CORBA::ORB_var orb =
91 CORBA::ORB_init (argc, argv);
93 CORBA::Object_var poa_object =
94 orb->resolve_initial_references("RootPOA");
96 PortableServer::POA_var root_poa =
97 PortableServer::POA::_narrow (poa_object.in ());
99 if (CORBA::is_nil (root_poa.in ()))
100 ACE_ERROR_RETURN ((LM_ERROR,
101 " (%P|%t) Panic: nil RootPOA\n"),
104 PortableServer::POAManager_var poa_manager =
105 root_poa->the_POAManager ();
107 if (parse_args (argc, argv) != 0)
108 return 1;
110 One_Impl *one_impl = 0;
111 ACE_NEW_RETURN (one_impl,
112 One_Impl (orb.in ()),
114 Two_Impl *two_impl = 0;
115 ACE_NEW_RETURN (two_impl,
116 Two_Impl (orb.in ()),
119 Three_Impl *three_impl = 0;
120 ACE_NEW_RETURN (three_impl,
121 Three_Impl (orb.in ()),
124 PortableServer::ServantBase_var owner_transfer1 (one_impl);
125 PortableServer::ServantBase_var owner_transfer2 (two_impl);
126 PortableServer::ServantBase_var owner_transfer3 (three_impl);
128 CORBA::PolicyList policies; // Empty policy list.
130 PortableServer::POA_var first_poa =
131 root_poa->create_POA ("first POA",
132 poa_manager.in (),
133 policies);
135 PortableServer::ObjectId_var oid1 =
136 first_poa->activate_object (one_impl);
138 PortableServer::ObjectId_var oid2 =
139 first_poa->activate_object (two_impl);
141 PortableServer::ObjectId_var oid3 =
142 first_poa->activate_object (three_impl);
144 // Output the IOR to the <ior_output_file>
145 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
146 if (output_file == 0)
147 ACE_ERROR_RETURN ((LM_ERROR,
148 "Cannot open output file for writing IOR: %s\n",
149 ior_output_file),
151 ACE_OS::fprintf (output_file, "%s", "started");
152 ACE_OS::fclose (output_file);
154 MT_Task task (first_poa.in (),
155 one_impl,
156 two_impl,
157 three_impl);
159 if (task.activate (THR_NEW_LWP | THR_JOINABLE,
160 32) != 0)
161 ACE_ERROR_RETURN ((LM_ERROR,
162 "Cannot activate threads\n"),
165 task.thr_mgr ()->wait ();
167 poa_manager->activate ();
169 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - test finished\n"));
171 root_poa->destroy (1, 1);
173 orb->destroy ();
175 catch (const CORBA::Exception& ex)
177 ex._tao_print_exception ("Exception caught:");
178 return 1;
181 return 0;