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

Added read from config file

parent 5c33d4c0
......@@ -13,6 +13,13 @@
#define MAX_EVENTS 10
#define DEBUG (0)
struct config {
int listening_port;
int cache_size;
int thread_pool_size;
};
void gen_random(char *s, const int len) {
static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
......@@ -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 i;
int next_thread_to_assign = 0;
pthread_t *worker_threads;
int pool_thread_size = 50; // TODO: get pool thread size from config file
int sockfd, newsockfd, portno, clilen, n;
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[50][2]; // TODO: initialize pipes dynamically
worker_threads = malloc(pool_thread_size * sizeof(pthread_t));
for( i=0; i < pool_thread_size; i++ ) {
worker_threads = malloc(settings->thread_pool_size * sizeof(pthread_t));
for( i=0; i < settings->thread_pool_size; i++ ) {
pipe(pipes[i]);
pthread_create( &worker_threads[i], NULL, worker, &pipes[i]);
}
......@@ -169,7 +221,7 @@ int main (int argc, int argv) {
exit(1);
}
memset(&serv_addr, 0, sizeof(serv_addr));
portno = 8000;
portno = settings->listening_port;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = INADDR_ANY;
......@@ -183,7 +235,7 @@ int main (int argc, int argv) {
listen(sockfd, 5);
clilen = sizeof(cli_addr);
init_cache();
init_cache(settings->cache_size);
storage_init();
while(1) {
......@@ -193,7 +245,7 @@ int main (int argc, int argv) {
perror("ERROR on accept");
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;
......
......@@ -10,13 +10,13 @@
#include "LRU.h"
#define ATOMIC_TEST_AND_SET __atomic_test_and_set
#define CLEAR __atomic_clear
#define MAX_SIZE 10
struct KV *array[MAX_SIZE];
struct KV *array[];
struct queue *qu=NULL;
struct queue *last;
unsigned _Atomic lock=0;
int maxsize;
void remove_element_from_deque(char *key)
......@@ -81,7 +81,7 @@ int remove_front_element()
qu = qu->next;
assert(temp != 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)
{
......@@ -96,7 +96,7 @@ int remove_front_element()
int find_empty_location()
{
for(int i=0;i<MAX_SIZE;i++)
for(int i=0;i<maxsize;i++)
{
if(array[i]->valid == FALSE)
{
......@@ -110,7 +110,7 @@ int find_empty_location()
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)
{
......@@ -130,7 +130,7 @@ int cache_del(char *key)
void cache_put(char *key, char *value)
{
int indx=-1;
for(int i=0;i<MAX_SIZE;i++)
for(int i=0;i<maxsize;i++)
{
if(array[i]->key!=NULL) {
if(strcmp(array[i]->key , key) == 0)
......@@ -163,7 +163,7 @@ void cache_put(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)
{
......@@ -178,8 +178,10 @@ int cache_get(char *key, char *value)
return 0;
}
void init_cache() {
for(int j=0;j<MAX_SIZE;j++)
void init_cache(int size) {
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]->valid = FALSE;
......@@ -188,7 +190,7 @@ void init_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);
}
......
......@@ -26,7 +26,7 @@ struct queue{
struct queue *next;
};
extern struct KV *array[MAX_SIZE];
extern struct KV *array[];
extern struct queue *qu;
extern struct queue *last;
......
No preview for this file type
aasasdads=23
asdsadasdasdasd=29
asdsadasdasdasd=29
asdsadasdasdasd=29
asdsadasdasdasdasdasd=231
\ No newline at end of file
max_threads=5
no_of_dsads=10
adsasaddsa=22223
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()
char *line;
off_t len=0;
char* arrParam[20];
char* arrValue[20];
int arrValue[20];
int k=0;
while(getline(&line,&len,fp)!=-1)
{
char *param=strtok(line,"=");
char *value =strtok(NULL,"=");
arrParam[k]=param;
arrValue[k++]=value;
printf("%s:%s",arrParam[k-1],arrValue[k-1]);
arrValue[k++]=atoi(value);
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