summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/AV/Endpoint_Strategy.cpp
blob: 7de5721a9681c95079418873fefa2fadfa8f63dd (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    cos
//
// = FILENAME
//   Endpoint_Strategy.cpp
//
// = AUTHOR
//    Sumedh Mungee <sumedh@cs.wustl.edu>
//    
//
// ============================================================================

#include "orbsvcs/AV/Endpoint_Strategy.h"

ACE_RCSID(AV, Endpoint_Strategy, "$Id$")

// ----------------------------------------------------------------------
// TAO_AV_Endpoint_Strategy
// ----------------------------------------------------------------------

// Constructor
TAO_AV_Endpoint_Strategy::TAO_AV_Endpoint_Strategy (void)
{
}

// Destructor.
TAO_AV_Endpoint_Strategy::~TAO_AV_Endpoint_Strategy (void)
{
//   if (CORBA::is_nil (this->stream_endpoint_a_) == 0)
//     CORBA::release (this->stream_endpoint_a_);

//   if (CORBA::is_nil (this->stream_endpoint_b_) == 0)
//     CORBA::release (this->stream_endpoint_b_);

//   if (CORBA::is_nil (this->stream_endpoint_b_) == 0)
//     CORBA::release (this->vdev_);

}

// The base class defines the "failure" case, so that unless the
// subclasses override this, the call will fail. This is done so that
// subclasses need only define the calls that they want to support,
// and the remaining calls will fail automagically
int
TAO_AV_Endpoint_Strategy::create_A (AVStreams::StreamEndPoint_A_ptr &stream_endpoint,
                                    AVStreams::VDev_ptr &vdev,
                                    CORBA::Environment &env)
{
  ACE_ERROR_RETURN ((LM_ERROR,
                     "(%P|%t) Error creating A endpoint\n"),
                    -1);
}

// The base class defines the "failure" case, so that unless the
// subclasses override this, the call will fail. This is done so that
// subclasses need only define the calls that they want to support,
// and the remaining calls will fail automagically
int
TAO_AV_Endpoint_Strategy::create_B (AVStreams::StreamEndPoint_B_ptr &stream_endpoint,
                                    AVStreams::VDev_ptr &vdev,
                                    CORBA::Environment &env)
{
  ACE_ERROR_RETURN ((LM_ERROR,
                     "(%P|%t) Error creating B endpoint\n"),
                    -1);
}


// ----------------------------------------------------------------------
// TAO_AV_Endpoint_Process_Strategy
// ----------------------------------------------------------------------

// Constructor
TAO_AV_Endpoint_Process_Strategy::TAO_AV_Endpoint_Process_Strategy (ACE_Process_Options *process_options)
  : process_options_ (process_options),
    pid_ (-1)
{
  ACE_OS::hostname (this->host_,
                    sizeof this->host_);
}

// Destructor.
TAO_AV_Endpoint_Process_Strategy::~TAO_AV_Endpoint_Process_Strategy (void)
{
}

// Spawns the process, and waits for it to finish booting.
// Then uses bind_to_naming_service, get_stream_endpoint, and get_vdev
// to get the object references to the various objects created in the
// child 
int
TAO_AV_Endpoint_Process_Strategy::activate (void)
{
  ACE_Process process;

  // Create a new process to contain this endpoint
  this->pid_ = process.spawn (*this->process_options_);

  // Process creation failed
  if (this->pid_ == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) ACE_Process:: spawn failed: %p\n",
                       "spawn"),
                      -1);

  // Create a unique semaphore name, using my hostname, and pid.
  char sem_str [BUFSIZ];
  
  // create a unique semaphore name
  ACE_OS::sprintf (sem_str,
                   "%s:%s:%ld",
                   "TAO_AV_Process_Semaphore",
                   this->host_,
                   this->pid_);
  
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) semaphore is %s\n",
              sem_str));
  // Create the semaphore
  ACE_Process_Semaphore semaphore (0, // 0 means that the
                                   // semaphore is locked initially
                                   sem_str);
  
  // wait until the child finishes booting
  while (1)
    {
      if (semaphore.acquire () == -1)
        {
          // See if my child process is still alive -- if not, return an error
          if (ACE_OS::kill (this->pid_,
                            0) == -1)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "(%P|%t) Process_Strategy: Process being waited on died unexpectedly.\n"),
                              -1);
          // if we were not interrupted due to a EINTR, break
          if (errno != EINTR)
            break;
        }
      else
        break;
    }

  // The job of the semaphore is done, remove it.
  if (semaphore.remove () == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "(%P|%t) semaphore remove failed: %p\n",
                       "remove"),
                      -1);
  
  TAO_TRY 
    {
      // Get ourselves a Naming service
      this->bind_to_naming_service (TAO_TRY_ENV);
      TAO_CHECK_ENV;

      // Get the stream endpoint created by the child from the naming service
      this->get_stream_endpoint (TAO_TRY_ENV);
      TAO_CHECK_ENV;

      // Get the Vdev created by the child from the naming service
      this->get_vdev (TAO_TRY_ENV);
      TAO_CHECK_ENV;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("TAO_Endpoint_Process_Strategy::activate");
      return -1;
    }      
  TAO_ENDTRY;

  return 0;
}

// Get ourselves a Naming service reference
int
TAO_AV_Endpoint_Process_Strategy::bind_to_naming_service (CORBA::Environment &env)
{
  if (CORBA::is_nil (this->naming_context_.in ()) == 0)
    return 0;

  CORBA::Object_var naming_obj =
    TAO_ORB_Core_instance ()->orb ()->resolve_initial_references ("NameService");

  if (CORBA::is_nil (naming_obj.in ()))
    ACE_ERROR_RETURN ((LM_ERROR,
                       " (%P|%t) Unable to resolve the Name Service.\n"),
                      -1);
  this->naming_context_ =
    CosNaming::NamingContext::_narrow (naming_obj.in (),
                                       env);
  TAO_CHECK_ENV_RETURN (env, -1);

  return 0;
}

// Get the VDev created in the child process from the namingservice
int
TAO_AV_Endpoint_Process_Strategy::get_vdev (CORBA::Environment &env)
{
  char vdev_name [BUFSIZ];
  ACE_OS::sprintf (vdev_name,
                   "%s:%s:%d",
                   "VDev",
                   this->host_,
                   this->pid_);

  ACE_DEBUG ((LM_DEBUG,"(%P|%t)%s\n",vdev_name));

  // Create the name
  CosNaming::Name VDev_Name (1);
  VDev_Name.length (1);
  VDev_Name [0].id = CORBA::string_dup (vdev_name);

  // Get the CORBA::Object
  CORBA::Object_var vdev =
    this->naming_context_->resolve (VDev_Name,
                             env);
  TAO_CHECK_ENV_RETURN (env, -1);

  // Narrow it
  this->vdev_ =
    AVStreams::VDev::_narrow (vdev.in (),
                              env);
  TAO_CHECK_ENV_RETURN (env, -1);

  // Check if valid
  if (CORBA::is_nil (this->vdev_))
    ACE_ERROR_RETURN ((LM_ERROR,
                       " could not resolve Stream_Endpoint_B in Naming service <%s>\n"),
                      -1);
  return 0;
}

// ----------------------------------------------------------------------
// TAO_AV_Endpoint_Process_Strategy_A
// ----------------------------------------------------------------------

// Constructor
TAO_AV_Endpoint_Process_Strategy_A::TAO_AV_Endpoint_Process_Strategy_A (ACE_Process_Options *process_options)
  : TAO_AV_Endpoint_Process_Strategy (process_options)
{
}

// Destructor
TAO_AV_Endpoint_Process_Strategy_A::~TAO_AV_Endpoint_Process_Strategy_A (void)
{
}

// the "A" type endpoint creator
int
TAO_AV_Endpoint_Process_Strategy_A::create_A (AVStreams::StreamEndPoint_A_ptr &stream_endpoint,
                                           AVStreams::VDev_ptr &vdev,
                                           CORBA::Environment &env)
{
  // use the baseclass activate
  if (this->activate () == -1)
    ACE_ERROR_RETURN ((LM_ERROR, 
                       "(%P|%t) TAO_AV_Endpoint_Process_Strategy: Error in activate ()\n"),
                      -1);
  
  // return the object references
  stream_endpoint = this->stream_endpoint_a_;
  vdev = this->vdev_;
  return 0;
  
}

// Gets the stream endpoint object reference from the naming service
int
TAO_AV_Endpoint_Process_Strategy_A::get_stream_endpoint (CORBA::Environment &env)
{
  char stream_endpoint_name[BUFSIZ];
  ACE_OS::sprintf (stream_endpoint_name,
                   "%s:%s:%d",
                   "Stream_Endpoint_A",
                   this->host_,
                   this->pid_);

  ACE_DEBUG ((LM_DEBUG,"(%P|%t)%s\n",stream_endpoint_name));

  // Create the name
  CosNaming::Name Stream_Endpoint_A_Name (1);
  
  Stream_Endpoint_A_Name.length (1);
  Stream_Endpoint_A_Name [0].id = CORBA::string_dup (stream_endpoint_name);

  // Get the CORBA::Object
  CORBA::Object_var stream_endpoint_a =
    this->naming_context_->resolve (Stream_Endpoint_A_Name,
                                    env);
  TAO_CHECK_ENV_RETURN (env, -1);
  
  // Narrow the reference
  this->stream_endpoint_a_ =
    AVStreams::StreamEndPoint_A::_narrow (stream_endpoint_a.in (),
                                          env);
  TAO_CHECK_ENV_RETURN (env, -1);
  
  // Check for validity
  if (CORBA::is_nil (this->stream_endpoint_a_))
    ACE_ERROR_RETURN ((LM_ERROR,
                       " could not resolve Stream_Endpoint_A in Naming service <%s>\n"),
                      -1);
  return 0;
}

// ----------------------------------------------------------------------
// TAO_AV_Endpoint_Process_Strategy_B
// ----------------------------------------------------------------------

// Constructor
TAO_AV_Endpoint_Process_Strategy_B::TAO_AV_Endpoint_Process_Strategy_B (ACE_Process_Options *process_options)
  : TAO_AV_Endpoint_Process_Strategy (process_options)
{
}

// Destructor
TAO_AV_Endpoint_Process_Strategy_B::~TAO_AV_Endpoint_Process_Strategy_B (void)
{
}

// Creates and returns a "B" type endpoint
int
TAO_AV_Endpoint_Process_Strategy_B::create_B (AVStreams::StreamEndPoint_B_ptr &stream_endpoint,
                                           AVStreams::VDev_ptr &vdev,
                                           CORBA::Environment &env)
{
  TAO_TRY
    {
    if (this->activate () == -1)
      ACE_ERROR_RETURN ((LM_ERROR, 
                         "(%P|%t) TAO_AV_Endpoint_Process_Strategy: Error in activate ()\n"),
                        -1);

    ACE_DEBUG ((LM_DEBUG,"(%P|%t)TAO_AV_Endpoint_Process_Strategy_B::create_B ()\n: stream_endpoint is:%s\n",
                TAO_ORB_Core_instance ()->orb ()->object_to_string (this->stream_endpoint_b_,
                                                                    TAO_TRY_ENV)));
    TAO_CHECK_ENV;
    stream_endpoint = this->stream_endpoint_b_;
    vdev = this->vdev_;
  }
  TAO_CATCHANY
    { 
      TAO_TRY_ENV.print_exception ("TAO_AV_Endpoint_Process_Strategy_B::create_B\n");
      return -1;
    }
  TAO_ENDTRY;
  return 0;
}

// Gets the B type stream_endpoint from the Naming service
int
TAO_AV_Endpoint_Process_Strategy_B::get_stream_endpoint (CORBA::Environment &env)
{
  char stream_endpoint_name[BUFSIZ];
  ACE_OS::sprintf (stream_endpoint_name,
                   "%s:%s:%d",
                   "Stream_Endpoint_B",
                   this->host_,
                   this->pid_);

  ACE_DEBUG ((LM_DEBUG,"(%P|%t)%s\n",stream_endpoint_name));

  // Create the name
  CosNaming::Name Stream_Endpoint_B_Name (1);
  
  Stream_Endpoint_B_Name.length (1);
  Stream_Endpoint_B_Name [0].id = CORBA::string_dup (stream_endpoint_name);
  
  // Get the CORBA::Object reference
  CORBA::Object_var stream_endpoint_b =
    this->naming_context_->resolve (Stream_Endpoint_B_Name,
                                    env);
  TAO_CHECK_ENV_RETURN (env, -1);

  // Narrow the reference
  this->stream_endpoint_b_ =
    AVStreams::StreamEndPoint_B::_narrow (stream_endpoint_b.in (),
                                          env);
  TAO_CHECK_ENV_RETURN (env, -1);

  // Check for validity
  if (CORBA::is_nil (this->stream_endpoint_b_))
    ACE_ERROR_RETURN ((LM_ERROR,
                       " could not resolve Stream_Endpoint_B in Naming service <%s>\n"),
                      -1);
  return 0;
}