Set whether to run ClientProxy at startup.

If it does not start, do not create an instance of ClientProxy.
Change the name of ClientProxy to QoSm1Proxy

Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
tomoaki
2018-07-31 15:49:25 +09:00
parent e942ee451d
commit df080f2851
11 changed files with 112 additions and 88 deletions

View File

@@ -40,7 +40,7 @@ $(SRCDIR)/MQTTSNGWPublishHandler.cpp \
$(SRCDIR)/MQTTSNGWSubscribeHandler.cpp \ $(SRCDIR)/MQTTSNGWSubscribeHandler.cpp \
$(SRCDIR)/MQTTSNGWEncapsulatedPacket.cpp \ $(SRCDIR)/MQTTSNGWEncapsulatedPacket.cpp \
$(SRCDIR)/MQTTSNGWForwarder.cpp \ $(SRCDIR)/MQTTSNGWForwarder.cpp \
$(SRCDIR)/MQTTSNGWClientProxy.cpp \ $(SRCDIR)/MQTTSNGWQoS-1Proxy.cpp \
$(SRCDIR)/$(OS)/$(SENSORNET)/SensorNetwork.cpp \ $(SRCDIR)/$(OS)/$(SENSORNET)/SensorNetwork.cpp \
$(SRCDIR)/$(OS)/Timer.cpp \ $(SRCDIR)/$(OS)/Timer.cpp \
$(SRCDIR)/$(OS)/Network.cpp \ $(SRCDIR)/$(OS)/Network.cpp \

View File

@@ -72,7 +72,7 @@ Client should know the MulticastIP and MulticastPortNo to send a SEARCHGW messag
when **ClientAuthentication** is YES, see MQTTSNGWClient.cpp line53, clients file specified by ClientsList is required. This file defines connect allowed clients by ClientId and SensorNetwork Address. e.g. IP address and Port No. when **ClientAuthentication** is YES, see MQTTSNGWClient.cpp line53, clients file specified by ClientsList is required. This file defines connect allowed clients by ClientId and SensorNetwork Address. e.g. IP address and Port No.
When **PredefinedTopic** is YES, Pre-definedTopicID file specified by PredefinedTopicList is effective. This file defines Pre-definedTopics of the clients. In this file, ClientID,TopicName and TopicID are declared in CSV format. When **PredefinedTopic** is YES, Pre-definedTopicID file specified by PredefinedTopicList is effective. This file defines Pre-definedTopics of the clients. In this file, ClientID,TopicName and TopicID are declared in CSV format.
When **Forwarder** is YES, Forwarder Encapsulation Message is available. Connectable Forwarders are specifed by ForwardersList file. In this file, ForwarderIds and those sensorNet addresses are declared in CSV format. When **Forwarder** is YES, Forwarder Encapsulation Message is available. Connectable Forwarders are specifed by ForwardersList file. In this file, ForwarderIds and those sensorNet addresses are declared in CSV format.
When **QoS-1** is YES, QoS-1 PUBLISH Message is available. Clients which allow to send it, are specifed by QoS-1ClientsList file. In this file, ClientsId and those sensorNet addresses are declared in CSV format. When **QoS-1** is YES, QoS-1 PUBLISH Message is available. Clients which allow to send it, are specifed by QoS-1ClientsList file. In this file, ClientsId and those sensorNet addresses are declared in CSV format. QoS-1ProxyName is a ClientId of the proxy.

View File

@@ -18,16 +18,17 @@ BrokerPortNo=1883
BrokerSecurePortNo=8883 BrokerSecurePortNo=8883
ClientAuthentication=NO ClientAuthentication=NO
ClientsList=/path/to/your_clients.conf #ClientsList=/path/to/your_clients.conf
PredefinedTopic=NO PredefinedTopic=NO
PredefinedTopicList=/path/to/your_predefinedTopic.conf #PredefinedTopicList=/path/to/your_predefinedTopic.conf
Forwarder=NO Forwarder=NO
ForwardersList=/path/to/your_forwarers.conf #ForwardersList=/path/to/your_forwarers.conf
QoS-1=NO QoS-1=NO
QoS-1ClientsList=/path/to/your_qos-1clients.conf OoS-1ProxyName=Proxy007
#QoS-1ClientsList=/path/to/your_qos-1clients.conf
#RootCAfile=/etc/ssl/certs/ca-certificates.crt #RootCAfile=/etc/ssl/certs/ca-certificates.crt
#RootCApath=/etc/ssl/certs/ #RootCApath=/etc/ssl/certs/

View File

@@ -128,21 +128,28 @@ void ClientRecvTask::run()
} }
else else
{ {
const char* clientName = _gateway->getClientProxy()->getClientId(_sensorNetwork->getSenderAddress()); /* when QoSm1Proxy is available, select QoS-1 PUBLISH message */
QoSm1Proxy* pxy = _gateway->getQoSm1Proxy();
if ( pxy )
{
/* get ClientId not Client which can send QoS-1 PUBLISH */
const char* clientName = pxy->getClientId(_sensorNetwork->getSenderAddress());
if ( clientName ) // This client is for QoS-1 PUBLISH. if ( clientName )
{
if ( packet->isQoSMinusPUBLISH() )
{ {
client = _gateway->getClientProxy()->getClient(); // point to the ClientProxy if ( packet->isQoSMinusPUBLISH() )
} {
else /* QoS1Proxy takes responsibility of the client */
{ client = _gateway->getQoSm1Proxy()->getClient();
client = _gateway->getClientProxy()->getClient(); }
log(clientName, packet); else
WRITELOG("%s %s %s can send only PUBLISH with QoS-1.%s\n", ERRMSG_HEADER, clientName, _sensorNetwork->getSenderAddress()->sprint(buf), ERRMSG_FOOTER); {
delete packet; client = _gateway->getQoSm1Proxy()->getClient();
continue; log(clientName, packet);
WRITELOG("%s %s %s can send only PUBLISH with QoS-1.%s\n", ERRMSG_HEADER, clientName, _sensorNetwork->getSenderAddress()->sprint(buf), ERRMSG_FOOTER);
delete packet;
continue;
}
} }
} }
else else
@@ -221,16 +228,6 @@ void ClientRecvTask::run()
log(client, packet, 0); log(client, packet, 0);
WRITELOG("%s Client(%s) is not connecting. message has been discarded.%s\n", ERRMSG_HEADER, _sensorNetwork->getSenderAddress()->sprint(buf), ERRMSG_FOOTER); WRITELOG("%s Client(%s) is not connecting. message has been discarded.%s\n", ERRMSG_HEADER, _sensorNetwork->getSenderAddress()->sprint(buf), ERRMSG_FOOTER);
delete packet; delete packet;
/* Send DISCONNECT */
if ( fwd == 0 )
{
packet = new MQTTSNPacket();
packet->setDISCONNECT(0);
ev = new Event();
ev->setClientSendEvent(_sensorNetwork->getSenderAddress(), packet);
_gateway->getClientSendQue()->post(ev);
}
} }
} }
} }

View File

@@ -73,7 +73,7 @@ void ClientSendTask::run()
log(client, packet); log(client, packet);
if ( client->isProxy() ) if ( client->isProxy() )
{ {
_gateway->getClientProxy()->send(packet); _gateway->getQoSm1Proxy()->send(packet);
continue; continue;
} }
rc = packet->unicast(_sensorNetwork, client->getSensorNetAddress()); rc = packet->unicast(_sensorNetwork, client->getSensorNetAddress());

View File

@@ -114,8 +114,12 @@ void PacketHandleTask::run()
_advertiseTimer.start(_gateway->getGWParams()->keepAlive * 1000UL); _advertiseTimer.start(_gateway->getGWParams()->keepAlive * 1000UL);
} }
/*------ Check ClientProxy to Connect or send PINGREQ ------*/ /*------ Check QoS-1 Proxy Connect or PINGREQ ------*/
_gateway->getClientProxy()->checkConnection(); QoSm1Proxy* pxy = _gateway->getQoSm1Proxy();
if ( pxy )
{
pxy->checkConnection();
}
} }
/*------ Handle SEARCHGW Message ---------*/ /*------ Handle SEARCHGW Message ---------*/

View File

@@ -127,7 +127,7 @@ void MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet)
/* reset PINGREQ of ClientProxy */ /* reset PINGREQ of ClientProxy */
if ( qos == 3 ) if ( qos == 3 )
{ {
_gateway->getClientProxy()->resetPingTimer(); _gateway->getQoSm1Proxy()->resetPingTimer();
} }
} }

View File

@@ -15,8 +15,9 @@
**************************************************************************************/ **************************************************************************************/
#include "MQTTSNGWQoS-1Proxy.h"
#include "MQTTSNGWDefines.h" #include "MQTTSNGWDefines.h"
#include "MQTTSNGWClientProxy.h"
#include "MQTTSNGateway.h" #include "MQTTSNGateway.h"
#include "SensorNetwork.h" #include "SensorNetwork.h"
#include <string> #include <string>
@@ -32,21 +33,21 @@ using namespace MQTTSNGW;
* Class ClientProxyElement * Class ClientProxyElement
*/ */
ClientProxyElement::ClientProxyElement(void) QoSm1ProxyElement::QoSm1ProxyElement(void)
: _clientId{0} : _clientId{0}
, _next{0} , _next{0}
{ {
} }
ClientProxyElement::ClientProxyElement(SensorNetAddress* addr, string* clientId) QoSm1ProxyElement::QoSm1ProxyElement(SensorNetAddress* addr, string* clientId)
: _next{0} : _next{0}
{ {
_clientId = *clientId; _clientId = *clientId;
_sensorNetAddr = *addr; _sensorNetAddr = *addr;
} }
ClientProxyElement::~ClientProxyElement(void) QoSm1ProxyElement::~QoSm1ProxyElement(void)
{ {
} }
@@ -55,14 +56,14 @@ ClientProxyElement::~ClientProxyElement(void)
* Class ClientProxy * Class ClientProxy
*/ */
ClientProxy:: ClientProxy(void) QoSm1Proxy:: QoSm1Proxy(void)
: _head{0} : _head{0}
{ {
_gateway = 0; _gateway = 0;
_client = 0; _client = 0;
} }
ClientProxy:: ClientProxy(Gateway* gw) QoSm1Proxy:: QoSm1Proxy(Gateway* gw)
: _head{0} : _head{0}
{ {
_gateway = gw; _gateway = gw;
@@ -70,35 +71,35 @@ ClientProxy:: ClientProxy(Gateway* gw)
} }
ClientProxy::~ClientProxy(void) QoSm1Proxy::~QoSm1Proxy(void)
{ {
if ( _head ) if ( _head )
{ {
ClientProxyElement* p = _head; QoSm1ProxyElement* p = _head;
while ( p ) while ( p )
{ {
ClientProxyElement* next = p->_next; QoSm1ProxyElement* next = p->_next;
delete p; delete p;
p = next; p = next;
} }
} }
} }
void ClientProxy::setGateway(Gateway* gw) void QoSm1Proxy::setGateway(Gateway* gw)
{ {
_gateway = gw; _gateway = gw;
} }
ClientProxyElement* ClientProxy::add(SensorNetAddress* addr, string* clientId) QoSm1ProxyElement* QoSm1Proxy::add(SensorNetAddress* addr, string* clientId)
{ {
ClientProxyElement* elm = new ClientProxyElement(addr, clientId); QoSm1ProxyElement* elm = new QoSm1ProxyElement(addr, clientId);
if ( _head == 0 ) if ( _head == 0 )
{ {
_head = elm; _head = elm;
} }
else else
{ {
ClientProxyElement* p = _head; QoSm1ProxyElement* p = _head;
while ( p ) while ( p )
{ {
if ( p->_next == 0 ) if ( p->_next == 0 )
@@ -115,9 +116,9 @@ ClientProxyElement* ClientProxy::add(SensorNetAddress* addr, string* clientId)
return elm; return elm;
} }
const char* ClientProxy::getClientId(SensorNetAddress* addr) const char* QoSm1Proxy::getClientId(SensorNetAddress* addr)
{ {
ClientProxyElement* p = _head; QoSm1ProxyElement* p = _head;
while ( p ) while ( p )
{ {
if ( p->_sensorNetAddr.isMatch(addr) ) if ( p->_sensorNetAddr.isMatch(addr) )
@@ -130,17 +131,17 @@ const char* ClientProxy::getClientId(SensorNetAddress* addr)
return 0; return 0;
} }
void ClientProxy::setClient(Client* client) void QoSm1Proxy::setClient(Client* client)
{ {
_client = client; _client = client;
} }
Client* ClientProxy::getClient(void) Client* QoSm1Proxy::getClient(void)
{ {
return _client; return _client;
} }
bool ClientProxy::setClientProxy(const char* fileName) bool QoSm1Proxy::setClientProxy(const char* fileName)
{ {
FILE* fp; FILE* fp;
char buf[MAX_CLIENTID_LENGTH + 256]; char buf[MAX_CLIENTID_LENGTH + 256];
@@ -191,7 +192,7 @@ bool ClientProxy::setClientProxy(const char* fileName)
} }
void ClientProxy::checkConnection(void) void QoSm1Proxy::checkConnection(void)
{ {
if ( _client->isDisconnect() || ( _client->isConnecting() && _responseTimer.isTimeup()) ) if ( _client->isDisconnect() || ( _client->isConnecting() && _responseTimer.isTimeup()) )
{ {
@@ -220,12 +221,12 @@ void ClientProxy::checkConnection(void)
} }
} }
void ClientProxy::resetPingTimer(void) void QoSm1Proxy::resetPingTimer(void)
{ {
_keepAliveTimer.start(KEEPALIVE_DURATION * 1000UL); _keepAliveTimer.start(KEEPALIVE_DURATION * 1000UL);
} }
void ClientProxy::send(MQTTSNPacket* packet) void QoSm1Proxy::send(MQTTSNPacket* packet)
{ {
if ( packet->getType() == MQTTSN_CONNACK ) if ( packet->getType() == MQTTSN_CONNACK )
{ {
@@ -242,7 +243,7 @@ void ClientProxy::send(MQTTSNPacket* packet)
} }
} }
void ClientProxy::sendStoredPublish(void) void QoSm1Proxy::sendStoredPublish(void)
{ {
MQTTSNPacket* msg = 0; MQTTSNPacket* msg = 0;

View File

@@ -14,8 +14,8 @@
* Tomoaki Yamaguchi - initial API and implementation and/or initial documentation * Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
**************************************************************************************/ **************************************************************************************/
#ifndef MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTPROXY_H_ #ifndef MQTTSNGATEWAY_SRC_MQTTSNGWQOS_1PROXY_H_
#define MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTPROXY_H_ #define MQTTSNGATEWAY_SRC_MQTTSNGWQOS_1PROXY_H_
#include "MQTTSNGateway.h" #include "MQTTSNGateway.h"
#include "MQTTGWPacket.h" #include "MQTTGWPacket.h"
@@ -29,27 +29,27 @@ namespace MQTTSNGW
{ {
class Gateway; class Gateway;
class ClientProxyElement class QoSm1ProxyElement
{ {
friend class ClientProxy; friend class QoSm1Proxy;
public: public:
ClientProxyElement(void); QoSm1ProxyElement(void);
ClientProxyElement(SensorNetAddress* addr, string* clientId); QoSm1ProxyElement(SensorNetAddress* addr, string* clientId);
~ClientProxyElement(void); ~QoSm1ProxyElement(void);
private: private:
SensorNetAddress _sensorNetAddr; SensorNetAddress _sensorNetAddr;
string _clientId; string _clientId;
ClientProxyElement* _next; QoSm1ProxyElement* _next;
}; };
class ClientProxy class QoSm1Proxy
{ {
public: public:
ClientProxy(void); QoSm1Proxy(void);
ClientProxy(Gateway* gw); QoSm1Proxy(Gateway* gw);
~ClientProxy(void); ~QoSm1Proxy(void);
bool setClientProxy(const char* fileName); bool setClientProxy(const char* fileName);
ClientProxyElement* add(SensorNetAddress* addr, string* clientId); QoSm1ProxyElement* add(SensorNetAddress* addr, string* clientId);
const char* getClientId(SensorNetAddress* addr); const char* getClientId(SensorNetAddress* addr);
void setClient(Client*); void setClient(Client*);
Client* getClient(void); Client* getClient(void);
@@ -65,7 +65,7 @@ private:
Gateway* _gateway; Gateway* _gateway;
Client* _client; Client* _client;
ClientProxyElement* _head; QoSm1ProxyElement* _head;
Timer _keepAliveTimer; Timer _keepAliveTimer;
Timer _responseTimer; Timer _responseTimer;
}; };
@@ -74,4 +74,4 @@ private:
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTPROXY_H_ */ #endif /* MQTTSNGATEWAY_SRC_MQTTSNGWQOS_1PROXY_H_ */

View File

@@ -30,7 +30,7 @@ Gateway::Gateway()
{ {
theMultiTaskProcess = this; theMultiTaskProcess = this;
theProcess = this; theProcess = this;
_clientProxy = new ClientProxy(this); _qosm1Proxy = 0;
_params.loginId = 0; _params.loginId = 0;
_params.password = 0; _params.password = 0;
_params.keepAlive = 0; _params.keepAlive = 0;
@@ -50,6 +50,7 @@ Gateway::Gateway()
_params.predefinedTopicFileName = 0; _params.predefinedTopicFileName = 0;
_params.forwarderListName = 0; _params.forwarderListName = 0;
_params.qosMinusClientListName = 0; _params.qosMinusClientListName = 0;
_params.qosm1proxyName = 0;
_packetEventQue.setMaxSize(MAX_INFLIGHTMESSAGES * MAX_CLIENTS); _packetEventQue.setMaxSize(MAX_INFLIGHTMESSAGES * MAX_CLIENTS);
} }
@@ -115,9 +116,14 @@ Gateway::~Gateway()
{ {
free(_params.qosMinusClientListName); free(_params.qosMinusClientListName);
} }
if ( _clientProxy ) if ( _params.qosm1proxyName )
{ {
delete _clientProxy; free(_params.qosm1proxyName);
}
if ( _qosm1Proxy )
{
delete _qosm1Proxy;
} }
} }
@@ -230,18 +236,32 @@ void Gateway::initialize(int argc, char** argv)
} }
} }
/* Set ClientProxy's Client */ /* Set QoSm1Proxy's Client */
MQTTSNString id = MQTTSNString_initializer;
id.cstring = const_cast<char*>(CLIENTPROXY);
Client* client = _clientList.createClient(0, &id, true, secure);
_clientProxy->setClient(client);
client->setPorxy(true);
_clientProxy->setGateway(this);
if (getParam("QoS-1", param) == 0 ) if (getParam("QoS-1", param) == 0 )
{ {
if (!strcasecmp(param, "YES") ) if (!strcasecmp(param, "YES") )
{ {
/* Set QoSm1Proxy's Client */
_qosm1Proxy = new QoSm1Proxy(this);
MQTTSNString id = MQTTSNString_initializer;
if (getParam("QoS-1ProxyName", param) == 0 )
{
string name = string(param);
id.cstring = const_cast<char*>(name.c_str());
}
else
{
id.cstring = const_cast<char*>(CLIENTPROXY);
}
Client* client = _clientList.createClient(0, &id, true, secure);
_qosm1Proxy->setClient(client);
client->setPorxy(true);
_qosm1Proxy->setGateway(this);
if (getParam("QoS-1ClientsList", param) == 0) if (getParam("QoS-1ClientsList", param) == 0)
{ {
fileName = string(param); fileName = string(param);
@@ -250,7 +270,7 @@ void Gateway::initialize(int argc, char** argv)
{ {
fileName = *getConfigDirName() + string(QOS_1CLIENT_LIST); fileName = *getConfigDirName() + string(QOS_1CLIENT_LIST);
} }
if ( !_clientProxy->setClientProxy(fileName.c_str()) ) if ( !_qosm1Proxy->setClientProxy(fileName.c_str()) )
{ {
throw Exception("Gateway::initialize: No QoS-1ClientsList file defined by the configuration.."); throw Exception("Gateway::initialize: No QoS-1ClientsList file defined by the configuration..");
} }
@@ -399,9 +419,9 @@ GatewayParams* Gateway::getGWParams(void)
return &_params; return &_params;
} }
ClientProxy* Gateway::getClientProxy(void) QoSm1Proxy* Gateway::getQoSm1Proxy(void)
{ {
return _clientProxy; return _qosm1Proxy;
} }
/*===================================== /*=====================================

View File

@@ -21,7 +21,7 @@
#include "MQTTSNPacket.h" #include "MQTTSNPacket.h"
#include "MQTTSNGWForwarder.h" #include "MQTTSNGWForwarder.h"
#include "MQTTSNGWClientProxy.h" #include "MQTTSNGWQoS-1Proxy.h"
namespace MQTTSNGW namespace MQTTSNGW
{ {
@@ -159,13 +159,14 @@ typedef struct
char* privateKey; char* privateKey;
char* predefinedTopicFileName; char* predefinedTopicFileName;
char* forwarderListName; char* forwarderListName;
char* qosm1proxyName;
char* qosMinusClientListName; char* qosMinusClientListName;
}GatewayParams; }GatewayParams;
/*===================================== /*=====================================
Class Gateway Class Gateway
=====================================*/ =====================================*/
class ClientProxy; class QoSm1Proxy;
class Gateway: public MultiTaskProcess{ class Gateway: public MultiTaskProcess{
public: public:
@@ -182,11 +183,11 @@ public:
SensorNetwork* getSensorNetwork(void); SensorNetwork* getSensorNetwork(void);
LightIndicator* getLightIndicator(void); LightIndicator* getLightIndicator(void);
GatewayParams* getGWParams(void); GatewayParams* getGWParams(void);
ClientProxy* getClientProxy(void); QoSm1Proxy* getQoSm1Proxy(void);
private: private:
ClientList _clientList; ClientList _clientList;
ClientProxy* _clientProxy; QoSm1Proxy* _qosm1Proxy;
ForwarderList _forwarderList; ForwarderList _forwarderList;
EventQue _packetEventQue; EventQue _packetEventQue;
EventQue _brokerSendQue; EventQue _brokerSendQue;