Endpoint_Strategy.cpp

Go to the documentation of this file.
00001 // Endpoint_Strategy.cpp,v 5.31 2006/03/14 06:14:24 jtc Exp
00002 
00003 // ============================================================================
00004 //
00005 // = LIBRARY
00006 //    cos
00007 //
00008 // = FILENAME
00009 //   Endpoint_Strategy.cpp
00010 //
00011 // = AUTHOR
00012 //    Sumedh Mungee <sumedh@cs.wustl.edu>
00013 //
00014 //
00015 // ============================================================================
00016 
00017 #include "orbsvcs/AV/Endpoint_Strategy.h"
00018 
00019 #include "tao/debug.h"
00020 #include "tao/ORB_Core.h"
00021 
00022 #include "ace/Process_Semaphore.h"
00023 
00024 ACE_RCSID(AV, Endpoint_Strategy, "Endpoint_Strategy.cpp,v 5.31 2006/03/14 06:14:24 jtc Exp")
00025 
00026 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00027 
00028 // ----------------------------------------------------------------------
00029 // TAO_AV_Endpoint_Strategy
00030 // ----------------------------------------------------------------------
00031 
00032 // Constructor
00033 TAO_AV_Endpoint_Strategy::TAO_AV_Endpoint_Strategy (void)
00034 {
00035 }
00036 
00037 // Destructor.
00038 TAO_AV_Endpoint_Strategy::~TAO_AV_Endpoint_Strategy (void)
00039 {
00040 
00041 }
00042 
00043 // The base class defines the "failure" case, so that unless the
00044 // subclasses override this, the call will fail. This is done so that
00045 // subclasses need only define the calls that they want to support,
00046 // and the remaining calls will fail automagically
00047 int
00048 TAO_AV_Endpoint_Strategy::create_A (AVStreams::StreamEndPoint_A_ptr & /* stream_endpoint */,
00049                                     AVStreams::VDev_ptr & /* vdev */
00050                                     ACE_ENV_ARG_DECL_NOT_USED)
00051 {
00052   ACE_ERROR_RETURN ((LM_ERROR,
00053                      "(%P|%t) Error creating A endpoint\n"),
00054                     -1);
00055 }
00056 
00057 // The base class defines the "failure" case, so that unless the
00058 // subclasses override this, the call will fail. This is done so that
00059 // subclasses need only define the calls that they want to support,
00060 // and the remaining calls will fail automagically
00061 int
00062 TAO_AV_Endpoint_Strategy::create_B (AVStreams::StreamEndPoint_B_ptr & /* stream_endpoint */,
00063                                     AVStreams::VDev_ptr & /*vdev */
00064                                     ACE_ENV_ARG_DECL_NOT_USED)
00065 {
00066   ACE_ERROR_RETURN ((LM_ERROR,
00067                      "(%P|%t) Error creating B endpoint\n"),
00068                     -1);
00069 }
00070 
00071 
00072 // ----------------------------------------------------------------------
00073 // TAO_AV_Endpoint_Process_Strategy
00074 // ----------------------------------------------------------------------
00075 
00076 // Constructor
00077 TAO_AV_Endpoint_Process_Strategy::TAO_AV_Endpoint_Process_Strategy (ACE_Process_Options *process_options)
00078   : process_options_ (process_options),
00079     pid_ (-1)
00080 {
00081   ACE_OS::hostname (this->host_,
00082                     sizeof this->host_);
00083 }
00084 
00085 // Destructor.
00086 TAO_AV_Endpoint_Process_Strategy::~TAO_AV_Endpoint_Process_Strategy (void)
00087 {
00088 }
00089 
00090 // Spawns the process, and waits for it to finish booting.
00091 // Then uses bind_to_naming_service, get_stream_endpoint, and get_vdev
00092 // to get the object references to the various objects created in the
00093 // child
00094 int
00095 TAO_AV_Endpoint_Process_Strategy::activate (void)
00096 {
00097   ACE_Process process;
00098 
00099   // Create a new process to contain this endpoint
00100   this->pid_ = process.spawn (*this->process_options_);
00101 
00102   // Process creation failed
00103   if (this->pid_ == -1)
00104     ACE_ERROR_RETURN ((LM_ERROR,
00105                        "(%P|%t) ACE_Process:: spawn failed: %p\n",
00106                        "spawn"),
00107                       -1);
00108 
00109   // Create a unique semaphore name, using my hostname, and pid.
00110   char sem_str [BUFSIZ];
00111 
00112   // create a unique semaphore name
00113   ACE_OS::sprintf (sem_str,
00114                    "%s:%s:%ld",
00115                    "TAO_AV_Process_Semaphore",
00116                    this->host_,
00117                    static_cast<long int> (this->pid_));
00118 
00119   ACE_DEBUG ((LM_DEBUG,
00120               "(%P|%t) semaphore is %s\n",
00121               sem_str));
00122   // Create the semaphore
00123   ACE_Process_Semaphore semaphore (0, // 0 means that the
00124                                    // semaphore is locked initially
00125                                    sem_str);
00126 
00127   // wait until the child finishes booting
00128   while (1)
00129     {
00130       if (semaphore.acquire () == -1)
00131         {
00132           // See if my child process is still alive -- if not, return an error
00133           if (ACE_OS::kill (this->pid_,
00134                             0) == -1)
00135             ACE_ERROR_RETURN ((LM_ERROR,
00136                                "(%P|%t) Process_Strategy: Process being waited on died unexpectedly.\n"),
00137                               -1);
00138           // if we were not interrupted due to a EINTR, break
00139           if (errno != EINTR)
00140             break;
00141         }
00142       else
00143         break;
00144     }
00145 
00146   // The job of the semaphore is done, remove it.
00147   if (semaphore.remove () == -1)
00148     ACE_ERROR_RETURN ((LM_ERROR,
00149                        "(%P|%t) semaphore remove failed: %p\n",
00150                        "remove"),
00151                       -1);
00152 
00153   ACE_DECLARE_NEW_CORBA_ENV;
00154   ACE_TRY
00155     {
00156       // Get ourselves a Naming service
00157       this->bind_to_naming_service (ACE_ENV_SINGLE_ARG_PARAMETER);
00158       ACE_TRY_CHECK;
00159 
00160       // Get the stream endpoint created by the child from the naming service
00161       this->get_stream_endpoint (ACE_ENV_SINGLE_ARG_PARAMETER);
00162       ACE_TRY_CHECK;
00163 
00164       // Get the Vdev created by the child from the naming service
00165       this->get_vdev (ACE_ENV_SINGLE_ARG_PARAMETER);
00166       ACE_TRY_CHECK;
00167     }
00168   ACE_CATCHANY
00169     {
00170       ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,"TAO_AV_Endpoint_Process_Strategy::activate");
00171       return -1;
00172     }
00173   ACE_ENDTRY;
00174   ACE_CHECK_RETURN (-1);
00175   return 0;
00176 }
00177 
00178 // Get ourselves a Naming service reference
00179 int
00180 TAO_AV_Endpoint_Process_Strategy::bind_to_naming_service (ACE_ENV_SINGLE_ARG_DECL)
00181 {
00182   ACE_TRY
00183     {
00184       if (CORBA::is_nil (this->naming_context_.in ()) == 0)
00185         return 0;
00186 
00187       CORBA::Object_var naming_obj =
00188         TAO_ORB_Core_instance ()->orb ()->resolve_initial_references ("NameService" ACE_ENV_ARG_PARAMETER);
00189       ACE_TRY_CHECK;
00190 
00191       if (CORBA::is_nil (naming_obj.in ()))
00192         ACE_ERROR_RETURN ((LM_ERROR,
00193                            " (%P|%t) Unable to resolve the Name Service.\n"),
00194                           -1);
00195       this->naming_context_ =
00196         CosNaming::NamingContext::_narrow (naming_obj.in ()
00197                                            ACE_ENV_ARG_PARAMETER);
00198       ACE_TRY_CHECK;
00199     }
00200   ACE_CATCHANY
00201     {
00202       ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,"TAO_AV_Endpoint_Process_Strategy::bind_to_naming_service");
00203       return -1;
00204     }
00205   ACE_ENDTRY;
00206   ACE_CHECK_RETURN (-1);
00207   return 0;
00208 }
00209 
00210 // Get the VDev created in the child process from the namingservice
00211 int
00212 TAO_AV_Endpoint_Process_Strategy::get_vdev (ACE_ENV_SINGLE_ARG_DECL)
00213 {
00214   ACE_TRY
00215     {
00216       char vdev_name [BUFSIZ];
00217       ACE_OS::sprintf (vdev_name,
00218                        "%s:%s:%ld",
00219                        "VDev",
00220                        this->host_,
00221                        (long) this->pid_);
00222 
00223       if (TAO_debug_level > 0) ACE_DEBUG ((LM_DEBUG,"(%P|%t)%s\n",vdev_name));
00224 
00225       // Create the name
00226       CosNaming::Name VDev_Name (1);
00227       VDev_Name.length (1);
00228       VDev_Name [0].id = CORBA::string_dup (vdev_name);
00229 
00230       // Get the CORBA::Object
00231       CORBA::Object_var vdev =
00232         this->naming_context_->resolve (VDev_Name
00233                                         ACE_ENV_ARG_PARAMETER);
00234       ACE_TRY_CHECK;
00235 
00236       // Narrow it
00237       this->vdev_ =
00238         AVStreams::VDev::_narrow (vdev.in ()
00239                                   ACE_ENV_ARG_PARAMETER);
00240       ACE_TRY_CHECK;
00241 
00242       // Check if valid
00243       if (CORBA::is_nil (this->vdev_.in() ))
00244         ACE_ERROR_RETURN ((LM_ERROR,
00245                            " could not resolve Stream_Endpoint_B in Naming service <%s>\n"),
00246                           -1);
00247     }
00248   ACE_CATCHANY
00249     {
00250       ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,"TAO_AV_Endpoint_Process_Strategy::get_vdev");
00251       return -1;
00252     }
00253   ACE_ENDTRY;
00254   ACE_CHECK_RETURN (-1);
00255   return 0;
00256 }
00257 
00258 // ----------------------------------------------------------------------
00259 // TAO_AV_Endpoint_Process_Strategy_A
00260 // ----------------------------------------------------------------------
00261 
00262 // Constructor
00263 TAO_AV_Endpoint_Process_Strategy_A::TAO_AV_Endpoint_Process_Strategy_A (ACE_Process_Options *process_options)
00264   : TAO_AV_Endpoint_Process_Strategy (process_options)
00265 {
00266 }
00267 
00268 // Destructor
00269 TAO_AV_Endpoint_Process_Strategy_A::~TAO_AV_Endpoint_Process_Strategy_A (void)
00270 {
00271 }
00272 
00273 // the "A" type endpoint creator
00274 int
00275 TAO_AV_Endpoint_Process_Strategy_A::create_A (AVStreams::StreamEndPoint_A_ptr &stream_endpoint,
00276                                            AVStreams::VDev_ptr &vdev
00277                                            ACE_ENV_ARG_DECL_NOT_USED)
00278 {
00279   // use the baseclass activate
00280   if (this->activate () == -1)
00281     ACE_ERROR_RETURN ((LM_ERROR,
00282                        "(%P|%t) TAO_AV_Endpoint_Process_Strategy: Error in activate ()\n"),
00283                       -1);
00284 
00285   // return the object references
00286   stream_endpoint = AVStreams::StreamEndPoint_A::_duplicate( this->stream_endpoint_a_.in() );
00287   vdev = AVStreams::VDev::_duplicate( this->vdev_.in() );
00288   return 0;
00289 
00290 }
00291 
00292 // Gets the stream endpoint object reference from the naming service
00293 int
00294 TAO_AV_Endpoint_Process_Strategy_A::get_stream_endpoint (ACE_ENV_SINGLE_ARG_DECL)
00295 {
00296   ACE_TRY
00297     {
00298       char stream_endpoint_name[BUFSIZ];
00299       ACE_OS::sprintf (stream_endpoint_name,
00300                        "%s:%s:%ld",
00301                        "Stream_Endpoint_A",
00302                        this->host_,
00303                        (long) this->pid_);
00304 
00305       if (TAO_debug_level > 0) ACE_DEBUG ((LM_DEBUG,"(%P|%t)%s\n",stream_endpoint_name));
00306 
00307       // Create the name
00308       CosNaming::Name Stream_Endpoint_A_Name (1);
00309 
00310       Stream_Endpoint_A_Name.length (1);
00311       Stream_Endpoint_A_Name [0].id = CORBA::string_dup (stream_endpoint_name);
00312 
00313       // Get the CORBA::Object
00314       CORBA::Object_var stream_endpoint_a =
00315         this->naming_context_->resolve (Stream_Endpoint_A_Name
00316                                         ACE_ENV_ARG_PARAMETER);
00317       ACE_TRY_CHECK;
00318 
00319       // Narrow the reference
00320       this->stream_endpoint_a_ =
00321         AVStreams::StreamEndPoint_A::_narrow (stream_endpoint_a.in ()
00322                                               ACE_ENV_ARG_PARAMETER);
00323       ACE_TRY_CHECK;
00324 
00325       // Check for validity
00326       if (CORBA::is_nil (this->stream_endpoint_a_.in() ))
00327         ACE_ERROR_RETURN ((LM_ERROR,
00328                            " could not resolve Stream_Endpoint_A in Naming service <%s>\n"),
00329                           -1);
00330     }
00331   ACE_CATCHANY
00332     {
00333       ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,"TAO_AV_Endpoint_Process_Strategy_A::get_stream_endpoint");
00334       return -1;
00335     }
00336   ACE_ENDTRY;
00337   ACE_CHECK_RETURN (-1);
00338   return 0;
00339 }
00340 
00341 // ----------------------------------------------------------------------
00342 // TAO_AV_Endpoint_Process_Strategy_B
00343 // ----------------------------------------------------------------------
00344 
00345 // Constructor
00346 TAO_AV_Endpoint_Process_Strategy_B::TAO_AV_Endpoint_Process_Strategy_B (ACE_Process_Options *process_options)
00347   : TAO_AV_Endpoint_Process_Strategy (process_options)
00348 {
00349 }
00350 
00351 // Destructor
00352 TAO_AV_Endpoint_Process_Strategy_B::~TAO_AV_Endpoint_Process_Strategy_B (void)
00353 {
00354 }
00355 
00356 // Creates and returns a "B" type endpoint
00357 int
00358 TAO_AV_Endpoint_Process_Strategy_B::create_B (AVStreams::StreamEndPoint_B_ptr &stream_endpoint,
00359                                               AVStreams::VDev_ptr &vdev
00360                                               ACE_ENV_ARG_DECL)
00361 {
00362   ACE_TRY
00363     {
00364     if (this->activate () == -1)
00365       ACE_ERROR_RETURN ((LM_ERROR,
00366                          "(%P|%t) TAO_AV_Endpoint_Process_Strategy: Error in activate ()\n"),
00367                         -1);
00368 
00369     if (TAO_debug_level > 0) ACE_DEBUG ((LM_DEBUG,"(%P|%t)TAO_AV_Endpoint_Process_Strategy_B::create_B ()\n: stream_endpoint is:%s\n",
00370                 TAO_ORB_Core_instance ()->orb ()->object_to_string (this->stream_endpoint_b_.in()
00371                                                                     ACE_ENV_ARG_PARAMETER)));
00372     ACE_TRY_CHECK;
00373     stream_endpoint = AVStreams::StreamEndPoint_B::_duplicate ( this->stream_endpoint_b_.in() );
00374     vdev = AVStreams::VDev::_duplicate( this->vdev_.in() );
00375   }
00376   ACE_CATCHANY
00377     {
00378       ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,"TAO_AV_Endpoint_Process_Strategy_B::create_B\n");
00379       return -1;
00380     }
00381   ACE_ENDTRY;
00382   ACE_CHECK_RETURN (-1);
00383   return 0;
00384 }
00385 
00386 // Gets the B type stream_endpoint from the Naming service
00387 int
00388 TAO_AV_Endpoint_Process_Strategy_B::get_stream_endpoint (ACE_ENV_SINGLE_ARG_DECL)
00389 {
00390   ACE_TRY
00391     {
00392       char stream_endpoint_name[BUFSIZ];
00393       ACE_OS::sprintf (stream_endpoint_name,
00394                        "%s:%s:%ld",
00395                        "Stream_Endpoint_B",
00396                        this->host_,
00397                        (long) this->pid_);
00398 
00399       if (TAO_debug_level > 0) ACE_DEBUG ((LM_DEBUG,"(%P|%t)%s\n",stream_endpoint_name));
00400 
00401       // Create the name
00402       CosNaming::Name Stream_Endpoint_B_Name (1);
00403 
00404       Stream_Endpoint_B_Name.length (1);
00405       Stream_Endpoint_B_Name [0].id = CORBA::string_dup (stream_endpoint_name);
00406 
00407       // Get the CORBA::Object reference
00408       CORBA::Object_var stream_endpoint_b =
00409         this->naming_context_->resolve (Stream_Endpoint_B_Name
00410                                         ACE_ENV_ARG_PARAMETER);
00411       ACE_TRY_CHECK;
00412 
00413       // Narrow the reference
00414       this->stream_endpoint_b_ =
00415         AVStreams::StreamEndPoint_B::_narrow (stream_endpoint_b.in ()
00416                                               ACE_ENV_ARG_PARAMETER);
00417       ACE_TRY_CHECK;
00418 
00419       // Check for validity
00420       if (CORBA::is_nil (this->stream_endpoint_b_.in() ))
00421         ACE_ERROR_RETURN ((LM_ERROR,
00422                            " could not resolve Stream_Endpoint_B in Naming service <%s>\n"),
00423                           -1);
00424     }
00425   ACE_CATCHANY
00426     {
00427       ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,"TAO_AV_Endpoint_Process_Strategy_B::get_stream_endpoint");
00428       return -1;
00429     }
00430   ACE_ENDTRY;
00431   ACE_CHECK_RETURN (-1);
00432   return 0;
00433 }
00434 
00435 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 13:44:41 2006 for TAO_AV by doxygen 1.3.6