-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.cpp
229 lines (216 loc) · 6.75 KB
/
main.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
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <vector>
#include "log.h"
#include "conn.h"
#include "mgr.h"
#include "processpool.h"
using std::vector;
static const char* version = "1.0";
static void usage( const char* prog )
{
log( LOG_INFO, __FILE__, __LINE__, "usage: %s [-h] [-v] [-f config_file]", prog );
}
int main( int argc, char* argv[] )
{
char cfg_file[1024];
memset( cfg_file, '\0', 100 );
int option;
// 命令参数处理设置
while ( ( option = getopt( argc, argv, "f:xvh" ) ) != -1 )
{
switch ( option )
{
case 'x':
{
set_loglevel( LOG_DEBUG );
break;
}
case 'v':
{
log( LOG_INFO, __FILE__, __LINE__, "%s %s", argv[0], version );
return 0;
}
case 'h':
{
usage( basename( argv[ 0 ] ) );
return 0;
}
case 'f':
{
memcpy( cfg_file, optarg, strlen( optarg ) );
break;
}
case '?':
{
log( LOG_ERR, __FILE__, __LINE__, "un-recognized option %c", option );
usage( basename( argv[ 0 ] ) );
return 1;
}
}
}
// 读取配置文件
if( cfg_file[0] == '\0' )
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "please specify the config file" );
return 1;
}
int cfg_fd = open( cfg_file, O_RDONLY );
if( !cfg_fd )
{
log( LOG_ERR, __FILE__, __LINE__, "read config file met error: %s", strerror( errno ) );
return 1;
}
struct stat ret_stat;
if( fstat( cfg_fd, &ret_stat ) < 0 )
{
log( LOG_ERR, __FILE__, __LINE__, "read config file met error: %s", strerror( errno ) );
return 1;
}
char* buf = new char [ret_stat.st_size + 1];
memset( buf, '\0', ret_stat.st_size + 1 );
/* 把配置文件读入buf中 */
ssize_t read_sz = read( cfg_fd, buf, ret_stat.st_size );
if ( read_sz < 0 )
{
log( LOG_ERR, __FILE__, __LINE__, "read config file met error: %s", strerror( errno ) );
return 1;
}
vector< host > balance_srv; // 本机地址
vector< host > logical_srv; // 实际的要负载均衡的主机地址
host tmp_host;
memset( tmp_host.m_hostname, '\0', 1024 );
char* tmp_hostname;
char* tmp_port;
char* tmp_conncnt;
bool opentag = false;
char* tmp = buf;
char* tmp2 = NULL;
char* tmp3 = NULL;
char* tmp4 = NULL;
/* The strpbrk() function locates the first occurrence in the string s('\n') of any of */
/* the bytes in the string(tmp) accept. */
// 解析配置文件
while( tmp2 = strpbrk( tmp, "\n" ) )
{
*tmp2++ = '\0';
/* The strstr() function finds the first occurrence of the substring needle("<logical_host>") in the */
/* string haystack(tmp). The terminating null bytes ('\0') are not compared. */
if( strstr( tmp, "<logical_host>" ) )
{
if( opentag )
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "parse config file failed" );
return 1;
}
opentag = true;
}
else if( strstr( tmp, "</logical_host>" ) )
{
if( !opentag )
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "parse config file failed" );
return 1;
}
logical_srv.push_back( tmp_host );
memset( tmp_host.m_hostname, '\0', 1024 );
opentag = false;
}
else if( tmp3 = strstr( tmp, "<name>" ) )
{
tmp_hostname = tmp3 + 6;
tmp4 = strstr( tmp_hostname, "</name>" );
if( !tmp4 )
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "parse config file failed" );
return 1;
}
*tmp4 = '\0';
memcpy( tmp_host.m_hostname, tmp_hostname, strlen( tmp_hostname ) );
}
else if( tmp3 = strstr( tmp, "<port>" ) )
{
tmp_port = tmp3 + 6;
tmp4 = strstr( tmp_port, "</port>" );
if( !tmp4 )
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "parse config file failed" );
return 1;
}
*tmp4 = '\0';
tmp_host.m_port = atoi( tmp_port );
}
else if( tmp3 = strstr( tmp, "<conns>" ) )
{
tmp_conncnt = tmp3 + 7;
tmp4 = strstr( tmp_conncnt, "</conns>" );
if( !tmp4 )
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "parse config file failed" );
return 1;
}
*tmp4 = '\0';
tmp_host.m_conncnt = atoi( tmp_conncnt );
}
else if( tmp3 = strstr( tmp, "Listen" ) )
{
tmp_hostname = tmp3 + 6;
tmp4 = strstr( tmp_hostname, ":" );
if( !tmp4 )
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "parse config file failed" );
return 1;
}
*tmp4++ = '\0';
tmp_host.m_port = atoi( tmp4 );
memcpy( tmp_host.m_hostname, tmp3, strlen( tmp3 ) );
balance_srv.push_back( tmp_host );
memset( tmp_host.m_hostname, '\0', 1024 );
}
tmp = tmp2;
}
if( balance_srv.size() == 0 || logical_srv.size() == 0 )
{
log( LOG_ERR, __FILE__, __LINE__, "%s", "parse config file failed" );
return 1;
}
const char* ip = balance_srv[0].m_hostname;
int port = balance_srv[0].m_port;
int listenfd = socket( PF_INET, SOCK_STREAM, 0 );
assert( listenfd >= 0 );
int ret = 0;
struct sockaddr_in address;
bzero( &address, sizeof( address ) );
address.sin_family = AF_INET;
inet_pton( AF_INET, ip, &address.sin_addr );
address.sin_port = htons( port );
ret = bind( listenfd, ( struct sockaddr* )&address, sizeof( address ) );
assert( ret != -1 );
ret = listen( listenfd, 5 );
assert( ret != -1 );
//memset( cfg_host.m_hostname, '\0', 1024 );
//memcpy( cfg_host.m_hostname, "127.0.0.1", strlen( "127.0.0.1" ) );
//cfg_host.m_port = 54321;
//cfg_host.m_conncnt = 5;
processpool< conn, host, mgr >* pool = processpool< conn, host, mgr >::create( listenfd, logical_srv.size() );
if( pool )
{
pool->run( logical_srv );
delete pool;
}
close( listenfd );
return 0;
}