summaryrefslogtreecommitdiff
path: root/TAO/tests/MProfile_Forwarding/Manager.cpp
blob: 7336bc42523e899112bb1ca63b6a193f5158de26 (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
//$Id$
#include "Manager.h"

#include "tao/IORManipulation/IORManip_Loader.h"

#include "ace/SString.h"
#include "ace/Get_Opt.h"
#include "ace/OS_NS_stdio.h"

const char *first_ior = 0;
const char *second_ior = 0;
const char *third_ior = 0;
const char *ior_output_file = 0;

int
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, "a:b:c:d:");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'a':
        first_ior = get_opts.opt_arg ();
        break;
      case 'b':
        second_ior = get_opts.opt_arg ();
        break;
      case 'c':
        third_ior = get_opts.opt_arg ();
        break;
      case 'd':
        ior_output_file = get_opts.opt_arg ();
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-a <iorfile>"
                           "-b <iorfile>"
                           "-c <iorfile>"
                           "\n",
                           argv [0]),
                          -1);
      }
  // Indicates sucessful parsing of the command line
  return 0;
}


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

  Manager manager;

  ACE_TRY
    {
      // Initilaize the ORB, POA etc.
      manager.init (argc,
                    argv
                    ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (parse_args (argc, argv) == -1)
        return -1;

      manager.activate_servant (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      manager.make_iors_register (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      manager.run (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Caught");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}

Manager::Manager (void)
  :orb_ (0),
   new_poa_var_ (0)
{
  //no-op
}

int
Manager::init (int argc,
               char *argv[]
               ACE_ENV_ARG_DECL)
{
  this->orb_ = CORBA::ORB_init (argc,
                                argv,
                                0
                                ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  // Obtain the RootPOA.
  CORBA::Object_var obj_var =
    this->orb_->resolve_initial_references ("RootPOA" ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  // Get the POA_var object from Object_var.
  PortableServer::POA_var root_poa_var =
    PortableServer::POA::_narrow (obj_var.in () ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  // Get the POAManager of the RootPOA.
  PortableServer::POAManager_var poa_manager_var =
    root_poa_var->the_POAManager (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  poa_manager_var->activate (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  // Policies for the childPOA to be created.
  CORBA::PolicyList policies (4);
  policies.length (4);

  // The next two policies are common to both
  // Id Assignment Policy
  policies[0] =
    root_poa_var->create_id_assignment_policy (PortableServer::USER_ID
                                               ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  // Lifespan policy
  policies[1] =
    root_poa_var->create_lifespan_policy (PortableServer::PERSISTENT
                                          ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  // Tell the POA to use a servant manager
  policies[2] =
    root_poa_var->create_request_processing_policy (PortableServer::USE_SERVANT_MANAGER
                                                    ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  // Servant Retention Policy -> Use a locator
  policies[3] =
    root_poa_var->create_servant_retention_policy (PortableServer::NON_RETAIN
                                                   ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  ACE_CString name = "newPOA";

  new_poa_var_ =
    root_poa_var->create_POA (name.c_str (),
                              poa_manager_var.in (),
                              policies
                              ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  // Creation of childPOAs is over. Destroy the Policy objects.
  for (CORBA::ULong i = 0;
       i < policies.length ();
       ++i)
    {
      CORBA::Policy_ptr policy = policies[i];
      policy->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);
    }

  return 0;
}


int
Manager::activate_servant (ACE_ENV_SINGLE_ARG_DECL)
{

  ACE_NEW_THROW_EX (this->servant_locator_,
                    Servant_Locator (this->orb_.in ()),
                    CORBA::NO_MEMORY ());
  ACE_CHECK_RETURN (-1);

  this->servant_locator_var_ = this->servant_locator_;

  // Set ServantLocator object as the servant Manager of
  // secondPOA.
  this->new_poa_var_->set_servant_manager (this->servant_locator_
                                           ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  // Try to create a reference with user created ID in new_poa
  // which uses ServantLocator.

  PortableServer::ObjectId_var second_foo_oid_var =
    PortableServer::string_to_ObjectId ("Simple_Server");

  this->new_manager_ior_ =
    new_poa_var_->create_reference_with_id (second_foo_oid_var.in (),
                                            "IDL:Simple_Server:1.0" ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  return 0;
}


int
Manager::make_iors_register (ACE_ENV_SINGLE_ARG_DECL)
{
  // First  server
  CORBA::Object_var object_primary =
    this->orb_->string_to_object (first_ior
                                  ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  //Second server
  CORBA::Object_var object_secondary =
    this->orb_->string_to_object (second_ior
                                  ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  if (third_ior == 0)
    ACE_DEBUG ((LM_DEBUG,
                "Here is the culprit \n"));
  // Third Server
  CORBA::Object_var object_tertiary =
    this->orb_->string_to_object (third_ior
                                  ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  // Get an object reference for the ORBs IORManipultion object!
  CORBA::Object_ptr IORM =
    this->orb_->resolve_initial_references (TAO_OBJID_IORMANIPULATION,
                                            0
                                            ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  TAO_IOP::TAO_IOR_Manipulation_ptr iorm =
    TAO_IOP::TAO_IOR_Manipulation::_narrow (IORM ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);


  // Create the list
  TAO_IOP::TAO_IOR_Manipulation::IORList iors (3);
  iors.length(3);
  iors [0] = CORBA::Object::_duplicate (object_primary.in ());
  iors [1] = CORBA::Object::_duplicate (object_secondary.in ());
  iors [2] = CORBA::Object::_duplicate (this->new_manager_ior_.in ());

  // Create a merged set 1;
  CORBA::Object_var merged_set1 =
    iorm->merge_iors (iors ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  if (object_secondary.in () == 0)
    ACE_DEBUG ((LM_DEBUG,
                " There is a problem 1 "));

  if (object_tertiary.in () == 0)
    ACE_DEBUG ((LM_DEBUG,
                " There is a problem \n"));

  TAO_IOP::TAO_IOR_Manipulation::IORList iors_again (3);
  iors_again.length(3);
  iors_again [0] = CORBA::Object::_duplicate (object_secondary.in ());
  iors_again [1] = CORBA::Object::_duplicate (object_tertiary.in ());
  iors_again [2] = CORBA::Object::_duplicate (this->new_manager_ior_.in ());

  // Create merged set 2
  CORBA::Object_var merged_set2 =
    iorm->merge_iors (iors_again ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  CORBA::String_var iorref1 =
    this->orb_->object_to_string (merged_set1.in ());

  if (ior_output_file != 0)
    {
      FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
      if (output_file == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Cannot open output file for writing IOR: %s",
                           ior_output_file),
                          1);
      ACE_OS::fprintf (output_file, "%s", iorref1.in ());
      ACE_OS::fclose (output_file);
    }

  this->servant_locator_->set (merged_set2);

  return 0;
}


int
Manager::run (ACE_ENV_SINGLE_ARG_DECL)
{
  this->orb_->run (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  return 0;
}