Skip to content

Commit 8cec589

Browse files
update
1 parent e26770a commit 8cec589

File tree

7 files changed

+84
-75
lines changed

7 files changed

+84
-75
lines changed

.gitignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
build/
2-
lib/
3-
.idea/
4-
cmake-build-debug/
5-
Lib/
2+
.idea
3+
cmake-build-debug
4+
Lib

Src/Center/Center.cpp

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ void center::wait_cli_login()
111111
};
112112

113113
//用于接收数据的缓冲区
114-
std::array<char, protocal::kCLI_LOGIN_PACSIZE_> packet {0};
115-
std::array<char, protocal::kCLI_LOGIN_UID_> uidbuf {0};
116-
std::array<unsigned char, protocal::kCLI_LOGIN_STATE_> statebuf {0};
114+
std::array<char, protocol::kCLI_LOGIN_PACSIZE_> packet {0};
115+
std::array<char, protocol::kCLI_LOGIN_UID_> uidbuf {0};
116+
std::array<unsigned char, protocol::kCLI_LOGIN_STATE_> statebuf {0};
117117
//创建socket
118118
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
119119
//服务端addr和客户端addr, 后者保存发送方的信息
@@ -142,20 +142,26 @@ void center::wait_cli_login()
142142
//线程主循环
143143
while(true)
144144
{
145-
int recv_num = recvfrom(sock_fd, packet.data(), packet.size(), MSG_WAITALL, (sockaddr*)&addr_cli, &place_holder_1);
145+
char recv_buf[8];
146+
memset(recv_buf, 0, sizeof(recv_buf));
147+
int recv_num = recvfrom(sock_fd,recv_buf, protocol::kCLI_LOGIN_PACSIZE_, MSG_WAITALL, (sockaddr*)&addr_cli, &place_holder_1);
146148
//MSG_WAITALL:要求阻塞操作,直到请求得到完整的满足。
147149
//如果捕捉到信号,错误或者连接断开发生,或者下次被接收的数据类型不同,仍会返回少于请求量的数据。
148150

149151
//可能是阻塞超时, 进行下次循环
150-
if(recv_num < 0)
152+
if(recv_num < 0){
153+
LOG_WARN << "recv from client error";
151154
continue;
155+
}
152156

157+
for(int i = 0; i < 8; i++)
158+
packet[i] = recv_buf[i];
153159
//按照协议格式进行解析, 并且得到ip等其他信息
154-
memcpy(uidbuf.data(), packet.data(), protocal::kCLI_LOGIN_UID_);
160+
memcpy(uidbuf.data(), packet.data(), protocol::kCLI_LOGIN_UID_);
155161
for(auto& ch :uidbuf)
156162
ch += '0';
157163
size_t uid = std::atol(uidbuf.data());
158-
memcpy(statebuf.data(), packet.data() + protocal::kCLI_LOGIN_UID_, protocal::kCLI_LOGIN_STATE_);
164+
memcpy(statebuf.data(), packet.data() + protocol::kCLI_LOGIN_UID_, protocol::kCLI_LOGIN_STATE_);
159165
size_t state = statebuf[0];
160166
//state暂时不做处理
161167

@@ -205,7 +211,8 @@ void center::wait_cli_login()
205211
else if(this->mirs_data_.size() == 1)
206212
available_mir = (*mirs_data_.begin()).first;
207213
//回复可用mir地址
208-
int send_num = sendto(sock_fd, &available_mir, sizeof(available_mir), 0, (sockaddr*)&addr_cli, sizeof(addr_cli));
214+
char send_buf[4] = {available_mir.seg0,available_mir.seg1,available_mir.seg2,available_mir.seg3};
215+
int send_num = sendto(sock_fd, send_buf, protocol::kCENT_RESPONSE_PACSIZE_, 0, (sockaddr*)&addr_cli, sizeof(addr_cli));
209216
login_count++;
210217

211218
//数据库记录日志
@@ -228,7 +235,7 @@ void center::wait_cli_login()
228235
atom_mutex_ = false;
229236

230237
//设置一段时间后pop掉队头的任务
231-
this->pool_.run( [this](){
238+
this->pool_.run( [this, &uid](){
232239
sleep(telemeter::setting->cli_login_cache_time_);
233240
while(true)
234241
{
@@ -240,8 +247,11 @@ void center::wait_cli_login()
240247
atom_mutex_ = false;
241248
LOG_INFO << "erase login cache of client: " << id;
242249
}
243-
else
250+
else{
251+
LOG_INFO << "cached client login, no reply for client: " << uid;
244252
continue;
253+
}
254+
245255
}
246256
});
247257
break;
@@ -271,9 +281,9 @@ void center::listen_mir_beat()
271281
size_t all_beat_count {0}; //用于判活算法
272282

273283
//用于接收数据的缓冲区
274-
std::array<uint8_t, protocal::kMIR_BEAT_PACSIZE_> packet {0};
275-
std::array<uint8_t, protocal::kMIR_BEAT_IP_> ipbuf {0};
276-
std::array<uint8_t, protocal::kMIR_BEAT_LOAD_> loadbuf {0};
284+
std::array<uint8_t, protocol::kMIR_BEAT_PACSIZE_> packet {0};
285+
std::array<uint8_t, protocol::kMIR_BEAT_IP_> ipbuf {0};
286+
std::array<uint8_t, protocol::kMIR_BEAT_LOAD_> loadbuf {0};
277287
//创建socket
278288
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
279289
//服务端addr和客户端addr, 后者保存发送方的信息
@@ -298,19 +308,28 @@ void center::listen_mir_beat()
298308
//线程主循环
299309
while(true)
300310
{
301-
int recv_num = recvfrom(sock_fd, packet.data(), packet.size(), MSG_WAITALL, (sockaddr*)&addr_mir, &place_holder_1);
311+
char recv_buf[8] = {0};
312+
memset(recv_buf, 0, sizeof(recv_buf));
313+
int recv_num = recvfrom(sock_fd, recv_buf, protocol::kMIR_BEAT_PACSIZE_, MSG_WAITALL, (sockaddr*)&addr_mir, &place_holder_1);
302314
//MSG_WAITALL:要求阻塞操作,直到请求得到完整的满足。
303315
//如果捕捉到信号,错误或者连接断开发生,或者下次被接收的数据类型不同,仍会返回少于请求量的数据。
304316

305317
//可能是阻塞超时, 进行下次循环
306-
if(recv_num < 0)
318+
if(recv_num < 0) {
319+
LOG_WARN << "recv from mirror error";
307320
continue;
321+
}
308322

323+
for(int i = 0; i < 8; i++)
324+
packet[i] = recv_buf[i];
309325
//按照协议格式进行解析, 并且得到ip等其他信息
310-
memcpy(ipbuf.data(), packet.data(), protocal::kMIR_BEAT_IP_);
311-
memcpy(loadbuf.data(), packet.data() + protocal::kMIR_BEAT_IP_, protocal::kMIR_BEAT_LOAD_);
326+
memcpy(ipbuf.data(), packet.data(), protocol::kMIR_BEAT_IP_);
327+
328+
memcpy(loadbuf.data(), packet.data() + protocol::kMIR_BEAT_IP_, protocol::kMIR_BEAT_LOAD_);
329+
330+
mir_ip = IP(ipbuf[0], ipbuf[1], ipbuf[2], ipbuf[3]);
331+
LOG_INFO << mir_ip.to_string() << " send packet " << packet.data();
312332

313-
mir_ip = (ipbuf[0] << 24) + (ipbuf[1] << 16) + (ipbuf[2] << 8) + (ipbuf[3]);
314333
if(mirs_data_.count(mir_ip) == 0)
315334
{
316335
mirs_data_.insert({mir_ip, MirDescript()});
@@ -324,17 +343,17 @@ void center::listen_mir_beat()
324343
all_beat_count++;
325344
if(all_beat_count >= mirs_data_.size() - 1)
326345
{
327-
for(auto& kvp : mirs_data_)
328-
{
329-
if(kvp.second.decre_and_get_beat() <= 0);
330-
{
331-
//说明有mir掉线了
332-
int i = 3; //占位避免编译器报warning
333-
//dblog(MIR_DISCONECT);
334-
mirs_data_.erase(kvp.first);
335-
LOG_INFO << "mirror disconnect: " << const_cast<IP*>(&kvp.first)->to_string();
336-
}
337-
}
346+
// for(auto& kvp : mirs_data_)
347+
// {
348+
// if(kvp.second.decre_and_get_beat() <= 0);
349+
// {
350+
// //说明有mir掉线了
351+
// int i = 3; //占位避免编译器报warning
352+
// //dblog(MIR_DISCONECT);
353+
// mirs_data_.erase(kvp.first);
354+
// LOG_INFO << "mirror disconnect: " << const_cast<IP*>(&kvp.first)->to_string();
355+
// }
356+
// }
338357
}
339358
}
340359

Src/Common/Udp.cpp

Lines changed: 0 additions & 2 deletions
This file was deleted.

Src/Mirror/Mirror.cpp

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ void Mirror::init() {
4949
telemeter::setting->db_password = std::string(line.begin() +line.find_first_of('[') + 1, line.begin() +line.find_first_of(']'));
5050
else if (line.find("center_ip") != std::string::npos)
5151
telemeter::setting->center_ip = std::string(line.begin() +line.find_first_of('[') + 1, line.begin() +line.find_first_of(']'));
52+
else if (line.find("this_ip") != std::string::npos)
53+
telemeter::setting->this_ip = std::string(line.begin() +line.find_first_of('[') + 1, line.begin() +line.find_first_of(']'));
5254
else if (line.find("update_pic_interval") != std::string::npos)
5355
telemeter::setting->update_pic_interval = std::stoi(
5456
std::string(line.begin() +line.find_first_of('[') + 1,line.begin() + line.find_first_of(']')));
@@ -107,6 +109,8 @@ void Mirror::init() {
107109

108110
void Mirror::start() {
109111

112+
113+
110114
//初始化配置文件及各数据
111115
this->init();
112116

@@ -152,15 +156,23 @@ void Mirror::start() {
152156

153157

154158
void Mirror::update_data_info() {
155-
// 发送ip地址
156-
// using Server::IP;
159+
160+
// 发送‘心跳包’
161+
int sock;
162+
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
163+
std::cerr << "sock error" << std::endl;
164+
struct sockaddr_in servaddr;
165+
memset(&servaddr, 0, sizeof(servaddr));
166+
servaddr.sin_family = AF_INET;
167+
// 设置发送端口
168+
servaddr.sin_port = htons(port::kMIR_BEAT_);
169+
// 设置发送ip
170+
/*修改:若发送失败,则沿用之前的ip*/
171+
servaddr.sin_addr.s_addr = inet_addr((telemeter::setting->center_ip).c_str());
157172

158173
// 更新间隔
159174
size_t time = 0;
160-
161-
unsigned char this_ip[4] = {'0', '0', '0', '0'};//解析ip
162-
163-
// 发送的息
175+
// 发送消息
164176
char send_buf[8] = {0};
165177
// 定义计算服务器负载情况的函数
166178
auto cal_ser_load = [&]() -> uint8_t {
@@ -181,14 +193,7 @@ void Mirror::update_data_info() {
181193

182194
auto get_packet = [&]() {
183195

184-
// getMyip
185-
std::string host;
186-
std::string ip;
187-
bool err = GetHostInfo(host, ip);
188-
if (!err) {
189-
//报错
190-
}
191-
IP this_ip(ip);
196+
IP this_ip(telemeter::setting->this_ip);
192197

193198
//calculate ServerLoad
194199
unsigned char serverLoad = cal_ser_load();
@@ -238,37 +243,20 @@ void Mirror::update_data_info() {
238243
//to_string(8byte) = 4+1+1+1+1 true/false
239244
//重置缓冲
240245
memset(send_buf, 0, sizeof(send_buf));
241-
242246
//获取负载情况
243247
std::vector<unsigned char> info = get_packet();
244-
245248
//更新负载情况
246249
for (int i = 0; i < 7; i++)
247250
send_buf[i] = info[i];
248-
249251
//最后位填充0
250252
send_buf[7] = '\0';
251-
252253
//重置time
253254
time = 1;
254255

255-
LOG_INFO << "update pack";
256256
}
257257

258-
// 发送‘心跳包’
259-
int sock;
260-
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
261-
std::cerr << "sock error" << std::endl;
262-
struct sockaddr_in servaddr;
263-
memset(&servaddr, 0, sizeof(servaddr));
264-
servaddr.sin_family = AF_INET;
265-
// 设置发送端口
266-
servaddr.sin_port = htons(port::kMIR_BEAT_);
267-
// 设置发送ip
268-
/*修改:若发送失败,则沿用之前的ip*/
269-
servaddr.sin_addr.s_addr = inet_addr((telemeter::setting->center_ip).c_str());
270258
// 发送缓存
271-
sendto(sock, send_buf, strlen(send_buf), 0, (struct sockaddr *) &servaddr, sizeof(servaddr));
259+
sendto(sock, send_buf, protocol::kMIR_BEAT_PACSIZE_, 0, (struct sockaddr *) &servaddr, sizeof(servaddr));
272260
// 统计发送心跳包次数
273261
time++;
274262
//心跳包频率
@@ -313,6 +301,8 @@ void Mirror::read_config() {
313301
set.db_password = std::string(line.begin() +line.find_first_of('[') + 1,line.begin() + line.find_first_of(']'));
314302
else if (line.find("center_ip") != std::string::npos)
315303
set.center_ip = std::string(line.begin() +line.find_first_of('[') + 1, line.begin() +line.find_first_of(']'));
304+
else if (line.find("this_ip") != std::string::npos)
305+
telemeter::setting->this_ip = std::string(line.begin() +line.find_first_of('[') + 1, line.begin() +line.find_first_of(']'));
316306
else if (line.find("update_pic_interval") != std::string::npos)
317307
set.update_pic_interval = std::stoi(std::string(line.begin() +line.find_first_of('[') + 1, line.begin() +line.find_first_of(']')));
318308
else if (line.find("utc_time: ") != std::string::npos)
@@ -536,7 +526,7 @@ void Mirror::on_message(const muduo::net::TcpConnectionPtr &connectionPtr,
536526
next_event = false;
537527

538528

539-
std::string pic_url = telemeter::setting->img_path + "/" + db_control::find_pic(this->conn, id) + ".jpg";
529+
std::string pic_url = "http://" + telemeter::setting->this_ip + ":" + std::to_string(port::kMIR_HTTP_) + telemeter::setting->img_path + "/" + db_control::find_pic(this->conn, id) + ".jpg";
540530

541531
std::deque <Message> msgs = db_control::find_mes(this->conn, id, std::to_string(stamp)); //统一utc时间
542532

@@ -554,9 +544,11 @@ void Mirror::on_message(const muduo::net::TcpConnectionPtr &connectionPtr,
554544
msgs_hash.push_back(std::hash<Message>()(*iter));
555545
}
556546

557-
if (new_hash == old)
547+
if (new_hash == old) {
558548
connectionPtr->send(json_control::no_need_update);
559-
else {
549+
550+
}else {
551+
cli_info_[id] = new_hash;
560552
ClassMes sendInfo;
561553
sendInfo.image_url = pic_url;
562554
sendInfo.messages = msgs;

include/Common/Telemeter.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct Setting
6666
std::string db_user;
6767
std::string db_password;
6868
std::string center_ip;
69+
std::string this_ip;
6970
int update_pic_interval;
7071
int utc_time;
7172
std::string update_calendar_time;
@@ -108,8 +109,8 @@ namespace telemeter {
108109
#endif
109110

110111
#ifdef SRC_MIRROR_H
111-
static Setting _set_cache_one = {1, 60, "home/jol", "Scucids-server","teacher","zhirui208+", "192.168.233.14", 6,8,"24:00","24:00",60};
112-
static Setting _set_cache_two = {1, 60, "home/jol", "Scucids-server","teacher","zhirui208+", "192.168.233.14", 6,8,"24:00","24:00",60};
112+
static Setting _set_cache_one = {1, 60, "/var/www/html/images", "Scucids-server","teacher","zhirui208+", "192.168.233.14","192.168.233.13", 6,8,"24:00","24:00",60};
113+
static Setting _set_cache_two = {1, 60, "/var/www/html/images", "Scucids-server","teacher","zhirui208+", "192.168.233.14", "192.168.233.13",6,8,"24:00","24:00",60};
113114
//主要使用这个指针获取当前配置, 每次读取数据到第二个指针中, 然后交换两个指针的值
114115
static Setting* setting = &_set_cache_one;
115116
static Setting* setting_copy = &_set_cache_two;

include/Common/Udp.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ namespace port
2424
const constexpr size_t kCLI_LOGIN_ = 20800;
2525
const constexpr size_t kCLI_BEAT_ = 20801;
2626
const constexpr size_t kMIR_BEAT_ = 20802;
27-
const constexpr size_t kINS_MSG_ = 20803;
27+
const constexpr size_t kMIR_HTTP_ = 20803;
2828
}
2929

30-
namespace protocal
30+
namespace protocol
3131
{
3232
//以字节计算的每个包的大小和协议中每一部分的大小, 二级缩进的和应该等于第一级缩进
3333
const constexpr size_t kCLI_LOGIN_PACSIZE_ = 8;

test_driver/client_bootstrap.csc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var uuid = "0x46a8e4d9515650e4"hex.shift_left(8)
1111
var reconnect_time = 0
1212

1313
function send_bootstrap()
14-
var header = "0011000100110001001100010011000100110001001100010011001000110000"
14+
var header = "12345670"
1515
var sock = new net.udp.socket
1616
sock.open_v4()
1717
foreach i in range(10) do sock.send_to(header, server_ep)

0 commit comments

Comments
 (0)