Jack Hansdampf / mbed-mqtt-GSOE1

Dependents:   ESP8266MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTSNGWAdapterManager.cpp Source File

MQTTSNGWAdapterManager.cpp

00001 /**************************************************************************************
00002  * Copyright (c) 2016, Tomoaki Yamaguchi
00003  *
00004  * All rights reserved. This program and the accompanying materials
00005  * are made available under the terms of the Eclipse Public License v1.0
00006  * and Eclipse Distribution License v1.0 which accompany this distribution.
00007  *
00008  * The Eclipse Public License is available at
00009  *    http://www.eclipse.org/legal/epl-v10.html
00010  * and the Eclipse Distribution License is available at
00011  *   http://www.eclipse.org/org/documents/edl-v10.php.
00012  *
00013  * Contributors:
00014  *    Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
00015  **************************************************************************************/
00016 #include "MQTTSNGWDefines.h"
00017 #include "MQTTSNGateway.h"
00018 #include "SensorNetwork.h"
00019 #include "MQTTSNGWProcess.h"
00020 #include "MQTTSNGWVersion.h"
00021 #include "MQTTSNGWClientRecvTask.h"
00022 #include "MQTTSNGWClientSendTask.h"
00023 #include "MQTTSNGWClient.h"
00024 #include "MQTTSNGWAggregater.h"
00025 #include "MQTTSNGWQoSm1Proxy.h"
00026 #include <string.h>
00027 using namespace MQTTSNGW;
00028 
00029 char* currentDateTime(void);
00030 
00031 /*=====================================
00032  Class AdapterManager
00033  =====================================*/
00034 AdapterManager::AdapterManager(Gateway* gw)
00035 {
00036     _gateway = gw;
00037     _forwarders = new ForwarderList();
00038     _qosm1Proxy = new QoSm1Proxy(gw);
00039     _aggregater = new Aggregater(gw);
00040 }
00041 
00042 
00043 void AdapterManager::initialize(void)
00044 {
00045     _aggregater->initialize();
00046     _forwarders->initialize(_gateway);
00047     _qosm1Proxy->initialize();
00048 }
00049 
00050 
00051 AdapterManager::~AdapterManager(void)
00052 {
00053     if ( _forwarders )
00054     {
00055         delete _forwarders;
00056     }
00057     if ( _qosm1Proxy )
00058     {
00059         delete _qosm1Proxy;
00060     }
00061     if ( _aggregater )
00062     {
00063         delete _aggregater;
00064     }
00065 }
00066 
00067 ForwarderList* AdapterManager::getForwarderList(void)
00068 {
00069     return _forwarders;
00070 }
00071 
00072 QoSm1Proxy* AdapterManager::getQoSm1Proxy(void)
00073 {
00074     return _qosm1Proxy;
00075 }
00076 
00077 Aggregater* AdapterManager::getAggregater(void)
00078 {
00079     return _aggregater;
00080 }
00081 
00082 bool AdapterManager::isAggregatedClient(Client* client)
00083 {
00084     if ( !_aggregater->isActive() || client->isQoSm1() || client->isAggregater() || client->isQoSm1Proxy())
00085     {
00086         return false;
00087     }
00088     else
00089     {
00090         return true;
00091     }
00092 }
00093 
00094 Client* AdapterManager::getClient(Client& client)
00095 {
00096     bool secure = client.isSecureNetwork();
00097     Client* newClient = &client;
00098     if ( client.isQoSm1() )
00099     {
00100         newClient = _qosm1Proxy->getAdapterClient(&client);
00101         _qosm1Proxy->resetPingTimer(secure);
00102     }
00103     else if ( client.isAggregated() )
00104 
00105     {
00106         newClient = _aggregater->getAdapterClient(&client);
00107         _aggregater->resetPingTimer(secure);
00108     }
00109 
00110     return newClient;
00111 }
00112 
00113 int AdapterManager::unicastToClient(Client* client, MQTTSNPacket* packet, ClientSendTask* task)
00114 {
00115     char pbuf[SIZE_OF_LOG_PACKET * 3];
00116     Forwarder* fwd = client->getForwarder();
00117     int rc = 0;
00118 
00119     if ( fwd )
00120     {
00121         MQTTSNGWEncapsulatedPacket encap(packet);
00122         WirelessNodeId* wnId = fwd->getWirelessNodeId(client);
00123         encap.setWirelessNodeId(wnId);
00124         WRITELOG(FORMAT_Y_W_G, currentDateTime(), encap.getName(), RIGHTARROW, fwd->getId(), encap.print(pbuf));
00125         task->log(client, packet);
00126         rc = encap.unicast(_gateway->getSensorNetwork(),fwd->getSensorNetAddr());
00127     }
00128     else
00129     {
00130         task->log(client, packet);
00131         if ( client->isQoSm1Proxy() )
00132         {
00133             _qosm1Proxy->send(packet, client);
00134         }
00135         else if ( client->isAggregater() )
00136         {
00137             _aggregater->send(packet, client);
00138         }
00139         else
00140         {
00141             rc = packet->unicast(_gateway->getSensorNetwork(), client->getSensorNetAddress());
00142         }
00143     }
00144     return rc;
00145 }
00146 
00147 void AdapterManager::checkConnection(void)
00148 {
00149     if ( _aggregater->isActive())
00150     {
00151         _aggregater->checkConnection();
00152     }
00153 
00154     if ( _qosm1Proxy->isActive())
00155     {
00156         _qosm1Proxy->checkConnection();
00157     }
00158 }
00159 
00160 Client* AdapterManager::convertClient(uint16_t msgId, uint16_t* clientMsgId)
00161 {
00162     return _aggregater->convertClient(msgId, clientMsgId);
00163 }
00164 
00165 bool AdapterManager::isAggregaterActive(void)
00166 {
00167     return _aggregater->isActive();
00168 }
00169 
00170 AggregateTopicElement* AdapterManager::createClientList(Topic* topic)
00171 {
00172     return _aggregater->createClientList(topic);
00173 }
00174 
00175 int AdapterManager::addAggregateTopic(Topic* topic, Client* client)
00176 {
00177     return _aggregater->addAggregateTopic(topic, client);
00178 }
00179 
00180 void AdapterManager::removeAggregateTopic(Topic* topic, Client* client)
00181 {
00182      _aggregater->removeAggregateTopic(topic, client);
00183 }
00184 
00185 void AdapterManager::removeAggregateTopicList(Topics* topics, Client* client)
00186 {
00187      _aggregater->removeAggregateTopicList(topics, client);
00188 }