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;
byteswritten += bytes;
bytestowrite -= bytes;
} else {
- //printf("Error in write\n");
+ Serial.println("Error in write");
exit(-1);
}
}
bytesread += bytes;
bytestoread -= bytes;
} else {
- //printf("Error in read\n");
- exit(-1);
+ if (!client->connected()) {
+ Serial.println("Client disconnected");
+ exit(-1);
+ }
}
}
}
if (url->get(i) == '/')
break;
+
if ( i == url->length()) {
//printf("ERROR in openURL\n");
exit(-1);
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 */
/* 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);
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);
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;
resptype = new Array<char>(7);
readURLData(&wc, resptype);
timer->endTime();
-
+
if (resptype->equals(getslot)) {
delete resptype;
Array<Slot *> *tmp = processSlots(&wc);
} 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)
bool foundError;
MyVector<TransactionStatus *> * transStatusList;
Table *t1;
-Table *t2;
void setup() {
+ Serial.begin();
timer = TimingSingleton_getInstance();
foundError = false;
- transStatusList = new MyVector<TransactionStatus *>();
- // Setup the 2 clients
+ transStatusList = new MyVector<TransactionStatus *>();
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];
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];
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);
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++) {
}
delete status;
}
-
+
if (foundError) {
// printf("Found Errors...\n");
} else {
delete transStatusList;
delete t1;
}
-