/**
 * Register this object.
 */
int ReplicationManagerFaultConsumerAdapter::init (
  CORBA::ORB_ptr orb)
{
  ACE_DEBUG ((
    LM_DEBUG,
    ACE_TEXT ("Entered ReplicationManagerFaultConsumerAdapter::init.\n")
  ));

  int result = 0;
  this->orb_ = CORBA::ORB::_duplicate (orb);

  //////////////////////////////////////////
  // resolve reference to detector factory
  ACE_DEBUG ((
    LM_DEBUG,
    ACE_TEXT ("ReplicationManagerFaultConsumerAdapter::init: ")
    ACE_TEXT ("Getting ready to read iorDetectorFile.\n")
  ));

  CORBA::Object_var detector_obj = this->orb_->string_to_object (
    this->detector_ior_);
  this->factory_ = ::FT::FaultDetectorFactory::_narrow (
    detector_obj.in());
  if (CORBA::is_nil (this->factory_.in()))
  {
    ACE_ERROR_RETURN ((
      LM_ERROR,
      ACE_TEXT ("ReplicationManagerFaultConsumerAdapter::init: ")
      ACE_TEXT ("FaultDetectorFactory IOR is nil: %s\n"),
      this->detector_ior_),
      -1);
  }

  //////////////////////////////////////////
  // resolve references to notifier
  ACE_DEBUG ((
    LM_DEBUG,
    ACE_TEXT ("ReplicationManagerFaultConsumerAdapter::init: ")
    ACE_TEXT ("Getting ready to read Notifier IOR file.\n")
  ));

  CORBA::Object_var notifier_ior = this->orb_->string_to_object (
    this->notifier_ior_);
  this->notifier_ = ::FT::FaultNotifier::_narrow (
    notifier_ior.in());
  if (CORBA::is_nil (this->notifier_.in()))
  {
    ACE_ERROR_RETURN ((
      LM_ERROR,
      ACE_TEXT ("ReplicationManagerFaultConsumerAdapter::init: ")
      ACE_TEXT ("FaultNotifier IOR is nil: %s\n"),
      this->notifier_ior_),
      -1);
  }

  // Create the real FaultConsumer.
  //
  // Note: We have to hang onto the servant class pointer so we can
  // invoke member functions on it, but we also give ownership of it
  // to a PortableServer::ServantBase_var.
  ACE_DEBUG ((
    LM_DEBUG,
    ACE_TEXT ("ReplicationManagerFaultConsumerAdapter::init: ")
    ACE_TEXT ("Getting ready to create the real FaultConsumer.\n")
  ));

  ACE_NEW_RETURN (this->p_fault_consumer_, TAO::FT_FaultConsumer (), -1);
  if (this->p_fault_consumer_ != 0)
  {
    this->consumer_servant_ = this->p_fault_consumer_;
  }

  //////////////////////////
  // Get ready to initialize the consumer.  We need to provide it
  // with the following:
  // - The POA in which it is to be activated.
  // - FT::FaultNotifier IOR.
  // - FT::ReplicationManager IOR (fake it for now).

  // Get the RootPOA from the ORB.
  CORBA::Object_var poa_obj = this->orb_->resolve_initial_references (
    "RootPOA");
  PortableServer::POA_var poa = PortableServer::POA::_narrow (
    poa_obj.in());

  // Create a fault analyzer.
  TAO::FT_FaultAnalyzer * analyzer = 0;
  ACE_NEW_RETURN (analyzer, TAO::FT_DefaultFaultAnalyzer (), -1);

  // Initialize the FaultConsumer.
  // It will activate itself in the POA we pass it and connect to the
  // Fault Notifier we pass it.
  ACE_DEBUG ((
    LM_DEBUG,
    ACE_TEXT ("ReplicationManagerFaultConsumerAdapter::init: ")
    ACE_TEXT ("Getting ready to initialize the real FaultConsumer.\n")
  ));

  result = this->p_fault_consumer_->init (
    poa.in(),
    this->notifier_.in(),
    analyzer);
  if (result != 0)
  {
    ACE_ERROR_RETURN ((
      LM_ERROR,
      ACE_TEXT ("ReplicationManagerFaultConsumerAdapter::init: ")
      ACE_TEXT ("Unable to initialize the real FaultConsumer.\n")),
      result);
    }

  this->identity_ = "ReplicationManagerFaultConsumerAdapter";

  // Activate the RootPOA.
  PortableServer::POAManager_var poa_manager =
    poa->the_POAManager ();
  poa_manager->activate ();

  /////////////////////////
  // Set up fault detectors
  if (result == 0)
  {
    ////////////////////////////////////
    // resolve references to replicas
    // create a fault detector for each replica
    size_t replicaCount = this->replica_iors_.size();
    ACE_DEBUG ((LM_DEBUG,
      ACE_TEXT ("Number of replicas being monitored: (%u)\n"),
      static_cast<unsigned int> (replicaCount)
    ));
    for (size_t nRep = 0; result == 0 && nRep < replicaCount; ++nRep)
    {
      const char * iorName = this->replica_iors_[nRep];
      CORBA::Object_var replica_obj = this->orb_->string_to_object (
        iorName);
      FT::PullMonitorable_var replica = FT::PullMonitorable::_narrow (
        replica_obj.in());
      if (CORBA::is_nil(replica.in()))
      {
        ACE_ERROR_RETURN ((
          LM_ERROR,
          ACE_TEXT ("ReplicationManagerFaultConsumerAdapter::init: ")
          ACE_TEXT ("Can't resolve Replica IOR: %s\n"),
          iorName),
          -1);
      }
      else
      {
        this->replicas_.push_back(replica);

        CORBA::String_var type_id = CORBA::string_dup("FaultDetector");

        TAO_PG::Properties_Encoder encoder;

        PortableGroup::Value value;
        value <<= notifier_.in ();
        encoder.add(::FT::FT_NOTIFIER, value);

        value <<= replica.in ();
        encoder.add(::FT::FT_MONITORABLE, value);

        FT::FTDomainId domain_id = 0;
        value <<= domain_id;
        encoder.add(::FT::FT_DOMAIN_ID, value);

        PortableGroup::Location object_location;
        object_location.length(2);
        object_location[0].id = CORBA::string_dup("test");
        object_location[1].id = CORBA::string_dup("Location_A");
        value <<= object_location;
        encoder.add(::FT::FT_LOCATION, value);

        PortableGroup::TypeId_var object_type = CORBA::string_dup (
          "IDL:org.omg/CosNaming/NamingContextExt:1.0");
        value <<= object_type.in ();
        encoder.add(::FT::FT_TYPE_ID, value);

        PortableGroup::ObjectGroupId group_id =
          static_cast<PortableGroup::ObjectGroupId> (6191982);
        value <<= group_id;
        encoder.add(::FT::FT_GROUP_ID, value);

        // allocate and populate the criteria
        PortableGroup::Criteria_var criteria;
        ACE_NEW_NORETURN (criteria,
          PortableGroup::Criteria);
        if (criteria.ptr() == 0)
        {
          ACE_ERROR_RETURN ((
            LM_ERROR,
            ACE_TEXT ("ReplicationManagerFaultConsumerAdapter::init: ")
            ACE_TEXT ("Error cannot allocate criteria.\n")),
            -1);
        }
        else
        {
          encoder.encode(criteria);
          PortableGroup::GenericFactory::FactoryCreationId_var factory_creation_id;

          this->factory_->create_object (
            type_id.in(),
            criteria.in(),
            factory_creation_id);
        }
      }
    }

    // Signal that we are ready to go.
    if (result == 0 && this->readyFile_ != 0)
    {
      FILE* ready = ACE_OS::fopen (this->readyFile_, "w");
      if (ready)
      {
        ACE_OS::fprintf (ready, "ready\n");
        ACE_OS::fclose (ready);
      }
    }
  }

  return result;
}
int TAO::Object_Group_Creator::create_detector_for_replica (
  CORBA::Object_ptr replica,
  const char * role,
  const char * type_id,
  PortableGroup::ObjectGroupId group_id,
  const PortableGroup::Location & location)
{
  int result = 0;

  if (this->have_replication_manager_)
  {
    CORBA::ULong detector_count = this->detector_infos_->length ();
    for (CORBA::ULong n_detector = 0; result == 0 && n_detector < detector_count; ++n_detector)
    {
      PortableGroup::FactoryInfo & info = (*this->detector_infos_)[n_detector];
      if ( info.the_location == location || n_detector + 1 == detector_count)
      {
        TAO_PG::Properties_Encoder encoder;

        PortableGroup::Value value;

        FT::PullMonitorable_ptr monitorable = FT::PullMonitorable::_narrow (replica);
        value <<= monitorable;
        encoder.add (::FT::FT_MONITORABLE, value);

        FT::FTDomainId domain_id = 0;
        value <<= domain_id;
        encoder.add (::FT::FT_DOMAIN_ID, value);

        value <<= location;
        encoder.add (::FT::FT_LOCATION, value);

        value <<= type_id;
        encoder.add (::FT::FT_TYPE_ID, value);

        value <<= group_id;
        encoder.add (::FT::FT_GROUP_ID, value);

        value <<= CORBA::string_dup (role);
        encoder.add (PortableGroup::role_criterion, value);

        // allocate and populate the criteria
        PortableGroup::Criteria_var criteria;
        ACE_NEW_NORETURN (criteria,
          PortableGroup::Criteria);
        if (criteria.ptr () == 0)
        {
          ACE_ERROR ( (LM_ERROR,
            "%T %n (%P|%t)Object_Group_Creater: Error cannot allocate criteria.\n"
            ));
            result = -1;
        }
        else
        {
          encoder.encode (criteria);
          PortableGroup::GenericFactory::FactoryCreationId_var factory_creation_id;

          info.the_factory->create_object (
            type_id,
            criteria.in (),
            factory_creation_id);
          result = 1;
        }
      }
    }
  }
  return result;
}
Exemple #3
0
/**
 * Register this object as necessary
 */
int StubFaultAnalyzer::init (CORBA::ORB_ptr orb)
{
    int result = 0;
    this->orb_ = CORBA::ORB::_duplicate (orb);
    //////////////////////////////////////////
    // resolve reference to detector factory
    CORBA::Object_var detector_obj = this->orb_->string_to_object(this->detector_ior_);
    this->factory_ = ::FT::FaultDetectorFactory::_narrow(detector_obj.in ());
    if (CORBA::is_nil(this->factory_.in ()))
    {
        ACE_OS::fprintf (stderr, "Can't resolve Detector Factory IOR %s\n",
                         ACE_TEXT_ALWAYS_CHAR (this->detector_ior_));
        result = -1;
    }

    //////////////////////////////////////////
    // resolve references to notifier
    CORBA::Object_var not_obj = this->orb_->string_to_object(this->notifier_ior_);
    this->notifier_ = ::FT::FaultNotifier::_narrow(not_obj.in ());
    if (CORBA::is_nil(this->notifier_.in ()))
    {
        ACE_OS::fprintf (stderr, "Can't resolve Notifier IOR %s\n",
                         ACE_TEXT_ALWAYS_CHAR (this->notifier_ior_));
        result = -1;
    }


    //////////////////////////////////
    // register fault consumers
    if (result == 0)
    {
        result = this->faultConsumer_.init (orb, this->notifier_);
    }

    if (result == 0)
    {
        result = this->batchConsumer_.init (orb, this->notifier_);
    }

    /////////////////////////
    // Set up fault detectors
    if (result == 0)
    {
        ////////////////////////////////////
        // resolve references to replicas
        // create a fault detector for each replica
        size_t replicaCount = this->replicaIORs.size();
        for(size_t nRep = 0; result == 0 && nRep < replicaCount; ++nRep)
        {
            const char * iorName = this->replicaIORs[nRep];
            CORBA::Object_var rep_obj = this->orb_->string_to_object(iorName);
            FT::PullMonitorable_var replica = FT::PullMonitorable::_narrow(rep_obj.in ());
            if (CORBA::is_nil(replica.in ()))
            {
                ACE_OS::fprintf (stderr, "Can't resolve Replica IOR %s\n", iorName);
                result = -1;
            }
            else
            {
                this->replicas_.push_back(replica);

                CORBA::String_var type_id = CORBA::string_dup("FaultDetector");

                TAO_PG::Properties_Encoder encoder;

                PortableGroup::Value value;
                value <<= this->notifier_.in ();
                encoder.add(::FT::FT_NOTIFIER, value);

                value <<= replica.in ();
                encoder.add(::FT::FT_MONITORABLE, value);

                FT::FTDomainId domain_id = 0;
                value <<= domain_id;
                encoder.add(::FT::FT_DOMAIN_ID, value);

                PortableGroup::Location object_location;
                object_location.length(1);
                object_location[0].id = CORBA::string_dup("Test location");
                value <<= object_location;
                encoder.add(::FT::FT_LOCATION, value);

                PortableGroup::TypeId object_type = CORBA::string_dup("dummy_type");
                value <<= object_type;
                encoder.add(::FT::FT_TYPE_ID, value);

                FT::ObjectGroupId group_id = 0;
                value <<= group_id;
                encoder.add(::FT::FT_GROUP_ID, value);

                // allocate and populate the criteria
                PortableGroup::Criteria_var criteria;
                ACE_NEW_NORETURN (criteria,
                                  PortableGroup::Criteria);
                if (criteria.ptr() == 0)
                {
                    ACE_ERROR((LM_ERROR,
                               "Error cannot allocate criteria.\n"
                              ));
                    result = -1;
                }
                else
                {
                    encoder.encode(criteria);
                    PortableGroup::GenericFactory::FactoryCreationId_var factory_creation_id;

                    ACE_DEBUG((LM_DEBUG,
                               "Call create_object with type: %s\n", type_id.in()
                              ));
                    this->factory_->create_object (
                        type_id.in(),
                        criteria.in(),
                        factory_creation_id);
                }
            }
        }

        if (result == 0 && this->readyFile_ != 0)
        {
            FILE *ready = ACE_OS::fopen (this->readyFile_, "w");
            if ( ready )
            {
                ACE_OS::fprintf (ready, "ready\n");
                ACE_OS::fclose (ready);
            }
        }
    }
    return result;
}