Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Revision:
6:cd7ba1ddb664
Parent:
2:45a6e44a4fb4
Child:
7:8159a2d12e4e
--- a/MbedClient.cpp	Thu Jul 10 16:20:31 2014 +0200
+++ b/MbedClient.cpp	Mon Jul 28 12:11:02 2014 +0200
@@ -32,17 +32,27 @@
 #include "b64.h"
 #include "mbed.h"
 
-MbedClient::MbedClient(const char* host, uint16_t port) :
+#define STATE_INIT 0
+#define STATE_IN_REQUEST 1
+#define STATE_SENT_ID 2
+#define STATE_SENT_DATA 3
+#define STATE_REQ_COMPLETE 4
+#define STATE_RECVD_RESPONSE 5
+#define STATE_INTERNAL_ERROR 6
+
+MbedClient::MbedClient(const char* host, uint16_t port, uint8_t tries) :
     _host(host),
+    _username(NULL),
+    _password(NULL),
     _port(port),
+    _tries(tries),
+    _state(STATE_INIT),
     _filter(_source),
     _source(_sock),
     _sink(_sock),
     _sock()
 {
-    _username = NULL;
-    _password = NULL;
-    _state = MBED_STATE_INIT;
+    _state = STATE_INIT;
 }
 
 MbedClient::~MbedClient()
@@ -51,8 +61,8 @@
 
 uint8_t MbedClient::setAuthorization(const char* username, const char* password)
 {
-    if (_state != MBED_STATE_INIT)
-        return CLIENT_INTERNAL_ERROR;
+    if (_state != STATE_INIT)
+        return internalError();
 
     _username = username;
     _password = password;
@@ -61,43 +71,50 @@
 
 uint8_t MbedClient::beginRequest()
 {
-    if (_state != MBED_STATE_INIT)
-        return CLIENT_INTERNAL_ERROR;
+    uint8_t tries;
+
+    if (_state != STATE_INIT)
+        return internalError();
 
-    if (_sock.connect(_host, _port) < 0) {
-        stop();
-        return CLIENT_CONNECTION_ERROR;
-    }
+    tries = _tries;
+    do {
+        if (_sock.connect(_host, _port) >= 0)
+            break;
+        _sock.close();
+    } while (--tries > 0);
+
+    if (tries == 0)
+        return connectionError();
 
     if ((!send("POST /s HTTP/1.0\r\n")) ||
         (!send("Host: ")) ||
         (!send(_host)) ||
         (!send("\r\n")))
-        return CLIENT_CONNECTION_ERROR;
+        return connectionError();
     
     if ((_username != NULL) && (strlen(_username) > 0) &&
         (_password != NULL) && (strlen(_password) > 0)) {
         if (!sendBasicAuth())
-            return CLIENT_CONNECTION_ERROR;
+            return connectionError();
     }
 
-    _state = MBED_STATE_IN_REQUEST;
+    _state = STATE_IN_REQUEST;
     return CLIENT_OK;
 }
 
 uint8_t MbedClient::sendIdentifier(const char* identifier)
 {
-    if (_state != MBED_STATE_IN_REQUEST)
-        return CLIENT_INTERNAL_ERROR;
+    if (_state != STATE_IN_REQUEST)
+        return internalError();
 
     if ((identifier != NULL) && (strlen(identifier) != 0)) {
         if ((!send("X-Id: ")) ||
             (!send(identifier)) ||
             (!send("\r\n")))
-            return CLIENT_CONNECTION_ERROR;
+            return connectionError();
     }
 
-    _state = MBED_STATE_SENT_ID;
+    _state = STATE_SENT_ID;
     return CLIENT_OK;
 }
 
@@ -105,8 +122,8 @@
 {
     size_t len; char lenstr[8];
     
-    if ((_state != MBED_STATE_IN_REQUEST) && (_state != MBED_STATE_SENT_ID))
-        return CLIENT_INTERNAL_ERROR;
+    if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID))
+        return internalError();
     
     len = generator.writtenLength();
     snprintf(lenstr, 8, "%ld", len);
@@ -114,48 +131,44 @@
     if ((!send("Content-Length: ")) ||
         (!send(lenstr)) ||
         (!send("\r\n\r\n")))
-        return CLIENT_CONNECTION_ERROR;
+        return connectionError();
 
-    if (generator.writeTo(_sink) != len) {
-        stop();
-        return CLIENT_CONNECTION_ERROR;
-    }
+    if (generator.writeTo(_sink) != len)
+        return connectionError();
     
-    _state = MBED_STATE_SENT_DATA;
+    _state = STATE_SENT_DATA;
     return CLIENT_OK;
 }
 
 uint8_t MbedClient::endRequest()
 {
-    if ((_state != MBED_STATE_IN_REQUEST) && (_state != MBED_STATE_SENT_ID) && (_state != MBED_STATE_SENT_DATA))
-        return CLIENT_INTERNAL_ERROR;
+    if ((_state != STATE_IN_REQUEST) &&
+        (_state != STATE_SENT_ID) &&
+        (_state != STATE_SENT_DATA))
+        return internalError();
     
-    if (_state != MBED_STATE_SENT_DATA) {
+    if (_state != STATE_SENT_DATA) {
         // send end of headers
         if (!send("\r\n"))
-            return CLIENT_CONNECTION_ERROR;
+            return connectionError();
     }
     
-    if (!_sink.flush()) {
-        stop();
-        return CLIENT_CONNECTION_ERROR;
-    }
+    if (!_sink.flush())
+        return connectionError();
     
-    _state = MBED_STATE_REQ_COMPLETE;
+    _state = STATE_REQ_COMPLETE;
     return CLIENT_OK;
 }
 
 uint8_t MbedClient::awaitResponse()
 {
-    if (_state != MBED_STATE_REQ_COMPLETE)
-        return CLIENT_INTERNAL_ERROR;
+    if (_state != STATE_REQ_COMPLETE)
+        return internalError();
     
-    if ((_filter.readStatus() != 200) || (!_filter.skipHeaders())) {
-        stop();
-        return CLIENT_CONNECTION_ERROR;
-    }
+    if ((_filter.readStatus() != 200) || (!_filter.skipHeaders()))
+        return connectionError();
     
-    _state = MBED_STATE_RECVD_RESPONSE;
+    _state = STATE_RECVD_RESPONSE;
     return CLIENT_OK;
 }
 
@@ -170,15 +183,13 @@
     _source.reset();
     _sink.reset();
     _filter.reset();
-    _state = MBED_STATE_INIT;
+    _state = STATE_INIT;
 }
 
 bool MbedClient::send(const char *str)
 {
-    if (_sink.write(str) != strlen(str)) {
-        stop();
+    if (_sink.write(str) != strlen(str))
         return false;
-    }
     return true;
 }
 
@@ -214,3 +225,16 @@
         return false;
     return true;
 }
+
+uint8_t MbedClient::internalError()
+{
+    _state = STATE_INTERNAL_ERROR;
+    return CLIENT_INTERNAL_ERROR;
+}
+
+uint8_t MbedClient::connectionError()
+{
+    _state = STATE_INTERNAL_ERROR;
+    return CLIENT_CONNECTION_ERROR;
+}
+