📄 application.cpp
字号:
this->consumer_ = proxy._retn ();
this->consumer_proxy_disconnect_.set_command (new_proxy_disconnect);
}
void
Heartbeat_Application::connect_as_consumer (ACE_ENV_SINGLE_ARG_DECL)
{
// Activate with poa.
RtecEventComm::PushConsumer_var consumer_ref;
PortableServer::POA_var poa = this->_default_POA ();
TAO_EC_Object_Deactivator deactivator;
activate (consumer_ref,
poa.in (),
this,
deactivator
ACE_ENV_ARG_PARAMETER);
ACE_CHECK;
// Obtain reference to ConsumerAdmin.
RtecEventChannelAdmin::ConsumerAdmin_var consumer_admin =
this->ec_->for_consumers (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_CHECK;
// Obtain ProxyPushSupplier..
RtecEventChannelAdmin::ProxyPushSupplier_var proxy =
consumer_admin->obtain_push_supplier (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_CHECK;
Supplier_Proxy_Disconnect new_proxy_disconnect (proxy.in ());
// Connect this consumer.
ACE_ConsumerQOS_Factory qos;
qos.start_disjunction_group (1);
qos.insert_type (ACE_ES_EVENT_ANY, 0);
proxy->connect_push_consumer (consumer_ref.in (),
qos.get_ConsumerQOS ()
ACE_ENV_ARG_PARAMETER);
ACE_CHECK;
// Update resource managers.
this->supplier_proxy_disconnect_.set_command (new_proxy_disconnect);
this->set_deactivator (deactivator);
}
int
Heartbeat_Application::handle_timeout (const ACE_Time_Value&,
const void*)
{
ACE_TRY_NEW_ENV
{
if (this->n_timeouts_++ < HEARTBEATS_TO_SEND)
{
RtecEventComm::EventSet events (1);
events.length (1);
// Events travelling through gateways must have a ttl count of at
// least 1!
events[0].header.ttl = 1;
events[0].header.type = HEARTBEAT;
events[0].header.source = SOURCE_ID;
// Store our hostname and process id in the data portion of
// the event.
events[0].data.payload.replace (MAXHOSTNAMELEN+11,
MAXHOSTNAMELEN+11,
(u_char *)this->hostname_and_pid_,
0);
this->consumer_->push (events ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
}
else
// We already sent the required number of heartbeats. Time to
// shutdown this app.
{
this->shutdown ();
}
}
ACE_CATCHANY
{
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
"Suppressed the following exception in "
"Heartbeat_Application::handle_timeout:\n");
}
ACE_ENDTRY;
return 0;
}
void
Heartbeat_Application::push (const RtecEventComm::EventSet &events
ACE_ENV_ARG_DECL_NOT_USED)
ACE_THROW_SPEC((CORBA::SystemException))
{
for (CORBA::ULong i = 0; i < events.length (); ++i)
{
// Figure out heartbeat source.
const u_char * buffer = events[i].data.payload.get_buffer ();
pid_t pid = *((pid_t*) buffer);
char * host = (char*) buffer + sizeof (pid);
// Update heartbeat database.
int found = 0;
size_t size = this->heartbeats_.size ();
for (size_t j = 0; j < size; ++j)
{
if (this->heartbeats_[j].pid == pid
&& ACE_OS::strcmp (this->heartbeats_[j].hostname, host)
== 0)
{
this->heartbeats_[j].total++;
found = 1;
break;
}
}
// Make new entry in the database.
if (!found)
{
if (this->heartbeats_.size (size + 1)
== -1)
{
ACE_ERROR ((LM_ERROR,
"Unable to add new entry "
"to heartbeat database \n"));
break;
}
this->heartbeats_[size].pid = pid;
this->heartbeats_[size].total = 1;
ACE_OS::memcpy (this->heartbeats_[size].hostname,
host,
ACE_OS::strlen (host) + 1);
}
}
}
void
Heartbeat_Application::disconnect_push_consumer (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
ACE_THROW_SPEC((CORBA::SystemException))
{
this->shutdown ();
}
void
Heartbeat_Application::destroy_ec (void)
{
if (!CORBA::is_nil (this->ec_.in ()))
{
ACE_TRY_NEW_ENV
{
this->ec_->destroy (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
}
ACE_CATCHANY
{
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
"Suppressed the following exception in "
"Application_Heartbeat::destroy_ec\n");
}
ACE_ENDTRY;
this->ec_ = RtecEventChannelAdmin::EventChannel::_nil ();
}
}
void
Heartbeat_Application::shutdown (void)
{
if (!this->initialized_)
return;
this->initialized_ = 0;
// Deregister from Reactor.
this->stop_timeouts ();
// Disconnect from ECs as a consumer.
this->supplier_proxy_disconnect_.execute ();
// Disconnect from EC as a supplier.
this->consumer_proxy_disconnect_.execute ();
if (destroy_ec_flag)
{
this->destroy_ec ();
}
// Deregister from POA.
this->deactivator_.deactivate ();
// Print out heartbeats report.
pid_t pid = ACE_OS::getpid ();
char hostname[MAXHOSTNAMELEN + 1];
if (gethostname (hostname, MAXHOSTNAMELEN) != 0)
{
ACE_ERROR ((LM_ERROR,
"Heartbeat_Application::shutdown - "
"cannot get hostname\n"));
hostname[0] = '\0';
}
ACE_DEBUG ((LM_DEBUG,
"%d@%s Received following heartbeats:\n",
pid, hostname));
for (size_t i = 0; i < this->heartbeats_.size (); ++i)
{
ACE_DEBUG ((LM_DEBUG,
"Host %s, pid %d - total of %u\n",
this->heartbeats_[i].hostname,
this->heartbeats_[i].pid,
this->heartbeats_[i].total));
}
// Shutdown the ORB.
ACE_TRY_NEW_ENV
{
this->orb_->shutdown (0 ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
}
ACE_CATCHANY
{
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
"The following exception occured in "
"Heartbeat_Application::shutdown:\n");
}
ACE_ENDTRY;
}
////////////////////////////////////////////////////////////
int
check_for_nil (CORBA::Object_ptr obj, const char *message)
{
if (CORBA::is_nil (obj))
ACE_ERROR_RETURN ((LM_ERROR,
"ERROR: Object reference <%s> is nil\n",
message),
-1);
else
return 0;
}
int
parse_args (int argc, char ** argv)
{
ACE_Get_Opt get_opt (argc, argv, "d");
int opt;
while ((opt = get_opt ()) != EOF)
{
switch (opt)
{
case 'd':
destroy_ec_flag = 1;
break;
case '?':
default:
ACE_DEBUG ((LM_DEBUG,
"Usage: %s "
"-d"
"\n",
argv[0]));
return -1;
}
}
return 0;
}
int
main (int argc, char *argv[])
{
// We may want this to be alive beyond the next block.
TAO_EC_Servant_Var<Heartbeat_Application> app;
ACE_TRY_NEW_ENV
{
// Initialize ORB and POA, POA Manager, parse args.
CORBA::ORB_var orb =
CORBA::ORB_init (argc, argv, "" ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
if (parse_args (argc, argv) == -1)
return 1;
CORBA::Object_var obj =
orb->resolve_initial_references ("RootPOA" ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
PortableServer::POA_var poa =
PortableServer::POA::_narrow (obj.in () ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
if (check_for_nil (poa.in (), "POA") == -1)
return 1;
PortableServer::POAManager_var manager =
poa->the_POAManager (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
// Obtain reference to EC.
obj = orb->resolve_initial_references ("Event_Service" ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
RtecEventChannelAdmin::EventChannel_var ec =
RtecEventChannelAdmin::EventChannel::_narrow (obj.in ()
ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
if (check_for_nil (ec.in (), "EC") == -1)
return 1;
// Init our application.
app = new Heartbeat_Application;
if (!app.in ())
return 1;
app->init (orb, ec ACE_ENV_ARG_PARAMETER);
ACE_TRY_CHECK;
// Allow processing of CORBA requests.
manager->activate (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
// Receive events from EC.
orb->run (ACE_ENV_SINGLE_ARG_PARAMETER);
ACE_TRY_CHECK;
}
ACE_CATCHANY
{
ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
"Exception in Heartbeat Application:");
// Since there was an exception, application might not have had
// a chance to shutdown.
app->shutdown ();
return 1;
}
ACE_ENDTRY;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -