Endpoint_Strategy.cpp

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

Generated on Tue Feb 2 17:47:49 2010 for TAO_AV by  doxygen 1.4.7