summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/Time_Service/Server_i.cpp
blob: 8a1f78f6fa4bb474a3635bd039b09de2870656cd (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// $Id$

#include "ace/Get_Opt.h"
#include "Server_i.h"

ACE_RCSID(Time_Service, Server_i, "$Id$")

// Constructor.
Server_i::Server_i (void)
  : ior_output_file_ (0),
    use_ir_ (0)
{
  // no-op.
}

// Destructor.

Server_i::~Server_i (void)
{
  // no-op.
}

// Parse the command-line arguments and set options.

int
Server_i::parse_args (void)
{
  ACE_Get_Opt get_opts (this->argc_, this->argv_, "do:i");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'd':  // debug flag.
        TAO_debug_level++;
        break;
      case 'o':  // output the IOR to a file.
        this->ior_output_file_ =
          ACE_OS::fopen (get_opts.optarg, "a");

        if (this->ior_output_file_ == 0)
          ACE_ERROR_RETURN ((LM_ERROR,
                             "[SERVER] Process/Thread Id : (%P/%t)Unable to open %s for writing: %p\n",
                             get_opts.optarg), -1);
        break;
      case 'i': // Use the Implementation Repository.
        this->use_ir_ = 1;
        break;
      case '?':  // display help for use of the server.
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "[SERVER] Process/Thread Id : (%P/%t)"
                           "usage:  %s"
                           " [-d]"
                           " [-o] <ior_output_file>"
                           " [-i] <Use the Implementation Repository>"
                           " [-r] <Register with the Implementation Repository>"
                           "\n",
                           argv_ [0]),
                          1);
      }

  // Indicates successful parsing of command line.
  return 0;
}

// Initialise the Naming Service and register the TimeService Object
// with it.

int
Server_i::init_naming_service (CORBA::Environment &ACE_TRY_ENV)
{
  ACE_TRY
    {
      // Initialize the POA.
      this->orb_manager_.init_child_poa (this->argc_,
                                         this->argv_,
                                         "time_server",
                                         ACE_TRY_ENV);
      ACE_TRY_CHECK;

      PortableServer::POA_ptr child_poa
        = this->orb_manager_.child_poa ();

      // Initialize the Naming Server. Note the Naming Server cannot
      // be initialized with the Root POA because it has to be a
      // persistent object reference.  Hence the need for child
      // POA. The servants need not be registered in the same POA. We
      // use the Root POA for the servants.

      if (this->my_name_server_.init (this->orb_.in (),
                                      child_poa) == -1)
        return -1;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "(%P|%t) Exception from init_naming_service ()\n");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}

// Initialize the Implementation Repository.

int
Server_i::init_IR (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      if (this->use_ir_ == 1)
        {
          ACE_NEW_RETURN (this->ir_helper_,
                          IR_Helper ("time_server",
                                     this->orb_manager_.child_poa (),
                                     this->orb_manager_.orb (),
                                     TAO_debug_level),
                          -1);

          this->ir_helper_->change_object (this->time_service_server_.in (),
                                           ACE_TRY_ENV);
        }

      ACE_TRY_CHECK;

      // Convert the IR server reference to a string.
      CORBA::String_var objref_server =
        this->orb_manager_.orb ()->object_to_string (this->time_service_server_.in (),
                                                     ACE_TRY_ENV);

      ACE_TRY_CHECK;

      // Print the IR server IOR on the console.
      ACE_DEBUG ((LM_DEBUG,
                  "[SERVER] Process/Thread : (%P/%t) The Time Service IREPO SERVER IOR is: <%s>\n",
                  objref_server.in ()));

      if (this->ior_output_file_)
        {
                // Write the IOR to the file.
                ACE_OS::fprintf (this->ior_output_file_,
                                 "%s\n",
                                 objref_server.in ());
                ACE_OS::fclose (this->ior_output_file_);
        }
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Exception:");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}

// Create a new time server object and register it with the child POA.
// Print the IOR of the registered server on the console and in a file.

int
Server_i::create_server (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {

      // Create a new server object.
      ACE_NEW_RETURN (this->time_service_server_impl_,
                      TAO_Time_Service_Server(this->use_ir_),
                      0);

      // Generate IOR of the <TimeService Server> and register with
      // POA.
      //this->time_service_server_ =
      //time_service_server_impl_->_this ();

      // Register a servant with the child poa.

      CORBA::String_var server_str =
        this->orb_manager_.activate_under_child_poa ("server",
                                                     this->time_service_server_impl_,
                                                     ACE_TRY_ENV);
      ACE_TRY_CHECK;

      PortableServer::ObjectId_var id =
        PortableServer::string_to_ObjectId ("server");

      CORBA::Object_var server_ref =
        this->orb_manager_.child_poa ()->id_to_reference (id.in (),
                                                          ACE_TRY_ENV);

      this->time_service_server_ = CosTime::TimeService::_narrow (server_ref.in (),
                                                                  ACE_TRY_ENV);


      ACE_TRY_CHECK;

      // All this !! just to register a servant with the child poa.
      // Instead of using _this ().

      //Convert the server reference to a string.

      CORBA::String_var objref_server =
        this->orb_->object_to_string (server_ref.in (),
                                      ACE_TRY_ENV);

      // Print the server IOR on the console.
      ACE_DEBUG ((LM_DEBUG,
                  "[SERVER] Process/Thread Id : (%P/%t) The Time Service "
                  "SERVER IOR without Implementation Repository is: <%s>\n",
                   objref_server.in ()));

      // Print the IOR to a file if we are not using the Implementation Repository.
      // If we are using the implementation repository then the 'changed IOR' is
      // written to a file inside the init_IR ().

      if ((this->ior_output_file_) && (this->use_ir_ == 0))
        {
          // Write the IOR to the file.
          ACE_OS::fprintf (this->ior_output_file_,
                           "%s\n",
                           objref_server.in ());
          ACE_OS::fclose (this->ior_output_file_);
        }
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception in Server_i::create_server ()");
      return -1;
    }
  ACE_ENDTRY;
  return 0;
}


// This function checks if this is the first server being executed. If yes,
// the call to resolve raises an exception and 1 is returned. If there is
// no exception 0 is returned.

int
Server_i::if_first_server (CosNaming::Name &server_context_name)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      this->my_name_server_->resolve
        (server_context_name, ACE_TRY_ENV);
      ACE_TRY_CHECK;
    }
  ACE_CATCH (CORBA::UserException, userex)
    {
      ACE_UNUSED_ARG (userex);
      return 1;
    }
  ACE_ENDTRY;
  return 0;
}

// Bind the Server in the context 'ServerContext' with the name
// 'Server:<hostname>'.

int
Server_i::register_server (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      CosNaming::Name server_context_name;
      server_context_name.length (1);
      server_context_name[0].id = CORBA::string_dup ("ServerContext");

      CosNaming::NamingContext_var server_context;

      // If this is the first server then get a new Naming Context
      // and bind it to the Naming Server.

      if (if_first_server (server_context_name))
        {
          // Get context.
          server_context =
            this->my_name_server_->new_context (ACE_TRY_ENV);
          ACE_TRY_CHECK;

          // Bind.
          this->my_name_server_->rebind_context (server_context_name,
                                                 server_context.in (),
                                                 ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }

      char host_name[MAXHOSTNAMELEN];
      char server_mc_name[MAXHOSTNAMELEN];
      ACE_OS::hostname (host_name,MAXHOSTNAMELEN);

      CosNaming::Name server_name (server_context_name);

      server_name.length (2);
      strcpy (server_mc_name, "Server:");
      strcat (server_mc_name, host_name);
      server_name[1].id = CORBA::string_dup (server_mc_name);

      // Bind the compound name (ServerContext(Server:<hostname>))
      // to the Naming Server.

      this->my_name_server_->rebind (server_name,
                                     this->time_service_server_.in (),
                                     ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG,
                  "Binding ServerContext -> %s\n",
                  server_name[1].id.in ()));
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "(%P|%t) Exception from init_naming_service ()\n");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}

// Initialize the server. If a filename is specified with the -f
// commandline option, the server writes its IOR to the file besides
// binding itself with the Naming Service.

int
Server_i::init (int argc,
                char *argv[],
                CORBA::Environment &ACE_TRY_ENV)
{
  this->argc_ = argc;
  this->argv_ = argv;

  ACE_TRY
    {

      // Call the init of <TAO_ORB_Manager> to initialize the ORB and
      // create a child POA under the root POA.

      if (this->orb_manager_.init_child_poa (argc,
                                             argv,
                                             "time_server",
                                             ACE_TRY_ENV) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                                 "%p\n",
                                 "init_child_poa"),
                                -1);
      ACE_TRY_CHECK;

      int result = this->parse_args ();

      if (result != 0)
        return result;

      // Get the orb.
      this->orb_ = this->orb_manager_.orb ();

      // Use the Naming Service Register the above implementation with the Naming Service.
      if (this->use_ir_ == 0)
        this->init_naming_service (ACE_TRY_ENV);

      ACE_TRY_CHECK;

      // Create the server object.
      this->create_server ();

      // Initialize the IR.
      if (this->use_ir_ == 1)
        this->init_IR ();

      // Register the server object with the Naming Service.
      if (this->use_ir_ == 0)
        this->register_server ();

    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Exception:");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}


// Initialize the IR Helper and run the event loop for ORB.

int
Server_i::run (CORBA::Environment &ACE_TRY_ENV)
{
  ACE_TRY
    {

      if (this->use_ir_ == 1)
        {
          this->ir_helper_->notify_startup (ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }

      if (this->orb_manager_.run (ACE_TRY_ENV) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "[SERVER] Process/Thread Id : (%P/%t) Server_i::run"),
                          -1);

      if (this->use_ir_ == 1)
        {
          this->ir_helper_->notify_shutdown (ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "Exception:");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}