From 79080115c6326a9770214e8620c060c27265ef6c Mon Sep 17 00:00:00 2001 From: bdemsky Date: Sat, 7 Apr 2018 10:21:52 -0700 Subject: [PATCH] Updates --- version2/src/C/CloudComm.cpp | 55 ++++++++++++----- version2/src/C/Table.cpp | 2 - version2/src/C/Test.ino | 113 ++++------------------------------- 3 files changed, 51 insertions(+), 119 deletions(-) diff --git a/version2/src/C/CloudComm.cpp b/version2/src/C/CloudComm.cpp index ddd0c7b..9898039 100755 --- a/version2/src/C/CloudComm.cpp +++ b/version2/src/C/CloudComm.cpp @@ -113,17 +113,40 @@ void CloudComm::initCrypt() { mac->init(key); } +char * toStr(char * buffer, int64_t num) { + int index = 0; + if (num == 0) { + buffer[index++] = '0'; + } else + while(num) { + buffer[index++] = (char) ('0' + (num % 10)); + num = num / 10; + } + int init = 0; + buffer[index] = 0; + index--; + while(init < index) { + char tmp = buffer[init]; + buffer[init] = buffer[index]; + buffer[index] = tmp; + init++; + index--; + } + return buffer; +} + /* * Builds the URL for the given request. */ IoTString *CloudComm::buildRequest(bool isput, int64_t sequencenumber, int64_t maxentries) { const char *reqstring = isput ? "req=putslot" : "req=getslot"; char *buffer = (char *) malloc(baseurl->length() + 200); + char prbuffer[30]; memcpy(buffer, baseurl->internalBytes()->internalArray(), baseurl->length()); int offset = baseurl->length(); - offset += sprintf(&buffer[offset], "?%s&seq=%" PRId64, reqstring, sequencenumber); + offset += sprintf(&buffer[offset], "?%s&seq=%s" , reqstring, toStr(prbuffer,sequencenumber)); if (maxentries != 0) - sprintf(&buffer[offset], "&max=%" PRId64, maxentries); + sprintf(&buffer[offset], "&max=%s" , toStr(prbuffer,maxentries)); IoTString *urlstr = new IoTString(buffer); free(buffer); return urlstr; @@ -137,7 +160,7 @@ void loopWrite(TCPClient * client, char *array, int bytestowrite) { byteswritten += bytes; bytestowrite -= bytes; } else { - //printf("Error in write\n"); + Serial.println("Error in write"); exit(-1); } } @@ -151,8 +174,10 @@ void loopRead(TCPClient * client, char *array, int bytestoread) { bytesread += bytes; bytestoread -= bytes; } else { - //printf("Error in read\n"); - exit(-1); + if (!client->connected()) { + Serial.println("Client disconnected"); + exit(-1); + } } } } @@ -167,6 +192,7 @@ WebConnection openURL(IoTString *url) { if (url->get(i) == '/') break; + if ( i == url->length()) { //printf("ERROR in openURL\n"); exit(-1); @@ -176,7 +202,6 @@ WebConnection openURL(IoTString *url) { memcpy(host, &url->internalBytes()->internalArray()[7], i - 7); host[i - 7] = 0; //printf("%s\n", host); - char *message = (char *)malloc(sizeof("POST HTTP/1.1\r\n") + sizeof("Host: \r\n") + 2 * url->length()); /* fill in the parameters */ @@ -184,20 +209,16 @@ WebConnection openURL(IoTString *url) { /* copy data */ memcpy(&message[post], &url->internalBytes()->internalArray()[i], url->length() - i); int endpost = sprintf(&message[post + url->length() - i], " HTTP/1.1\r\n"); - int hostlen = sprintf(&message[endpost + post + url->length() - i], "Host: "); memcpy(&message[endpost + post + url->length() + hostlen - i], host, i - 7); sprintf(&message[endpost + post + url->length() + hostlen - 7], "\r\n"); - WebConnection wc; wc.numBytes = -1; - if (!wc.client.connect(host, 80)) { myerror("ERROR connecting\n"); } free(host); - /* send the request */ int total = strlen(message); loopWrite(&wc.client, message, total); @@ -276,8 +297,11 @@ void readLine(WebConnection *wc, char *response, int numBytes) { char newchar; while (true) { int bytes = wc->client.read((unsigned char *) &newchar, 1); - if (bytes <= 0) + if (!wc->client.connected()) break; + if (bytes <= 0) + continue; + if (offset == (numBytes - 1)) { //printf("Response too long"); exit(-1); @@ -301,11 +325,11 @@ int getResponseCode(WebConnection *wc) { void readHeaders(WebConnection *wc) { char response[600]; int numBytes; - while (true) { readLine(wc, response, sizeof(response)); - if (response[0] == '\r') + if (response[0] == '\r') { return; + } else if (memcmp(response, "Content-Length:", sizeof("Content-Length:") - 1) == 0) { sscanf(response, "Content-Length: %d", &numBytes); wc->numBytes = numBytes; @@ -510,7 +534,7 @@ Array *CloudComm::putSlot(Slot *slot, int max) { resptype = new Array(7); readURLData(&wc, resptype); timer->endTime(); - + if (resptype->equals(getslot)) { delete resptype; Array *tmp = processSlots(&wc); @@ -523,8 +547,7 @@ Array *CloudComm::putSlot(Slot *slot, int max) { } else { delete resptype; wc.client.stop(); - //throw new Error("Bad response to putslot"); - myerror("Bad response to putslot\n"); + Serial.println("Bad response to putslot"); } /* } catch (SocketTimeoutException *e) { if (resptype != NULL) diff --git a/version2/src/C/Table.cpp b/version2/src/C/Table.cpp index 4fba386..ecca22b 100755 --- a/version2/src/C/Table.cpp +++ b/version2/src/C/Table.cpp @@ -337,14 +337,12 @@ void Table::init() { */ void Table::initTable() { cloud->initSecurity(); - // Create the first insertion into the block chain which is the table status Slot *s = new Slot(this, 1, localMachineId, localSequenceNumber); localSequenceNumber++; TableStatus *status = new TableStatus(s, numberOfSlots); s->addShallowEntry(status); Array *array = cloud->putSlot(s, numberOfSlots); - if (array == NULL) { array = new Array(1); array->set(0, s); diff --git a/version2/src/C/Test.ino b/version2/src/C/Test.ino index de6a0ff..408622c 100644 --- a/version2/src/C/Test.ino +++ b/version2/src/C/Test.ino @@ -9,24 +9,20 @@ TimingSingleton *timer; bool foundError; MyVector * transStatusList; Table *t1; -Table *t2; void setup() { + Serial.begin(); timer = TimingSingleton_getInstance(); foundError = false; - transStatusList = new MyVector(); - // Setup the 2 clients + transStatusList = new MyVector(); IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"); - IoTString * password = new IoTString("reallysecret"); + IoTString * password = new IoTString("reallysecret"); t1 = new Table(baseurl, password, 321, -1); - t1->initTable(); - - t2 = new Table(baseurl, password, 351, -1); - t2->update(); + t1->initTable(); baseurl->releaseRef(); password->releaseRef(); - + // Make the Keys for (int i = 0; i < NUMBER_OF_TESTS; i++) { char buffer[80]; @@ -34,24 +30,15 @@ void setup() { IoTString *ia = new IoTString(buffer); sprintf(buffer, "b%d", i); IoTString *ib = new IoTString(buffer); - sprintf(buffer, "c%d", i); - IoTString *ic = new IoTString(buffer); - sprintf(buffer, "d%d", i); - IoTString *id = new IoTString(buffer); t1->createNewKey(ia, 321); t1->createNewKey(ib, 351); - t2->createNewKey(ic, 321); - t2->createNewKey(id, 351); ia->releaseRef(); ib->releaseRef(); - ic->releaseRef(); - id->releaseRef(); } } void loop() { - // Do Updates for the keys for (int i = 0; i < NUMBER_OF_TESTS; i++) { char buffer[80]; @@ -62,43 +49,22 @@ void loop() { sprintf(buffer, "b%d", i); IoTString * iKeyB = new IoTString(buffer); IoTString * iValueB = new IoTString(buffer); - - sprintf(buffer, "c%d", i); - IoTString * iKeyC = new IoTString(buffer); - IoTString * iValueC = new IoTString(buffer); - - sprintf(buffer, "d%d", i); - IoTString * iKeyD = new IoTString(buffer); - IoTString * iValueD = new IoTString(buffer); t1->startTransaction(); t1->put(iKeyA, iValueA); transStatusList->add(t1->commitTransaction()); iKeyA->releaseRef(); iValueA->releaseRef(); - + t1->startTransaction(); t1->put(iKeyB, iValueB); transStatusList->add(t1->commitTransaction()); iKeyB->releaseRef(); iValueB->releaseRef(); - - t2->startTransaction(); - t2->put(iKeyC, iValueC); - transStatusList->add(t2->commitTransaction()); - iKeyC->releaseRef(); - iValueC->releaseRef(); - - t2->startTransaction(); - t2->put(iKeyD, iValueD); - transStatusList->add(t2->commitTransaction()); - iKeyD->releaseRef(); - iValueD->releaseRef(); } - + t1->update(); - t2->update(); - + for (int i = 0; i < NUMBER_OF_TESTS; i++) { char buffer[80]; sprintf(buffer, "a%d", i); @@ -108,80 +74,26 @@ void loop() { sprintf(buffer, "b%d", i); IoTString * iKeyB = new IoTString(buffer); IoTString * iValueB = new IoTString(buffer); - - sprintf(buffer, "c%d", i); - IoTString * iKeyC = new IoTString(buffer); - IoTString * iValueC = new IoTString(buffer); - - sprintf(buffer, "d%d", i); - IoTString * iKeyD = new IoTString(buffer); - IoTString * iValueD = new IoTString(buffer); - + IoTString *testValA1 = t1->getCommitted(iKeyA); IoTString *testValB1 = t1->getCommitted(iKeyB); - IoTString *testValC1 = t1->getCommitted(iKeyC); - IoTString *testValD1 = t1->getCommitted(iKeyD); - - IoTString *testValA2 = t2->getCommitted(iKeyA); - IoTString *testValB2 = t2->getCommitted(iKeyB); - IoTString *testValC2 = t2->getCommitted(iKeyC); - IoTString *testValD2 = t2->getCommitted(iKeyD); - + if ((testValA1 == NULL) || (testValA1->equals(iValueA) == false)) { // printf("Key-Value t1 incorrect: keyA\n"); foundError = true; } - + if ((testValB1 == NULL) || (testValB1->equals(iValueB) == false)) { // printf("Key-Value t1 incorrect: keyB\n"); foundError = true; } - if ((testValC1 == NULL) || (testValC1->equals(iValueC) == false)) { - // printf("Key-Value t1 incorrect: keyC\n"); - foundError = true; - } - - if ((testValD1 == NULL) || (testValD1->equals(iValueD) == false)) { - // printf("Key-Value t1 incorrect: keyD\n"); - foundError = true; - } - - if ((testValA2 == NULL) || (testValA2->equals(iValueA) == false)) { - // printf("Key-Value t2 incorrect: keyA testValA2\n"); - foundError = true; - } - - if ((testValB2 == NULL) || (testValB2->equals(iValueB) == false)) { -// printf("Key-Value t2 incorrect: keyB testValB2\n"); - foundError = true; - } - - if ((testValC2 == NULL) || (testValC2->equals(iValueC) == false)) { - // printf("Key-Value t2 incorrect: keyC testValC2\n"); - foundError = true; - } - - if ((testValD2 == NULL) || (testValD2->equals(iValueD) == false)) { -// printf("Key-Value t2 incorrect: keyD testValD2\n"); - foundError = true; - } iKeyA->releaseRef(); iValueA->releaseRef(); iKeyB->releaseRef(); iValueB->releaseRef(); - iKeyC->releaseRef(); - iValueC->releaseRef(); - iKeyD->releaseRef(); - iValueD->releaseRef(); testValA1->releaseRef(); - testValA2->releaseRef(); testValB1->releaseRef(); - testValB2->releaseRef(); - testValC1->releaseRef(); - testValC2->releaseRef(); - testValD1->releaseRef(); - testValD2->releaseRef(); } for (uint i = 0; i < transStatusList->size(); i++) { @@ -192,7 +104,7 @@ void loop() { } delete status; } - + if (foundError) { // printf("Found Errors...\n"); } else { @@ -201,4 +113,3 @@ void loop() { delete transStatusList; delete t1; } - -- 2.34.1