Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / Bug_3647_Regression / middle_server.cpp
blob28fc2dcc3826f628d676659ec4075bb9425fb9b4
1 // -*- C++ -*-
2 #include "Middle_Impl.h"
4 #include "tao/Utils/PolicyList_Destroyer.h"
5 #include "tao/Utils/RIR_Narrow.h"
6 #include "tao/Strategies/advanced_resource.h"
7 #include "tao/Messaging/Messaging.h"
8 #include "tao/AnyTypeCode/Any.h"
9 #include "ace/Get_Opt.h"
10 #include "ace/OS_NS_stdio.h"
12 bool verbose = false;
13 const ACE_TCHAR *ior_output_file = ACE_TEXT ("middle.ior");
14 const ACE_TCHAR *ior = ACE_TEXT ("file://backend.ior");
15 Messaging::SyncScope scope = Messaging::SYNC_NONE;
16 long timeout = 2;
18 void
19 usage(ACE_TCHAR const *cmd,
20 ACE_TCHAR const *msg)
22 ACE_ERROR ((LM_ERROR,
23 ACE_TEXT("Usage: %s ")
24 ACE_TEXT("-v ")
25 ACE_TEXT("-k <ior> ")
26 ACE_TEXT("-o <iorfile> ")
27 ACE_TEXT("-s <NONE|TRANSPORT|SERVER|TARGET|DELAYED> ")
28 ACE_TEXT("-t timeout ")
29 ACE_TEXT("\n")
30 ACE_TEXT(" %s\n"),
31 cmd, msg));
34 int
35 parse_args (int argc, ACE_TCHAR *argv[])
37 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("vo:k:s:t:"));
38 int c;
39 ACE_TCHAR const *sname = ACE_TEXT("NONE");
40 ACE_TCHAR const *stimeout = 0;
42 while ((c = get_opts ()) != -1)
43 switch (c)
45 case 'v':
46 verbose = true;
47 break;
49 case 'o':
50 ior_output_file = get_opts.opt_arg ();
51 break;
53 case 'k':
54 ior = get_opts.opt_arg ();
55 break;
57 case 's':
58 sname = get_opts.opt_arg ();
59 break;
61 case 't':
62 stimeout = get_opts.opt_arg();
63 break;
65 case '?':
66 default:
67 usage(argv[0], ACE_TEXT("unknown argument"));
68 return -1;
71 if (ACE_OS::strcmp(sname, ACE_TEXT("NONE")) == 0) {
72 scope = Messaging::SYNC_NONE;
73 } else if (ACE_OS::strcmp(sname, ACE_TEXT("TRANSPORT")) == 0) {
74 scope = Messaging::SYNC_WITH_TRANSPORT;
75 } else if (ACE_OS::strcmp(sname, ACE_TEXT("SERVER")) == 0) {
76 scope = Messaging::SYNC_WITH_SERVER;
77 } else if (ACE_OS::strcmp(sname, ACE_TEXT("TARGET")) == 0) {
78 scope = Messaging::SYNC_WITH_TARGET;
79 } else if (ACE_OS::strcmp(sname, ACE_TEXT("DELAYED")) == 0) {
80 scope = TAO::SYNC_DELAYED_BUFFERING;
81 } else {
82 usage(argv[0], ACE_TEXT("Invalid scope value"));
83 return -1;
86 if (stimeout != 0)
88 ACE_TCHAR *end;
89 long tmp = ACE_OS::strtol(stimeout, &end, 10);
90 if (end == 0 || *end != '\0')
92 usage(argv[0], ACE_TEXT("Invalid timeout value"));
93 return -1;
95 timeout = tmp;
98 // Indicates successful parsing of the command line
99 return 0;
103 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
107 CORBA::ORB_var orb =
108 CORBA::ORB_init (argc, argv);
110 CORBA::Object_var poa_object =
111 orb->resolve_initial_references("RootPOA");
113 PortableServer::POA_var root_poa =
114 PortableServer::POA::_narrow (poa_object.in ());
116 if (CORBA::is_nil (root_poa.in ()))
118 ACE_ERROR_RETURN ((LM_ERROR,
119 "backend_server(%P|%t) - panic: nil RootPOA\n"),
123 PortableServer::POAManager_var poa_manager =
124 root_poa->the_POAManager ();
126 if (parse_args (argc, argv) != 0)
127 return 1;
129 CORBA::Object_var tmp = orb->string_to_object(ior);
131 // one second in TimeT units
132 TimeBase::TimeT const second = 10 * TimeBase::TimeT(1000000);
134 CORBA::Any timeout_as_any;
135 timeout_as_any <<= TimeBase::TimeT(timeout * second);
137 CORBA::Any scope_as_any;
138 scope_as_any <<= scope;
140 TAO::Utils::PolicyList_Destroyer plist(1);
141 plist.length(2);
142 plist[0] =
143 orb->create_policy(Messaging::RELATIVE_RT_TIMEOUT_POLICY_TYPE,
144 timeout_as_any);
145 plist[1] =
146 orb->create_policy(Messaging::SYNC_SCOPE_POLICY_TYPE,
147 scope_as_any);
149 CORBA::PolicyCurrent_var policy_current =
150 TAO::Utils::RIR_Narrow<CORBA::PolicyCurrent>::narrow(
151 orb.in (),
152 "PolicyCurrent");
154 policy_current->set_policy_overrides(
155 plist, CORBA::ADD_OVERRIDE);
157 Bug_3647_Regression::Backend_var backend =
158 Bug_3647_Regression::Backend::_narrow(tmp.in ());
160 if (CORBA::is_nil (backend.in ()))
162 ACE_ERROR_RETURN ((LM_DEBUG,
163 "middle_server(%P|%t) - nil backend reference <%s>\n",
164 ior),
168 using namespace Bug_3647_Regression;
169 PortableServer::ServantBase_var impl(
170 new Middle_Impl(backend.in(), orb.in(), verbose));
172 PortableServer::ObjectId_var id =
173 root_poa->activate_object (impl.in());
175 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
178 Bug_3647_Regression::Middle_var middle =
179 Bug_3647_Regression::Middle::_narrow (object.in ());
181 CORBA::String_var ior = orb->object_to_string (middle.in ());
183 // Output the IOR to the <ior_output_file>
184 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
185 if (output_file == 0)
186 ACE_ERROR_RETURN ((LM_ERROR,
187 "middle_server(%P|%t) - Cannot open output file "
188 "for writing IOR: %s\n",
189 ior_output_file),
191 ACE_OS::fprintf (output_file, "%s", ior.in ());
192 ACE_OS::fclose (output_file);
194 poa_manager->activate ();
196 orb->run ();
198 ACE_DEBUG ((LM_DEBUG,
199 "middle_server(%P|%t) - event loop finished\n"));
201 root_poa->destroy (true, true);
203 orb->destroy ();
205 catch (const CORBA::Exception& ex)
207 ACE_DEBUG ((LM_DEBUG,
208 "middle_server"));
209 ex._tao_print_exception ("Exception caught:");
210 return 1;
213 return 0;