Commit 092592f7 authored by Samarth Joshi's avatar Samarth Joshi

Added read from config file

parent 5c33d4c0
...@@ -13,6 +13,13 @@ ...@@ -13,6 +13,13 @@
#define MAX_EVENTS 10 #define MAX_EVENTS 10
#define DEBUG (0) #define DEBUG (0)
struct config {
int listening_port;
int cache_size;
int thread_pool_size;
};
void gen_random(char *s, const int len) { void gen_random(char *s, const int len) {
static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
...@@ -144,21 +151,66 @@ void *worker(void *args) { ...@@ -144,21 +151,66 @@ void *worker(void *args) {
} }
int read_config(struct config *settings) {
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
char *param;
char *value;
int i;
fp = fopen("settings.conf", "r");
if (fp == NULL)
return -1;
while ((read = getline(&line, &len, fp)) != -1) {
i = 0;
if(line[0]=='#') {
break;
} else {
param=strtok(line,"=");
value=strtok(NULL,"=");
if(strcmp(param,"listening_port")==0) {
settings->listening_port = atoi(value);
} else if (strcmp(param,"cache_size")==0) {
settings->cache_size = atoi(value);
} else if (strcmp(param,"thread_pool_size")==0) {
settings->thread_pool_size = atoi(value);
} else {
}
}
}
fclose(fp);
}
int main (int argc, int argv) { int main (int argc, int argv) {
int i; int i;
int next_thread_to_assign = 0; int next_thread_to_assign = 0;
pthread_t *worker_threads; pthread_t *worker_threads;
int pool_thread_size = 50; // TODO: get pool thread size from config file
int sockfd, newsockfd, portno, clilen, n; int sockfd, newsockfd, portno, clilen, n;
struct sockaddr_in serv_addr, cli_addr; struct sockaddr_in serv_addr, cli_addr;
struct config *settings;
settings = (struct config *) malloc(sizeof(struct config));
if(read_config(settings)<0) {
printf("Invalid config file!\n");
return -1;
}
printf("%d\n", settings->listening_port);
printf("%d\n", settings->cache_size);
printf("%d\n", settings->thread_pool_size);
return 0;
//int *pipes = (int*) malloc(pool_thread_size * 2 * sizeof(pipes)); //int *pipes = (int*) malloc(pool_thread_size * 2 * sizeof(pipes));
int pipes[50][2]; // TODO: initialize pipes dynamically int pipes[50][2]; // TODO: initialize pipes dynamically
worker_threads = malloc(pool_thread_size * sizeof(pthread_t)); worker_threads = malloc(settings->thread_pool_size * sizeof(pthread_t));
for( i=0; i < pool_thread_size; i++ ) { for( i=0; i < settings->thread_pool_size; i++ ) {
pipe(pipes[i]); pipe(pipes[i]);
pthread_create( &worker_threads[i], NULL, worker, &pipes[i]); pthread_create( &worker_threads[i], NULL, worker, &pipes[i]);
} }
...@@ -169,7 +221,7 @@ int main (int argc, int argv) { ...@@ -169,7 +221,7 @@ int main (int argc, int argv) {
exit(1); exit(1);
} }
memset(&serv_addr, 0, sizeof(serv_addr)); memset(&serv_addr, 0, sizeof(serv_addr));
portno = 8000; portno = settings->listening_port;
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno); serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_addr.s_addr = INADDR_ANY;
...@@ -183,7 +235,7 @@ int main (int argc, int argv) { ...@@ -183,7 +235,7 @@ int main (int argc, int argv) {
listen(sockfd, 5); listen(sockfd, 5);
clilen = sizeof(cli_addr); clilen = sizeof(cli_addr);
init_cache(); init_cache(settings->cache_size);
storage_init(); storage_init();
while(1) { while(1) {
...@@ -193,7 +245,7 @@ int main (int argc, int argv) { ...@@ -193,7 +245,7 @@ int main (int argc, int argv) {
perror("ERROR on accept"); perror("ERROR on accept");
write(pipes[next_thread_to_assign][1], &newsockfd, sizeof(newsockfd)); write(pipes[next_thread_to_assign][1], &newsockfd, sizeof(newsockfd));
next_thread_to_assign = (next_thread_to_assign+1) % pool_thread_size; next_thread_to_assign = (next_thread_to_assign+1) % settings->thread_pool_size;
} }
return 0; return 0;
......
...@@ -10,13 +10,13 @@ ...@@ -10,13 +10,13 @@
#include "LRU.h" #include "LRU.h"
#define ATOMIC_TEST_AND_SET __atomic_test_and_set #define ATOMIC_TEST_AND_SET __atomic_test_and_set
#define CLEAR __atomic_clear #define CLEAR __atomic_clear
#define MAX_SIZE 10
struct KV *array[MAX_SIZE]; struct KV *array[];
struct queue *qu=NULL; struct queue *qu=NULL;
struct queue *last; struct queue *last;
unsigned _Atomic lock=0; unsigned _Atomic lock=0;
int maxsize;
void remove_element_from_deque(char *key) void remove_element_from_deque(char *key)
...@@ -81,7 +81,7 @@ int remove_front_element() ...@@ -81,7 +81,7 @@ int remove_front_element()
qu = qu->next; qu = qu->next;
assert(temp != NULL); assert(temp != NULL);
assert(qu != NULL); assert(qu != NULL);
for(int i=0;i<MAX_SIZE;i++) for(int i=0;i<maxsize;i++)
{ {
if(strcmp(array[i]->key , temp->key) == 0) if(strcmp(array[i]->key , temp->key) == 0)
{ {
...@@ -96,7 +96,7 @@ int remove_front_element() ...@@ -96,7 +96,7 @@ int remove_front_element()
int find_empty_location() int find_empty_location()
{ {
for(int i=0;i<MAX_SIZE;i++) for(int i=0;i<maxsize;i++)
{ {
if(array[i]->valid == FALSE) if(array[i]->valid == FALSE)
{ {
...@@ -110,7 +110,7 @@ int find_empty_location() ...@@ -110,7 +110,7 @@ int find_empty_location()
int cache_del(char *key) int cache_del(char *key)
{ {
for(int i=0;i<MAX_SIZE;i++) for(int i=0;i<maxsize;i++)
{ {
if(strcmp(array[i]->key , key) == 0) if(strcmp(array[i]->key , key) == 0)
{ {
...@@ -130,7 +130,7 @@ int cache_del(char *key) ...@@ -130,7 +130,7 @@ int cache_del(char *key)
void cache_put(char *key, char *value) void cache_put(char *key, char *value)
{ {
int indx=-1; int indx=-1;
for(int i=0;i<MAX_SIZE;i++) for(int i=0;i<maxsize;i++)
{ {
if(array[i]->key!=NULL) { if(array[i]->key!=NULL) {
if(strcmp(array[i]->key , key) == 0) if(strcmp(array[i]->key , key) == 0)
...@@ -163,7 +163,7 @@ void cache_put(char *key, char *value) ...@@ -163,7 +163,7 @@ void cache_put(char *key, char *value)
int cache_get(char *key, char *value) int cache_get(char *key, char *value)
{ {
for(int i=0;i<MAX_SIZE;i++) for(int i=0;i<maxsize;i++)
{ {
if(strcmp(array[i]->key , key) == 0 && array[i]->valid) if(strcmp(array[i]->key , key) == 0 && array[i]->valid)
{ {
...@@ -178,8 +178,10 @@ int cache_get(char *key, char *value) ...@@ -178,8 +178,10 @@ int cache_get(char *key, char *value)
return 0; return 0;
} }
void init_cache() { void init_cache(int size) {
for(int j=0;j<MAX_SIZE;j++) maxsize = size;
array = (struct KV *) malloc(maxsize * sizeof(struct KV));
for(int j=0;j<maxsize;j++)
{ {
array[j] = (KV *)malloc(sizeof(KV)); array[j] = (KV *)malloc(sizeof(KV));
array[j]->valid = FALSE; array[j]->valid = FALSE;
...@@ -188,7 +190,7 @@ void init_cache() { ...@@ -188,7 +190,7 @@ void init_cache() {
} }
void print_cache() { void print_cache() {
for(int j=0;j<MAX_SIZE;j++) for(int j=0;j<maxsize;j++)
{ {
printf("[%d] (%s) : (%s)\n", array[j]->valid, array[j]->key, array[j]->value); printf("[%d] (%s) : (%s)\n", array[j]->valid, array[j]->key, array[j]->value);
} }
......
...@@ -26,7 +26,7 @@ struct queue{ ...@@ -26,7 +26,7 @@ struct queue{
struct queue *next; struct queue *next;
}; };
extern struct KV *array[MAX_SIZE]; extern struct KV *array[];
extern struct queue *qu; extern struct queue *qu;
extern struct queue *last; extern struct queue *last;
......
No preview for this file type
aasasdads=23 max_threads=5
asdsadasdasdasd=29 no_of_dsads=10
asdsadasdasdasd=29 adsasaddsa=22223
asdsadasdasdasd=29
asdsadasdasdasdasdasd=231
\ No newline at end of file
listening_port=8000
cache_size=10
thread_pool_size=10
\ No newline at end of file
No preview for this file type
...@@ -204,15 +204,15 @@ int main() ...@@ -204,15 +204,15 @@ int main()
char *line; char *line;
off_t len=0; off_t len=0;
char* arrParam[20]; char* arrParam[20];
char* arrValue[20]; int arrValue[20];
int k=0; int k=0;
while(getline(&line,&len,fp)!=-1) while(getline(&line,&len,fp)!=-1)
{ {
char *param=strtok(line,"="); char *param=strtok(line,"=");
char *value =strtok(NULL,"="); char *value =strtok(NULL,"=");
arrParam[k]=param; arrParam[k]=param;
arrValue[k++]=value; arrValue[k++]=atoi(value);
printf("%s:%s",arrParam[k-1],arrValue[k-1]); printf("%s:%d\n",arrParam[k-1],arrValue[k-1]);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment