From 88ea00202a33c62a0ebcb584cdf7ca356bc4a386 Mon Sep 17 00:00:00 2001
From: Matt Strapp <strap012@umn.edu>
Date: Tue, 17 Nov 2020 19:23:03 -0600
Subject: help

---
 P3/.vscode/launch.json |  36 +++++++++++++++++++++++
 P3/.vscode/tasks.json  |  21 +++++++++++++
 P3/server.c            |  78 +++++++++++++++++++++++++++----------------------
 P3/web_server_sol      | Bin
 4 files changed, 100 insertions(+), 35 deletions(-)
 create mode 100644 P3/.vscode/launch.json
 create mode 100644 P3/.vscode/tasks.json
 mode change 100644 => 100755 P3/web_server_sol

diff --git a/P3/.vscode/launch.json b/P3/.vscode/launch.json
new file mode 100644
index 0000000..4f832a7
--- /dev/null
+++ b/P3/.vscode/launch.json
@@ -0,0 +1,36 @@
+{
+    // Use IntelliSense to learn about possible attributes.
+    // Hover to view descriptions of existing attributes.
+    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": [
+    {
+        "name": "Build & Launch",
+        "type": "cppdbg",
+        "request": "launch",
+        "program": "${workspaceFolder}/web_server",
+        "args": ["1025", "testing", "1", "1", "0", "26", "0"],
+        "stopAtEntry": false,
+        
+        "cwd": "${workspaceFolder}",
+        "environment": [],
+        "externalConsole": false,
+        "MIMode": "gdb",
+        "setupCommands": [
+            {
+                "description": "Enable pretty-printing for gdb",
+                "text": "-enable-pretty-printing",
+                "ignoreFailures": true
+            },
+        ],
+        // "customLaunchSetupCommands": [
+        //     {
+        //         "description": "PLEASE WORK",
+        //         "text": "set detach-on-fork off",
+        //         "ignoreFailures": false
+        //     }
+        //],
+        "preLaunchTask": "Make",
+    },
+    ]
+}
\ No newline at end of file
diff --git a/P3/.vscode/tasks.json b/P3/.vscode/tasks.json
new file mode 100644
index 0000000..9080b5b
--- /dev/null
+++ b/P3/.vscode/tasks.json
@@ -0,0 +1,21 @@
+{
+    // See https://go.microsoft.com/fwlink/?LinkId=733558
+    // for the documentation about the tasks.json format
+    "version": "2.0.0",
+    "tasks": [
+        {
+            "label": "Make",
+            "type": "shell",
+            "command": "make",
+            "group": {
+                "kind": "build",
+                "isDefault": true
+            },
+            "presentation": {
+                "reveal": "silent",
+                "clear": true
+            }
+        },
+        
+    ]
+}
\ No newline at end of file
diff --git a/P3/server.c b/P3/server.c
index a57d616..4e0b965 100644
--- a/P3/server.c
+++ b/P3/server.c
@@ -108,9 +108,15 @@ char* getContentType(char * mybuf) {
 
 // Function to open and read the file from the disk into the memory
 // Add necessary arguments as needed
-int readFromDisk(const char* fileName, char* buffer) {
+int readFromDisk(char* fileName, char* buffer, long fileSize) {
     // Open and read the contents of file given the request
-    
+    FILE* requestFile = fopen(fileName, "r"); 
+    if (requestFile == NULL) {
+      return -1; // Error handle
+    }
+    fgets(buffer, fileSize, requestFile);
+    fclose(requestFile);
+    return 0;
 }
 
 /**********************************************************************************/
@@ -155,7 +161,7 @@ void * dispatch(void *arg) {
 // Function to retrieve the request from the queue, process it and then return a result to the client
 void * worker(void *arg) {
   //TODO: Fix this (gcc does not like this case as void* and int are different sizes)
-  int id = (int) arg;
+  int id = *(int*) arg;
   long numbytes;
   unsigned long long numReqs = 0;
   while (1) {
@@ -174,21 +180,24 @@ void * worker(void *arg) {
      // Log the request into the file and terminal
     pthread_mutex_lock(&logLock);
     FILE* log = fopen("../webserver_log", "a");
-    fprintf(log, "[%lld][%ld][%d][%s][%s][%s]", ++numReqs, pthread_self(), request->fd, "Request String", "Bytes/Error", "CACHE");
-    fprintf(stdout, "[%lld][%ld][%d][%s][%s][%s]", ++numReqs, pthread_self(), request->fd, "Request String", "Bytes/Error", "CACHE");
+    fprintf(log, "[%d][%lld][%d][%s][%s][%s]", id, ++numReqs, request->fd, "Request String", "Bytes/Error", "CACHE");
+    fprintf(stdout, "[%d][%lld][%d][%s][%s][%s]", id, ++numReqs, request->fd, "Request String", "Bytes/Error", "CACHE");
     fclose(log);
     pthread_mutex_unlock(&logLock);
      // return the result
-    char *workerBuf = (char *)calloc(BUFF_SIZE, sizeof(char));
     //TODO! Fix this holy shit 
-    // call readFromDisk and read that shit into workerBuf and then call 
-    readFromDisk(... workerBuf); 
-    //use fstat's stat_st to get numbytes?
-    struct stat* oob;
+    // call readFromDisk and read that shit into workerBuf and then call
+    struct stat *oob = NULL;
     fstat(request->fd, oob);
     numbytes = oob->st_size;
-    //TODO: add return_error as well for when trying to find something that does not exist.
-    return_result(request->fd, getContentType(request->request), workerBuf, numbytes);
+    char *workerBuf = (char *) calloc(numbytes, sizeof(char));
+    if (readFromDisk(request->request, workerBuf, numbytes) != 0) {
+      return_error(request->fd, request->request);
+    } else {
+      //use fstat's stat_st to get numbytes?
+      
+      return_result(request->fd, getContentType(request->request), workerBuf, numbytes);
+    }
   }
   return NULL;
 }
@@ -203,37 +212,35 @@ static void eggs(int signo) {
   exitFlag |= 1;
 }
 int main(int argc, char **argv) {
-
+printf("I AM THE FIRST DEBUGGING STATEMENT\n");
   // Error check on number of arguments
   if(argc != 8){
     printf("usage: %s port path num_dispatcher num_workers dynamic_flag queue_length cache_size\n", argv[0]);
     return -1;
   }
-
   // Get the input args
-
   //Port
-  port = atoi(argv[3]);
-
+  port = atoi(argv[1]);
+  printf("I AM A DEBUGGING STATEMENT 1\n");
   //Webroot path
-  path = argv[4];
-
+  path = argv[2];
+  printf("I AM A DEBUGGING STATEMENT 2\n");
   //(static) number of dispatchers
-  dispatchers = atoi(argv[5]);
-
+  dispatchers = atoi(argv[3]);
+  printf("I AM A DEBUGGING STATEMENT 3\n");
   //(static) number of workers
-  workers = atoi(argv[6]);
-
+  workers = atoi(argv[4]);
+  printf("I AM A DEBUGGING STATEMENT 4\n");
   //Dynamic worker flag
-  dynFlag = atoi(argv[7]);
-
+  dynFlag = atoi(argv[5]);
+  printf("I AM A DEBUGGING STATEMENT 5\n");
   //Queue Length
-  qLen = atoi(argv[8]);
-
+  qLen = atoi(argv[6]);
+  printf("I AM A DEBUGGING STATEMENT 6\n");
   //Max cache size
-  cSiz = atoi(argv[9]);
-
- /* -- ERROR CHECKING -- */
+  cSiz = atoi(argv[7]);
+  printf("I AM A DEBUGGING STATEMENT 7\n");
+  /* -- ERROR CHECKING -- */
   if (port < 1025 || port > 65535) {
     printf("Invalid port. Port must be greater than 1024 or less than 65536.\n");
     return -1;
@@ -253,6 +260,7 @@ int main(int argc, char **argv) {
   /* -- END ERROR CHECKING -- */
 
   // Change SIGINT action for graceful termination
+  printf("I AM A DEBUGGING STATEMENT\n");
   struct sigaction act;
   act.sa_handler = eggs;
   act.sa_flags = 0;
@@ -280,7 +288,7 @@ int main(int argc, char **argv) {
       exit(-2);
     }
   } 
-
+  printf("I AM A DEBUGGING STATEMENT\n");
   //Make sure threads are all detached
   pthread_attr_t attr;
   pthread_attr_init(&attr);
@@ -310,10 +318,10 @@ int main(int argc, char **argv) {
   //Server loop (RUNS FOREVER)
   while (1) {
     //TODO: Add something else?
-
+    int i = 0;
     // Terminate server gracefully
     if (exitFlag){
-      printf("SIGINT caught, exiting now.\n");
+      printf("\nSIGINT caught, exiting now.\n");
       // Print the number of pending requests in the request queue
       /*TODO*/
       // close log file
@@ -322,8 +330,8 @@ int main(int argc, char **argv) {
         return -6;
       }
       // Remove cache (extra credit B)
-      if (cSiz != 0)
-        free(dynQ);
+      //if (cSiz != 0)
+        //free(dynQ);
       printf("Cache has successfully been cleared.\nExiting now.\n");
       return 0;
     }
diff --git a/P3/web_server_sol b/P3/web_server_sol
old mode 100644
new mode 100755
-- 
cgit v1.2.3