summaryrefslogtreecommitdiff
path: root/TAO/tests/POA/EndpointPolicy/server.cpp
blob: b9a84c2470495044e6a77b734a43a7fe143b8625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// $Id$

#include "Hello.h"
#include "tao/EndpointPolicy/EndpointPolicy.h"
#include "tao/EndpointPolicy/IIOPEndpointValue_i.h"
#include "tao/PI_Server/PI_Server.h"
#include "tao/ORB_Constants.h"
#include "ace/OS_NS_strings.h"
#include "ace/OS_NS_stdio.h"

ACE_RCSID (Hello,
           server,
           "$Id$")

const char *good_ior_file = "good.ior";
const char *bad_ior_file = "bad.ior";
const char *root_ior_file = "root.ior";

CORBA::Short endpoint_port = 12345;
int verbose = 0;

int
parse_args (int argc, char *argv[])
{
  for (int c = 1; c < argc; c++) {
    if (ACE_OS::strcasecmp(argv[c],"-g") == 0)
      {
        good_ior_file = argv[++c];
      }
    else if (ACE_OS::strcasecmp(argv[c],"-b") == 0)
      {
        bad_ior_file = argv[++c];
      }
    else if (ACE_OS::strcasecmp(argv[c],"-p") == 0)
      {
        endpoint_port = ACE_OS::atoi (argv[++c]);
      }
    else if (ACE_OS::strcasecmp(argv[c],"-v") == 0)
      {
        verbose = 1;
      }
    else if (strstr(argv[c],"-ORB") == argv[c])
      {
        c++;
        continue;
      }
    else
      ACE_ERROR_RETURN ((LM_ERROR,
                         "usage:  %s "
                         "-g <goodiorfile> "
                         "-b <badiorfile> "
                         "-p <port> "
                         "-v "
                         "\n",
                         argv [0]),
                        -1);
  }
  // Indicates sucessful parsing of the command line
  return 0;
}

void
add_endpoint_args(int &argc, char**argv, char** &largv)
{
  largv = new char*[argc+4];
  for (int i = 0; i < argc; i++)
    largv[i] = argv[i];
  largv[argc++] = "-ORBEndpoint";
  largv[argc] = new char[100];
  ACE_OS::sprintf (largv[argc++],"iiop://localhost:%d",endpoint_port);
  largv[argc++] = "-ORBEndpoint";
  largv[argc] = new char[100];
  ACE_OS::sprintf (largv[argc++],"iiop://localhost:%d/hostname_in_ior=unreachable",endpoint_port+1);
}

int
main (int argc, char *argv[])
{

  CORBA::ORB_var orb;
  CORBA::Object_var obj;
  PortableServer::POA_var root_poa;
  PortableServer::POAManagerFactory_var poa_manager_factory;

  try
    {
      if (parse_args (argc, argv) != 0)
        return 1;
      char **largv;
      add_endpoint_args(argc,argv, largv);
      orb =
        CORBA::ORB_init (argc, largv, "EndpointPolicy");

      obj =
        orb->resolve_initial_references("RootPOA");

      root_poa =
        PortableServer::POA::_narrow (obj.in ());

      if (CORBA::is_nil (root_poa.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Panic: nil RootPOA\n"),
                          1);

      poa_manager_factory
        = root_poa->the_POAManagerFactory ();
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception("initialization error ");
      return 1;
    }

  // Currently, only single ENDPOINT_POLICY_TYPE policy is supported in
  // the POAManager. A PolicyError exception with BAD_POLICY reason is
  // raised if the POAManager has multiple policies.
  bool policy_error_test = false;
  CORBA::PolicyList policies;

  try
    {
      policies.length (2);
      PortableServer::POAManager_var poa_manager
        = poa_manager_factory->create_POAManager ("wrongPOAManager",
                                                  policies);
    }
  catch (CORBA::PolicyError &ex)
    {
      if (ex.reason == CORBA::BAD_POLICY)
        policy_error_test = true;
    }

  if (! policy_error_test)
    return 1;

  policy_error_test = false;

  // A PolicyError exception with BAD_POLICY reason is raised if
  // the POAManager has other policies.
  try
    {
      policies.length (1);

      CORBA::Any policy_value;
      policy_value <<= PortableServer::ORB_CTRL_MODEL;

      policies[0] = orb->create_policy (PortableServer::THREAD_POLICY_ID,
                                        policy_value);

      PortableServer::POAManager_var poa_manager
        = poa_manager_factory->create_POAManager ("wrongPOAManager",
                                                  policies);
    }
  catch (CORBA::PolicyError &ex)
    {
      if (ex.reason == CORBA::BAD_POLICY)
        policy_error_test = true;
    }

  if (! policy_error_test)
    return 1;

  policy_error_test = false;

#if 0
  // @@@ mesnier_p@ociweb.com
  // This test is currently blocked out due to the vagueries of hostname
  // creation at intermediate stages of endpoint production. See my note
  // in tao/EndpointPolicy/IIOPEndpointValue_i.cpp (validate_acceptor())
  // for more information about this situation.

  // A PolicyError exception with UNSUPPORTED_POLICY_VALUE reason
  // is raised if the Endpoint policy does not contain an endpoint
  // that match any endpoint the ORB is listening on.
  try
    {
      policies.length (1);

      CORBA::ULong port = 0xdead;
      EndpointPolicy::EndpointValueBase_var iiop_endpoint
        = new IIOPEndpointValue_i("localhost", port);

      EndpointPolicy::EndpointList list;
      list.length (1);

      list[0] = iiop_endpoint;

      CORBA::Any policy_value;
      policy_value <<= list;

      policies[0] = orb->create_policy (EndpointPolicy::ENDPOINT_POLICY_TYPE,
                                        policy_value);

      PortableServer::POAManager_var poa_manager
        = poa_manager_factory->create_POAManager ("wrongPOAManager",
                                                  policies);
    }
  catch (CORBA::PolicyError &ex)
    {
      if (ex.reason == CORBA::UNSUPPORTED_POLICY_VALUE)
        policy_error_test = true;
    }

  if (! policy_error_test)
    return 1;
#endif
  //-----------------------------------------------------------------------


  // Create two valid endpoint policies. One to match each of the generated
  // endpoint arguments supplied to ORB_init().
  PortableServer::POAManager_var good_pm;
  PortableServer::POAManager_var bad_pm;

  policies.length (1);

  EndpointPolicy::EndpointValueBase_var iiop_endpoint =
    new IIOPEndpointValue_i("localhost", endpoint_port);

  EndpointPolicy::EndpointList list;
  list.length (1);
  list[0] = iiop_endpoint;

  try
    {
      CORBA::Any policy_value;
      policy_value <<= list;
      policies[0] = orb->create_policy (EndpointPolicy::ENDPOINT_POLICY_TYPE,
                                        policy_value);
      good_pm = poa_manager_factory->create_POAManager ("goodPOAManager",
                                                        policies);
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception ("Failed to create reachable POA manager");
      return 1;
    }

  list[0] = new IIOPEndpointValue_i("unreachable", endpoint_port+1);
  try
    {
      CORBA::Any policy_value;
      policy_value <<= list;
      policies[0] = orb->create_policy (EndpointPolicy::ENDPOINT_POLICY_TYPE,
                                        policy_value);
      bad_pm = poa_manager_factory->create_POAManager ("badPOAManager",
                                                        policies);
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception ("Failed to create unreachable POA manager");
      return 1;
    }

  try
    {
      PortableServer::ObjectId_var oid;
      CORBA::Object_var o = CORBA::Object::_nil();
      FILE *output_file= 0;
      // Create poas assiciated with the each the good poa manager and the
      // bad poa manager.
      policies.length(0);
      PortableServer::POA_var good_poa =
        root_poa->create_POA ("goodPOA",
                              good_pm.in (),
                              policies);

      PortableServer::POA_var bad_poa =
        root_poa->create_POA ("badPOA",
                              bad_pm.in (),
                              policies);

      Hello *hello_impl;
      ACE_NEW_RETURN (hello_impl,
                      Hello (orb.in ()),
                      1);
      PortableServer::ServantBase_var owner_transfer(hello_impl);

      ACE_DEBUG ((LM_DEBUG, "Creating IOR from root poa\n"));

      oid = root_poa->activate_object (hello_impl);
      o = root_poa->id_to_reference (oid.in ());

      CORBA::String_var root_ior =
        orb->object_to_string (o.in ());

      output_file= ACE_OS::fopen (root_ior_file, "w");
      if (output_file == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot open output file for writing IOR: %s",
                           root_ior_file),
                          1);
      ACE_OS::fprintf (output_file, "%s", root_ior.in ());
      ACE_OS::fclose (output_file);

      ACE_DEBUG ((LM_DEBUG, "Creating IOR from bad poa\n"));

      oid = bad_poa->activate_object (hello_impl);
      o = bad_poa->id_to_reference (oid.in());

      CORBA::String_var bad_ior =
        orb->object_to_string (o.in ());

      ACE_DEBUG ((LM_DEBUG, "Creating IOR from good poa\n"));

      oid = good_poa->activate_object (hello_impl);
      o = good_poa->id_to_reference (oid.in ());

      CORBA::String_var good_ior =
        orb->object_to_string (o.in ());

      output_file= ACE_OS::fopen (good_ior_file, "w");
      if (output_file == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot open output file for writing IOR: %s",
                           good_ior_file),
                          1);
      ACE_OS::fprintf (output_file, "%s", good_ior.in ());
      ACE_OS::fclose (output_file);

      output_file= ACE_OS::fopen (bad_ior_file, "w");
      if (output_file == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot open output file for writing IOR: %s",
                           bad_ior_file),
                          1);
      ACE_OS::fprintf (output_file, "%s", bad_ior.in ());
      ACE_OS::fclose (output_file);

      good_pm->activate ();
      bad_pm->activate ();

      orb->run ();
      ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
    }
  catch (CORBA::Exception &ex)
    {
      ex._tao_print_exception ("cannot run server");
    }
  root_poa->destroy (1, 1);

  orb->destroy ();

  return 0;
}