109 lines
2.5 KiB
C++
109 lines
2.5 KiB
C++
|
|
/*
|
||
|
|
Copyright (c) 2013 IOnU Security Inc. All rights reserved
|
||
|
|
Created October 2013 by Kendrick Webster
|
||
|
|
|
||
|
|
K2Daemon/ClientMap.cpp - implementation for ClientMap.h
|
||
|
|
*/
|
||
|
|
#if !defined(NDEBUG)
|
||
|
|
#define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
|
||
|
|
#define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#include <boost/multi_index_container.hpp>
|
||
|
|
#include <boost/multi_index/hashed_index.hpp>
|
||
|
|
#include <boost/multi_index/member.hpp>
|
||
|
|
#include <string>
|
||
|
|
#include <stdint.h>
|
||
|
|
#include "global.h"
|
||
|
|
#include "ClientMap.h"
|
||
|
|
|
||
|
|
using boost::multi_index_container;
|
||
|
|
using namespace boost::multi_index;
|
||
|
|
|
||
|
|
namespace ClientMap
|
||
|
|
{
|
||
|
|
struct CMapping
|
||
|
|
{
|
||
|
|
id_t id;
|
||
|
|
std::string urn;
|
||
|
|
};
|
||
|
|
struct by_id {};
|
||
|
|
struct by_urn {};
|
||
|
|
typedef multi_index_container<
|
||
|
|
CMapping,
|
||
|
|
indexed_by<
|
||
|
|
hashed_unique<
|
||
|
|
tag<by_id>,
|
||
|
|
BOOST_MULTI_INDEX_MEMBER(CMapping, id_t, id)
|
||
|
|
>,
|
||
|
|
hashed_unique<
|
||
|
|
tag<by_urn>,
|
||
|
|
BOOST_MULTI_INDEX_MEMBER(CMapping, std::string, urn)
|
||
|
|
>
|
||
|
|
>
|
||
|
|
> client_map_t;
|
||
|
|
typedef client_map_t::index<by_id>::type& id_index_t;
|
||
|
|
typedef client_map_t::index<by_urn>::type& urn_index_t;
|
||
|
|
typedef client_map_t::index<by_id>::type::iterator id_iterator_t;
|
||
|
|
typedef client_map_t::index<by_urn>::type::iterator urn_iterator_t;
|
||
|
|
|
||
|
|
static client_map_t client_map;
|
||
|
|
}
|
||
|
|
|
||
|
|
const char* ClientMap::UrnFromId(id_t id)
|
||
|
|
{
|
||
|
|
id_index_t idx = client_map.get<by_id>();
|
||
|
|
id_iterator_t it = idx.find(id);
|
||
|
|
if (it != idx.end())
|
||
|
|
{
|
||
|
|
return it->urn.c_str();
|
||
|
|
}
|
||
|
|
return NULL;
|
||
|
|
}
|
||
|
|
|
||
|
|
ClientMap::id_t ClientMap::IdFromUrn(const char* urn)
|
||
|
|
{
|
||
|
|
urn_index_t idx = client_map.get<by_urn>();
|
||
|
|
urn_iterator_t it = idx.find(urn);
|
||
|
|
if (it != idx.end())
|
||
|
|
{
|
||
|
|
return it->id;
|
||
|
|
}
|
||
|
|
return NULL_ID;
|
||
|
|
}
|
||
|
|
|
||
|
|
ClientMap::id_t ClientMap::Map(const char* urn)
|
||
|
|
{
|
||
|
|
urn_index_t idx = client_map.get<by_urn>();
|
||
|
|
urn_iterator_t it = idx.find(urn);
|
||
|
|
if (it != idx.end())
|
||
|
|
{
|
||
|
|
return it->id;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
static id_t next_id = 0;
|
||
|
|
CMapping m;
|
||
|
|
m.id = ++next_id;
|
||
|
|
m.urn = urn;
|
||
|
|
idx.insert(m);
|
||
|
|
return m.id;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void ClientMap::Unmap(const char* urn)
|
||
|
|
{
|
||
|
|
urn_index_t idx = client_map.get<by_urn>();
|
||
|
|
urn_iterator_t it = idx.find(urn);
|
||
|
|
if (it != idx.end())
|
||
|
|
{
|
||
|
|
logprintf(Log::debug2, "removing client mapping: id(%d) <--> urn(%s)", it->id, urn);
|
||
|
|
idx.erase(it);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
unsigned int ClientMap::Count(void)
|
||
|
|
{
|
||
|
|
return client_map.size();
|
||
|
|
}
|