From df080f28510e948ec09735393a6f23834cedf065 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 31 Jul 2018 15:49:25 +0900 Subject: [PATCH] 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 --- MQTTSNGateway/Makefile | 2 +- MQTTSNGateway/README.md | 2 +- MQTTSNGateway/gateway.conf | 9 ++-- MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp | 43 ++++++++--------- MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp | 2 +- .../src/MQTTSNGWPacketHandleTask.cpp | 8 +++- MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp | 2 +- ...ClientProxy.cpp => MQTTSNGWQoS-1Proxy.cpp} | 45 ++++++++--------- ...SNGWClientProxy.h => MQTTSNGWQoS-1Proxy.h} | 30 ++++++------ MQTTSNGateway/src/MQTTSNGateway.cpp | 48 +++++++++++++------ MQTTSNGateway/src/MQTTSNGateway.h | 9 ++-- 11 files changed, 112 insertions(+), 88 deletions(-) rename MQTTSNGateway/src/{MQTTSNGWClientProxy.cpp => MQTTSNGWQoS-1Proxy.cpp} (82%) rename MQTTSNGateway/src/{MQTTSNGWClientProxy.h => MQTTSNGWQoS-1Proxy.h} (72%) diff --git a/MQTTSNGateway/Makefile b/MQTTSNGateway/Makefile index f9a2c6f..0eca7fd 100644 --- a/MQTTSNGateway/Makefile +++ b/MQTTSNGateway/Makefile @@ -40,7 +40,7 @@ $(SRCDIR)/MQTTSNGWPublishHandler.cpp \ $(SRCDIR)/MQTTSNGWSubscribeHandler.cpp \ $(SRCDIR)/MQTTSNGWEncapsulatedPacket.cpp \ $(SRCDIR)/MQTTSNGWForwarder.cpp \ -$(SRCDIR)/MQTTSNGWClientProxy.cpp \ +$(SRCDIR)/MQTTSNGWQoS-1Proxy.cpp \ $(SRCDIR)/$(OS)/$(SENSORNET)/SensorNetwork.cpp \ $(SRCDIR)/$(OS)/Timer.cpp \ $(SRCDIR)/$(OS)/Network.cpp \ diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index 2144ec5..9444a3a 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -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 **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 **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. diff --git a/MQTTSNGateway/gateway.conf b/MQTTSNGateway/gateway.conf index a888771..d3f5fe6 100644 --- a/MQTTSNGateway/gateway.conf +++ b/MQTTSNGateway/gateway.conf @@ -18,16 +18,17 @@ BrokerPortNo=1883 BrokerSecurePortNo=8883 ClientAuthentication=NO -ClientsList=/path/to/your_clients.conf +#ClientsList=/path/to/your_clients.conf PredefinedTopic=NO -PredefinedTopicList=/path/to/your_predefinedTopic.conf +#PredefinedTopicList=/path/to/your_predefinedTopic.conf Forwarder=NO -ForwardersList=/path/to/your_forwarers.conf +#ForwardersList=/path/to/your_forwarers.conf 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 #RootCApath=/etc/ssl/certs/ diff --git a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp index c17eaf9..a996051 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp @@ -128,21 +128,28 @@ void ClientRecvTask::run() } 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 ( packet->isQoSMinusPUBLISH() ) + if ( clientName ) { - client = _gateway->getClientProxy()->getClient(); // point to the ClientProxy - } - else - { - client = _gateway->getClientProxy()->getClient(); - 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; + if ( packet->isQoSMinusPUBLISH() ) + { + /* QoS1Proxy takes responsibility of the client */ + client = _gateway->getQoSm1Proxy()->getClient(); + } + else + { + client = _gateway->getQoSm1Proxy()->getClient(); + 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 @@ -221,16 +228,6 @@ void ClientRecvTask::run() 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); 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); - } } } } diff --git a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp index 76fe572..54107a5 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp @@ -73,7 +73,7 @@ void ClientSendTask::run() log(client, packet); if ( client->isProxy() ) { - _gateway->getClientProxy()->send(packet); + _gateway->getQoSm1Proxy()->send(packet); continue; } rc = packet->unicast(_sensorNetwork, client->getSensorNetAddress()); diff --git a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp index e1c5e09..53ad17a 100644 --- a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp @@ -114,8 +114,12 @@ void PacketHandleTask::run() _advertiseTimer.start(_gateway->getGWParams()->keepAlive * 1000UL); } - /*------ Check ClientProxy to Connect or send PINGREQ ------*/ - _gateway->getClientProxy()->checkConnection(); + /*------ Check QoS-1 Proxy Connect or PINGREQ ------*/ + QoSm1Proxy* pxy = _gateway->getQoSm1Proxy(); + if ( pxy ) + { + pxy->checkConnection(); + } } /*------ Handle SEARCHGW Message ---------*/ diff --git a/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp b/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp index dd763cf..aedb5df 100644 --- a/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp @@ -127,7 +127,7 @@ void MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet) /* reset PINGREQ of ClientProxy */ if ( qos == 3 ) { - _gateway->getClientProxy()->resetPingTimer(); + _gateway->getQoSm1Proxy()->resetPingTimer(); } } diff --git a/MQTTSNGateway/src/MQTTSNGWClientProxy.cpp b/MQTTSNGateway/src/MQTTSNGWQoS-1Proxy.cpp similarity index 82% rename from MQTTSNGateway/src/MQTTSNGWClientProxy.cpp rename to MQTTSNGateway/src/MQTTSNGWQoS-1Proxy.cpp index cf430f0..5858655 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientProxy.cpp +++ b/MQTTSNGateway/src/MQTTSNGWQoS-1Proxy.cpp @@ -15,8 +15,9 @@ **************************************************************************************/ +#include "MQTTSNGWQoS-1Proxy.h" + #include "MQTTSNGWDefines.h" -#include "MQTTSNGWClientProxy.h" #include "MQTTSNGateway.h" #include "SensorNetwork.h" #include @@ -32,21 +33,21 @@ using namespace MQTTSNGW; * Class ClientProxyElement */ -ClientProxyElement::ClientProxyElement(void) +QoSm1ProxyElement::QoSm1ProxyElement(void) : _clientId{0} , _next{0} { } -ClientProxyElement::ClientProxyElement(SensorNetAddress* addr, string* clientId) +QoSm1ProxyElement::QoSm1ProxyElement(SensorNetAddress* addr, string* clientId) : _next{0} { _clientId = *clientId; _sensorNetAddr = *addr; } -ClientProxyElement::~ClientProxyElement(void) +QoSm1ProxyElement::~QoSm1ProxyElement(void) { } @@ -55,14 +56,14 @@ ClientProxyElement::~ClientProxyElement(void) * Class ClientProxy */ -ClientProxy:: ClientProxy(void) +QoSm1Proxy:: QoSm1Proxy(void) : _head{0} { _gateway = 0; _client = 0; } -ClientProxy:: ClientProxy(Gateway* gw) +QoSm1Proxy:: QoSm1Proxy(Gateway* gw) : _head{0} { _gateway = gw; @@ -70,35 +71,35 @@ ClientProxy:: ClientProxy(Gateway* gw) } -ClientProxy::~ClientProxy(void) +QoSm1Proxy::~QoSm1Proxy(void) { if ( _head ) { - ClientProxyElement* p = _head; + QoSm1ProxyElement* p = _head; while ( p ) { - ClientProxyElement* next = p->_next; + QoSm1ProxyElement* next = p->_next; delete p; p = next; } } } -void ClientProxy::setGateway(Gateway* gw) +void QoSm1Proxy::setGateway(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 ) { _head = elm; } else { - ClientProxyElement* p = _head; + QoSm1ProxyElement* p = _head; while ( p ) { if ( p->_next == 0 ) @@ -115,9 +116,9 @@ ClientProxyElement* ClientProxy::add(SensorNetAddress* addr, string* clientId) return elm; } -const char* ClientProxy::getClientId(SensorNetAddress* addr) +const char* QoSm1Proxy::getClientId(SensorNetAddress* addr) { - ClientProxyElement* p = _head; + QoSm1ProxyElement* p = _head; while ( p ) { if ( p->_sensorNetAddr.isMatch(addr) ) @@ -130,17 +131,17 @@ const char* ClientProxy::getClientId(SensorNetAddress* addr) return 0; } -void ClientProxy::setClient(Client* client) +void QoSm1Proxy::setClient(Client* client) { _client = client; } -Client* ClientProxy::getClient(void) +Client* QoSm1Proxy::getClient(void) { return _client; } -bool ClientProxy::setClientProxy(const char* fileName) +bool QoSm1Proxy::setClientProxy(const char* fileName) { FILE* fp; 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()) ) { @@ -220,12 +221,12 @@ void ClientProxy::checkConnection(void) } } -void ClientProxy::resetPingTimer(void) +void QoSm1Proxy::resetPingTimer(void) { _keepAliveTimer.start(KEEPALIVE_DURATION * 1000UL); } -void ClientProxy::send(MQTTSNPacket* packet) +void QoSm1Proxy::send(MQTTSNPacket* packet) { 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; diff --git a/MQTTSNGateway/src/MQTTSNGWClientProxy.h b/MQTTSNGateway/src/MQTTSNGWQoS-1Proxy.h similarity index 72% rename from MQTTSNGateway/src/MQTTSNGWClientProxy.h rename to MQTTSNGateway/src/MQTTSNGWQoS-1Proxy.h index 43e0acc..c5db5e4 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientProxy.h +++ b/MQTTSNGateway/src/MQTTSNGWQoS-1Proxy.h @@ -14,8 +14,8 @@ * Tomoaki Yamaguchi - initial API and implementation and/or initial documentation **************************************************************************************/ -#ifndef MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTPROXY_H_ -#define MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTPROXY_H_ +#ifndef MQTTSNGATEWAY_SRC_MQTTSNGWQOS_1PROXY_H_ +#define MQTTSNGATEWAY_SRC_MQTTSNGWQOS_1PROXY_H_ #include "MQTTSNGateway.h" #include "MQTTGWPacket.h" @@ -29,27 +29,27 @@ namespace MQTTSNGW { class Gateway; -class ClientProxyElement +class QoSm1ProxyElement { - friend class ClientProxy; + friend class QoSm1Proxy; public: - ClientProxyElement(void); - ClientProxyElement(SensorNetAddress* addr, string* clientId); - ~ClientProxyElement(void); + QoSm1ProxyElement(void); + QoSm1ProxyElement(SensorNetAddress* addr, string* clientId); + ~QoSm1ProxyElement(void); private: SensorNetAddress _sensorNetAddr; string _clientId; - ClientProxyElement* _next; + QoSm1ProxyElement* _next; }; -class ClientProxy +class QoSm1Proxy { public: - ClientProxy(void); - ClientProxy(Gateway* gw); - ~ClientProxy(void); + QoSm1Proxy(void); + QoSm1Proxy(Gateway* gw); + ~QoSm1Proxy(void); bool setClientProxy(const char* fileName); - ClientProxyElement* add(SensorNetAddress* addr, string* clientId); + QoSm1ProxyElement* add(SensorNetAddress* addr, string* clientId); const char* getClientId(SensorNetAddress* addr); void setClient(Client*); Client* getClient(void); @@ -65,7 +65,7 @@ private: Gateway* _gateway; Client* _client; - ClientProxyElement* _head; + QoSm1ProxyElement* _head; Timer _keepAliveTimer; Timer _responseTimer; }; @@ -74,4 +74,4 @@ private: -#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTPROXY_H_ */ +#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWQOS_1PROXY_H_ */ diff --git a/MQTTSNGateway/src/MQTTSNGateway.cpp b/MQTTSNGateway/src/MQTTSNGateway.cpp index d9b0b8e..069ac5a 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.cpp +++ b/MQTTSNGateway/src/MQTTSNGateway.cpp @@ -30,7 +30,7 @@ Gateway::Gateway() { theMultiTaskProcess = this; theProcess = this; - _clientProxy = new ClientProxy(this); + _qosm1Proxy = 0; _params.loginId = 0; _params.password = 0; _params.keepAlive = 0; @@ -50,6 +50,7 @@ Gateway::Gateway() _params.predefinedTopicFileName = 0; _params.forwarderListName = 0; _params.qosMinusClientListName = 0; + _params.qosm1proxyName = 0; _packetEventQue.setMaxSize(MAX_INFLIGHTMESSAGES * MAX_CLIENTS); } @@ -115,9 +116,14 @@ Gateway::~Gateway() { 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 */ - MQTTSNString id = MQTTSNString_initializer; - id.cstring = const_cast(CLIENTPROXY); - Client* client = _clientList.createClient(0, &id, true, secure); - _clientProxy->setClient(client); - client->setPorxy(true); - _clientProxy->setGateway(this); + /* Set QoSm1Proxy's Client */ - if (getParam("QoS-1", param) == 0 ) + if (getParam("QoS-1", param) == 0 ) { 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(name.c_str()); + } + else + { + id.cstring = const_cast(CLIENTPROXY); + } + Client* client = _clientList.createClient(0, &id, true, secure); + _qosm1Proxy->setClient(client); + client->setPorxy(true); + _qosm1Proxy->setGateway(this); + + if (getParam("QoS-1ClientsList", param) == 0) { fileName = string(param); @@ -250,7 +270,7 @@ void Gateway::initialize(int argc, char** argv) { 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.."); } @@ -399,9 +419,9 @@ GatewayParams* Gateway::getGWParams(void) return &_params; } -ClientProxy* Gateway::getClientProxy(void) +QoSm1Proxy* Gateway::getQoSm1Proxy(void) { - return _clientProxy; + return _qosm1Proxy; } /*===================================== diff --git a/MQTTSNGateway/src/MQTTSNGateway.h b/MQTTSNGateway/src/MQTTSNGateway.h index dbe6ae4..b547e0a 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.h +++ b/MQTTSNGateway/src/MQTTSNGateway.h @@ -21,7 +21,7 @@ #include "MQTTSNPacket.h" #include "MQTTSNGWForwarder.h" -#include "MQTTSNGWClientProxy.h" +#include "MQTTSNGWQoS-1Proxy.h" namespace MQTTSNGW { @@ -159,13 +159,14 @@ typedef struct char* privateKey; char* predefinedTopicFileName; char* forwarderListName; + char* qosm1proxyName; char* qosMinusClientListName; }GatewayParams; /*===================================== Class Gateway =====================================*/ -class ClientProxy; +class QoSm1Proxy; class Gateway: public MultiTaskProcess{ public: @@ -182,11 +183,11 @@ public: SensorNetwork* getSensorNetwork(void); LightIndicator* getLightIndicator(void); GatewayParams* getGWParams(void); - ClientProxy* getClientProxy(void); + QoSm1Proxy* getQoSm1Proxy(void); private: ClientList _clientList; - ClientProxy* _clientProxy; + QoSm1Proxy* _qosm1Proxy; ForwarderList _forwarderList; EventQue _packetEventQue; EventQue _brokerSendQue;