-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.cpp
147 lines (122 loc) · 3.55 KB
/
filter.cpp
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
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <iostream>
#include <evhttp/HttpServer.h>
#include <evhttp/Config.h>
#include <evhttp/HttpServletFactory.h>
#include "DealItem.h"
#include "ReadForm.h"
#include "FilterSearch.h"
#include "Util.h"
//#include "SendfileIOPumpHttpServlet.h"
//#include "RWIOPumpHttpServlet.h"
//#include "FileServerHttpServlet.h"
#include <locale.h>
#define SERVER_PORT 10000
class TestHttpServletFactory : public HttpServletFactory{
public:
TestHttpServletFactory(){
}
virtual ~TestHttpServletFactory(){}
virtual HttpServlet* create(const char* path){
printf("%s\n",path);
if(strcmp(path, "/search") == 0 || strcmp(path, "/search/") == 0){
return new ReadForm();
}else if(strcmp(path, "/add") == 0 || strcmp(path, "/add/") == 0){
return new DealItem();
}
return new DealItem();
/*
if(strcmp(path, "/hello") == 0){
return new HelloHttpServlet();
}else if(strcmp(path, "/form") == 0){
return new ReadForm();
}else if(strncmp(path, "/sf/", 4) == 0){
return new SendfileIOPumpHttpServlet();
}else if(strncmp(path, "/rw/", 4) == 0){
return new RWIOPumpHttpServlet();
}else if(strncmp(path, "/file/", 6) == 0){
return new FileServerHttpServlet();
}
return new HelloHttpServlet();
*/
}
void free(HttpServlet* servlet){
delete servlet;
}
};
void usage() {
fprintf(stderr,"Usage: ./filter [options]\n");
fprintf(stderr," ./filter -v or --version\n");
fprintf(stderr," ./filter -h or --help\n");
fprintf(stderr,"Examples:\n");
fprintf(stderr," ./filter (run the server with default key.txt)\n");
fprintf(stderr," ./filter -f ./key.txt\n");
fprintf(stderr," ./filter -p 10000\n");
exit(1);
}
void daemonize(void) {
int fd;
if (fork() != 0) exit(0); /* parent exits */
setsid(); /* create a new session */
/* Every output goes to /dev/null. If Redis is daemonized but
* * * the 'logfile' is set to 'stdout' in the configuration file
* * * it will not log at all. */
if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO) close(fd);
}
}
int main(int argc, char** argv) {
int ch;
string filename = "./key.txt";
int port = SERVER_PORT;
bool deamon = false;
while((ch = getopt(argc,argv,"f:vhdp:"))!= -1) {
switch (ch) {
case 'f':
printf("option a:'%s'\n",optarg);
filename = optarg;
break;
case 'd':
deamon = true;
break;
case 'p':
port = atoi(optarg);
break;
case 'v':
return 1;
case 'h':
usage();
return 1;
default:
break;
}
}
Config config;
config.workProcessCount = 8;
if(!Util::setfd("filter.log")) {
cout << " open filter.log error."<<endl;
return 1;
}
//配置HttpServer
TestHttpServletFactory servletFactory;
HttpServer httpServer(port, &servletFactory, &config);
uint32_t num = FilterSearchMgr::Instance()->Init(filename);
cout << "the number of keys is " << num << endl;
if(num == 0) {
exit(0);
}
if(deamon == true) {
daemonize();
}
//启动HttpServer
if(!httpServer.start()){
return 1;
}
//进入EV事件循环
HttpServer::loop();
}