12 const char * query_str="QUERY_STRING";
13 const char * uri_str="REQUEST_URI";
14 const char * method_str="REQUEST_METHOD";
15 const char * iotcloudroot_str="IOTCLOUD_ROOT";
16 const char * length_str="CONTENT_LENGTH";
18 IoTQuery::IoTQuery(FCGX_Request *request) :
30 requestsequencenumber(0),
31 numqueueentries(DEFAULT_SIZE),
35 IoTQuery::~IoTQuery() {
46 bool IoTQuery::checkDirectory() {
48 int err=stat(directory, &s);
51 return S_ISDIR(s.st_mode);
54 void IoTQuery::decodeQuery() {
55 int len=strlen(query);
56 char * str=new char[len+1];
57 memcpy(str, query, len+1);
61 char *command=strsep(&tok_ptr, "&");
62 if (strncmp(command, "putslot", 7) == 0)
65 if (strncmp(command, "getslot", 7) == 0)
68 /* Load Sequence Number for request */
69 char *sequencenumber_str = strsep(&tok_ptr, "&");
71 if (sequencenumber_str != NULL)
72 requestsequencenumber = strtol(sequencenumber_str, NULL, 10);
74 /* Update size if we get request */
75 char * numqueueentries_str = tok_ptr;
76 if (numqueueentries_str != NULL)
77 numqueueentries = strtol(numqueueentries_str, NULL, 10);
82 void IoTQuery::getSlot() {
86 void IoTQuery::putSlot() {
90 void IoTQuery::processQuery() {
95 if (strncmp(method, "POST", 4) != 0)
98 if (directory == NULL ||
102 if (!openStatusFile())
116 void IoTQuery::readData() {
118 data = new char[length+1];
119 memset(data, 0, length+1);
120 cin.read(data, length);
125 } while (!cin.eof());
128 void IoTQuery::getQuery() {
129 uri = FCGX_GetParam(uri_str, request->envp);
130 query = FCGX_GetParam(query_str, request->envp);
131 method = FCGX_GetParam(method_str, request->envp);
132 iotcloudroot = FCGX_GetParam(iotcloudroot_str, request->envp);
134 char * reqlength = FCGX_GetParam(length_str, request->envp);
136 length=strtol(reqlength, NULL, 10);
142 void IoTQuery::getDirectory() {
143 char * split = strchr((char *)uri, '?');
146 int split_len = (int) (split-uri);
147 int rootdir_len = strlen(iotcloudroot);
148 int directory_len = split_len + rootdir_len + 1;
149 directory = new char[directory_len];
150 memcpy(directory, iotcloudroot, rootdir_len);
151 memcpy(directory + rootdir_len, uri, split_len);
152 directory[directory_len]=0;
155 int doread(int fd, void *ptr, size_t count, off_t offset) {
157 size_t bytesread=pread(fd, ptr, count, offset);
158 if (bytesread==count) {
160 } else if (bytesread==0) {
166 void IoTQuery::updateStatusFile() {
167 pwrite(fd, &numqueueentries, sizeof(numqueueentries), OFFSET_MAX);
168 pwrite(fd, &firstentry, sizeof(firstentry), OFFSET_FIRST);
169 pwrite(fd, &lastentry, sizeof(lastentry), OFFSET_LAST);
172 bool IoTQuery::openStatusFile() {
173 char statusfile[]="queuestatus";
174 int len=strlen(directory);
176 char * filename=new char[len+sizeof(statusfile)+2];
177 memcpy(filename, directory, len);
179 memcpy(filename+len+1, statusfile, sizeof(statusfile));
180 filename[len+sizeof(statusfile)+1]=0;
181 fd=open(filename, O_CREAT| O_RDWR, S_IRUSR| S_IWUSR);
189 if (doread(fd, &size, sizeof(size), OFFSET_MAX))
190 numqueueentries=size;
195 if (doread(fd, &entry, sizeof(entry), OFFSET_FIRST))
200 if (doread(fd, &entry, sizeof(entry), OFFSET_LAST))