code written, not debugged
authorBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Fri, 22 Jul 2016 21:09:03 +0000 (14:09 -0700)
committerBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Fri, 22 Jul 2016 21:09:03 +0000 (14:09 -0700)
src/server/iotquery.cpp
src/server/iotquery.h

index d2d80e4..39dacab 100644 (file)
@@ -90,8 +90,69 @@ void doWrite(int fd, char *data, long long length) {
        } 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() {
@@ -113,6 +174,15 @@ 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) {
@@ -122,8 +192,6 @@ char * IoTQuery::getSlotFileName(long long slot) {
        return filename;
 }
 
-
-
 void IoTQuery::removeOldestSlot() {
        if (oldestentry!=0) {
                char * filename=getSlotFileName(oldestentry);
index 2136a2d..6e35f9c 100644 (file)
@@ -16,6 +16,7 @@ class IoTQuery {
        void processQuery();
        
  private:
+       void sendResponse(char *data, int length);
        void getQuery();
        void getDirectory();
        void readData();