first commit
This commit is contained in:
126
CS4210/Project 2/client/src/threads.c
Normal file
126
CS4210/Project 2/client/src/threads.c
Normal file
@@ -0,0 +1,126 @@
|
||||
#include "include/threads.h"
|
||||
#include "include/defs.h"
|
||||
#include "include/networking.h"
|
||||
#include "include/test.h"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
int threads = 0;
|
||||
char ready;
|
||||
|
||||
void *makeRequest(void *in) {
|
||||
int i;
|
||||
int sock_fail=0;
|
||||
int total_bytes_received;
|
||||
int sendlen;
|
||||
char http_request[HTTP_REQUEST_SIZE];
|
||||
Request *r = (Request*)in;
|
||||
long diff_secs, diff_usecs;
|
||||
struct timeval tv;
|
||||
struct timezone tz;
|
||||
|
||||
//Detach thread
|
||||
pthread_detach(pthread_self());
|
||||
|
||||
while(ready != 1);
|
||||
|
||||
//printf("Thread %d starting\n", pthread_self());
|
||||
|
||||
//Timing stuff
|
||||
gettimeofday(&tv, &tz);
|
||||
diff_secs = tv.tv_sec;
|
||||
diff_usecs = tv.tv_usec;
|
||||
|
||||
|
||||
for (i=0, sock_fail=0; i < r->iterations; i++) {
|
||||
int sock;
|
||||
|
||||
for(;;){
|
||||
sock = createTCPSocket(r->hostName, r->port_number);
|
||||
if(sock<0) {
|
||||
sock_fail++;
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
memset(http_request, 0, sizeof(char)*HTTP_REQUEST_SIZE);
|
||||
|
||||
sprintf(http_request, "GET %s HTTP/1.1\r\n\r\n\0" , r->fileName);
|
||||
sendlen=strlen(http_request) + 1;
|
||||
|
||||
// printf("Requesting: %s\n", http_request);
|
||||
|
||||
if(send(sock, http_request, sendlen, 0) != sendlen)
|
||||
{
|
||||
DieWithError("send() failed");
|
||||
}
|
||||
|
||||
{
|
||||
int bytes_received = 0;
|
||||
char get_buffer[RECV_SIZE];
|
||||
|
||||
total_bytes_received=0;
|
||||
|
||||
for(;;) {
|
||||
memset(get_buffer, 0, sizeof(char)*RECV_SIZE);
|
||||
bytes_received = recv(sock, get_buffer, RECV_SIZE, 0);
|
||||
|
||||
/* printf("Bytes gotten: %d\n", bytes_received); */
|
||||
|
||||
if(bytes_received < 0) {
|
||||
continue;
|
||||
} else if (bytes_received == 0) {
|
||||
break;
|
||||
} else {
|
||||
total_bytes_received += bytes_received;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
closesocket(sock);
|
||||
#else
|
||||
close(sock);
|
||||
#endif
|
||||
|
||||
// printf("Finished receiving File.\n");
|
||||
}
|
||||
|
||||
|
||||
gettimeofday(&tv, &tz);
|
||||
diff_secs = tv.tv_sec - diff_secs;
|
||||
diff_usecs = tv.tv_usec - diff_usecs;
|
||||
timenormal(diff_secs,diff_usecs)
|
||||
|
||||
printf("%s, %d, %ld, %ld, %d, %d\n",r->fileName, r->iterations,
|
||||
diff_secs, diff_usecs, sock_fail,
|
||||
total_bytes_received);
|
||||
|
||||
fflush(stdout);
|
||||
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void init_threads(int numThreads, Request** rList, int numFiles) {
|
||||
int i, j;
|
||||
|
||||
ready = 0;
|
||||
|
||||
for (i=0, j=0; i < numThreads; i++, j++) {
|
||||
if (j >= numFiles) {
|
||||
j = 0;
|
||||
}
|
||||
pthread_create(malloc(sizeof(pthread_t)),NULL,makeRequest,rList[j]);
|
||||
}
|
||||
|
||||
ready = 1;
|
||||
}
|
||||
Reference in New Issue
Block a user