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) {
delete directory;
if (data)
delete data;
- if (dir != NULL)
- closedir(dir);
}
bool IoTQuery::checkDirectory() {
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() {
char * reqlength = FCGX_GetParam(length_str, request->envp);
if (length) {
- length=strtol(reqlength, NULL, 10);
+ length=strtoll(reqlength, NULL, 10);
} else {
length=0;
}
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() {
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;