-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhippo.c
318 lines (300 loc) · 9.01 KB
/
hippo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* Hippo Memory Cached
* Copyright (C) 2012 heaven. All rights reserved.
*
* Hippo is a mini open-source cache daemon, mainly used in dynamic data cache.
*
* Use and distribution licensed under the BSD license. See
* the LICENSE file for full text.
*
* To learn more open-source code, visit: http://code.google.com/p/heavenmvc/
* Email: wangwei([email protected])
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <netdb.h>
#include <dirent.h>
#include <time.h>
#include <getopt.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#define _GNU_SOURCE
#include <pthread.h>
#include<malloc.h>
#include "anet.h"
#include "hash.h"
#include "event.h"
#include "util.h"
#include "command.h"
#include "hippo.h"
//close idle client
void close_time_out_client(void) {
/*redisClient *c;
listIter *li;
listNode *ln;
time_t now = time(NULL);
li = listGetIterator(server.clients,AL_START_HEAD);
if (!li) return;
while ((ln = listNextElement(li)) != NULL) {
c = listNodeValue(ln);
if (now - c->lastinteraction > server.maxidletime) {
redisLog(REDIS_DEBUG,"Closing idle client");
freeClient(c);
}
}
listReleaseIterator(li);*/
}
// Output environment and configure information
void print_server_config(){
fprintf(stderr, "===================================\n");
fprintf(stderr, " hippo Configure information\n");
fprintf(stderr, "===================================\n");
fprintf(stderr, "Is-Debug\t = %s\n", IS_DEBUG ? "Yes" : "No");
fprintf(stderr, "Is-Daemon\t = %s\n", IS_DAEMON ? "Yes" : "No");
fprintf(stderr, "Port\t\t = %d\n", SERVERPORT);
fprintf(stderr, "Max-Client\t = %d\n", MAX_CLIENT_NUM);
fprintf(stderr, "Max-MemSize\t = %d\n", MAX_MEM_SIZE);
fprintf(stderr, "===================================\n\n");
}
//Print status
void print_server_status(){
char buf[8192];
int visit_total;
visit_total = status->visit_add + status->visit_del + status->visit_get;
sprintf(buf, "=============Status===============\n\
STAT version %s\r\n\
STAT pid %d\r\n\
STAT start_time %d\r\n\
STAT run_time %d\r\n\
STAT mem_total %d\r\n\
STAT mem_used %d\r\n\
STAT item_total %d\r\n\
STAT visit_total %d\r\n\
STAT visit_add %d\r\n\
STAT visit_del %d\r\n\
STAT visit_get %d\r\n\
STAT is_debug %d\r\n\
STAT is_daemon %d\r\n\
STAT port %d\r\n\
STAT max_client %d\r\n\
STAT max_tablesize %d\r\n\
===============END================\n",
status->version, status->pid, status->start_time, status->run_time,
status->mem_total, status->mem_used, status->item_total,
visit_total, status->visit_add, status->visit_del, status->visit_get
, status->is_debug, status->is_daemon, status->port
, status->max_client, /*status->max_mem_size,*/ status->max_tablesize);
printf("%s", buf);
}
// Init status
void init_server_status(){
status = (hippo_status *)malloc(sizeof(hippo_status));
memset(status, 0, sizeof(hippo_status));
sprintf(status->version, "%s", VERSION);
status->pid = getpid();
status->start_time = time( (time_t *)NULL );
status->run_time = 0;
status->mem_total = MAX_MEM_SIZE;
status->mem_used = 0;
status->item_total = 0;
status->visit_add = 0;
status->visit_del = 0;
status->visit_get = 0;
status->is_debug=IS_DEBUG;
status->is_daemon=IS_DAEMON;
status->port=SERVERPORT;
status->max_client=MAX_CLIENT_NUM;
//status->max_mem_size=MAX_MEM_SIZE;
status->max_tablesize=HASH_TABLE_MAX_SIZE;
}
// Setting status
void set_server_status( unsigned mem_used, unsigned item_total, unsigned visit_add, unsigned visit_del, unsigned visit_get ){
time_t currtime;
currtime = time((time_t *)NULL);
status->run_time = currtime - status->start_time;
status->mem_used += mem_used;
status->item_total += item_total;
status->visit_add += visit_add;
status->visit_del += visit_del;
status->visit_get += visit_get;
}
// Get status
void get_server_status(){
time_t currtime;
currtime = time((time_t *)NULL);
status->run_time = currtime - status->start_time;
}
// Get memory use size
unsigned get_mem_used(){
unsigned ret;
ret = status->mem_used;
return ret;
}
//free
void free_client(int client_sock) {
aeDeleteFileEvent(server.el,client_sock,AE_READABLE);
aeDeleteFileEvent(server.el,client_sock,AE_WRITABLE);
close(client_sock);
}
// Process client request
int hippo_proc_client( int client_sock ){
hippo_client client;
hippo_command *cmd;
int ret;
char head[BUFFER_SIZE], **head_arr, *method;
int head_num;
FILE *fp;
// read and explode cleent input
fp = fdopen(client_sock, "r");
memset(head, 0, sizeof(head));
if ( fgets(head, BUFFER_SIZE, fp) == NULL ){
send_error_to_client(client_sock, E_SERVER, "not recv client message");
return FALSE;
}
explode(head, ' ', &head_arr, &head_num);
method = trim( strtolower( head_arr[0] ) );
cmd=lookup_command(method);
if (cmd){
client.fd=client_sock;
client.last_cmd_time=time( (time_t *)NULL );
ret=cmd->proc(&client,head_arr,head_num);
return ret;
}else { //exception
send_error_to_client(client_sock, E_CLIENT, "That method is not implemented");
return FALSE;
}
}
// access a client and handle
void hippo_handle_client( int client_sock){
int nread;
int ret;
while(1){
anet_non_block(NULL,client_sock);
anet_tcp_no_delay(NULL,client_sock);
ioctl(client_sock, FIONREAD, &nread);
if ( nread > 0 ){
ret = hippo_proc_client( client_sock);
if (ret == EXIT){
free_client(client_sock);
server.client_num--;
current_client_num();
break;
}
}
}
}
// thread callback function
void *hippo_thread_callback(void *arg) {
int *clientsock;
clientsock = (int *)arg;
hippo_handle_client(*clientsock);
//close(*clientsock);
pthread_exit(NULL);
return NULL;
}
// Posix thread process new connection
void hippo_thread(aeEventLoop *el,int serversock,void *privdata, int mask){
int clientsock, *arg;
struct sockaddr_in client_addr;
unsigned clientlen;
pthread_attr_t thread_attr;
void *thread_result;
HIPPO_NOTUSED(el);
HIPPO_NOTUSED(privdata);
HIPPO_NOTUSED(mask);
// setting pthread attribute
pthread_attr_init(&thread_attr);
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
// run until cancelled
while (1){
int cport;
char cip[128];
pthread_t thread;
unsigned int clientlen = sizeof(client_addr);
memset(server.currtime, 0, sizeof(server.currtime));
getdate(server.currtime);
// wait for client connection
if ((clientsock = anet_accept(server.neterr, serversock, cip, &cport)) < 0){
die("Failed to accept client connection");
}else{
server.client_num++;
}
// use thread process new connection
if(server.client_num>MAX_CLIENT_NUM){
server.client_num--;
free_client(clientsock);
}else{
current_client_num();
arg = &clientsock;
if (pthread_create(&thread, &thread_attr, hippo_thread_callback, (void *)arg) != 0){
die("Create new thread failed");
}
}
}
// destory pthread attribute
(void)pthread_attr_destroy(&thread_attr);
}
void current_client_num(){
getdate(server.currtime);
fprintf(stdout, "[%s] current client num of connection is %d.\n", server.currtime,server.client_num);
}
void init_server(){
signal(SIGCHLD, SIG_IGN);
signal(SIGTERM, sig_term);
//signal(SIGSTOP,SIG_IGN);//ctrl+z
//signal(SIGINT,SIG_IGN);//ctrl+c
init_server_status();
if(status->is_debug){
print_server_config();
}
if(status->is_daemon){
init_daemon();
}else{
getdate(server.currtime);
fprintf(stdout, "[%s] Start server listening at port %d ...\n", server.currtime, status->port);
fprintf(stdout, "[%s] Waiting client connection ...\n", server.currtime);
}
hash_table_init();
server.el=aeCreateEventLoop();
server.client_num=0;
server.maxidletime=MAXIDLETIME;
server.fd = anet_tcp_server(server.neterr, status->port, NULL);
if(server.fd==-1){
die("connect fail.\n");
exit(1);
}
aeCreateTimeEvent(server.el, 1000, flush_hash_table_expire_data, NULL, NULL);
}
// signal control
void sig_term(int signo) {
FILE *fp;
char info[50];
if(signo == SIGTERM){
sprintf(info, "program terminated.\r\n");
write_file_lock(info);
//exit(0);
}
}
int main(){
init_server();
// multiplexing IO
if (aeCreateFileEvent(server.el, server.fd, AE_READABLE,hippo_thread, NULL, NULL) == AE_ERR) die("creating file event fail");
aeMain(server.el);
aeDeleteEventLoop(server.el);
return 1;
}