Skip to content

Commit bd185d4

Browse files
update
1 parent ba0ae7e commit bd185d4

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

include/Telemeter.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ const constexpr uint _permanent_thread_num_ = 3; //center的常驻线程数量
4444
//mirror的setting, POD type
4545
struct Setting
4646
{
47-
int center_ip_;
48-
//int backup_center_ip_;
4947
uint8_t mir_beat_interval_;
50-
uint8_t //数据库ip?
48+
uint8_t mir_report_interval_;
49+
uint8_t class_cache_time_;
50+
uint8_t class_interval_;
5151
};
5252

5353
#endif
@@ -68,6 +68,13 @@ namespace telemeter
6868
#endif
6969

7070
#ifdef SRC_MIRROR_H
71+
72+
Setting _set_cache_one = {600,1,60,600,55};
73+
Setting _set_cache_two = {600,1,60,600,55};
74+
75+
Setting* setting = &_set_cache_one;
76+
Setting* setting_copy = &_set_cache_two;
77+
7178
#endif
7279

7380

include/mirror/Mirror.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,35 @@
22

33

44
void Mirror::start() {
5-
//任务队列最大数量
5+
//任务队列最大数量
66
pool_.setMaxQueueSize(30);
7-
pool_.start(_permanent_thread_num_ + 5;)
8-
9-
10-
//提交任务
7+
//用std::hard_ware_concurrency()确定硬件支持的最大并发数量, 该数量 * 2为此程序运行时占用线程数
8+
pool_.start(static_cast<int>(2 * std::thread::hardware_concurrency()));
9+
auto thd_start = [&](const char* info){
10+
std::clog << "thread start: " << info << std::endl;
11+
};
1112

12-
this->pool_.run([this]() { read_config(); });
1313
//读取配置文件
14+
this->pool_.run([this]() { read_config(); });
15+
thd_start("read config");
16+
//发送负载情况
1417
this->pool_.run([this]() { update_data_info(); });
15-
//初始化数据库
16-
this->pool_.run([this]() { update_data_info(); });
18+
thd_start("update data info");
1719
//更新数据缓存
18-
this->pool_.run([this]() { listen_cli_beat(); });
20+
this->pool_.run([this]() { update_clis_data() });
21+
thd_start("updae clients data")
22+
1923
//建立cli链接
24+
this->pool_.run([this]() { listen_cli_beat(); });
25+
thd_start("listen clients beat");
2026

2127
//输出日志
2228
}
2329

2430
void Mirror::read_config() {
25-
//json格式
26-
static std::string config_file_path{"CenterConfig.txt"};
31+
32+
// 后可以考虑使用json格式
33+
static std::string config_file_path{"MirrorConfig.txt"};
2734
static std::ifstream in{config_file_path, std::ios::in};
2835
Setting set{};
2936
memset(&set, 0, sizeof(Setting));
@@ -47,6 +54,8 @@ void Mirror::read_config() {
4754
else if (line.find("cli_class") != std::string::npos)
4855
set.mir_max_disbeat_time_ = std::stoi(
4956
std::string(line.find_first_of('[') + 1, line.find_first_of(']')));
57+
else if(line.find("class_interval") != std::string::bois)
58+
set.class_interval_ = std::stoi( std::string(line.find_first_of('[') + 1, line.find_first_of(']')));
5059
}
5160
};
5261

@@ -223,3 +232,5 @@ void Mirror::update_clis_data() {
223232
}
224233
}
225234

235+
236+

include/mirror/Mirror.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef SRC_MIRROR_H
2+
#define SRC_MIRROR_H
3+
14
#include "muduo/base/ThreadPool.h"
25
#include "muduo/net/InetAddress.h"
36
#include "muduo/net/TcpServer.h"
@@ -14,12 +17,6 @@
1417

1518
class Mirror
1619
{
17-
class setting{
18-
unsigned next_read_this_file_interval;
19-
unsigned mir_heart_beat_interval;
20-
unsigned mir_load_report_interval;
21-
unsigned cli_class_info_cache_time;
22-
};
2320
private:
2421
DetailLoadState load_;
2522
std::queue<Message> messages_;
@@ -31,6 +28,11 @@ class Mirror
3128
muduo::net::EventLoop loop_;
3229
muduo::net::TcpServer server_;
3330
muduo::ThreadPool pool_;
31+
32+
// 数据库配置相关,仅在程序开始时使用
33+
const std::string db_dsn = "MariaDB-server";
34+
const std::string db_name = "root";
35+
const std::string db_passwd = "root";
3436
private:
3537
//mirror start:
3638

@@ -56,3 +58,5 @@ class Mirror
5658
void start();
5759

5860
};
61+
62+
#endif //SRC_MIRROR_H

include/mirror/MirrorConfig.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
next_read: [600]s //多少秒之后重新读取配置文件, 需要不停机调试则尽量缩短, 但不建议小于30(半分钟)
1+
next_read_interval: [600]s //多少秒之后重新读取配置文件, 需要不停机调试则尽量缩短, 但不建议小于30(半分钟)
22
mir_heart_beat_interval: [1]s //心跳的间隔
33
mir_load_report_interval: [60] //多少个心跳包后更新负载情况
44
cli_class_info_cache_time: [600]s //课程信息在cli断联后缓存时间, 如果内存紧张可以适当缩短

0 commit comments

Comments
 (0)