-
Notifications
You must be signed in to change notification settings - Fork 0
/
lb.cpp~
294 lines (226 loc) · 6.83 KB
/
lb.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
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
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "read_config.h"
#include "common.h"
#include "server_config.h"
#include "client_config.h"
#include "lbconfig.h"
#include "Log/listener.h"
class LB{
private:
int forid;
int cup_sockfd,sup_sockfd;//对应client_udp_port ,server_udp_port的文件描述符
char mesg[MAXLINE];
socklen_t clilen ;
listener li;
char tmp_str[2000];
struct Information{
t_msg msg;
struct sockaddr_in servaddr;
SA * servaddrs;
SA *pcliaddr;
int serverID;
void *l_b;
};
public:
map<int,int>map_sockfd;
vector<int>vec_port;
int client_udp_port ;
int server_udp_port ;
int num_pid;
int* server_ports;
int* server_vis;
int* ServerID;
LB();
~LB(){
delete server_vis;
delete server_ports;
delete ServerID;
close(cup_sockfd);
close(sup_sockfd);
}
void init(int port);
void dg_echo();
void Cycle(Information inf);
void sg_echo(Information inf);
void send_to_client(Information inf,int sockfd);
void output_log(char *content);
static void* Cycles(void *args);
};
LB::LB(){
li.create_listen_thread();
}
void LB::output_log(char *content){
if(li.no_show()){
cout<<endl;
cout<<"---------------------------"<<endl;
cout<<content<<endl;
cout<<"---------------------------"<<endl;
}
}
void LB::send_to_client(Information inf,int sockfd){//将应答返回给客户端
sprintf(tmp_str,"time will send to client. value is: %s",inf.msg.data);
this->output_log(tmp_str);
int n = sendto(sockfd,&inf.msg, MAXLINE, 0, inf.pcliaddr, clilen);
if (n < 0){
sprintf(tmp_str,"Time send error sendto return value is:%d\n",n);
this->output_log(tmp_str);
}else{
sprintf(tmp_str,"time is sended successfully");
this->output_log(tmp_str);
}
}
void LB::init(int port){ //初始化 两个udp端口
struct sockaddr_in servaddr,cliaddr;
int sizes = 7000;
clilen = sizeof(cliaddr);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
if(port == client_udp_port){
cup_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bind(cup_sockfd, (SA *) &servaddr, sizeof(servaddr));
}
else {
sup_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bind(sup_sockfd, (SA *) &servaddr, sizeof(servaddr));
}
cout<<"port "<<port<<" is work"<<endl;
if(port == client_udp_port)dg_echo();
}
void LB::Cycle(Information inf){// 轮转调度一个服务器
int forkid,ii,ports = -1;
struct timeval tv;
while(1){
for(int i=0; i<num_pid;i++){
if(!server_vis[i]){
ii = i;
ports = ServerID[i];
break;
}
}
if(ports!=-1)break;
if(ports==-1)sleep(10);//无服务器等待
}
server_vis[ii]=1;
sprintf(tmp_str,"LB choose the %d server its port is:%d\n",ports,map_sockfd[ports]);
this->output_log(tmp_str);
inf.msg.dst_id = ServerID[ii];
bzero(&inf.servaddr, sizeof(inf.servaddr));
inf.servaddr.sin_family = AF_INET;
inf.servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
inf.servaddr.sin_port = htons(map_sockfd[ports]);
inf.serverID = ii;
inf.servaddrs = (SA *)&inf.servaddr;
sg_echo(inf);
}
void* LB::Cycles(void *args){
struct Information *inf;
inf = (struct Information *)args;
LB* l_b = (LB *)inf->l_b;
l_b->Cycle(*inf);
}
void LB::sg_echo(Information inf){ //发送给一个服务器
sprintf(tmp_str,"Send a request to one server and choose a server");
this->output_log(tmp_str);
fd_set readfds;
struct timeval tv;
int sizes = 7000;
setsockopt(sup_sockfd, SOL_SOCKET, SO_SNDBUF, &sizes, sizeof(sizes));
int n = sendto(sup_sockfd,&inf.msg, MAXLINE, 0,inf.servaddrs, clilen);
if (n < 0){
sprintf(tmp_str,"Send a request to one server and choose a server");
this->output_log(tmp_str);
}
while(1){
FD_ZERO(&readfds);
// add our descriptors to the set
FD_SET(sup_sockfd, &readfds);
// FD_SET(s2, &readfds);
// wait until either socket has data ready to be recv()d (timeout 10.5 secs)
tv.tv_sec = 10;
tv.tv_usec = 500000;
int rv = select(sup_sockfd+1, &readfds, NULL, NULL, &tv);
/* if (rv == -1) {
perror("select"); // error occurred in select()
} else if (rv == 0) {
printf("Timeout occurred! No data after 10.5 seconds.\n");
} */
if(rv > 0){
recvfrom(sup_sockfd, &inf.msg, MAXLINE, 0, inf.servaddrs, &clilen);
break;
}
}
send_to_client(inf,cup_sockfd);//结果返回个客户端
sprintf(tmp_str,"Get the time from server its value:%s",inf.msg.data);
this->output_log(tmp_str);
server_vis[inf.serverID]=0;
sprintf(tmp_str,"%d server is ready ",inf.serverID);
this->output_log(tmp_str);
}
void
LB::dg_echo()//接收客户端请求
{
socklen_t len;
char mesg[MAXLINE];
int size = 7000;
struct sockaddr_in servaddr;
SA * pcliaddr;
// signal(SIGINT, recvfrom_int);
setsockopt(cup_sockfd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
for ( ; ; ) {
len = clilen;
t_msg clientmsg;
recvfrom(cup_sockfd, &clientmsg, MAXLINE, 0, pcliaddr, &len);
sprintf(tmp_str,"LB accepted client's request:\n client dist_id:%d-----LB's id:%d-----message's type:%d-----messages'id:%d",clientmsg.dst_id,LBID,clientmsg.msg_type,clientmsg.src_id);
this->output_log(tmp_str);
struct Information *infs ;
struct Information inf;
infs = (struct Information *)malloc(sizeof(struct Information));
infs->pcliaddr = pcliaddr;
infs->serverID = -1;
infs->msg = clientmsg;
infs->servaddr = servaddr;
infs->l_b = this;
inf = *infs;
if(clientmsg.dst_id != LBID){//消息中的dst_id不等于自己的id
strcpy(inf.msg.data,"abondon");
send_to_client(inf,cup_sockfd);
}
else {//如果消息中的dst_id等于自己的id
// clientmsg.dst_id =
pthread_t thread_id;
int err;
//
err = pthread_create(&thread_id,NULL,Cycles,(void *)infs);
if(err != 0){ //error
printf("can't create thread: %s\n",strerror(err));
}
}
// int n = sendto(sockfd, ctime(&timer), MAXLINE, 0, pcliaddr, clilen);
}
}
int main(){
// freopen ("myfiles.txt","w",stdout);
LB l_b;
server_config *se = new server_config();
se->deal_data();
l_b.num_pid = se->get_num_pid();
l_b.ServerID = se->get_id();
l_b.server_vis = (int *)malloc(sizeof(int)*l_b.num_pid);
memset(l_b.server_vis,0,sizeof(int)*l_b.num_pid);
l_b.server_ports = se->get_port();
lb_config *lb = new lb_config();
lb->deal_data();
l_b.server_udp_port = lb->get_server_udb_port();
l_b.client_udp_port = lb->get_client_udb_port();
for (int i = 0;i < l_b.num_pid;i++){
l_b.map_sockfd[l_b.ServerID[i]]= l_b.server_ports[i];
}
l_b.init(l_b.server_udp_port);
l_b.init(l_b.client_udp_port);
}