int main(void) {
- // Backup the stdio streambufs
- streambuf * cin_streambuf = cin.rdbuf();
- streambuf * cout_streambuf = cout.rdbuf();
- streambuf * cerr_streambuf = cerr.rdbuf();
-
- FCGX_Request request;
-
- FCGX_Init();
- FCGX_InitRequest(&request, 0, 0);
-
- while (FCGX_Accept_r(&request) == 0) {
- fcgi_streambuf cin_fcgi_streambuf(request.in);
- fcgi_streambuf cout_fcgi_streambuf(request.out);
- fcgi_streambuf cerr_fcgi_streambuf(request.err);
-
- cin.rdbuf(&cin_fcgi_streambuf);
- cout.rdbuf(&cout_fcgi_streambuf);
- cerr.rdbuf(&cerr_fcgi_streambuf);
- IoTQuery * iotquery=new IoTQuery(&request);
- iotquery->processQuery();
- delete iotquery;
- }
-
- // restore stdio streambufs
- cin.rdbuf(cin_streambuf);
- cout.rdbuf(cout_streambuf);
- cerr.rdbuf(cerr_streambuf);
-
- return 0;
+ // Backup the stdio streambufs
+ streambuf * cin_streambuf = cin.rdbuf();
+ streambuf * cout_streambuf = cout.rdbuf();
+ streambuf * cerr_streambuf = cerr.rdbuf();
+
+ FCGX_Request request;
+
+ FCGX_Init();
+ FCGX_InitRequest(&request, 0, 0);
+
+ while (FCGX_Accept_r(&request) == 0) {
+ fcgi_streambuf cin_fcgi_streambuf(request.in);
+ fcgi_streambuf cout_fcgi_streambuf(request.out);
+ fcgi_streambuf cerr_fcgi_streambuf(request.err);
+
+ cin.rdbuf(&cin_fcgi_streambuf);
+ cout.rdbuf(&cout_fcgi_streambuf);
+ cerr.rdbuf(&cerr_fcgi_streambuf);
+ IoTQuery * iotquery=new IoTQuery(&request);
+ iotquery->processQuery();
+ delete iotquery;
+ }
+
+ // restore stdio streambufs
+ cin.rdbuf(cin_streambuf);
+ cout.rdbuf(cout_streambuf);
+ cerr.rdbuf(cerr_streambuf);
+
+ return 0;
}
const char * length_str="CONTENT_LENGTH";
IoTQuery::IoTQuery(FCGX_Request *request) :
- request(request),
- data(NULL),
- directory(NULL),
- uri(NULL),
- query(NULL),
- method(NULL),
- iotcloudroot(NULL),
- dir(NULL),
- length(0),
- firstentry(0),
- lastentry(0),
- requestsequencenumber(0),
- numqueueentries(DEFAULT_SIZE),
- fd(-1)
-{
+ request(request),
+ data(NULL),
+ directory(NULL),
+ uri(NULL),
+ query(NULL),
+ method(NULL),
+ iotcloudroot(NULL),
+ dir(NULL),
+ length(0),
+ firstentry(0),
+ lastentry(0),
+ requestsequencenumber(0),
+ numqueueentries(DEFAULT_SIZE),
+ fd(-1) {
}
IoTQuery::~IoTQuery() {
- if (fd >= 0)
- close(fd);
- if (directory)
- delete directory;
- if (data)
- delete data;
- if (dir != NULL)
- closedir(dir);
+ if (fd >= 0)
+ close(fd);
+ if (directory)
+ delete directory;
+ if (data)
+ delete data;
+ if (dir != NULL)
+ closedir(dir);
}
bool IoTQuery::checkDirectory() {
- struct stat s;
- int err=stat(directory, &s);
- if (-1 == err)
- return false;
- return S_ISDIR(s.st_mode);
+ struct stat s;
+ int err=stat(directory, &s);
+ if (-1 == err)
+ return false;
+ return S_ISDIR(s.st_mode);
}
void IoTQuery::decodeQuery() {
- int len=strlen(query);
- char * str=new char[len+1];
- memcpy(str, query, len+1);
- char *tok_ptr=str;
-
- /* Parse commands */
- char *command=strsep(&tok_ptr, "&");
- if (strncmp(command, "putslot", 7) == 0)
- reqPutSlot = true;
-
- if (strncmp(command, "getslot", 7) == 0)
- reqGetSlot = true;
-
- /* Load Sequence Number for request */
- char *sequencenumber_str = strsep(&tok_ptr, "&");
-
- if (sequencenumber_str != NULL)
- requestsequencenumber = strtol(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);
-
- delete str;
+ int len=strlen(query);
+ char * str=new char[len+1];
+ memcpy(str, query, len+1);
+ char *tok_ptr=str;
+
+ /* Parse commands */
+ char *command=strsep(&tok_ptr, "&");
+ if (strncmp(command, "putslot", 7) == 0)
+ reqPutSlot = true;
+
+ if (strncmp(command, "getslot", 7) == 0)
+ reqGetSlot = true;
+
+ /* Load Sequence Number for request */
+ char *sequencenumber_str = strsep(&tok_ptr, "&");
+
+ if (sequencenumber_str != NULL)
+ requestsequencenumber = strtol(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);
+
+ delete str;
}
void IoTQuery::getSlot() {
-
+
}
void IoTQuery::putSlot() {
-
+
}
void IoTQuery::processQuery() {
- getQuery();
- getDirectory();
- readData();
-
- if (strncmp(method, "POST", 4) != 0)
- return;
-
- if (directory == NULL ||
- !checkDirectory())
- return;
-
- if (!openStatusFile())
- return;
-
- flock(fd, LOCK_EX);
-
- decodeQuery();
-
- if (reqGetSlot)
- getSlot();
- else if (reqPutSlot)
- putSlot();
- else return;
+ getQuery();
+ getDirectory();
+ readData();
+
+ if (strncmp(method, "POST", 4) != 0)
+ return;
+
+ if (directory == NULL ||
+ !checkDirectory())
+ return;
+
+ if (!openStatusFile())
+ return;
+
+ flock(fd, LOCK_EX);
+
+ decodeQuery();
+
+ if (reqGetSlot)
+ getSlot();
+ else if (reqPutSlot)
+ putSlot();
+ else return;
}
void IoTQuery::readData() {
- if (length) {
- data = new char[length+1];
- memset(data, 0, length+1);
- cin.read(data, length);
- }
- do {
- char dummy;
- cin >> dummy;
- } while (!cin.eof());
+ if (length) {
+ data = new char[length+1];
+ memset(data, 0, length+1);
+ cin.read(data, length);
+ }
+ do {
+ char dummy;
+ cin >> dummy;
+ } while (!cin.eof());
}
void IoTQuery::getQuery() {
- uri = FCGX_GetParam(uri_str, request->envp);
- query = FCGX_GetParam(query_str, request->envp);
- method = FCGX_GetParam(method_str, request->envp);
- iotcloudroot = FCGX_GetParam(iotcloudroot_str, request->envp);
-
- char * reqlength = FCGX_GetParam(length_str, request->envp);
- if (length) {
- length=strtol(reqlength, NULL, 10);
- } else {
- length=0;
- }
+ uri = FCGX_GetParam(uri_str, request->envp);
+ query = FCGX_GetParam(query_str, request->envp);
+ method = FCGX_GetParam(method_str, request->envp);
+ iotcloudroot = FCGX_GetParam(iotcloudroot_str, request->envp);
+
+ char * reqlength = FCGX_GetParam(length_str, request->envp);
+ if (length) {
+ length=strtol(reqlength, NULL, 10);
+ } else {
+ length=0;
+ }
}
void IoTQuery::getDirectory() {
- char * split = strchr((char *)uri, '?');
- if (split == NULL)
- return;
- int split_len = (int) (split-uri);
- int rootdir_len = strlen(iotcloudroot);
- int directory_len = split_len + rootdir_len + 1;
- directory = new char[directory_len];
- memcpy(directory, iotcloudroot, rootdir_len);
- memcpy(directory + rootdir_len, uri, split_len);
- directory[directory_len]=0;
+ char * split = strchr((char *)uri, '?');
+ if (split == NULL)
+ return;
+ int split_len = (int) (split-uri);
+ int rootdir_len = strlen(iotcloudroot);
+ int directory_len = split_len + rootdir_len + 1;
+ directory = new char[directory_len];
+ memcpy(directory, iotcloudroot, rootdir_len);
+ memcpy(directory + rootdir_len, uri, split_len);
+ directory[directory_len]=0;
}
int doread(int fd, void *ptr, size_t count, off_t offset) {
- do {
- size_t bytesread=pread(fd, ptr, count, offset);
- if (bytesread==count) {
- return 1;
- } else if (bytesread==0) {
- return 0;
- }
- } while(1);
+ do {
+ size_t bytesread=pread(fd, ptr, count, offset);
+ if (bytesread==count) {
+ return 1;
+ } else if (bytesread==0) {
+ return 0;
+ }
+ } while(1);
}
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, &numqueueentries, sizeof(numqueueentries), OFFSET_MAX);
+ pwrite(fd, &firstentry, sizeof(firstentry), OFFSET_FIRST);
+ pwrite(fd, &lastentry, sizeof(lastentry), OFFSET_LAST);
}
bool IoTQuery::openStatusFile() {
- char statusfile[]="queuestatus";
- int len=strlen(directory);
-
- char * filename=new char[len+sizeof(statusfile)+2];
- memcpy(filename, directory, len);
- filename[len]='/';
- memcpy(filename+len+1, statusfile, sizeof(statusfile));
- filename[len+sizeof(statusfile)+1]=0;
- fd=open(filename, O_CREAT| O_RDWR, S_IRUSR| S_IWUSR);
- delete filename;
-
- if (fd < 0)
- return false;
-
- int size;
- int needwrite=0;
- if (doread(fd, &size, sizeof(size), OFFSET_MAX))
- numqueueentries=size;
- else
- needwrite=1;
-
- long entry;
- if (doread(fd, &entry, sizeof(entry), OFFSET_FIRST))
- firstentry=entry;
- else
- needwrite=1;
-
- if (doread(fd, &entry, sizeof(entry), OFFSET_LAST))
- lastentry=entry;
- else
- needwrite=1;
-
- if (needwrite)
- updateStatusFile();
-
- return true;
+ char statusfile[]="queuestatus";
+ int len=strlen(directory);
+
+ char * filename=new char[len+sizeof(statusfile)+2];
+ memcpy(filename, directory, len);
+ filename[len]='/';
+ memcpy(filename+len+1, statusfile, sizeof(statusfile));
+ filename[len+sizeof(statusfile)+1]=0;
+ fd=open(filename, O_CREAT| O_RDWR, S_IRUSR| S_IWUSR);
+ delete filename;
+
+ if (fd < 0)
+ return false;
+
+ int size;
+ int needwrite=0;
+ if (doread(fd, &size, sizeof(size), OFFSET_MAX))
+ numqueueentries=size;
+ else
+ needwrite=1;
+
+ long entry;
+ if (doread(fd, &entry, sizeof(entry), OFFSET_FIRST))
+ firstentry=entry;
+ else
+ needwrite=1;
+
+ if (doread(fd, &entry, sizeof(entry), OFFSET_LAST))
+ lastentry=entry;
+ else
+ needwrite=1;
+
+ if (needwrite)
+ updateStatusFile();
+
+ return true;
}
-
+