mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-16 08:56:51 +01:00
@@ -51,38 +51,41 @@ ClientList::~ClientList()
|
||||
|
||||
void ClientList::initialize(bool aggregate)
|
||||
{
|
||||
if (theGateway->getGWParams()->clientAuthentication )
|
||||
if (theGateway->getGWParams()->clientAuthentication)
|
||||
{
|
||||
int type = TRANSPEARENT_TYPE;
|
||||
if ( aggregate )
|
||||
{
|
||||
type = AGGREGATER_TYPE;
|
||||
}
|
||||
setClientList(type);
|
||||
int type = TRANSPEARENT_TYPE;
|
||||
if (aggregate)
|
||||
{
|
||||
type = AGGREGATER_TYPE;
|
||||
}
|
||||
setClientList(type);
|
||||
_authorize = true;
|
||||
}
|
||||
|
||||
if ( theGateway->getGWParams()->predefinedTopic )
|
||||
if (theGateway->getGWParams()->predefinedTopic)
|
||||
{
|
||||
setPredefinedTopics(aggregate);
|
||||
setPredefinedTopics(aggregate);
|
||||
}
|
||||
}
|
||||
|
||||
void ClientList::setClientList(int type)
|
||||
{
|
||||
if (!createList(theGateway->getGWParams()->clientListName, type))
|
||||
{
|
||||
throw Exception("ClientList::setClientList No client list defined by config file.");
|
||||
}
|
||||
if (!createList(theGateway->getGWParams()->clientListName, type))
|
||||
{
|
||||
throw Exception(
|
||||
"ClientList::setClientList No client list defined by config file.");
|
||||
}
|
||||
}
|
||||
|
||||
void ClientList::setPredefinedTopics(bool aggrecate)
|
||||
{
|
||||
if ( !readPredefinedList(theGateway->getGWParams()->predefinedTopicFileName, aggrecate) )
|
||||
{
|
||||
throw Exception("ClientList::setPredefinedTopics No predefindTopi list defined by config file.");
|
||||
if (!readPredefinedList(theGateway->getGWParams()->predefinedTopicFileName,
|
||||
aggrecate))
|
||||
{
|
||||
throw Exception(
|
||||
"ClientList::setPredefinedTopics No predefindTopi list defined by config file.");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -148,17 +151,19 @@ bool ClientList::createList(const char* fileName, int type)
|
||||
forwarder = (data.find("forwarder") != string::npos);
|
||||
secure = (data.find("secureConnection") != string::npos);
|
||||
stable = !(data.find("unstableLine") != string::npos);
|
||||
if ( (qos_1 && type == QOSM1PROXY_TYPE) || (!qos_1 && type == AGGREGATER_TYPE) )
|
||||
if ((qos_1 && type == QOSM1PROXY_TYPE)
|
||||
|| (!qos_1 && type == AGGREGATER_TYPE))
|
||||
{
|
||||
createClient(&netAddr, &clientId, stable, secure, type);
|
||||
createClient(&netAddr, &clientId, stable, secure, type);
|
||||
}
|
||||
else if ( forwarder && type == FORWARDER_TYPE)
|
||||
else if (forwarder && type == FORWARDER_TYPE)
|
||||
{
|
||||
theGateway->getAdapterManager()->getForwarderList()->addForwarder(&netAddr, &clientId);
|
||||
theGateway->getAdapterManager()->getForwarderList()->addForwarder(
|
||||
&netAddr, &clientId);
|
||||
}
|
||||
else if (type == TRANSPEARENT_TYPE )
|
||||
else if (type == TRANSPEARENT_TYPE)
|
||||
{
|
||||
createClient(&netAddr, &clientId, stable, secure, type);
|
||||
createClient(&netAddr, &clientId, stable, secure, type);
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -179,7 +184,8 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
|
||||
FILE* fp;
|
||||
char buf[MAX_CLIENTID_LENGTH + 256];
|
||||
size_t pos0, pos1;
|
||||
MQTTSNString clientId = MQTTSNString_initializer;;
|
||||
MQTTSNString clientId = MQTTSNString_initializer;
|
||||
;
|
||||
bool rc = false;
|
||||
|
||||
if ((fp = fopen(fileName, "r")) != 0)
|
||||
@@ -201,12 +207,12 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
|
||||
}
|
||||
|
||||
pos0 = data.find_first_of(",");
|
||||
pos1 = data.find(",", pos0 + 1) ;
|
||||
pos1 = data.find(",", pos0 + 1);
|
||||
string id = data.substr(0, pos0);
|
||||
clientId.cstring = strdup(id.c_str());
|
||||
string topicName = data.substr(pos0 + 1, pos1 - pos0 -1);
|
||||
string topicName = data.substr(pos0 + 1, pos1 - pos0 - 1);
|
||||
uint16_t topicID = stoul(data.substr(pos1 + 1));
|
||||
createPredefinedTopic( &clientId, topicName, topicID, aggregate);
|
||||
createPredefinedTopic(&clientId, topicName, topicID, aggregate);
|
||||
free(clientId.cstring);
|
||||
}
|
||||
fclose(fp);
|
||||
@@ -214,7 +220,8 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
|
||||
}
|
||||
else
|
||||
{
|
||||
WRITELOG("ClientList can not open the Predefined Topic List. %s\n", fileName);
|
||||
WRITELOG("ClientList can not open the Predefined Topic List. %s\n",
|
||||
fileName);
|
||||
return false;
|
||||
}
|
||||
return rc;
|
||||
@@ -222,7 +229,7 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
|
||||
|
||||
void ClientList::erase(Client*& client)
|
||||
{
|
||||
if ( !_authorize && client->erasable())
|
||||
if (!_authorize && client->erasable())
|
||||
{
|
||||
_mutex.lock();
|
||||
Client* prev = client->_prevClient;
|
||||
@@ -247,7 +254,7 @@ void ClientList::erase(Client*& client)
|
||||
}
|
||||
_clientCnt--;
|
||||
Forwarder* fwd = client->getForwarder();
|
||||
if ( fwd )
|
||||
if (fwd)
|
||||
{
|
||||
fwd->eraseClient(client);
|
||||
}
|
||||
@@ -259,14 +266,14 @@ void ClientList::erase(Client*& client)
|
||||
|
||||
Client* ClientList::getClient(SensorNetAddress* addr)
|
||||
{
|
||||
if ( addr )
|
||||
if (addr)
|
||||
{
|
||||
_mutex.lock();
|
||||
Client* client = _firstClient;
|
||||
|
||||
while (client != nullptr)
|
||||
{
|
||||
if (client->getSensorNetAddress()->isMatch(addr) )
|
||||
if (client->getSensorNetAddress()->isMatch(addr))
|
||||
{
|
||||
_mutex.unlock();
|
||||
return client;
|
||||
@@ -280,38 +287,38 @@ Client* ClientList::getClient(SensorNetAddress* addr)
|
||||
|
||||
Client* ClientList::getClient(int index)
|
||||
{
|
||||
Client* client = _firstClient;
|
||||
int p = 0;
|
||||
while ( client != nullptr )
|
||||
{
|
||||
if ( p == index )
|
||||
{
|
||||
return client;
|
||||
}
|
||||
else
|
||||
{
|
||||
client = client->_nextClient;
|
||||
p++;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
Client* client = _firstClient;
|
||||
int p = 0;
|
||||
while (client != nullptr)
|
||||
{
|
||||
if (p == index)
|
||||
{
|
||||
return client;
|
||||
}
|
||||
else
|
||||
{
|
||||
client = client->_nextClient;
|
||||
p++;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
Client* ClientList::getClient(MQTTSNString* clientId)
|
||||
{
|
||||
_mutex.lock();
|
||||
Client* client = _firstClient;
|
||||
const char* clID =clientId->cstring;
|
||||
const char* clID = clientId->cstring;
|
||||
|
||||
if (clID == nullptr )
|
||||
if (clID == nullptr)
|
||||
{
|
||||
clID = clientId->lenstring.data;
|
||||
}
|
||||
|
||||
while (client != nullptr)
|
||||
{
|
||||
if (strncmp((const char*)client->getClientId(), clID, MQTTSNstrlen(*clientId)) == 0 )
|
||||
if (strncmp((const char*) client->getClientId(), clID,
|
||||
MQTTSNstrlen(*clientId)) == 0)
|
||||
{
|
||||
_mutex.unlock();
|
||||
return client;
|
||||
@@ -322,51 +329,53 @@ Client* ClientList::getClient(MQTTSNString* clientId)
|
||||
return 0;
|
||||
}
|
||||
|
||||
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId, int type)
|
||||
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
|
||||
int type)
|
||||
{
|
||||
return createClient(addr, clientId, false, false, type);
|
||||
return createClient(addr, clientId, false, false, type);
|
||||
}
|
||||
|
||||
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId, bool unstableLine, bool secure, int type)
|
||||
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
|
||||
bool unstableLine, bool secure, int type)
|
||||
{
|
||||
Client* client = nullptr;
|
||||
|
||||
/* anonimous clients */
|
||||
if ( _clientCnt > MAX_CLIENTS )
|
||||
if (_clientCnt > MAX_CLIENTS)
|
||||
{
|
||||
return 0; // full of clients
|
||||
}
|
||||
|
||||
client = getClient(addr);
|
||||
if ( client )
|
||||
if (client)
|
||||
{
|
||||
return client;
|
||||
}
|
||||
|
||||
/* creat a new client */
|
||||
client = new Client(secure);
|
||||
if ( addr )
|
||||
if (addr)
|
||||
{
|
||||
client->setClientAddress(addr);
|
||||
}
|
||||
client->setSensorNetType(unstableLine);
|
||||
if ( MQTTSNstrlen(*clientId) )
|
||||
if (MQTTSNstrlen(*clientId))
|
||||
{
|
||||
client->setClientId(*clientId);
|
||||
}
|
||||
else
|
||||
{
|
||||
MQTTSNString dummyId MQTTSNString_initializer;
|
||||
MQTTSNString dummyId MQTTSNString_initializer;
|
||||
dummyId.cstring = strdup("");
|
||||
client->setClientId(dummyId);
|
||||
free(dummyId.cstring);
|
||||
free(dummyId.cstring);
|
||||
}
|
||||
|
||||
if ( type == AGGREGATER_TYPE )
|
||||
if (type == AGGREGATER_TYPE)
|
||||
{
|
||||
client->setAggregated();
|
||||
client->setAggregated();
|
||||
}
|
||||
else if ( type == QOSM1PROXY_TYPE )
|
||||
else if (type == QOSM1PROXY_TYPE)
|
||||
{
|
||||
client->setQoSm1();
|
||||
}
|
||||
@@ -374,7 +383,7 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
|
||||
_mutex.lock();
|
||||
|
||||
/* add the list */
|
||||
if ( _firstClient == nullptr )
|
||||
if (_firstClient == nullptr)
|
||||
{
|
||||
_firstClient = client;
|
||||
_endClient = client;
|
||||
@@ -390,66 +399,68 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
|
||||
return client;
|
||||
}
|
||||
|
||||
Client* ClientList::createPredefinedTopic( MQTTSNString* clientId, string topicName, uint16_t topicId, bool aggregate)
|
||||
Client* ClientList::createPredefinedTopic(MQTTSNString* clientId,
|
||||
string topicName, uint16_t topicId, bool aggregate)
|
||||
{
|
||||
if ( topicId == 0 )
|
||||
{
|
||||
WRITELOG("Invalid TopicId. Predefined Topic %s, TopicId is 0. \n", topicName.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
if (topicId == 0)
|
||||
{
|
||||
WRITELOG("Invalid TopicId. Predefined Topic %s, TopicId is 0. \n",
|
||||
topicName.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if ( strcmp(clientId->cstring, common_topic) == 0 )
|
||||
{
|
||||
theGateway->getTopics()->add((const char*)topicName.c_str(), topicId);
|
||||
return nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
Client* client = getClient(clientId);
|
||||
if (strcmp(clientId->cstring, common_topic) == 0)
|
||||
{
|
||||
theGateway->getTopics()->add((const char*) topicName.c_str(), topicId);
|
||||
return nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
Client* client = getClient(clientId);
|
||||
|
||||
if ( _authorize && client == nullptr )
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
if (_authorize && client == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* anonimous clients */
|
||||
if ( _clientCnt > MAX_CLIENTS )
|
||||
{
|
||||
return nullptr; // full of clients
|
||||
}
|
||||
/* anonimous clients */
|
||||
if (_clientCnt > MAX_CLIENTS)
|
||||
{
|
||||
return nullptr; // full of clients
|
||||
}
|
||||
|
||||
if ( client == nullptr )
|
||||
{
|
||||
/* creat a new client */
|
||||
client = new Client();
|
||||
client->setClientId(*clientId);
|
||||
if ( aggregate )
|
||||
{
|
||||
client->setAggregated();
|
||||
}
|
||||
_mutex.lock();
|
||||
if (client == nullptr)
|
||||
{
|
||||
/* creat a new client */
|
||||
client = new Client();
|
||||
client->setClientId(*clientId);
|
||||
if (aggregate)
|
||||
{
|
||||
client->setAggregated();
|
||||
}
|
||||
_mutex.lock();
|
||||
|
||||
/* add the list */
|
||||
if ( _firstClient == nullptr )
|
||||
{
|
||||
_firstClient = client;
|
||||
_endClient = client;
|
||||
}
|
||||
else
|
||||
{
|
||||
_endClient->_nextClient = client;
|
||||
client->_prevClient = _endClient;
|
||||
_endClient = client;
|
||||
}
|
||||
_clientCnt++;
|
||||
_mutex.unlock();
|
||||
}
|
||||
/* add the list */
|
||||
if (_firstClient == nullptr)
|
||||
{
|
||||
_firstClient = client;
|
||||
_endClient = client;
|
||||
}
|
||||
else
|
||||
{
|
||||
_endClient->_nextClient = client;
|
||||
client->_prevClient = _endClient;
|
||||
_endClient = client;
|
||||
}
|
||||
_clientCnt++;
|
||||
_mutex.unlock();
|
||||
}
|
||||
|
||||
// create Topic & Add it
|
||||
client->getTopics()->add((const char*)topicName.c_str(), topicId);
|
||||
client->_hasPredefTopic = true;
|
||||
return client;
|
||||
}
|
||||
// create Topic & Add it
|
||||
client->getTopics()->add((const char*) topicName.c_str(), topicId);
|
||||
client->_hasPredefTopic = true;
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t ClientList::getClientCount()
|
||||
@@ -462,4 +473,3 @@ bool ClientList::isAuthorized()
|
||||
return _authorize;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user