Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Revision:
17:b3a4b4bdfc59
Parent:
14:56478403e340
Child:
18:16192696c106
--- a/MbedClient.cpp	Tue Mar 03 14:51:54 2015 +0000
+++ b/MbedClient.cpp	Wed Mar 04 09:35:25 2015 +0000
@@ -26,11 +26,12 @@
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
-#include "MbedClient.h"
 #include <stdlib.h>
 #include <string.h>
 #include "b64.h"
 #include "mbed.h"
+#include "MbedClient.h"
+#include "logging.h"
 
 #define STATE_INIT 0
 #define STATE_IN_REQUEST 1
@@ -39,8 +40,11 @@
 #define STATE_REQ_COMPLETE 4
 #define STATE_RECVD_RESPONSE 5
 #define STATE_INTERNAL_ERROR 6
+#define MBCL_DBG(...) aDebug(__VA_ARGS__)
+//#define MBCL_DBG(fmt, ...)
+#define DNS_ENTRY_DURATION 50
 
-MbedClient::MbedClient(const char* host, uint16_t port, uint8_t tries) :
+MbedClient::MbedClient(const char* host, uint16_t port, MDMSerial& mdm, uint8_t tries) :
     _host(host),
     _username(NULL),
     _password(NULL),
@@ -51,7 +55,9 @@
     _filter(_source),
     _source(_sock),
     _sink(_sock),
-    _sock()
+    _sock(),
+    _mdm(mdm),
+    cachedIPValid(0)
 {
 }
 
@@ -66,7 +72,7 @@
 
     _username = username;
     _password = password;
-    MBCL_DBG("Set authorization to %s:%s", username, password);
+    MBCL_DBG("\033[32mMbedClient:\033[39m Set authorization to %s:%s\r\n", username, password);
     return CLIENT_OK;
 }
 
@@ -75,7 +81,7 @@
     if (_state != STATE_INIT)
         return internalError();
 
-    MBCL_DBG("Beginning SmartREST request.");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Beginning SmartREST request.\r\n");
     _source.setTimeout(60000);
     if (!connect())
         return connectionError();
@@ -95,7 +101,7 @@
     // set stream request flag to later set the timeout right
     _isStreamRequest = true;
 
-    MBCL_DBG("Beginning SmartREST request.");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Beginning SmartREST request.\r\n");
     _source.setTimeout(60000);
     if (!connect())
         return connectionError();
@@ -112,7 +118,7 @@
     if (_state != STATE_IN_REQUEST)
         return internalError();
 
-    MBCL_DBG("Sending template identifier.");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Sending template identifier.\r\n");
     if ((identifier != NULL) && (strlen(identifier) != 0)) {
         if ((!send("X-Id: ")) ||
             (!send(identifier)) ||
@@ -130,7 +136,7 @@
     if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID))
         return internalError();
     
-    MBCL_DBG("Sending request payload.");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Sending request payload.\r\n");
     len = generator.writtenLength();
     if ((!send("Content-Length: ")) ||
         (_sink.write((unsigned long)len) == 0) ||
@@ -150,7 +156,7 @@
         (_state != STATE_SENT_DATA))
         return internalError();
     
-    MBCL_DBG("Ending request.");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Ending request.\r\n");
 
     if (_state != STATE_SENT_DATA) {
         // send end of headers
@@ -172,10 +178,10 @@
     if (_state != STATE_REQ_COMPLETE)
         return internalError();
     
-    MBCL_DBG("Awaiting response...");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Awaiting response...\r\n");
 
     status = _filter.readStatus();
-    MBCL_DBG("Status code: %u", status);
+    MBCL_DBG("\033[32mMbedClient:\033[39m Status code: %u\r\n", status);
 
     if ((status != 200) || (!_filter.skipHeaders()))
         return connectionError();
@@ -195,8 +201,8 @@
 
 void MbedClient::stop()
 {
-    MBCL_DBG("Resetting client.");
-    MBCL_DBG("Bytes tramsmitted sofar: %zu", packetSize);
+    MBCL_DBG("\033[32mMbedClient:\033[39m Resetting client.\r\n");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Bytes tramsmitted sofar: %zu\r\n", packetSize);
     _isStreamRequest = false;
     _sock.close();
     _source.reset();
@@ -211,13 +217,24 @@
 
     tries = _tries;
     do {
-        MBCL_DBG("Connecting to %s:%u", _host, _port);
-        if (_sock.connect(_host, _port) >= 0)
+        if (cachedIPValid == 0) {
+            MDMParser::IP ip = _mdm.gethostbyname(_host);
+            if (ip == NOIP) 
+                continue;
+            const unsigned char *c = (const unsigned char*)&ip;
+            snprintf(cachedIP, sizeof(cachedIP), "%u.%u.%u.%u", c[3], c[2], c[1], c[0]);
+            MBCL_DBG("\033[32mMbedClient:\033[39m Connecting to %s:%u with resolved IP %s\r\n", _host, _port, cachedIP);
+        } else {
+            MBCL_DBG("\033[32mMbedClient:\033[39m Connecting to %s:%u\r\n", cachedIP, _port);
+        }
+        if (_sock.connect(cachedIP, _port) >= 0)
             break;
+        cachedIPValid = 0;
         _sock.close();
-        MBCL_DBG("Connection atempt failed.");
+        MBCL_DBG("\033[32mMbedClient:\033[39m Connection attempt failed.\r\n");
     } while (--tries > 0);
 
+    cachedIPValid = (cachedIPValid+1) % DNS_ENTRY_DURATION;
     return (tries > 0);
 }
 
@@ -228,7 +245,7 @@
 
 bool MbedClient::sendRequestHeader(const char *uri)
 {
-    MBCL_DBG("Sending request header.");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Sending request header.\r\n");
     if ((!send("POST ")) ||
         (!send(uri)) ||
         (!send(" HTTP/1.0\r\n")) ||
@@ -280,14 +297,14 @@
 
 uint8_t MbedClient::internalError()
 {
-    MBCL_DBG("Internal error occurred.");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Internal error occurred.\r\n");
     _state = STATE_INTERNAL_ERROR;
     return CLIENT_INTERNAL_ERROR;
 }
 
 uint8_t MbedClient::connectionError()
 {
-    MBCL_DBG("Connection error occurred.");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Connection error occurred.\r\n");
     _state = STATE_INTERNAL_ERROR;
     return CLIENT_CONNECTION_ERROR;
 }