diff -ruN ./0.8/Makefile ./0.9/Makefile --- ./0.8/Makefile Fri May 10 19:19:38 2002 +++ ./0.9/Makefile Wed Jun 5 15:29:22 2002 @@ -4,8 +4,8 @@ all: jikos_cheetah -jikos_cheetah: connection.o main.o rmd160.o jikos_cheetah.o errors.o server.o - gcc ${OPT} ${LDOPT} connection.o main.o rmd160.o jikos_cheetah.o errors.o server.o -o ${NAME} +jikos_cheetah: connection.o main.o rmd160.o jikos_cheetah.o errors.o server.o log.o + gcc ${OPT} ${LDOPT} connection.o main.o rmd160.o jikos_cheetah.o errors.o server.o log.o -o ${NAME} connection.o: connection.h configuration.h errors.c rmd160.c connection.c gcc ${OPT} -c connection.c -o connection.o @@ -25,6 +25,8 @@ server.o: server.c server.h configuration.h gcc ${OPT} -c server.c -o server.o +log.o: log.c log.h + gcc ${OPT} -c log.c -o log.o clean: rm -f *.o;rm jikos_cheetah diff -ruN ./0.8/config.h ./0.9/config.h --- ./0.8/config.h Wed Jun 5 13:43:53 2002 +++ ./0.9/config.h Wed Jun 5 14:29:02 2002 @@ -1,2 +1,2 @@ #define TCP_PORT 8080 -#define VERSION "0.8" +#define VERSION "0.9" diff -ruN ./0.8/connection.c ./0.9/connection.c --- ./0.8/connection.c Wed Jun 5 13:42:21 2002 +++ ./0.9/connection.c Wed Jun 5 16:31:12 2002 @@ -25,6 +25,10 @@ extern int number_free[MAX_SERVER_ID+1]; +/* helper to do buffered write to logfile (from log.c) */ +extern void add_to_log(int type, char *ipaddr, int server_id, int first_visit_flag, + int timestamp_1, int timestamp_2, int request_count); + /* adds new item to global items[] * for description of data structures, see connection.h * if we've run out of free nodes, allocate PTR_BLOCKSIZE @@ -366,7 +370,7 @@ i += 7; while(isspace(connection->request_text[i])) i++; li = 0; - got_S_cookie = 0; +/* got_S_cookie = 0;*/ /* this is BUG, why is it here?? */ for (;((connection->request_text[i] != '\r') && (i < connection->request_text_size)) && (i<1024); i++, li++) { @@ -440,6 +444,7 @@ * this way, to be more readable and obvious */ N = connection->server_id; + fprintf(stderr,"gSc: %d gVc: %d\n",got_S_cookie, got_V_cookie); if ( (got_V_cookie && !got_S_cookie) || (!got_V_cookie && !got_S_cookie) || (got_S_cookie && !got_V_cookie)){ /* we need to create new item - only setting of first_visit_flag @@ -454,6 +459,14 @@ N, connection->set_cookie); add_new_item(N, time(NULL), connection->set_cookie, got_V_cookie, (char *)strdup(connection->ip_addr)); + + /* do (buffered) log write */ + add_to_log(0, connection->ip_addr, connection->server_id, + got_V_cookie, /* <=> 1st_visit */ + connection->last_request, + 0, /* FIXME: co tu ma byt? za zadani nejasne */ + connection->request_count); + } else if (got_V_cookie && got_S_cookie){ item_struct *tmp_item; @@ -476,7 +489,16 @@ if (tmp_item->request_count < 0) tmp_item->request_count--; connection->do_set_cookie = 1; connection->set_cookie = (char *) strdup(tmp_item->identifier); + connection->last_request = tmp_item->last_request; + connection->request_count = tmp_item->request_count; } + add_to_log(0, connection->ip_addr, connection->server_id, + got_V_cookie, /* <=> 1st_visit */ + connection->last_request, + 0, /* FIXME: co tu ma byt? za zadani nejasne */ + connection->request_count); + + } else { /* did got only S cookie - weird */ @@ -579,6 +601,7 @@ connection->server_id, DOMAIN_NAME); write(socket, write_buffer, strlen(write_buffer)); + } } diff -ruN ./0.8/connection.h ./0.9/connection.h --- ./0.8/connection.h Thu May 16 17:36:34 2002 +++ ./0.9/connection.h Wed Jun 5 15:57:14 2002 @@ -25,6 +25,8 @@ #define SESSION_TIMEOUT 30*60 #define DOMAIN_NAME "dummy.domain.cz" #define PTR_BLOCKSIZE 32 +#define LOG_BUFFER_ITEMS 3 +#define LOG_FILENAME "CHEETAH.LOG" /* item struct is a round-liked list, allocated PT_BLOCKSIZE * nodes in advance. items[i] allocated in init_item_struct() @@ -40,7 +42,7 @@ int id; time_t first_request; time_t last_request; - long long request_count; + int request_count; char *identifier; int first_visit_flag; char *ip_addr; @@ -73,6 +75,9 @@ char *ip_addr; char *set_cookie; int do_set_cookie; + + int last_request; + int request_count; }; #define CONNECTION(x) ((connection *)x) diff -ruN ./0.8/jikos_cheetah.c ./0.9/jikos_cheetah.c --- ./0.8/jikos_cheetah.c Thu May 16 17:09:46 2002 +++ ./0.9/jikos_cheetah.c Wed Jun 5 15:59:28 2002 @@ -8,6 +8,8 @@ extern sig_atomic_t terminate; /*extern void free_item_struct();*/ +extern sig_atomic_t alrm_pending; + int socket_ready_to_read (int socket) { @@ -52,6 +54,12 @@ connection_ptr this_connection; + /* here potential pending alarms should be handled */ + if ((int)alrm_pending == 1){ + alrm_pending = 0; + alarm(10); + fprintf(stderr, "Budik handled\n"); + } if ((ret = socket_ready_to_read (jikos_cheetah->server_socket)) > 0) { clilen = sizeof(cli_addr); diff -ruN ./0.8/log.c ./0.9/log.c --- ./0.8/log.c Thu Jan 1 01:00:00 1970 +++ ./0.9/log.c Wed Jun 5 16:28:47 2002 @@ -0,0 +1,80 @@ + +#include "log.h" + +log_struct *log_buffer[LOG_BUFFER_ITEMS]; +int log_filled_items; + +void init_log_struct() +{ + int i; + + for (i = 0; i < LOG_BUFFER_ITEMS; i++){ + log_buffer[i] = (log_struct *) malloc(sizeof(log_struct)); + if (!log_buffer[i]){ + /* FIXME: all other malloc()s should look like this */ + fprintf(stderr,"init_log_struct(): malloc() err\n"); + exit(0); + } + log_filled_items = 0; + } +} + +void logfile_flush() +{ + int i,q; + time_t curtime; + char buff[1024]; /* ;) */ + char log_fname[1024]; + + curtime = time(NULL); + snprintf(buff, 512, "%d",(int)curtime); + strncpy(log_fname, LOG_FILENAME, 512); + strncat(log_fname, buff, 512); + + fprintf(stderr, "flushing log to %s\n", log_fname); + + i = open(log_fname, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + if (i <= 0){ + fprintf(stderr,"FATAL: err opening log file!!!\n"); + exit(0); + } + for (q = 0; q <= log_filled_items; q++){ + snprintf(buff, 1024, "%d %s %d %d %d %d %d\n", + log_buffer[q]->type, log_buffer[q]->ipaddr, + log_buffer[q]->server_id, + log_buffer[q]->first_visit_flag, + log_buffer[q]->timestamp_1, + log_buffer[q]->timestamp_2, + log_buffer[q]->request_count); + write (i, buff, strlen(buff)); + } + close(i); + +} + +void add_to_log(int type, char *ipaddr, int server_id, int first_visit_flag, + int timestamp_1, int timestamp_2, int request_count) +{ + int i; + + i = log_filled_items; /* just to be shorter ;) */ + + log_buffer[i]->type = type; + log_buffer[i]->ipaddr = ipaddr; + log_buffer[i]->server_id = server_id; + log_buffer[i]->first_visit_flag = first_visit_flag; + log_buffer[i]->timestamp_1 = timestamp_1; + log_buffer[i]->timestamp_2 = timestamp_2; + log_buffer[i]->request_count = request_count; + + if (log_filled_items == LOG_BUFFER_ITEMS-1){ + /* flush the logfile */ + logfile_flush(); + log_filled_items = 0; + } + else{ + log_filled_items++; + } + +} + diff -ruN ./0.8/log.h ./0.9/log.h --- ./0.8/log.h Thu Jan 1 01:00:00 1970 +++ ./0.9/log.h Wed Jun 5 15:47:51 2002 @@ -0,0 +1,24 @@ +#ifndef __LOG_H_ +#define __LOG_H_ + +/* data structures acting as a buffer between daemon and logfile */ + +#include "connection.h" +#include +#include +#include + + +typedef struct _log_struct log_struct; + +struct _log_struct { + int type; + char *ipaddr; + int server_id; + int timestamp_1; + int timestamp_2; + int first_visit_flag; + int request_count; +}; + +#endif diff -ruN ./0.8/main.c ./0.9/main.c --- ./0.8/main.c Thu May 16 17:07:02 2002 +++ ./0.9/main.c Wed Jun 5 16:06:36 2002 @@ -12,8 +12,14 @@ */ int number_free[MAX_SERVER_ID+1]; +/* should be also int */ +sig_atomic_t alrm_pending = 0; + sig_atomic_t terminate = 0; +/* TODO: this is messy ;) - put into header file */ +extern void init_log_struct(); + void free_item_struct() { int i; @@ -78,11 +84,18 @@ } } -void sig(int dummy){ +void sig(int dummy) +{ fprintf(stderr,"Exit signalled\n"); terminate = 1; } +void alrm(int dummy) +{ + fprintf(stderr,"budik\n"); + alrm_pending = 1; +} + int main (int argc, char **argv) { @@ -91,12 +104,23 @@ srandom(567); web_server = jikos_cheetah_new (); + init_item_struct(); + init_log_struct(); + + /* next alarm will be scheduled after this one is handled */ + alarm(10); + s1 = (struct sigaction *)malloc(sizeof(struct sigaction)); s2 = (struct sigaction *)malloc(sizeof(struct sigaction)); s1->sa_handler = sig; sigaction(SIGTERM, s1, s2); s1->sa_handler = sig; sigaction(SIGINT, s1, s2); + + s1->sa_handler = alrm; s1->sa_flags = SA_RESTART; + sigaction(SIGALRM, s1, s2); + free(s1); free(s2); + while (!terminate) jikos_cheetah_step (web_server); fprintf(stderr,"Shutting down\n");