//togo_hash.c void togo_hash_test() { togo_log(DEBUG, "Testing togo_hash.c start ============================"); int i, j; //togo_djb_hash i = togo_djb_hash("WOSHISHEN"); j = togo_djb_hash("asdsadsad"); if (i == 321158909 && j == 1542131117) { togo_log(DEBUG, "Testing function:togo_djb_hash .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_djb_hash .............................FAIL"); togo_exit(); } //togo_murmur_hash2 i = togo_murmur_hash2("WOSHISHEN", togo_strlen("WOSHISHEN")); j = togo_murmur_hash2("asdsadsad", togo_strlen("asdsadsad")); if (i == 1501669989 && j == 844110999) { togo_log(DEBUG, "Testing function:togo_djb_hash .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_djb_hash .............................FAIL"); togo_exit(); } togo_log(DEBUG, "Testing togo_hash.c end ============================\r\n"); }
static void togo_wt_setup(TOGO_WORKER_THREAD *worker_thread) { int pipe_fds[2]; if (pipe(pipe_fds) < 0) { togo_log(ERROR, "Can't create pipe."); togo_exit(); } worker_thread->notify_receive_fd = pipe_fds[0]; worker_thread->notify_send_fd = pipe_fds[1]; if (pthread_mutex_init(&worker_thread->mutex_lock, NULL) != 0) { togo_log(ERROR, "Can't init pthread mutex lock."); togo_exit(); } togo_q_init(worker_thread); worker_thread->base = event_base_new(); if (!worker_thread->base) { togo_log(ERROR, "Can't alloc event base."); togo_exit(); } worker_thread->pipe_event = event_new(worker_thread->base, worker_thread->notify_receive_fd, EV_READ | EV_PERSIST, togo_wt_process, worker_thread); event_add(worker_thread->pipe_event, NULL); }
//togo_pool.c void togo_pool_test() { togo_log(DEBUG, "Testing togo_pool.c start ============================"); void * p; void * p1; //togo_pool_create TOGO_POOL * pool = togo_pool_create(10 * 1024); if (pool->total_size == 10288 && pool->max == 10 * 1024) { togo_log(DEBUG, "Testing function:togo_pool_create .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_pool_create .............................FAIL"); togo_exit(); } //togo_pool_calloc p1 = togo_pool_calloc(pool, 10 * 1024); p = togo_pool_calloc(pool, 10); togo_strcpy(p, "woshishen"); if (strcmp(p, "woshishen") == 0 && togo_strlen(p) == 9 && pool->large->size == 10 * 1024) { togo_log(DEBUG, "Testing function:togo_pool_calloc .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_pool_calloc .............................FAIL"); togo_exit(); } //togo_pool_realloc(pool, 20); p = togo_pool_realloc(pool, (void *) p, 10, 15); togo_strcpy(p, "woshishen11234"); if (strcmp(p, "woshishen11234") == 0 && togo_strlen(p) == 14) { togo_log(DEBUG, "Testing function:togo_pool_realloc .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_pool_realloc .............................FAIL"); togo_exit(); } //togo_pool_free_data togo_pool_free_data(pool, (void *) p); togo_pool_free_large(pool, (void *) p1); if (pool->large == NULL) { togo_log(DEBUG, "Testing function:togo_pool_free_large .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_pool_free_large .............................FAIL"); togo_exit(); } togo_log(DEBUG, "Testing togo_pool.c end ============================\r\n"); }
static void * togo_mt_process(void* args) { struct event_base *base_ev; struct event *ev; evutil_socket_t server_socketfd; struct sockaddr_in server_addr; togo_memzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; if (strcmp(togo_global_c->ip, TOGO_C_DEFAULT_IP) == 0) { server_addr.sin_addr.s_addr = INADDR_ANY; } else { server_addr.sin_addr.s_addr = inet_addr(togo_global_c->ip); } if (togo_global_c->port == 0) { server_addr.sin_port = htons(TOGO_C_DEFAULT_PORT); } else { server_addr.sin_port = htons(togo_global_c->port); } server_socketfd = socket(PF_INET, SOCK_STREAM, 0); if (server_socketfd < 0) { togo_log(ERROR, "Socket error."); togo_exit(); } evutil_make_listen_socket_reuseable(server_socketfd); evutil_make_socket_nonblocking(server_socketfd); BOOL is_set = togo_wt_set_socket_opt(server_socketfd); if (is_set == FALSE) { togo_log(ERROR, "togo_wt_set_socket_opt error."); togo_exit(); } if (bind(server_socketfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)) < 0) { togo_log(ERROR, "bind error."); togo_exit(); } listen(server_socketfd, 32); togo_log(INFO, "Togo Server Start......."); /* event_base */ base_ev = event_base_new(); ev = event_new(base_ev, server_socketfd, EV_TIMEOUT | EV_READ | EV_PERSIST, togo_mt_doaccept, base_ev); event_add(ev, NULL); event_base_dispatch(base_ev); //loop event_base_free(base_ev); }
static void togo_m_cache_create_area(uint32_t msize, uint32_t i, TOGO_M_CACHE_AREA * area) { TOGO_M_CACHE_AREA * curr_area; TOGO_M_CACHE_CHUNK * chunk; uint32_t chunk_item_size; curr_area = (TOGO_M_CACHE_AREA *) (area + i); chunk = togo_m_cache_create_chunk(curr_area); if (chunk == NULL) { togo_log(INFO, "Initialize modules_cache's TOGO_M_CACHE_CHUNK fail."); togo_exit(); } chunk_item_size = TOGO_M_CACHE_CHUNK_SIZE / msize; curr_area->msize = msize; curr_area->lru_head = NULL; curr_area->lru_tail = NULL; curr_area->free_list = NULL; curr_area->chunk_list = chunk; curr_area->chunk_curr = chunk; curr_area->chunk_item_curr = 0; curr_area->chunk_item_num = chunk_item_size; curr_area->total_size = TOGO_M_CACHE_CHUNK_SIZE; curr_area->total_chunk = 1; curr_area->total_item = chunk_item_size; curr_area->used_item = 0; curr_area->free_item = chunk_item_size; pthread_mutex_init(&curr_area->lock, NULL); }
static void togo_config_init() { BOOL ret = togo_read_config(TOGO_C_PATH); if (ret == FALSE) { BOOL retTry = togo_read_config(TOGO_C_DEFAULT); if (retTry == FALSE) { togo_log(ERROR, "Read config file Fail"); togo_exit(); } } }
void togo_m_queue_init(void) { togo_m_queue_pool = togo_pool_create(TOGO_M_QUEUE_POOL_SIZE); if (togo_m_queue_pool == NULL) { togo_log(ERROR, "Initialize modules_queue's pool fail."); togo_exit(); } togo_m_queue_hashtable = togo_hashtable_init(togo_m_queue_pool); if (togo_m_queue_hashtable == NULL) { togo_log(ERROR, "Initialize modules_queue's hashtable fail."); togo_exit(); } togo_m_queue_fblock = (TOGO_M_QUEUE_FBLOCK *) togo_pool_calloc( togo_m_queue_pool, sizeof(TOGO_M_QUEUE_FBLOCK)); if (togo_m_queue_fblock == NULL) { togo_log(ERROR, "Initialize modules_queue's free block fail."); togo_exit(); } pthread_mutex_init(&togo_m_queue_fblock->flock, NULL); pthread_mutex_init(&togo_m_queue_glock, NULL); }
void togo_m_cache_init(void) { uint32_t total_area, curr, i; uint32_t * area_table; TOGO_M_CACHE_AREA * area; togo_m_cache_pool = togo_pool_create(TOGO_M_CACHE_POOL_SIZE); if (togo_m_cache_pool == NULL) { togo_log(ERROR, "Initialize modules_cache's POOL fail."); togo_exit(); } togo_m_cache_hashtable = togo_hashtable_init(togo_m_cache_pool); if (togo_m_cache_hashtable == NULL) { togo_log(ERROR, "Initialize modules_cache's HASHTABLE fail."); togo_exit(); } togo_m_cache = (TOGO_M_CACHE *) togo_pool_calloc(togo_m_cache_pool, sizeof(TOGO_M_CACHE)); if (togo_m_cache == NULL) { togo_log(ERROR, "Initialize modules_cache's TOGO_M_CACHE fail."); togo_exit(); } togo_m_cache->pool = togo_m_cache_pool; togo_m_cache->total_area = 0; togo_m_cache->total_hit = 0; togo_m_cache->total_read = 0; togo_m_cache->total_size = 0; togo_m_cache->total_write = 0; togo_m_cache->is_flush = FALSE; pthread_mutex_init(&togo_m_cache->glock, NULL); /* Initialize the area table!*/ area_table = (uint32_t *) togo_pool_alloc(togo_m_cache_pool, sizeof(uint32_t) * TOGO_M_CACHE_AREA_TABLE_DEFAULT_SIZE); if (area_table == NULL) { togo_log(ERROR, "Initialize modules_cache's area_table fail."); togo_exit(); } togo_m_cache->area_table = area_table; total_area = i = 1; curr = TOGO_M_CACHE_ITEM_START; *area_table = curr; while (curr != TOGO_M_CACHE_CHUNK_SIZE) { total_area++; curr = (uint32_t)(curr * TOGO_M_CACHE_ITEM_POWER); if (curr >= TOGO_M_CACHE_CHUNK_SIZE) { curr = TOGO_M_CACHE_CHUNK_SIZE; } *(area_table + i) = curr; i++; } togo_m_cache->total_area = total_area; /* Initialize the memory of area!*/ area = (TOGO_M_CACHE_AREA *) togo_pool_calloc(togo_m_cache_pool, sizeof(TOGO_M_CACHE_AREA) * total_area); if (area == NULL) { togo_log(ERROR, "Initialize modules_cache's TOGO_M_CACHE_AREA fail."); togo_exit(); } togo_m_cache->area = area; for (i = 0; i < total_area; i++) { curr = *(area_table + i); togo_m_cache_create_area(curr, i, area); } }
//togo_string.c void togo_string_test() { togo_log(DEBUG, "Testing togo_string.c start ============================"); u_char c; char * dst = togo_alloc(100); char * str2 = togo_alloc(100); //togo_encode_base64 int i; char * str = "SAODOSADO988766556asdsad"; i = togo_encode_base64(dst, str); if (strcmp(dst, "U0FPRE9TQURPOTg4NzY2NTU2YXNkc2Fk") == 0 && strlen("U0FPRE9TQURPOTg4NzY2NTU2YXNkc2Fk") == i) { togo_log(DEBUG, "Testing function:togo_encode_base64 .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_encode_base64 .............................FAIL"); togo_exit(); } //togo_decode_base64 i = togo_decode_base64(str2, dst); if (strcmp(str, str2) == 0 && strlen(str2) == i) { togo_log(DEBUG, "Testing function:togo_decode_base64 .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_decode_base64 .............................FAIL "); togo_exit(); } //togo_tolower c = togo_tolower('X'); if (c == 'x') { togo_log(DEBUG, "Testing function:togo_tolower .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_tolower .............................FAIL "); togo_exit(); } //togo_toupper c = togo_toupper('a'); if (c == 'A') { togo_log(DEBUG, "Testing function:togo_toupper .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_toupper .............................FAIL "); togo_exit(); } //togo_strcmp char * s1 = "woshishen"; char * s2 = "woshishen"; i = togo_strcmp(s1, s2); if (i == 0) { togo_log(DEBUG, "Testing function:togo_strcmp .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_strcmp .............................FAIL "); togo_exit(); } //togo_strncmp char * s11 = "woshishen"; char * s22 = "woshidfdf"; i = togo_strncmp(s11, s22, 5); if (i == 0) { togo_log(DEBUG, "Testing function:togo_strncmp .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_strncmp .............................FAIL "); togo_exit(); } //togo_strchr char * strchr_p = togo_strchr("woshishen", 's'); if (*strchr_p == 's') { togo_log(DEBUG, "Testing function:togo_strchr .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_strchr .............................FAIL "); togo_exit(); } //togo_strstr char * strstr_p = togo_strstr("woshishen", "shi"); if (*strstr_p == 's') { togo_log(DEBUG, "Testing function:togo_strstr .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_strstr .............................FAIL "); togo_exit(); } //togo_strlen i = togo_strlen("woshishen"); if (i == 9) { togo_log(DEBUG, "Testing function:togo_strlen .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_strlen .............................FAIL "); togo_exit(); } //togo_atofp(u_char *line, size_t n, size_t point) i = togo_atofp("10.98", 5, 2); if (i == 1098) { togo_log(DEBUG, "Testing function:togo_atofp .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_atofp .............................FAIL "); togo_exit(); } //togo_atoi(u_char *line, size_t n); i = togo_atoi("1024", 4); if (i == 1024) { togo_log(DEBUG, "Testing function:togo_atoi .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_atoi .............................FAIL "); togo_exit(); } //togo_cpystrn(u_char *s1, char *s2, size_t len) togo_cpystrn(dst, "woshishen", 8); if (togo_strcmp(dst, "woshishe") == 0) { togo_log(DEBUG, "Testing function:togo_cpystrn .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_cpystrn .............................FAIL %s", dst); togo_exit(); } //togo_strtolower togo_cpystrn(dst, "WOSHISHEN", 9); char * x111 = togo_strtolower(dst); if (togo_strcmp(x111, "woshishen") == 0) { togo_log(DEBUG, "Testing function:togo_strtolower .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_strtolower .............................FAIL "); togo_exit(); } //togo_strtolower togo_cpystrn(dst, "woshishen", 9); char * x222 = togo_strtoupper(dst); if (togo_strcmp(x222, "WOSHISHEN") == 0) { togo_log(DEBUG, "Testing function:togo_strtoupper .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_strtoupper .............................FAIL "); togo_exit(); } //togo_strpos char * ddd = "woshishen"; int ddds = togo_strpos(ddd, 'i'); if (ddds == 4) { togo_log(DEBUG, "Testing function:togo_strpos .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_strpos .............................FAIL "); togo_exit(); } //togo_strrpos int ddds2 = togo_strrpos(ddd, 'i'); if (ddds2 == 4) { togo_log(DEBUG, "Testing function:togo_strrpos .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_strrpos .............................FAIL "); togo_exit(); } //togo_trim togo_cpystrn(dst, " woshishen ", 15); char * dst1 = togo_trim(dst); if (togo_strcmp(dst1, "woshishen") == 0) { togo_log(DEBUG, "Testing function:togo_trim .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_trim .............................FAIL"); togo_exit(); } //togo_ltrim togo_cpystrn(dst, " woshishen ", 15); char * dst2 = togo_ltrim(dst); if (togo_strcmp(dst2, "woshishen ") == 0) { togo_log(DEBUG, "Testing function:togo_ltrim .............................OK "); } else { togo_log(DEBUG, "Testing function:togo_ltrim .............................FAIL "); togo_exit(); } //togo_rtrim togo_cpystrn(dst, " woshishen ", 15); char * dst3 = togo_rtrim(dst); if (togo_strcmp(dst3, " woshishen") == 0) { togo_log(DEBUG, "Testing function:togo_rtrim .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_rtrim .............................FAIL "); togo_exit(); } togo_log(DEBUG, "Testing togo_string.c end ============================"); }
static void togo_argv_init(int argc, char *argv[]) { int oc; char ec; char * config_path = NULL; char * port = NULL; char * daemon = NULL; char * worker_thread_num = NULL; /* Process command-line arguments */ while ((oc = getopt(argc, argv, "c:" /* Set configuration file */ "p:" /* Set Tcp port*/ "n:" /* Set the number of worker thread*/ "d" /* Set the programs runs in the backgroud */ )) != -1) { switch (oc) { case 'c': if (optarg != NULL) { config_path = optarg; } break; case 'p': if (optarg != NULL) { port = optarg; } break; case 'd': daemon = "1"; break; case 'n': if (optarg != NULL) { worker_thread_num = optarg; } break; default: ec = (char) optopt; togo_log(ERROR, "Illegal argument."); togo_exit(); break; } } /* Read the config file */ if (config_path != NULL) { BOOL isRet = togo_read_config(config_path); if (isRet == FALSE) { togo_config_init(config_path); } } else { togo_config_init(config_path); } if (port != NULL) { togo_set_config(TOGO_C_PORT, port); } if (daemon != NULL) { togo_set_config(TOGO_C_DAEMON, "1"); } if (worker_thread_num != NULL) { togo_set_config(TOGO_C_WTN, worker_thread_num); } }