more code
authorBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Fri, 22 Jul 2016 20:21:58 +0000 (13:21 -0700)
committerBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Fri, 22 Jul 2016 20:21:58 +0000 (13:21 -0700)
src/server/.dir-locals.el
src/server/iotquery.cpp
src/server/iotquery.h

index ce85e5f..e166a2e 100644 (file)
@@ -1 +1,2 @@
-((nil . ((indent-tabs-mode . t))))
\ No newline at end of file
+((nil . ((indent-tabs-mode . t))))
+
index 8fc9fad..d2d80e4 100644 (file)
@@ -23,10 +23,9 @@ IoTQuery::IoTQuery(FCGX_Request *request) :
        query(NULL),
        method(NULL),
        iotcloudroot(NULL),
-       dir(NULL),
        length(0),
-       firstentry(0),
-       lastentry(0),
+       oldestentry(0),
+       newestentry(0),
        requestsequencenumber(0),
        numqueueentries(DEFAULT_SIZE),
        fd(-1) {
@@ -39,8 +38,6 @@ IoTQuery::~IoTQuery() {
                delete directory;
        if (data)
                delete data;
-       if (dir != NULL)
-               closedir(dir);
 }
 
 bool IoTQuery::checkDirectory() {
@@ -69,22 +66,71 @@ void IoTQuery::decodeQuery() {
        char *sequencenumber_str = strsep(&tok_ptr, "&");
 
        if (sequencenumber_str != NULL)
-               requestsequencenumber = strtol(sequencenumber_str, NULL, 10);
+               requestsequencenumber = strtoll(sequencenumber_str, NULL, 10);
 
        /* Update size if we get request */
        char * numqueueentries_str = tok_ptr;
        if (numqueueentries_str != NULL)
-               numqueueentries = strtol(numqueueentries_str, NULL, 10);
+               numqueueentries = strtoll(numqueueentries_str, NULL, 10);
 
        delete str;
 }
 
-void IoTQuery::getSlot() {
+void doWrite(int fd, char *data, long long length) {
+       long long offset=0;
+       do {
+               long long byteswritten=write(fd, &data[offset], length);
+               if (byteswritten > 0) {
+                       length -= byteswritten;
+                       offset += byteswritten;
+               } else {
+                       cerr << "Bytes not written";
+                       return;
+               }
+       } while(length != 0);
+}
 
+void IoTQuery::getSlot() {
+       
 }
 
 void IoTQuery::putSlot() {
+       if (requestsequencenumber!=(newestentry+1)) {
+               getSlot();
+               return;
+       }
+       
+       int numberofliveslots=(int) ((newestentry-oldestentry)+1);
+       if (numberofliveslots >=  numqueueentries) {
+               //need to drop slot
+               removeOldestSlot();
+       }
+       //write slot data out to file
+       char *filename = getSlotFileName(requestsequencenumber);
+       int slotfd = open(filename, O_CREAT|O_WRONLY, S_IRUSR| S_IWUSR);
+       doWrite(slotfd, data, length);
+       close(slotfd);
+       delete filename;
+       newestentry = requestsequencenumber; // update sequence number
+       updateStatusFile(); // update counts
+}
+
+char * IoTQuery::getSlotFileName(long long slot) {
+       int directorylen=strlen(directory);
+       char * filename=new char[25+directorylen];//19 digits for long number + 4 characters for SLOT + 1 character for null termination
+       snprintf(filename, 24+directorylen+1, "%s/SLOT%lld", directory, slot);
+       return filename;
+}
+
 
+
+void IoTQuery::removeOldestSlot() {
+       if (oldestentry!=0) {
+               char * filename=getSlotFileName(oldestentry);
+               unlink(filename);
+               delete filename;
+       }
+       oldestentry++;
 }
 
 void IoTQuery::processQuery() {
@@ -133,7 +179,7 @@ void IoTQuery::getQuery() {
 
        char * reqlength = FCGX_GetParam(length_str, request->envp);
        if (length) {
-               length=strtol(reqlength, NULL, 10);
+               length=strtoll(reqlength, NULL, 10);
        } else {
                length=0;
        }
@@ -165,8 +211,8 @@ int doread(int fd, void *ptr, size_t count, off_t offset) {
 
 void IoTQuery::updateStatusFile() {
        pwrite(fd, &numqueueentries, sizeof(numqueueentries), OFFSET_MAX);
-       pwrite(fd, &firstentry, sizeof(firstentry), OFFSET_FIRST);
-       pwrite(fd, &lastentry, sizeof(lastentry), OFFSET_LAST);
+       pwrite(fd, &oldestentry, sizeof(oldestentry), OFFSET_OLD);
+       pwrite(fd, &newestentry, sizeof(newestentry), OFFSET_NEW);
 }
 
 bool IoTQuery::openStatusFile() {
@@ -191,14 +237,14 @@ bool IoTQuery::openStatusFile() {
        else
                needwrite=1;
 
-       long entry;
-       if (doread(fd, &entry, sizeof(entry), OFFSET_FIRST))
-               firstentry=entry;
+       long long entry;
+       if (doread(fd, &entry, sizeof(entry), OFFSET_OLD))
+               oldestentry=entry;
        else
                needwrite=1;
 
-       if (doread(fd, &entry, sizeof(entry), OFFSET_LAST))
-               lastentry=entry;
+       if (doread(fd, &entry, sizeof(entry), OFFSET_NEW))
+               newestentry=entry;
        else
                needwrite=1;
 
index 5aba593..2136a2d 100644 (file)
@@ -3,12 +3,11 @@
 #include <iostream>
 #include "fcgio.h"
 #include "fcgi_stdio.h"
-#include <dirent.h>
 
 #define DEFAULT_SIZE 128
 #define OFFSET_MAX 0
-#define OFFSET_FIRST 4
-#define OFFSET_LAST 12
+#define OFFSET_OLD 4
+#define OFFSET_NEW 12
 
 class IoTQuery {
  public:
@@ -26,6 +25,8 @@ class IoTQuery {
        void decodeQuery();
        void getSlot();
        void putSlot();
+       void removeOldestSlot();
+       char * getSlotFileName(long long);
        
        FCGX_Request * request;
        char *data;
@@ -34,11 +35,10 @@ class IoTQuery {
        const char * query;
        const char * method;
        const char * iotcloudroot;
-       DIR *dir;
-       long length;
-       long firstentry;
-       long lastentry;
-       long requestsequencenumber;
+       long long length;
+       long long oldestentry;
+       long long newestentry;
+       long long requestsequencenumber;
        int numqueueentries;
        int fd;
        bool reqGetSlot;