} while(length != 0);
}
+bool doRead(int fd, void *buf, int numbytes) {
+ int offset=0;
+ char *ptr=(char *)buf;
+ do {
+ int bytesread=read(fd, ptr+offset, numbytes);
+ if (bytesread > 0) {
+ offset += bytesread;
+ numbytes -= bytesread;
+ } else
+ return false;
+ } while (numbytes!=0);
+ return true;
+}
+
void IoTQuery::getSlot() {
+ int numrequeststosend = (int)((newestentry-requestsequencenumber)+1);
+ long long numbytes = 0;
+ int filesizes[numrequeststosend];
+ int fdarray[numrequeststosend];
+ int index=0;
+ for(long long seqn = requestsequencenumber; seqn <= newestentry; seqn++, index++) {
+ struct stat st;
+ char *filename=getSlotFileName(seqn);
+ if (stat(filename, &st) == 0) {
+ fdarray[index]=open(filename, O_RDONLY);
+ filesizes[index]=st.st_size;
+ numbytes+=filesizes[index];
+ } else {
+ fdarray[index]=-1;
+ filesizes[index]=0;
+ }
+ delete filename;
+ }
+ const char header[]="getdata";
+ long long size=sizeof(header)+sizeof(numrequeststosend)+4*numrequeststosend+numbytes; //header + payload + file count + sizes
+ char * response = new char[size];
+ long long offset=0;
+ memcpy(response, header, sizeof(header));
+ offset+=sizeof(header);
+ memcpy(response + offset, &numrequeststosend, sizeof(numrequeststosend));
+ offset+=sizeof(numrequeststosend);
+ for(int i=0;i<numrequeststosend;i++) {
+ memcpy(response + offset, &filesizes[i], sizeof(int));
+ offset+=sizeof(int);
+ }
+
+ //copy file data
+ for(int i=0;i<numrequeststosend;i++) {
+ if (fdarray[i]>=0) {
+ doRead(fdarray[i], response+offset, filesizes[i]);
+ offset+=filesizes[i];
+ }
+ }
+ //write response out
+ sendResponse(response, size);
+
+ //cleanup
+ delete response;
+ for(int i=0;i<numrequeststosend;i++) {
+ if (fdarray[i] >= 0)
+ close(fdarray[i]);
+ }
}
void IoTQuery::putSlot() {
delete filename;
newestentry = requestsequencenumber; // update sequence number
updateStatusFile(); // update counts
+ char command[]="putdata";
+ sendResponse(command, sizeof(command));
+}
+
+void IoTQuery::sendResponse(char * bytes, int len) {
+ cout << "Accept-Ranges: bytes\r\n"
+ << "Content-Length: " << len << "\r\n"
+ << "\r\n";
+ cout.write(bytes, len);
}
char * IoTQuery::getSlotFileName(long long slot) {
return filename;
}
-
-
void IoTQuery::removeOldestSlot() {
if (oldestentry!=0) {
char * filename=getSlotFileName(oldestentry);