171 lines
4.3 KiB
C++

//----------------------------------------------------------------------------
//
// Copyright (C) Intel Corporation, 2006 - 2007.
//
// File: DevicePresence.cpp
//
// Contents: Notifies management consoles on tunnel connections.
//
// Notes:
//----------------------------------------------------------------------------
#include "DevicePresence.h"
#include "global.h"
#include "SOAPCommunicator.h"
int DevicePresence::start(void)
{
ACE_DEBUG((MY_TRACE
ACE_TEXT(" Device Presence task opened\n")));
if (_is_open)
return 0;
_is_open = true;
this->msg_queue()->high_water_mark(*getMaxQueueSize());
// Register this handler in the reactor
ACE_Reactor *TPReactor = ACE_Reactor::instance ();
this->reactor (TPReactor);
// Start the notification strategy
_notification_strategy = new ACE_Reactor_Notification_Strategy(
ACE_Reactor::instance(),
this,
ACE_Event_Handler::WRITE_MASK);
this->_notification_strategy->mask(ACE_Event_Handler::WRITE_MASK);
this->msg_queue()->notification_strategy(_notification_strategy);
return 0;
}
// Send Notification to MC
void DevicePresence::sendEvent( string &MEAddress ,
unsigned short MEPort,
string &ME_UUID,
string &MPSAddress,
unsigned short MPSHttpPort,
unsigned short MPSSocksPort,
mps__ConnectionStateTypeDefinition state)
{
NotificationMessage *msg = new NotificationMessage( MEAddress ,
MEPort,
ME_UUID,
MPSAddress,
MPSHttpPort,
MPSSocksPort,
state);
this->putq(msg);
}
int DevicePresence::handle_output (ACE_HANDLE)
{
ACE_TRACE(ACE_TEXT("AMT_Tunnel_Handler::handle_output"));
STATUS res = STATUS_SUCCESS;
ACE_Message_Block *mb = 0;
while (! this->msg_queue()->is_empty())
{
//try to acquire the mutex
// if failed - some other thread is running handle_output now.
TRY_ACQUIRE_GUARD_RETURN( ACE_Recursive_Thread_Mutex ,
lock,
_output_mutex,
0);
int qcount = this->getq(mb);
if (mb != 0)
{
// Send this message to it's destination
sendEvent((NotificationMessage*)mb);
ACE_Message_Block::release (mb);
}
}
_output_mutex.release();
return 0;
}
// handle close
int DevicePresence::handle_close(ACE_HANDLE, ACE_Reactor_Mask close_mask)
{
ACE_Reactor * TPReactor = ACE_Reactor::instance ();
TPReactor->remove_handler (this,
ACE_Event_Handler::ALL_EVENTS_MASK |
ACE_Event_Handler::DONT_CALL); // Don't call handle_close
this->msg_queue_->flush();
this->reactor(0);
return 0;
}
//-----------------------------------------
// Sends events to MC already exist in MC list.
// if evnt_reason is connect - sends connect event, etc.
//-----------------------------------------
void DevicePresence::sendEvent(NotificationMessage *msg)
{
if (msg == NULL) return;
ACE_READ_GUARD(ACE_RW_Thread_Mutex,
lock,
_mc_list.guard());
MCList::const_iterator i;
for(i=_mc_list.begin(); i != _mc_list.end(); ++i)
{
const URL_Wrapper * mc = *i;
//send alert
if (_soap_comm == NULL)
{
if (mc->validateServer() != STATUS_SUCCESS)
{
ACE_DEBUG((MY_DEBUG ACE_TEXT("Can not notify address %s.\n"),
mc->getURLStr().c_str()));
continue;
}
const bool* needAuthenticationPtr = getMCNeedAuthentication();
const ACE_TString* MCUsername = getMCUsername();
const ACE_TString* MCPassword = getMCPassword();
if (needAuthenticationPtr == NULL || MCUsername == NULL || MCPassword == NULL) {
ACE_DEBUG((MY_DEBUG
ACE_TEXT("Failed to get MC Authentication details\n")));
return;
}
_soap_comm = new SOAPCommunicator( mc->getURLStr().c_str(),
*needAuthenticationPtr,
MCUsername->c_str(),
MCPassword->c_str());
}
else
{
if (mc->validateServer() != STATUS_SUCCESS)
{
ACE_DEBUG((MY_DEBUG ACE_TEXT("Can not notify address %s.\n"),
mc->getURLStr().c_str()));
continue;
}
_soap_comm->SetTarget((mc->getURLStr()).c_str());
}
if(_soap_comm->SendEvent(msg->_MEAddress.c_str(),
msg->_MEPort,
msg->_ME_UUID,
msg->_MPSAddress,
msg->_MPSHttpPort,
msg->_MPSSocksPort,
msg->_state) != STATUS_SUCCESS)
{
ACE_DEBUG((MY_DEBUG ACE_TEXT("Can not notify address %s.\n"),
mc->getURLStr().c_str()));
}
}
}