=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / tests / RTCORBA / Client_Protocol / server.cpp
bloba880f583214d190a9ecdae2238173351b0ab820c
1 #include "testS.h"
2 #include "ace/Get_Opt.h"
3 #include "tao/RTCORBA/RTCORBA.h"
4 #include "tao/RTCORBA/RT_Policy_i.h"
5 #include "tao/RTPortableServer/RTPortableServer.h"
6 #include "tao/Strategies/advanced_resource.h"
8 class Test_i : public POA_Test
10 // = TITLE
11 // An implementation for the Test interface in test.idl
13 public:
14 Test_i (CORBA::ORB_ptr orb);
15 // ctor
17 void test_method ();
19 //FUZZ: disable check_for_lack_ACE_OS
20 void shutdown ();
21 //FUZZ: enable check_for_lack_ACE_OS
23 private:
24 CORBA::ORB_var orb_;
25 // The ORB
28 Test_i::Test_i (CORBA::ORB_ptr orb)
29 : orb_ (CORBA::ORB::_duplicate (orb))
33 void
34 Test_i::test_method ()
36 ACE_DEBUG ((LM_DEBUG,
37 "Test method invoked on the sever\n"));
40 void
41 Test_i::shutdown ()
43 ACE_DEBUG ((LM_DEBUG,
44 "(%P|%t) Shutting down\n"));
45 this->orb_->shutdown (false);
48 //*************************************************************************
50 const ACE_TCHAR *ior_output_file1 = ACE_TEXT("test1.ior");
51 const ACE_TCHAR *ior_output_file2 = ACE_TEXT("test2.ior");
52 CORBA::ULong protocol_type = 0;
54 // Parse command-line arguments.
55 int
56 parse_args (int argc, ACE_TCHAR *argv[])
58 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("s:c:p:"));
59 int c, result;
61 while ((c = get_opts ()) != -1)
62 switch (c)
64 case 's':
65 ior_output_file1 = get_opts.opt_arg ();
66 break;
68 case 'c':
69 ior_output_file2 = get_opts.opt_arg ();
70 break;
72 case 'p':
73 result = ::sscanf (ACE_TEXT_ALWAYS_CHAR (get_opts.opt_arg ()),
74 "%u",
75 &protocol_type);
76 if (result == 0 || result == EOF)
77 ACE_ERROR_RETURN ((LM_ERROR,
78 "Unable to process <-p> option"),
79 -1);
80 break;
82 case '?':
83 default:
84 ACE_ERROR_RETURN ((LM_ERROR,
85 "usage: %s "
86 "-s <iorfile> "
87 "-c <iorfile> "
88 "-p <protocol_type> "
89 "\n",
90 argv [0]),
91 -1);
94 return 0;
97 int
98 check_for_nil (CORBA::Object_ptr obj, const char *msg)
100 if (CORBA::is_nil (obj))
101 ACE_ERROR_RETURN ((LM_ERROR,
102 "ERROR: Object reference <%C> is nil\n",
103 msg),
104 -1);
105 else
106 return 0;
110 create_object (PortableServer::POA_ptr poa,
111 CORBA::ORB_ptr orb,
112 Test_i *server_impl,
113 const ACE_TCHAR *filename)
115 // Register with poa.
116 PortableServer::ObjectId_var id =
117 poa->activate_object (server_impl);
119 CORBA::Object_var server =
120 poa->id_to_reference (id.in ());
122 // Print out the IOR.
123 CORBA::String_var ior =
124 orb->object_to_string (server.in ());
126 ACE_DEBUG ((LM_DEBUG, "<%C>\n\n", ior.in ()));
128 // Print ior to the file.
129 if (filename != 0)
131 FILE *output_file= ACE_OS::fopen (filename, "w");
132 if (output_file == 0)
133 ACE_ERROR_RETURN ((LM_ERROR,
134 "Cannot open output file for writing IOR: %s",
135 filename),
136 -1);
137 ACE_OS::fprintf (output_file, "%s", ior.in ());
138 ACE_OS::fclose (output_file);
141 return 0;
145 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
149 // ORB.
150 CORBA::ORB_var orb =
151 CORBA::ORB_init (argc, argv);
153 // Parse arguments.
154 if (parse_args (argc, argv) != 0)
155 return -1;
157 // RTORB.
158 CORBA::Object_var object =
159 orb->resolve_initial_references ("RTORB");
160 RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow (object.in ());
161 if (check_for_nil (rt_orb.in (), "RTORB") == -1)
162 return -1;
164 // RootPOA.
165 object =
166 orb->resolve_initial_references("RootPOA");
167 PortableServer::POA_var root_poa =
168 PortableServer::POA::_narrow (object.in ());
169 if (check_for_nil (root_poa.in (), "RootPOA") == -1)
170 return -1;
172 // POAManager.
173 PortableServer::POAManager_var poa_manager =
174 root_poa->the_POAManager ();
176 // Create child POA with RTCORBA::ClientProtocolPolicy set.
177 RTCORBA::ProtocolList protocols;
178 protocols.length (1);
179 protocols[0].protocol_type = protocol_type;
180 protocols[0].orb_protocol_properties =
181 TAO_Protocol_Properties_Factory::create_orb_protocol_property
182 (protocol_type);
183 protocols[0].transport_protocol_properties =
184 TAO_Protocol_Properties_Factory::create_transport_protocol_property
185 (protocol_type, orb->orb_core ());
187 CORBA::PolicyList poa_policy_list;
188 poa_policy_list.length (1);
189 poa_policy_list[0] =
190 rt_orb->create_client_protocol_policy (protocols);
192 PortableServer::POA_var poa =
193 root_poa->create_POA ("Child_POA",
194 poa_manager.in (),
195 poa_policy_list);
197 // Servant.
198 Test_i server_impl (orb.in ());
200 // Create object 1.
201 int result;
202 ACE_DEBUG ((LM_DEBUG, "\nActivated object one as "));
203 result = create_object (poa.in (), orb.in (), &server_impl,
204 ior_output_file1);
205 if (result == -1)
206 return -1;
208 // Create object 2.
209 ACE_DEBUG ((LM_DEBUG, "\nActivated object two as "));
210 result = create_object (root_poa.in (), orb.in (), &server_impl,
211 ior_output_file2);
212 if (result == -1)
213 return -1;
215 // Run ORB Event loop.
216 poa_manager->activate ();
218 orb->run ();
220 ACE_DEBUG ((LM_DEBUG, "Server ORB event loop finished\n\n"));
222 catch (const CORBA::Exception& ex)
224 ex._tao_print_exception (
225 "Unexpected exception caught in ClientProtocolPolicy: test server");
226 return -1;
229 return 0;