Browse Source

be proper, use headers

master
Brett Langdon 13 years ago
parent
commit
0a72ba145c
12 changed files with 116 additions and 80 deletions
  1. +0
    -21
      src/common.c
  2. +11
    -0
      src/common.h
  3. +14
    -9
      src/handlers.c
  4. +14
    -0
      src/handlers.h
  5. +11
    -6
      src/main.c
  6. +0
    -0
      src/proxy.
  7. +5
    -18
      src/proxy.c
  8. +15
    -0
      src/proxy.h
  9. +12
    -19
      src/queue.c
  10. +23
    -0
      src/queue.h
  11. +2
    -7
      src/util.c
  12. +9
    -0
      src/util.h

+ 0
- 21
src/common.c View File

@ -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

+ 11
- 0
src/common.h View File

@ -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

+ 14
- 9
src/handlers.c View File

@ -1,9 +1,14 @@
#ifndef FAST_CACHE_HANDLERS
#define FAST_CACHE_HANDLERS
#include <kclangc.h>
#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

+ 14
- 0
src/handlers.h View File

@ -0,0 +1,14 @@
#ifndef FAST_CACHE_HANDLERS
#define FAST_CACHE_HANDLERS
#include <kclangc.h>
#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

+ 11
- 6
src/main.c View File

@ -3,12 +3,17 @@
#include <pthread.h>
#include <signal.h>
#include <sys/socket.h>
#include <unistd.h>
#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);


+ 0
- 0
src/proxy. View File


+ 5
- 18
src/proxy.c View File

@ -1,16 +1,6 @@
#ifndef FAST_CACHE_PROXY
#define FAST_CACHE_PROXY
#include <curl/curl.h>
#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

+ 15
- 0
src/proxy.h View File

@ -0,0 +1,15 @@
#ifndef FAST_CACHE_PROXY
#define FAST_CACHE_PROXY
#include <curl/curl.h>
#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

+ 12
- 19
src/queue.c View File

@ -1,32 +1,19 @@
#ifndef FAST_CACHE_QUEUE
#define FAST_CACHE_QUEUE
#include "queue.h"
#include <kclangc.h>
#include <pthread.h>
#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;
}

+ 23
- 0
src/queue.h View File

@ -0,0 +1,23 @@
#ifndef FAST_CACHE_QUEUE
#define FAST_CACHE_QUEUE
#include <kclangc.h>
#include <pthread.h>
#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

+ 2
- 7
src/util.c View File

@ -1,7 +1,4 @@
#ifndef FAST_CACHE_UTIL
#define FAST_CACHE_UTIL
#include <kclangc.h>
#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

+ 9
- 0
src/util.h View File

@ -0,0 +1,9 @@
#ifndef FAST_CACHE_UTIL
#define FAST_CACHE_UTIL
#include <kclangc.h>
void list_shift(KCLIST* list, char* next);
void lower(char* word);
void tokenize(KCLIST* tokens, char* base, char* delimiter);
#endif

Loading…
Cancel
Save