Main Page   Data Structures   File List   Data Fields   Globals   Related Pages  

client.c

00001 /***************************************************************************
00002                                  client.c
00003                              -------------------
00004     begin                : Thu Jan 17 13:48:35 CET 2002
00005     copyright            : (C) 2002 by Christian Hoenig & Gunter Ohrner
00006     email                : pdepp@customcdrom.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 
00019 #include <errno.h>
00020 #include <pthread.h>
00021 
00022 #include <sys/time.h>
00023 #include <unistd.h>
00024 #include <netdb.h>
00025 #include <sys/socket.h> 
00026 #include <netinet/in.h> 
00027 
00028 
00029 #include "client.h"
00030 
00031 #include "pdeppd/xmalloc.h"
00032 #include "pdeppd/bufplist.h"
00033 
00034 #include "deppmark.h"
00035 
00045 int cConnect(char* buf, struct Buffer* file_to_catch)
00046 {
00047   int sock = socket(PF_INET, SOCK_STREAM,0);
00048   struct sockaddr_in serv_addr;
00049 /*  int bytes_read; */
00050 
00051   serv_addr.sin_family = AF_INET;
00052   serv_addr.sin_port   = port;
00053   serv_addr.sin_addr   = host_addr;
00054 
00055   if (connect(sock,(struct sockaddr*) &serv_addr, sizeof(struct sockaddr_in)) == -1)
00056   {
00057     fprintf(stderr, "Error: connect: failed %s\n", strerror(errno));
00058     return -1;
00059   }
00060 
00061   strcpy(buf, "GET ");
00062   strcpy(buf +4, file_to_catch->data);
00063   strcpy(buf +4 +file_to_catch->len, " HTTP/1.0\r\n\r\n");
00064   write(sock, buf, 4 + file_to_catch->len + 9 + 4);
00065 
00066   while((/* bytes_read = */ read(sock, buf, buf_len-1)) > 0)
00067   {
00068 /*    buf[bytes_read] = '\0'; */
00069 /*    printf("%s", buf); */
00070   };
00071     
00072   shutdown(sock, 2);
00073   close(sock);
00074 
00075   return 0;  
00076 }
00077 
00082 void cPause(long int msec)
00083 {
00084   pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
00085   pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00086   struct timespec tv;
00087   struct timeval now;
00088   long int timetowait_sec = msec / 1000;
00089   long int timetowait_nsec = (msec % 1000) * 1000000;
00090 
00091   pthread_mutex_lock(&mut);
00092   gettimeofday(&now, NULL);
00093 
00094   tv.tv_sec  = now.tv_sec         + timetowait_sec;
00095   tv.tv_nsec = now.tv_usec * 1000 + timetowait_nsec;
00096 
00097   pthread_cond_timedwait(&cond, &mut, &tv);
00098 
00099   pthread_mutex_unlock(&mut);
00100 }
00101 
00110 int virtualClient(void* num) 
00111 {
00112   unsigned thrd_num = (unsigned) num;
00113   unsigned cnt = 0;
00114   char* method_buf;
00115   BufpList file_list_clone;
00116 
00117 #ifndef NDEBUG
00118   printf("Thread number %u is going to bed...\n", thrd_num);
00119 #endif
00120 
00121   // wait (num/10) seconds before executing...
00122   cPause(thrd_num*100);
00123 
00124 #ifndef NDEBUG
00125   printf("Thread number %u woke up - GO!!!\n", thrd_num);
00126 #endif
00127 
00128   // malloc is not thread-safe
00129   pthread_mutex_lock(&malloc_mut);
00130   method_buf = pd_malloc(buf_len);
00131   pthread_mutex_unlock(&malloc_mut);
00132 
00133   // clone filelist
00134   file_list_clone = bufpListClone(file_list);
00135 
00136   bufpListFirst(file_list_clone);
00137   
00138   // seek to a "random" position within the list... <g>
00139   for(cnt = 0; cnt < thrd_num; ++cnt)
00140   {
00141     if (bufpListIsTail(file_list_clone))
00142       bufpListFirst(file_list_clone);
00143     else
00144       bufpListNext(file_list_clone);
00145   }
00146 
00147   // Ok, let's go!
00148   for(cnt = 0; cnt < repeat; ++cnt)
00149   {
00150 //    bufpListFirst(file_list_clone);
00151     while (!bufpListIsTail(file_list_clone))
00152     {
00153       if (cConnect(method_buf, bufpListGetPayload(file_list_clone)) == -1)
00154         break;
00155 
00156       /* Wait some time before issuing the next request... */
00157       cPause(delay * 100);
00158 
00159       bufpListNext(file_list_clone);
00160     }
00161   }
00162 
00163   bufpListTerminate(file_list_clone);
00164 
00165   // free is not thread-safe
00166   pthread_mutex_lock(&malloc_mut);
00167   pd_free(method_buf);
00168   pthread_mutex_unlock(&malloc_mut);
00169 
00170   return 0; 
00171 }
00172 
00173 

Generated on Fri Jan 25 22:40:30 2002 for PDepp Webserver by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001