From 0a72ba145cd9ed82254bc34809e620e9880d0d13 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Mon, 22 Jul 2013 18:20:45 -0400 Subject: [PATCH] be proper, use headers --- src/common.c | 21 --------------------- src/common.h | 11 +++++++++++ src/handlers.c | 23 ++++++++++++++--------- src/handlers.h | 14 ++++++++++++++ src/main.c | 17 +++++++++++------ src/proxy. | 0 src/proxy.c | 23 +++++------------------ src/proxy.h | 15 +++++++++++++++ src/queue.c | 31 ++++++++++++------------------- src/queue.h | 23 +++++++++++++++++++++++ src/util.c | 9 ++------- src/util.h | 9 +++++++++ 12 files changed, 116 insertions(+), 80 deletions(-) delete mode 100644 src/common.c create mode 100644 src/common.h create mode 100644 src/handlers.h create mode 100644 src/proxy. create mode 100644 src/proxy.h create mode 100644 src/queue.h create mode 100644 src/util.h diff --git a/src/common.c b/src/common.c deleted file mode 100644 index c7f60b4..0000000 --- a/src/common.c +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef FAST_CACHE_COMMON -#define FAST_CACHE_COMMON -#include "queue.c" - -static sig_atomic_t misses = 0; -static sig_atomic_t hits = 0; -static sig_atomic_t connections = 0; - -static KCDB* db; -static int server; -static queue requests; - -static const char* VERSION = "0.0.1"; -const char* STATS_FORMAT = - "STAT connections %d\r\n" - "STAT hits %d\r\n" - "STAT misses %d\r\n" - "STAT hit_ratio %2.4f\r\n" - "%s" - "END\r\n"; -#endif diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..b942997 --- /dev/null +++ b/src/common.h @@ -0,0 +1,11 @@ +#ifndef FAST_CACHE_COMMON +#define FAST_CACHE_COMMON +#include "queue.h" + +extern sig_atomic_t misses; +extern sig_atomic_t hits; +extern sig_atomic_t connections; +KCDB* db; +int server; +QUEUE requests; +#endif diff --git a/src/handlers.c b/src/handlers.c index 143477f..7017b8f 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -1,9 +1,14 @@ -#ifndef FAST_CACHE_HANDLERS -#define FAST_CACHE_HANDLERS -#include +#include "handlers.h" -#include "common.c" -#include "queue.c" +const char* VERSION = "0.0.1"; +const char* STATS_FORMAT = + "STAT connections %d\r\n" + "STAT hits %d\r\n" + "STAT misses %d\r\n" + "STAT hit_ratio %2.4f\r\n" + "STAT backlog %d\r\n" + "%s" + "END\r\n"; void handle_stats(KCLIST* tokens, FILE* client){ char out[1024]; @@ -11,6 +16,7 @@ void handle_stats(KCLIST* tokens, FILE* client){ if(hits){ hit_ratio = (float)hits / (float)(hits + misses); } + int size = queue_size(&requests); char* status = kcdbstatus(db); KCLIST* stats = kclistnew(); tokenize(stats, status, "\n"); @@ -30,7 +36,7 @@ void handle_stats(KCLIST* tokens, FILE* client){ kclistdel(parts); strcat(status_buf, buf); } - sprintf(out, STATS_FORMAT, connections, hits, misses, hit_ratio, status_buf); + sprintf(out, STATS_FORMAT, connections, hits, misses, hit_ratio, size, status_buf); fputs(out, client); } @@ -67,7 +73,7 @@ void handle_get(KCLIST* tokens, FILE* client){ if(result_buffer){ if(strcmp(result_buffer, "0") == 0){ ++misses; - sprintf(out, "VALUE %s 0 2\r\n{}\r\nEND\r\n", key); + sprintf(out, "VALUE %s 0 0\r\n\r\nEND\r\n", key); } else{ ++hits; sprintf(out, "VALUE %s 0 %d\r\n%s\r\nEND\r\n", key, (int)strlen(result_buffer), result_buffer); @@ -92,7 +98,7 @@ int handle_command(char* buffer, FILE* client){ KCLIST* tokens = kclistnew(); tokenize(tokens, buffer, " "); list_shift(tokens, command); - if(command){ + if(command != NULL){ if(strcmp(command, "get") == 0){ handle_get(tokens, client); } else if(strcmp(command, "stats") == 0){ @@ -116,4 +122,3 @@ int handle_command(char* buffer, FILE* client){ return status; } -#endif diff --git a/src/handlers.h b/src/handlers.h new file mode 100644 index 0000000..24ac82e --- /dev/null +++ b/src/handlers.h @@ -0,0 +1,14 @@ +#ifndef FAST_CACHE_HANDLERS +#define FAST_CACHE_HANDLERS +#include + +#include "common.h" +#include "queue.h" + +void handle_stats(KCLIST* tokens, FILE* client); +void handle_version(KCLIST* tokens, FILE* client); +void handle_flush(KCLIST* tokens, FILE* client); +void handle_delete(KCLIST* tokens, FILE* client); +void handle_get(KCLIST* tokens, FILE* client); +int handle_command(char* buffer, FILE* client); +#endif diff --git a/src/main.c b/src/main.c index dc0eea0..9811258 100644 --- a/src/main.c +++ b/src/main.c @@ -3,12 +3,17 @@ #include #include #include +#include -#include "common.c" -#include "handlers.c" -#include "proxy.c" -#include "queue.c" -#include "util.c" +#include "common.h" +#include "handlers.h" +#include "proxy.h" +#include "queue.h" +#include "util.h" + +sig_atomic_t misses = 0; +sig_atomic_t hits = 0; +sig_atomic_t connections = 0; void* worker(void* arg){ @@ -57,7 +62,7 @@ void on_signal(){ int main(){ signal(SIGINT, on_signal); - int pool_size = 5; + int pool_size = 10; pthread_t pool[pool_size]; queue_init(&requests); diff --git a/src/proxy. b/src/proxy. new file mode 100644 index 0000000..e69de29 diff --git a/src/proxy.c b/src/proxy.c index 2a11c36..a83d873 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -1,16 +1,6 @@ -#ifndef FAST_CACHE_PROXY -#define FAST_CACHE_PROXY -#include +#include "proxy.h" -#include "common.c" -#include "queue.c" - -struct curl_result { - char* data; - size_t size; -}; - -size_t curl_write(char* ptr, size_t size, size_t nmemb, struct curl_result* result){ +size_t curl_write(char* ptr, size_t size, size_t nmemb, CURL_RESULT* result){ size_t realsize = size * nmemb; result->data= realloc(result->data, result->size + realsize + 1); @@ -29,14 +19,14 @@ void* call_proxy(void* arg){ while(1){ queue_get(&requests, next); char* url = (char*)malloc(1024 * sizeof(char)); - sprintf(url, "http://127.0.0.1:8000/%s", next); + sprintf(url, "http://httpbin.org/get?%s", next); CURL* curl = curl_easy_init(); if(!curl){ fprintf(stderr, "Could not initialize curl\n"); - return; + return NULL; } - struct curl_result* result = malloc(sizeof(struct curl_result)); + CURL_RESULT* result = malloc(sizeof(CURL_RESULT)); result->data = malloc(sizeof(char)); result->data[0] = 0; result->size = 0; @@ -53,6 +43,3 @@ void* call_proxy(void* arg){ free(result); } } - - -#endif diff --git a/src/proxy.h b/src/proxy.h new file mode 100644 index 0000000..d0b5e2e --- /dev/null +++ b/src/proxy.h @@ -0,0 +1,15 @@ +#ifndef FAST_CACHE_PROXY +#define FAST_CACHE_PROXY + +#include +#include "common.h" +#include "queue.h" + +typedef struct { + char* data; + size_t size; +} CURL_RESULT; + +size_t curl_write(char* ptr, size_t size, size_t nmemb, CURL_RESULT* result); +void* call_proxy(void* arg); +#endif diff --git a/src/queue.c b/src/queue.c index b797675..27deb7a 100644 --- a/src/queue.c +++ b/src/queue.c @@ -1,32 +1,19 @@ -#ifndef FAST_CACHE_QUEUE -#define FAST_CACHE_QUEUE +#include "queue.h" -#include -#include - -#include "util.c" - -typedef struct { - KCLIST* list; - pthread_mutex_t mutex; - pthread_cond_t cond; - int size; -} queue; - -void queue_del(queue* q){ +void queue_del(PTR_QUEUE q){ kclistdel(*(&q->list)); pthread_mutex_destroy(&q->mutex); pthread_cond_destroy(&q->cond); } -void queue_init(queue* q){ +void queue_init(PTR_QUEUE q){ *(&q->list) = kclistnew(); *(&q->size) = 0; pthread_mutex_init(&q->mutex, NULL); pthread_cond_init(&q->cond, NULL); } -void queue_add(queue* q, char* value){ +void queue_add(PTR_QUEUE q, char* value){ pthread_mutex_lock(&q->mutex); kclistpush(*(&q->list), value, strlen(value) + 1); *(&q->size) += 1; @@ -34,7 +21,7 @@ void queue_add(queue* q, char* value){ pthread_cond_signal(&q->cond); } -void queue_get(queue* q, char* value){ +void queue_get(PTR_QUEUE q, char* value){ pthread_mutex_lock(&q->mutex); while(*(&q->size) == 0){ pthread_cond_wait(&q->cond, &q->mutex); @@ -44,4 +31,10 @@ void queue_get(queue* q, char* value){ pthread_mutex_unlock(&q->mutex); } -#endif +int queue_size(PTR_QUEUE q){ + int size = 0; + pthread_mutex_lock(&q->mutex); + size = *(&q->size); + pthread_mutex_unlock(&q->mutex); + return size; +} diff --git a/src/queue.h b/src/queue.h new file mode 100644 index 0000000..20cc26b --- /dev/null +++ b/src/queue.h @@ -0,0 +1,23 @@ +#ifndef FAST_CACHE_QUEUE +#define FAST_CACHE_QUEUE + +#include +#include +#include "util.h" + +typedef struct { + KCLIST* list; + pthread_mutex_t mutex; + pthread_cond_t cond; + int size; +} QUEUE; + +typedef QUEUE* PTR_QUEUE; + +void queue_del(PTR_QUEUE q); +void queue_init(PTR_QUEUE q); +void queue_add(PTR_QUEUE q, char* value); +void queue_get(PTR_QUEUE q, char* value); +int queue_size(PTR_QUEUE q); + +#endif diff --git a/src/util.c b/src/util.c index c3f86ba..09cf8d1 100644 --- a/src/util.c +++ b/src/util.c @@ -1,7 +1,4 @@ -#ifndef FAST_CACHE_UTIL -#define FAST_CACHE_UTIL - -#include +#include "util.h" void list_shift(KCLIST* list, char* next){ size_t size; @@ -24,7 +21,7 @@ void tokenize(KCLIST* tokens, char* base, char* delimiter){ char* remainder; char* token; char* ptr = base; - while(token = strtok_r(ptr, delimiter, &remainder)){ + while((token = strtok_r(ptr, delimiter, &remainder))){ int last; int len = strlen(token); for(last = len - 1; last >= 0; --last){ @@ -42,5 +39,3 @@ void tokenize(KCLIST* tokens, char* base, char* delimiter){ } free(token); } - -#endif diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..d5a3c4b --- /dev/null +++ b/src/util.h @@ -0,0 +1,9 @@ +#ifndef FAST_CACHE_UTIL +#define FAST_CACHE_UTIL + +#include + +void list_shift(KCLIST* list, char* next); +void lower(char* word); +void tokenize(KCLIST* tokens, char* base, char* delimiter); +#endif