⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 driver.cpp

📁 这是广泛使用的通信开源项目,对于大容量,高并发的通讯要求完全能够胜任,他广泛可用于网络游戏医学图像网关的高qos要求.更详细的内容可阅读相应的材料
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Driver.cpp,v 1.29 2003/11/02 23:27:22 dhinton Exp

#include "Driver.h"
#include "Consumer.h"
#include "Supplier.h"

#include "orbsvcs/Event_Service_Constants.h"
#include "orbsvcs/Event_Utilities.h"
#include "orbsvcs/Event/EC_Default_Factory.h"
#include "orbsvcs/Event/EC_Event_Channel.h"

#include "tao/ORB_Constants.h"

#include "ace/Sched_Params.h"
#include "ace/Arg_Shifter.h"
#include "ace/High_Res_Timer.h"
#include "ace/Stats.h"
#include "ace/OS_NS_errno.h"

#if !defined(EC_DISABLE_OLD_EC)
#include "EC_Scheduler_Info.h"
#endif

#if !defined (__ACE_INLINE__)
#include "Driver.i"
#endif /* __ACE_INLINE__ */

ACE_RCSID(EC_Tests, EC_Driver, "Driver.cpp,v 1.29 2003/11/02 23:27:22 dhinton Exp")

EC_Driver::EC_Driver (void)
  :  n_consumers_ (1),
     consumers_ (0),
     n_suppliers_ (1),
     suppliers_ (0),
     tasks_ (0),
     burst_count_  (100),
     burst_size_   (100),
     payload_size_ (0),
     burst_pause_  (10000),
     consumer_type_start_ (ACE_ES_EVENT_UNDEFINED),
     consumer_type_count_ (1),
     consumer_type_shift_ (2),
     supplier_type_start_ (ACE_ES_EVENT_UNDEFINED),
     supplier_type_count_ (1),
     supplier_type_shift_ (2),
     pid_file_name_ (0),
     verbose_ (0),
     thr_create_flags_ (THR_NEW_LWP|THR_BOUND|THR_SCHED_FIFO),
     use_remote_ec_ (0),
     event_service_name_ ("EventService"),
     use_old_ec_ (0),
#if !defined(TAO_EC_DISABLE_OLD_EC)
     module_factory_ (0),
#endif
     ec_impl_ (0)
{
  TAO_EC_Default_Factory::init_svcs ();
}

EC_Driver::~EC_Driver (void)
{
}

int
EC_Driver::run (int argc, char* argv[])
{
  ACE_TRY_NEW_ENV
    {
      // Calibrate the high resolution timer *before* starting the
      // test.
      ACE_High_Res_Timer::calibrate ();

      this->run_init (argc, argv ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      this->execute_test (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;

      this->dump_results ();

      this->run_cleanup (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "EC_Driver::run");
    }
  ACE_CATCHALL
    {
      ACE_ERROR ((LM_ERROR, "EC_Driver (%P|%t) non-corba exception raised\n"));
    }
  ACE_ENDTRY;
  return 0;
}

void
EC_Driver::run_init (int &argc, char* argv[]
                     ACE_ENV_ARG_DECL)
{
  this->initialize_orb_and_poa (argc, argv ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (this->parse_args (argc, argv))
    ACE_THROW (CORBA::INTERNAL (TAO_DEFAULT_MINOR_CODE,
                                CORBA::COMPLETED_NO));

  if (this->verbose ())
    this->print_args ();

  if (this->pid_file_name_ != 0)
    {
      FILE* pid = ACE_OS::fopen (this->pid_file_name_, "w");
      if (pid != 0)
        {
          ACE_OS::fprintf (pid, "%ld\n",
                           ACE_static_cast (long, ACE_OS::getpid ()));
          ACE_OS::fclose (pid);
        }
    }

  if (this->move_to_rt_class () == -1)
    ACE_THROW (CORBA::INTERNAL (TAO_DEFAULT_MINOR_CODE,
                                CORBA::COMPLETED_NO));

  this->initialize_ec_impl (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  if (this->allocate_consumers () == -1)
    ACE_THROW (CORBA::NO_MEMORY (TAO_DEFAULT_MINOR_CODE,
                                 CORBA::COMPLETED_NO));

  if (this->allocate_suppliers () == -1)
    ACE_THROW (CORBA::NO_MEMORY (TAO_DEFAULT_MINOR_CODE,
                                 CORBA::COMPLETED_NO));

  this->connect_clients (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
}

void
EC_Driver::run_cleanup (ACE_ENV_SINGLE_ARG_DECL)
{
  this->disconnect_clients (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->shutdown_clients (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->destroy_ec (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  if (this->verbose ())
    ACE_DEBUG ((LM_DEBUG, "EC_Driver (%P|%t) channel destroyed\n"));

  this->deactivate_ec (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  if (this->verbose ())
    ACE_DEBUG ((LM_DEBUG, "EC_Driver (%P|%t) channel deactivated\n"));

  this->cleanup_tasks ();
  this->cleanup_suppliers ();
  this->cleanup_consumers ();
  this->cleanup_ec ();

  this->root_poa_->destroy (1, 1 ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;
  this->root_poa_ = PortableServer::POA::_nil ();

  this->orb_->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
  this->orb_ = CORBA::ORB::_nil ();
}

void
EC_Driver::initialize_orb_and_poa (int &argc, char* argv[]
                                   ACE_ENV_ARG_DECL)
{
  this->orb_ =
    CORBA::ORB_init (argc, argv, "" ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  CORBA::Object_var poa_object =
    this->orb_->resolve_initial_references("RootPOA" ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (CORBA::is_nil (poa_object.in ()))
    {
      ACE_ERROR ((LM_ERROR,
                  "EC_Driver (%P|%t) Unable to initialize the POA.\n"));
      return;
    }

  this->root_poa_ =
    PortableServer::POA::_narrow (poa_object.in () ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  PortableServer::POAManager_var poa_manager =
    this->root_poa_->the_POAManager (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  poa_manager->activate (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
}

void
EC_Driver::print_args (void) const
{
  ACE_DEBUG ((LM_DEBUG,
              "Execution parameters:\n"
              "  consumers = <%d>\n"
              "  suppliers = <%d>\n"
              "  burst count = <%d>\n"
              "  burst size = <%d>\n"
              "  event size = <%d>\n"
              "  burst pause = <%d>\n"
              "  consumer type start = <%d>\n"
              "  consumer type count = <%d>\n"
              "  consumer type shift = <%d>\n"
              "  supplier type start = <%d>\n"
              "  supplier type count = <%d>\n"
              "  supplier type shift = <%d>\n"
              "  pid file name = <%s>\n",

              this->n_consumers_,
              this->n_suppliers_,
              this->burst_count_,
              this->burst_size_,
              this->payload_size_,
              this->burst_pause_,

              this->consumer_type_start_,
              this->consumer_type_count_,
              this->consumer_type_shift_,
              this->supplier_type_start_,
              this->supplier_type_count_,
              this->supplier_type_shift_,

              this->pid_file_name_?this->pid_file_name_:"nil"
              ) );
}

int
EC_Driver::move_to_rt_class (void)
{
  int priority =
    (ACE_Sched_Params::priority_min (ACE_SCHED_FIFO)
     + ACE_Sched_Params::priority_max (ACE_SCHED_FIFO)) / 2;
  priority = ACE_Sched_Params::next_priority (ACE_SCHED_FIFO,
                                                  priority);
  // Enable FIFO scheduling, e.g., RT scheduling class on Solaris.

  if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO,
                                              priority,
                                              ACE_SCOPE_PROCESS)) != 0)
    {
      if (ACE_OS::last_error () == EPERM)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "EC_Driver (%P|%t): user is not superuser, "
                      "so remain in time-sharing class\n"));
          this->thr_create_flags_ = THR_NEW_LWP;
        }
      else
        ACE_ERROR ((LM_ERROR,
                    "EC_Driver (%P|%t): sched_params failed\n"));
    }

  if (ACE_OS::thr_setprio (priority) == -1)
    {
      ACE_ERROR ((LM_ERROR, "EC_Driver (%P|%t) main thr_setprio failed,"
                  "no real-time features\n"));
    }
  return 0;
}

void
EC_Driver::initialize_ec_impl (ACE_ENV_SINGLE_ARG_DECL)
{
#if !defined(EC_DISABLE_REMOTE_EC)
  if (this->use_remote_ec_ == 1)
    {
      this->obtain_remote_ec (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;
      return;
    }
#endif

#if !defined(EC_DISABLE_OLD_EC)
  if (this->use_old_ec_ == 1)
    {
      this->initialize_old_ec (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;
      return;
    }
#endif

  this->initialize_new_ec (ACE_ENV_SINGLE_ARG_PARAMETER);
}

#if !defined(EC_DISABLE_REMOTE_EC)
void
EC_Driver::obtain_remote_ec (ACE_ENV_SINGLE_ARG_DECL)
{
  CosNaming::NamingContext_var naming_context =
    this->get_naming_context (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  CosNaming::Name channel_name (1);
  channel_name.length (1);
  channel_name[0].id = CORBA::string_dup (this->event_service_name_);

  CORBA::Object_var tmp =
    naming_context->resolve (channel_name ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  this->event_channel_ =
    RtecEventChannelAdmin::EventChannel::_narrow (tmp.in () ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;
}

CosNaming::NamingContext_ptr
EC_Driver::get_naming_context (ACE_ENV_SINGLE_ARG_DECL)
{
  CORBA::Object_var naming_obj =
    this->orb_->resolve_initial_references ("NameService" ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (CosNaming::NamingContext::_nil ());

  if (CORBA::is_nil (naming_obj.in ()))
    ACE_ERROR ((LM_ERROR,
                "EC_Driver (%P|%t) Unable to obtain the "
                "Naming Service.\n"));

  return CosNaming::NamingContext::_narrow (naming_obj.in () ACE_ENV_ARG_PARAMETER);
}
#endif

#if !defined(EC_DISABLE_OLD_EC)
int
EC_Driver::initialize_old_ec (ACE_ENV_SINGLE_ARG_DECL)
{
  CosNaming::NamingContext_var naming_context =
    this->get_naming_context (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  // This is the name we (potentially) register the Scheduling
  // Service in the Naming Service.
  CosNaming::Name schedule_name (1);
  schedule_name.length (1);
  schedule_name[0].id = CORBA::string_dup ("ScheduleService");

  if (this->use_runtime_scheduler_ == 1)
    ACE_NEW (this->scheduler_impl_,
             ACE_Runtime_Scheduler (runtime_configs_size,
                                    runtime_configs,
                                    runtime_infos_size,
                                    runtime_infos));
  else
    ACE_NEW (this->scheduler_impl_,
             ACE_Config_Scheduler ());


  this->scheduler_ =
    this->scheduler_impl_._this (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  CORBA::String_var str =
    this->orb_->object_to_string (scheduler.in () ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;
  ACE_DEBUG ((LM_DEBUG,
              "EC_Driver (%P|%t) The (local) scheduler IOR is <%s>\n",
              str.in ()));


  if (this->reactive_ec_ == 1)
    ACE_NEW_RETURN (this->module_factory_,
                    TAO_Reactive_Module_Factory,
                    -1);
  else
    ACE_NEW_RETURN (this->module_factory_,
                    TAO_Default_Module_Factory,
                    -1);

  ACE_NEW_RETURN (this->ec_impl_,
                  ACE_EventChannel (scheduler.in (), 1,
                                    ACE_DEFAULT_EVENT_CHANNEL_TYPE,
                                    this->module_factory_),
                  -1);

  this->event_channel_ =
    this->ec_impl_->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);
}
#endif /* */

void
EC_Driver::initialize_new_ec (ACE_ENV_SINGLE_ARG_DECL)
{
  TAO_EC_Event_Channel_Attributes attr (this->root_poa_.in (),
                                        this->root_poa_.in ());

  this->modify_attributes (attr);

  TAO_EC_Event_Channel *ec =
    new TAO_EC_Event_Channel (attr);
  this->ec_impl_ = ec;

  ec->activate (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->event_channel_ =
    this->ec_impl_->_this (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
}

void
EC_Driver::deactivate_ec (ACE_ENV_SINGLE_ARG_DECL)
{
#if !defined(EC_DISABLE_REMOTE_EC)
  if (this->use_remote_ec_ == 1)
    return;
#endif

  {
    // Deactivate the EC
    PortableServer::POA_var poa =
      this->ec_impl_->_default_POA (ACE_ENV_SINGLE_ARG_PARAMETER);
    ACE_CHECK;
    PortableServer::ObjectId_var id =
      poa->servant_to_id (this->ec_impl_ ACE_ENV_ARG_PARAMETER);
    ACE_CHECK;
    poa->deactivate_object (id.in () ACE_ENV_ARG_PARAMETER);
    ACE_CHECK;
  }

  if (this->verbose ())
    ACE_DEBUG ((LM_DEBUG, "EC_Driver (%P|%t) EC deactivated\n"));

#if !defined(EC_DISABLE_OLD_EC)
  if (this->use_old_ec_ == 1)
    {
      // Deactivate the Scheduler
      PortableServer::POA_var poa =
        scheduler_impl._default_POA (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK;
      PortableServer::ObjectId_var id =
        poa->servant_to_id (&scheduler_impl ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
      poa->deactivate_object (id.in () ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;

      if (this->verbose ())
        ACE_DEBUG ((LM_DEBUG, "EC_Driver (%P|%t) scheduler deactivated\n"));
    }
#endif
}

void
EC_Driver::destroy_ec (ACE_ENV_SINGLE_ARG_DECL)
{
  this->event_channel_->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
}

int
EC_Driver::allocate_consumers (void)
{
  ACE_NEW_RETURN (this->consumers_,
                  EC_Consumer*[this->n_consumers_],
                  -1);
  for (int i = 0; i < this->n_consumers_; ++i)
    this->consumers_[i] =
      this->allocate_consumer (i);
  return 0;
}

int
EC_Driver::allocate_suppliers (void)
{
  ACE_NEW_RETURN (this->suppliers_,
                  EC_Supplier*[this->n_suppliers_],
                  -1);
  for (int i = 0; i < this->n_suppliers_; ++i)
    this->suppliers_[i] =
      this->allocate_supplier (i);
  return 0;
}

EC_Consumer*
EC_Driver::allocate_consumer (int i)
{
  return new EC_Consumer (this,
                          this->consumers_ + i);
}

EC_Supplier*
EC_Driver::allocate_supplier (int i)
{
  return new EC_Supplier (this,
                          this->suppliers_ + i);
}

void
EC_Driver::connect_clients (ACE_ENV_SINGLE_ARG_DECL)
{
  this->connect_consumers (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->connect_suppliers (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
}

void
EC_Driver::disconnect_clients (ACE_ENV_SINGLE_ARG_DECL)
{
  this->disconnect_suppliers (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->disconnect_consumers (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
}

void
EC_Driver::shutdown_clients (ACE_ENV_SINGLE_ARG_DECL)
{
  this->shutdown_suppliers (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->shutdown_consumers (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
}

void
EC_Driver::connect_consumers (ACE_ENV_SINGLE_ARG_DECL)
{
  RtecEventChannelAdmin::ConsumerAdmin_var consumer_admin =
    this->event_channel_->for_consumers (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  for (int i = 0; i < this->n_consumers_; ++i)
    {
      this->connect_consumer (consumer_admin.in (), i ACE_ENV_ARG_PARAMETER);
      ACE_CHECK;
    }
  if (this->verbose ())
    ACE_DEBUG ((LM_DEBUG, "EC_Driver (%P|%t) connected consumer(s)\n"));
}

void
EC_Driver::connect_consumer (
    RtecEventChannelAdmin::ConsumerAdmin_ptr consumer_admin,
    int i
    ACE_ENV_ARG_DECL)
{
  RtecEventChannelAdmin::ConsumerQOS qos;
  int shutdown_event_type;
  this->build_consumer_qos (i, qos, shutdown_event_type ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  this->consumers_[i]->connect (consumer_admin,
                                qos,
                                shutdown_event_type
                                ACE_ENV_ARG_PARAMETER);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -