189 lines
6.0 KiB
C++
189 lines
6.0 KiB
C++
//----------------------------------------------------------------------------
|
|
//
|
|
// Copyright (C) 2010 Intel Corporation
|
|
//
|
|
// File: ConnectedSystems.cpp
|
|
//
|
|
// Contents: This class handles maps of sets of connected systems
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
#include "TunnelManager.h"
|
|
#include "ConnectedSystems.h"
|
|
#include "OptionsUtils.h"
|
|
|
|
//*****************************************************************************
|
|
// Construct Connected_Systems_Enumeration_Data by retrieve current connected
|
|
// systems from Tannel_Manager
|
|
// Initiate _systems_set_iterator to the begin of the set
|
|
// Initiate Expires to current time plus EnumCtxTimeout config param
|
|
//*****************************************************************************
|
|
Connected_Systems_Enumeration_Data::Connected_Systems_Enumeration_Data()
|
|
{
|
|
ACE_TRACE("Connected_Systems_Enumeration_Data::Connected_Systems_Enumeration_Data");
|
|
ACE_Write_Guard< ACE_Recursive_Thread_Mutex > tunnel_lock(Tunnel_Manager::instance().guard());
|
|
if (tunnel_lock.locked () == 0)
|
|
{
|
|
ACE_DEBUG((MY_DEBUG
|
|
ACE_TEXT("Error locking Tannel_Manager (%x)\n"),
|
|
this));
|
|
throw ("Error locking");
|
|
}
|
|
|
|
const TUNNEL_MAP* _tunnel_map = Tunnel_Manager::instance().get_tunnel_map();
|
|
TUNNEL_MAP::const_iterator tunnel_map_iterator;
|
|
|
|
for(tunnel_map_iterator =_tunnel_map->begin();
|
|
tunnel_map_iterator != _tunnel_map->end();
|
|
tunnel_map_iterator++)
|
|
{
|
|
ACE_CString fullAddress = tunnel_map_iterator->first;
|
|
ACE_CString::size_type sep = fullAddress.find(":",0);
|
|
if (sep == ACE_CString::npos) {
|
|
continue;
|
|
}
|
|
ACE_CString fqdn = fullAddress.substr(0,sep);
|
|
_systems_set.insert(fqdn);
|
|
}
|
|
|
|
_systems_set_iterator = _systems_set.begin();
|
|
_expires = time(NULL) + *(getEnumCtxTimeout());
|
|
}
|
|
|
|
//*****************************************************************************
|
|
// Contructor of Connected_Systems
|
|
//*****************************************************************************
|
|
Connected_Systems::Connected_Systems():_curr_context(0)
|
|
{
|
|
}
|
|
|
|
|
|
|
|
//*****************************************************************************
|
|
// Contructor of Connected_Systems
|
|
//*****************************************************************************
|
|
bool Connected_Systems::add_new_enumeration(ACE_UINT32 &context, const ConnectedSystemsEnumerationDataPtr& cs)
|
|
{
|
|
|
|
ACE_TRACE("Connected_Systems_Enumeration_Data::add_new_enumeration");
|
|
ACE_WRITE_GUARD_RETURN( ACE_Recursive_Thread_Mutex,
|
|
lock,
|
|
rwmutex_,
|
|
false);
|
|
|
|
do{
|
|
++_curr_context;
|
|
if (_curr_context == UINT_MAX-1)
|
|
_curr_context = 1;
|
|
} while (_connected_systems_map.find(_curr_context) != _connected_systems_map.end());
|
|
|
|
_connected_systems_map[_curr_context] = cs;
|
|
|
|
context = _curr_context;
|
|
|
|
return true;
|
|
}
|
|
|
|
//*****************************************************************************
|
|
// Checks if context exist in _connected_systems_map
|
|
//*****************************************************************************
|
|
bool Connected_Systems::context_exists(ACE_UINT32 context)
|
|
{
|
|
ACE_READ_GUARD_RETURN( ACE_Recursive_Thread_Mutex,
|
|
lock,
|
|
rwmutex_,
|
|
false);
|
|
|
|
return (_connected_systems_map.find(context) != _connected_systems_map.end());
|
|
}
|
|
|
|
//*****************************************************************************
|
|
// Retrieves pointer to enumeration context
|
|
//*****************************************************************************
|
|
ConnectedSystemsEnumerationDataPtr* Connected_Systems::get_connected_system_enumeration_data_ptr(ACE_UINT32 context)
|
|
{
|
|
ACE_TRACE("Connected_Systems_Enumeration_Data::get_connected_system_enumeration_data_ptr");
|
|
ACE_READ_GUARD_RETURN( ACE_Recursive_Thread_Mutex,
|
|
lock,
|
|
rwmutex_,
|
|
NULL);
|
|
|
|
CONNECTED_SYSTEMS_MAP::iterator connected_system_iterator = _connected_systems_map.find(context) ;
|
|
if (connected_system_iterator == _connected_systems_map.end())
|
|
{
|
|
ACE_DEBUG((MY_DEBUG
|
|
ACE_TEXT("Context %d does not exist)\n"),
|
|
context));
|
|
return NULL;
|
|
}
|
|
return (&connected_system_iterator->second);
|
|
}
|
|
|
|
|
|
//*****************************************************************************
|
|
// Erases a context from _connected_systems_map
|
|
//*****************************************************************************
|
|
bool Connected_Systems::release_context(ACE_UINT32 context)
|
|
{
|
|
ACE_TRACE("Connected_Systems_Enumeration_Data::release_context");
|
|
ACE_WRITE_GUARD_RETURN( ACE_Recursive_Thread_Mutex,
|
|
lock,
|
|
rwmutex_,
|
|
false);
|
|
|
|
CONNECTED_SYSTEMS_MAP::iterator connected_system_iterator = _connected_systems_map.find(context) ;
|
|
if (connected_system_iterator == _connected_systems_map.end())
|
|
{
|
|
ACE_DEBUG((MY_DEBUG
|
|
ACE_TEXT("Context %d does not exist)\n"),
|
|
context));
|
|
return false;
|
|
}
|
|
_connected_systems_map.erase(connected_system_iterator);
|
|
return true;
|
|
}
|
|
|
|
|
|
//*****************************************************************************
|
|
// Checks tha map and release expired contexts
|
|
//*****************************************************************************
|
|
bool Connected_Systems::release_expired_enumerations(void)
|
|
{
|
|
ACE_TRACE("Connected_Systems_Enumeration_Data::release_expired_enumerations");
|
|
ACE_WRITE_GUARD_RETURN( ACE_Recursive_Thread_Mutex,
|
|
lock,
|
|
rwmutex_,
|
|
false);
|
|
|
|
CONNECTED_SYSTEMS_MAP::iterator connected_systems_iterator;
|
|
|
|
const time_t curr = time(NULL);
|
|
|
|
for(connected_systems_iterator = _connected_systems_map.begin();
|
|
connected_systems_iterator != _connected_systems_map.end();
|
|
)
|
|
{
|
|
if (curr > connected_systems_iterator->second->get_expires())
|
|
{
|
|
ACE_DEBUG((MY_DEBUG
|
|
ACE_TEXT("Enumeration context (%d) expired)\n"),
|
|
connected_systems_iterator->first));
|
|
_connected_systems_map.erase(connected_systems_iterator++);
|
|
}
|
|
else
|
|
++connected_systems_iterator;
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
//*****************************************************************************
|
|
// Checks tha map size to see if maximum concurrent enumeration exceeeded
|
|
//*****************************************************************************
|
|
bool Connected_Systems::max_concurrent_enums_exceeded(void)
|
|
{
|
|
return (_connected_systems_map.size() >= *getMaxConcurrentEnums());
|
|
}
|