Guest User

Untitled

a guest
Feb 20th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.14 KB | None | 0 0
  1. /* A simple server in the internet domain using TCP
  2.    The port number is passed as an argument */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <time.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <errno.h>
  14. #include <pthread.h>
  15.  
  16. #define BACKLOG 5
  17. /*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  18. typedef struct
  19. {
  20.     char* filename;
  21.     int sock_fd;
  22.     size_t file_size;
  23. }ready;
  24.  
  25. struct linked_li
  26. {
  27.     ready block;
  28.     struct linked_li *next;
  29. };
  30.  
  31. struct linked_li *head = NULL;
  32. ready queue[20];
  33. //ready sched;
  34.  
  35. int newsockfd,sockfd;
  36. char buffer[256];
  37. char* file_path;
  38. static int count = 0;
  39. pthread_t th[10];
  40.  
  41. pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
  42. pthread_mutex_t mutex_queue = PTHREAD_MUTEX_INITIALIZER;
  43.  
  44. /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
  45.  
  46. void error(const char *msg)
  47. {
  48.     perror(msg);
  49.     exit(1);
  50. }
  51.  
  52. void wait_time(int seconds)
  53. {
  54.     clock_t endwait;
  55.     endwait = clock () + seconds * CLOCKS_PER_SEC ;
  56.     while (clock() < endwait) {}
  57. }
  58.  
  59. void *proc_req();
  60. void* process(void *data);
  61. void* fun_sched();
  62.  
  63.  
  64. //listl linked_li;
  65.  
  66. int main(int argc, char *argv[])
  67. {
  68.      int portno;
  69.      int k;
  70.      socklen_t clilen;
  71.      int check_delay = 1;
  72.  
  73.      struct sockaddr_in serv_addr, cli_addr;
  74.  
  75.      if (argc < 2) {
  76.          fprintf(stderr,"ERROR, no port provided\n");
  77.          exit(EXIT_FAILURE);
  78.      }
  79.      sockfd = socket(AF_INET, SOCK_STREAM, 0);
  80.      if (sockfd < 0)
  81.      {
  82.         error("ERROR opening socket");
  83.      }
  84.  
  85.      bzero((char *) &serv_addr, sizeof(serv_addr));
  86.      portno = atoi(argv[1]);
  87.      serv_addr.sin_family = AF_INET;
  88.      serv_addr.sin_addr.s_addr = INADDR_ANY;
  89.      serv_addr.sin_port = htons(portno);
  90.      if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
  91.      {
  92.          error("ERROR on binding");
  93.      }
  94.      listen(sockfd,BACKLOG);
  95.      clilen = sizeof(cli_addr);
  96.  
  97.      pthread_t th_Q,th_Sched;
  98.      int ret_th;
  99.      ret_th = pthread_create(&th_Q,NULL,proc_req,NULL);
  100.      if(ret_th)
  101.      error("Thread not created");
  102.      //introducing delay
  103.      if(check_delay)
  104.      {
  105.          wait_time(30);
  106.          check_delay = 0;
  107.      }
  108.  
  109. // TODO (hkoduri#1#): change arg\--done
  110.     // printf("After delay\n");
  111.      ret_th = pthread_create(&th_Sched,NULL,fun_sched,NULL);
  112.      if(ret_th)
  113.      error("Sched Thread not created");
  114.     // printf("after scheduled thread in main\n ");
  115.      pthread_join(th_Q,NULL);
  116.      pthread_join(th_Sched,NULL);
  117.      for(k=0;k<10;k++)
  118.      {
  119.         pthread_join(th[k],NULL);
  120.      }
  121.      close(sockfd);
  122.      return 0;
  123. }
  124.  
  125. void *proc_req()
  126. {
  127.  
  128.     int n;
  129.     int i=0;
  130.     char* str;
  131.     char* str_last;
  132.     char* file_path1;
  133.     struct linked_li *curr;
  134.     struct linked_li *pointer;
  135.  
  136.     while(1)
  137.     {
  138.       newsockfd = accept(sockfd,NULL,NULL);
  139.       if (newsockfd < 0)
  140.       {
  141.          error("ERROR on accept");
  142.       }
  143.  
  144.      bzero(buffer,256);
  145.      n = read(newsockfd,buffer,255);
  146.      if (n < 0)
  147.      {
  148.          error("ERROR reading from socket");
  149.      }
  150.  
  151.      else
  152.      {
  153.          printf("The request from the browser is: %s\n",buffer);
  154.          str = strtok(buffer," ");
  155.          printf("The request type is %s \n",str);
  156.          //discard = strtok(NULL,"/");
  157.          file_path1 = strtok(NULL," ");
  158.          file_path = (char*) malloc(strlen(file_path1)-1);
  159.          strncpy(file_path,file_path1+1,strlen(file_path1)-1);
  160.          printf("The filepath to retrieve is %s \n",file_path);
  161.          str_last = strtok(NULL,"\r");
  162.          printf("The http version is %s \n",str_last);
  163.  
  164.          //getting information about the file
  165.  
  166.          FILE *file;
  167.          //DIR *dp;
  168.         struct dirent *dirp;
  169.         struct stat statbuf,dirstat;
  170.         char buf[1024],*data,timebuf[128];
  171.         char *dirpath;
  172.         //int i=0;
  173.         time_t t;
  174.         long len;
  175.         if(stat(file_path,&statbuf)<0)
  176.         {
  177.             printf("File not found\n");
  178.             exit(EXIT_FAILURE);
  179.         }
  180. /*
  181.         t = time(NULL);
  182.         strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&t));
  183.         printf("Date: %s\r\n", timebuf);//Printing timestamp
  184.         printf("Server: %s\n",SERVERNAME);//printing servername
  185.         //read(hSocket,buf,sizeof(buf));
  186.         //method = strtok(buf, " ");
  187.         //path = strtok(NULL, " ");
  188.         //path1=(char *)malloc(strlen(path));
  189.         //strncpy(path1,path+1,strlen(path)-1);
  190.         //path1[strlen(path)-1]='\0';
  191.         //protocol = strtok(NULL, "\r");
  192.         //printf("%s\n%s\n%s\n%s\n",path,method,protocol,path1);
  193.         if(stat(file_path,&statbuf)<0)
  194.         {
  195.             printf("File not found\n");
  196.             return;
  197.         }
  198.         if(S_ISREG(statbuf.st_mode))
  199.         {
  200.             strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&statbuf.st_mtime));
  201.             printf("Last-Modified: %s\n",timebuf);
  202.             //printf("Content-Type: %s\n",get_content_type(path1));
  203.             printf("Content-Length: %d\n",statbuf.st_size);
  204.             file = fopen(file_path, "r");
  205.             fseek(file,0,SEEK_END);
  206.             len=ftell(file);
  207.             rewind(file);
  208.             data=(char *)malloc(sizeof(char)*len);
  209.             printf("%d\n",strlen(data));
  210.             write(hSocket,"<html><body>",13);
  211.             if((n = fread(data, 1, len, file)) > 0)
  212.             {
  213.                 write(hSocket,data,len);
  214.             }
  215.             write(hSocket,"</html></body>",15);
  216.             fclose(file);
  217.             free(data);
  218.             free(path1);
  219.         }
  220.         if(S_ISDIR(statbuf.st_mode))
  221.         {
  222.             dirpath=malloc(strlen(path1)+10);
  223.             strcpy(dirpath,path1);
  224.             strcat(dirpath,"index.html");
  225.             printf("%s\n",dirpath);
  226.             if(stat(dirpath,&dirstat)<0)
  227.             {
  228.                 if((dp=opendir(path1))==NULL)
  229.                 {
  230.                     printf("Couldn't open directory:%s",path1);
  231.                     return;
  232.                 }
  233.                 write(hSocket,"<html><body><h1>The Directory contains the following files<br></h1>",68);
  234.                 while ((dirp = readdir(dp)) != NULL)
  235.                 {
  236.                     write(hSocket,dirp->d_name,strlen(dirp->d_name));
  237.                     write(hSocket,"<br>",4);
  238.                 }
  239.                 write(hSocket,"</html></body>",15);
  240.                 closedir(dp);
  241.             }
  242.             else
  243.             {
  244.                 strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&statbuf.st_mtime));
  245.                 printf("Last-Modified: %s\n",timebuf);
  246.                 printf("Content-Type: %s\n",get_content_type(path1));
  247.                 printf("Content-Length: %d\n",statbuf.st_size);
  248.                 file = fopen(dirpath, "r");
  249.                 fseek(file,0,SEEK_END);
  250.                 len=ftell(file);
  251.                 rewind(file);
  252.                 data=(char *)malloc(sizeof(char)*len);
  253.                 write(hSocket,"<html><body>",13);
  254.                 if ((n = fread(data, 1, len, file)) > 0)
  255.                 {
  256.                     write(hSocket,data,len);
  257.                 }
  258.                 write(hSocket,"</html></body>",15);
  259.                 fclose(file);
  260.                 free(data);
  261.                 free(path1);
  262.                 free(dirpath);
  263.             }
  264.         }*/
  265.  
  266.          //fill up the ready queue
  267.         // int j=0;
  268.          pthread_mutex_lock(&mutex1);
  269.          printf("\nBefore head null\n");
  270.          if(head == NULL)
  271.          {
  272.             printf(" The first request\n");
  273.             head = (struct linked_li*) malloc(sizeof(struct linked_li));
  274.             head->block.file_size = statbuf.st_size;
  275.             printf("File size of head: %zd \n",head->block.file_size);
  276.             head->block.filename = (char*) malloc(strlen(file_path));
  277.             strcpy(head->block.filename, file_path);
  278.             printf("File name of head:%s\n",head->block.filename);
  279.             head->block.sock_fd = newsockfd;
  280.             printf("Socket ID of head: %d\n",head->block.sock_fd);
  281.             head->next = NULL;
  282.             pointer = head;
  283.  
  284.          }
  285.          else
  286.          {
  287.             printf("\n in else in procque");
  288.             curr = (struct linked_li*) malloc(sizeof(struct linked_li));
  289.             pointer->next = curr;
  290.             curr->block.file_size = statbuf.st_size;
  291.             printf("File size :%zd\n",curr->block.file_size);
  292.             curr->block.filename = (char*) malloc(strlen(file_path));
  293.             strcpy(curr->block.filename, file_path);
  294.             printf("File name : %s\n",curr->block.filename);
  295.             curr->block.sock_fd = newsockfd;
  296.             printf("Socket ID : %d\n",curr->block.sock_fd);
  297.             curr->next = NULL;
  298.             pointer = curr;
  299.          }
  300.  
  301.          //queue[i%20].file_size = statbuf.st_size;
  302.          //printf("The file size is %zd",queue[i%20].file_size);
  303.          //queue[i%20].filename = (char*) malloc(strlen(file_path));
  304.          //strcpy(queue[i%20].filename, file_path);
  305.          //queue[i%20].sock_fd = newsockfd;
  306.          printf("The newsockfd is %d",newsockfd);
  307.  
  308.          pthread_mutex_unlock(&mutex1);
  309.  
  310.         }//my else
  311.  
  312.      } //while
  313.  
  314. }
  315.  
  316. void *fun_sched()
  317. {
  318.  
  319.     int i = 0;
  320.     int rx;
  321.     struct linked_li *curr;
  322.     struct linked_li *pointer;
  323.     curr = head;
  324.     //FCFS
  325.     while(1)
  326.     {
  327.       if(head->block.filename!= NULL)
  328.       {
  329.         pthread_mutex_lock(&mutex1);
  330.         ready *sched = (ready*) malloc(sizeof(ready *));
  331.         sched->filename = (char*)malloc(strlen(curr->block.filename));
  332.         strcpy(sched->filename,curr->block.filename);
  333.         //printf("The filename in sched thread %s\n",sched.filename);
  334.         sched->file_size = curr->block.file_size;
  335.         //printf("The filesize in sched thread %zd\n",sched.file_size);
  336.         sched->sock_fd = curr->block.sock_fd;
  337.         //printf("The socket ID in sched thread %d\n",sched.sock_fd);
  338.         head = curr->next;
  339.         pointer = curr;
  340.         curr = curr->next;
  341.         free(pointer);
  342.         pthread_mutex_unlock(&mutex1);
  343.         printf(" adsdddddfdddf \n");
  344.         rx = pthread_create(&th[i],NULL,process,sched);
  345.         if(rx)
  346.         error("Individual thread not created");
  347.         free(sched);
  348.         printf("vareva\n");
  349.         fflush(stdout);
  350.         i++;
  351.       }
  352.  
  353.        /*if(queue[i%20].filename!="")
  354.        {
  355.          //printf("The filename is of queue %s",queue[i%20].filename);
  356.          pthread_mutex_lock( &mutex1 );
  357.            // printf("In scheduled thread\fgdkggkffgkn ");
  358.          sched.filename = (char*)malloc(strlen(queue[i%20].filename));
  359.          strcpy(sched.filename,queue[i%20].filename);
  360.          printf("In scheduled thread scehdfilename %s\n ",sched.filename);
  361.          sched.file_size = queue[i%20].file_size;
  362.          //printf("In scheduled thread %s\n ",sched.filename);
  363.          sched.sock_fd = queue[i%20].sock_fd;
  364.          printf("In scheduled thread sockfd %d\n ",sched.sock_fd);
  365.          free(&queue[i%20]);
  366.          pthread_mutex_unlock(&mutex1);
  367.          printf("Out of mutex \n");
  368.          pthread_create(&th[i%20],NULL,process,(void*)&sched);
  369.        }*/
  370.  
  371.     }
  372.     printf("No More request to schedule \n");
  373. }
  374.  
  375. void *process(void *data)
  376. {
  377.     int n;
  378.     ready *temp;
  379.     printf("In worker thread \n ");
  380.     temp = (ready*) data;
  381.     if(!data)
  382.     printf("data null\n");
  383.     printf("The temp sock_fd is %d\n",temp->sock_fd);
  384.     printf("The temp filename is %s\n",temp->filename);
  385.     printf("The temp filesize is %zd\n",temp->file_size);
  386.     //temp.filename = (char*) data.filename;
  387.     //temp.sock_fd = (int*) data.sock_fd;
  388.     //temp.file_size = (size_t*) data.file_size;
  389.     printf("In worker thread \n ");
  390.     n = write(temp->sock_fd,"getting your message from thread",32);
  391.     if (n < 0)
  392.     error("ERROR writing to socket");
  393.     close(temp->sock_fd);
  394.     pthread_exit(0);
  395. }
Add Comment
Please, Sign In to add comment