edits
authorbdemsky <bdemsky@uci.edu>
Thu, 8 Mar 2018 16:33:37 +0000 (08:33 -0800)
committerbdemsky <bdemsky@uci.edu>
Thu, 8 Mar 2018 16:33:37 +0000 (08:33 -0800)
version2/src/C/CloudComm.cc
version2/src/C/Test.C

index 224018b..9149f2d 100644 (file)
@@ -121,7 +121,7 @@ void CloudComm::initCrypt() {
 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);
-       memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+       memcpy(buffer, baseurl->internalBytes()->internalArray(), baseurl->length());
        int offset = baseurl->length();
        offset += sprintf(&buffer[offset], "?%s&seq=%" PRId64, reqstring, sequencenumber);
        if (maxentries != 0)
@@ -183,12 +183,12 @@ int openURL(IoTString *url) {
        /* fill in the parameters */
        int post = sprintf(message,"POST ");
        /* copy data */
-       memcpy(&message[post], url->internalBytes()->internalArray(), url->length());
-       int endpost = sprintf(&message[post+url->length()], " HTTP/1.1\r\n");
+       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()], "Host: ");
-       memcpy(&message[endpost + post + url->length()+hostlen], url->internalBytes()->internalArray(), url->length());
-       sprintf(&message[endpost + post + 2*url->length()+hostlen], "\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");
        
        /* create the socket */
        int sockfd = socket(AF_INET, SOCK_STREAM, 0);
@@ -217,7 +217,6 @@ int openURL(IoTString *url) {
        /* send the request */
        int total = strlen(message);
        loopWrite(sockfd, message, total);
-
        return sockfd;
 }
 
@@ -365,6 +364,46 @@ int getResponseCode(int fd) {
        return respcode;
 }
 
+void readHeaders(int fd) {
+       int state = 2;
+       char newchar;
+
+       while(true) {
+               int bytes = read(fd, &newchar, 1);
+               if (bytes <= 0)
+                       throw new Error("Headers malformed!");
+               switch (state) {
+               case 0:
+                       if (newchar == '\r')
+                               state = 1;
+                       break;
+               case 1:
+                       if (newchar == '\n')
+                               state = 2;
+                       else
+                               state = 0;
+                       break;
+               case 2:
+                       if (newchar == '\r')
+                               state = 3;
+                       else
+                               state = 0;
+                       break;
+               case 3:
+                       if (newchar == '\n')
+                               state = 4;
+                       else
+                               state = 0;
+                       break;
+               default:
+                       printf("ERROR in readHeaders\n");
+                       exit(-1);
+               }
+               if (state == 4)
+                       return;
+       }
+}
+
 void CloudComm::setSalt() {
        if (salt != NULL) {
                // Salt already sent to server so don't set it again
@@ -377,7 +416,7 @@ void CloudComm::setSalt() {
                random->nextBytes(saltTmp);
 
                char *buffer = (char *) malloc(baseurl->length() + 100);
-               memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+               memcpy(buffer, baseurl->internalBytes()->internalArray(), baseurl->length());
                int offset = baseurl->length();
                offset += sprintf(&buffer[offset], "?req=setsalt");
                IoTString *urlstr = new IoTString(buffer);
@@ -391,7 +430,8 @@ void CloudComm::setSalt() {
                if (responsecode != HttpURLConnection_HTTP_OK) {
                        throw new Error("Invalid response");
                }
-
+               close(fd);
+               
                timer->endTime();
                salt = saltTmp;
        } catch (Exception *e) {
@@ -406,7 +446,7 @@ bool CloudComm::getSalt() {
 
        try {
                char *buffer = (char *) malloc(baseurl->length() + 100);
-               memcpy(buffer, baseurl->internalBytes(), baseurl->length());
+               memcpy(buffer, baseurl->internalBytes()->internalArray(), baseurl->length());
                int offset = baseurl->length();
                offset += sprintf(&buffer[offset], "?req=getsalt");
                urlstr = new IoTString(buffer);
@@ -429,12 +469,15 @@ bool CloudComm::getSalt() {
        try {
                timer->startTime();
                int responsecode = getResponseCode(fd);
+               readHeaders(fd);
                if (responsecode != HttpURLConnection_HTTP_OK) {
                        throw new Error("Invalid response");
                }
                int salt_length = readURLInt(fd);
                Array<char> *tmp = new Array<char>(salt_length);
                readURLData(fd, tmp);
+               close(fd);
+
                salt = tmp;
                timer->endTime();
                return true;
@@ -526,19 +569,26 @@ Array<Slot *> *CloudComm::putSlot(Slot *slot, int max) {
 
        try {
                int respcode = getResponseCode(fd);
+               readHeaders(fd);
                timer->startTime();
                Array<char> *resptype = new Array<char>(7);
                readURLData(fd, resptype);
                timer->endTime();
 
                if (resptype->equals(getslot)) {
-                       return processSlots(fd);
+                       Array<Slot *> * tmp =processSlots(fd);
+                       close(fd);
+                       return tmp;
                } else if (resptype->equals(putslot)) {
+                       close(fd);
                        return NULL;
-               } else
+               } else {
+                       close(fd);
                        throw new Error("Bad response to putslot");
+               }
        } catch (SocketTimeoutException *e) {
                timer->endTime();
+               close(fd);
                throw new ServerException("putSlot failed", ServerException_TypeInputTimeout);
        } catch (Exception *e) {
                throw new Error("putSlot failed");
@@ -578,15 +628,19 @@ Array<Slot *> *CloudComm::getSlots(int64_t sequencenumber) {
        try {
                timer->startTime();
                int responsecode = getResponseCode(fd);
+               readHeaders(fd);
                Array<char> *resptype = new Array<char>(7);
                readURLData(fd, resptype);
                timer->endTime();
                if (!resptype->equals(getslot))
                        throw new Error("Bad Response: ");
 
-               return processSlots(fd);
+               Array<Slot*> * tmp=processSlots(fd);
+               close(fd);
+               return tmp;
        } catch (SocketTimeoutException *e) {
                timer->endTime();
+               close(fd);
                throw new ServerException("getSlots failed", ServerException_TypeInputTimeout);
        } catch (Exception *e) {
                throw new Error("getSlots failed");
index c804e75..14fec0d 100644 (file)
@@ -1,6 +1,166 @@
 #include "Table.h"
+#include "IoTString.h"
+#include "TimingSingleton.h"
+#include "TransactionStatus.h"
+
+#define NUMBER_OF_TESTS 2
 
 int main(int numargs, char ** args) {
-       Table * t = new Table(NULL, NULL, 0, 0);
-       delete t;
+       TimingSingleton * timer = TimingSingleton_getInstance();
+
+       bool foundError = false;
+       Vector<TransactionStatus *> * transStatusList = new Vector<TransactionStatus *>();
+
+       // Setup the 2 clients
+       Table * t1 = new Table(new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"), new IoTString("reallysecret"), 321, -1);
+       t1->initTable();
+       printf("T1 Ready\n");
+
+       Table * t2 = new Table(new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"), new IoTString("reallysecret"), 351, -1);
+       t2->update();
+       printf("T2 Ready\n");
+
+       // Make the Keys
+       printf("Setting up keys\n");
+       for (int i = 0; i < NUMBER_OF_TESTS; i++) {
+               printf("%d\n",i);
+               char buffer[80];
+               sprintf(buffer, "a%d", i);
+               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);
+       }
+       
+       // Do Updates for the keys
+       printf("Setting Key-Values...\n");
+       for (int i = 0; i < NUMBER_OF_TESTS; i++) {
+               printf("%d\n", i);
+               char buffer[80];
+               sprintf(buffer, "a%d", i);
+               IoTString * iKeyA = new IoTString(buffer);
+               IoTString * iValueA = new IoTString(buffer);
+
+               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->addKV(iKeyA, iValueA);
+               transStatusList->add(t1->commitTransaction());
+               t1->startTransaction();
+               t1->addKV(iKeyB, iValueB);
+               transStatusList->add(t1->commitTransaction());
+               
+               t2->startTransaction();
+               t2->addKV(iKeyC, iValueC);
+               transStatusList->add(t2->commitTransaction());
+               
+               t2->startTransaction();
+               t2->addKV(iKeyD, iValueD);
+               transStatusList->add(t2->commitTransaction());
+       }
+       printf("Updating Clients...\n");
+       t1->update();
+       t2->update();
+       
+       printf("Checking Key-Values...\n");
+       for (int i = 0; i < NUMBER_OF_TESTS; i++) {
+               char buffer[80];
+               sprintf(buffer, "a%d", i);
+               IoTString * iKeyA = new IoTString(buffer);
+               IoTString * iValueA = new IoTString(buffer);
+
+               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;
+               }
+       }
+
+       for (uint i = 0; i < transStatusList->size(); i++) {
+               TransactionStatus * status = transStatusList->get(i);
+               if (status->getStatus() != TransactionStatus_StatusCommitted) {
+                       foundError = true;
+                       printf("Status error\n");
+               }
+       }
+       
+       if (foundError) {
+               printf("Found Errors...\n");
+       } else {
+               printf("No Errors Found...\n");
+       }
 }
+