合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## 4) Dns中Route结构的定义 ### 4.1 Route类的单例模式设计 ​ 首先我们将Route类设计成单例,我们创建头文件和cpp文件. > lars_dns/include/dns_route.h ```c #pragma once class Route { public: //创建单例的方法 static void init() { _instance = new Route(); } static Route *instance() { //保证init方法在这个进程执行中,只执行一次 pthread_once(&_once, init); return _instance; } private: //构造函数私有化 Route(); Route(const Route&); const Route& operator=(const Route&); //单例 static Route* _instance; //单例锁 static pthread_once_t _once; /* ---- 属性 ---- */ //... }; ``` > lars_dns/src/dns_route.cpp ```c #include "dns_route.h" //单例对象 Route * Route::_instance = NULL; //用于保证创建单例的init方法只执行一次的锁 pthread_once_t Route::_once = PTHREAD_ONCE_INIT; ``` ### 4.2 Route中的map数据类型定义 ​ **这里的Route并非reactor中的router,这里的Route我们是把`modid/cmdid`与需要管理的远程服务器的`serverip/serverport`的一条对应关系叫一个`Route`。** ​ 我们用map来存储这些关系,其中key是modid/cmdid的一个二进制偏移量处理,而map的value是一个set集合,因为一个modid/cmdid可能对应多个host主机的ip和端口。具体的表现数据结构形式如下。 ![10-dns_route_structure.jpeg](https://upload-images.jianshu.io/upload_images/11093205-35e76f039a7031c3.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ​ 接下来,我们来定义一个相关代码: > lars_dns/include/dns_route.h ```c #pragma once #include <pthread.h> #include <ext/hash_map> #include <ext/hash_set> #include "mysql.h" using __gnu_cxx::hash_map; using __gnu_cxx::hash_set; //定义用来保存modID/cmdID与host的IP/host的port的对应的关系 数据类型 typedef hash_map< uint64_t, hash_set<uint64_t> > route_map; typedef hash_map< uint64_t, hash_set<uint64_t> >::iterator route_map_it; //定义用来保存host的IP/host的port的的集合 数据类型 typedef hash_set<uint64_t> host_set; typedef hash_set<uint64_t>::iterator host_set_it; class Route { public: //创建单例的方法 static void init() { _instance = new Route(); } static Route *instance() { //保证init方法在这个进程执行中,只执行一次 pthread_once(&_once, init); return _instance; } private: //构造函数私有化 Route(); Route(const Route&); const Route& operator=(const Route&); //单例 static Route* _instance; //单例锁 static pthread_once_t _once; /* ---- 属性 ---- */ //数据库 MYSQL _db_conn; //mysql链接 char _sql[1000]; //sql语句 //modid/cmdid---ip/port 对应的route关系map route_map *_data_pointer; //指向RouterDataMap_A 当前的关系map route_map *_temp_pointer; //指向RouterDataMap_B 临时的关系map pthread_rwlock_t _map_lock; }; ``` ### 4.3 Route初始化 > lars_dns/src/dns_route.cpp ```c #include <string> #include <stdlib.h> #include <unistd.h> #include "lars_reactor.h" #include "dns_route.h" #include "string.h" using namespace std; //单例对象 Route * Route::_instance = NULL; //用于保证创建单例的init方法只执行一次的锁 pthread_once_t Route::_once = PTHREAD_ONCE_INIT; Route::Route() { //1 初始化锁 pthread_rwlock_init(&_map_lock, NULL); //2 初始化map _data_pointer = new route_map();//RouterDataMap_A _temp_pointer = new route_map();//RouterDataMap_B //3 链接数据库 this->connect_db(); //4 查询数据库,创建_data_pointer 与 _temp_pointer 两个map this->build_maps(); } void Route::connect_db() { // --- mysql数据库配置--- string db_host = config_file::instance()->GetString("mysql", "db_host", "127.0.0.1"); short db_port = config_file::instance()->GetNumber("mysql", "db_port", 3306); string db_user = config_file::instance()->GetString("mysql", "db_user", "root"); string db_passwd = config_file::instance()->GetString("mysql", "db_passwd", "aceld"); string db_name = config_file::instance()->GetString("mysql", "db_name", "lars_dns"); mysql_init(&_db_conn); //超时断开 mysql_options(&_db_conn, MYSQL_OPT_CONNECT_TIMEOUT, "30"); //设置mysql链接断开后自动重连 my_bool reconnect = 1; mysql_options(&_db_conn, MYSQL_OPT_RECONNECT, &reconnect); if (!mysql_real_connect(&_db_conn, db_host.c_str(), db_user.c_str(), db_passwd.c_str(), db_name.c_str(), db_port, NULL, 0)) { fprintf(stderr, "Failed to connect mysql\n"); exit(1); } } void Route::build_maps() { int ret = 0; snprintf(_sql, 1000, "SELECT * FROM RouteData;"); ret = mysql_real_query(&_db_conn, _sql, strlen(_sql)); if ( ret != 0) { fprintf(stderr, "failed to find any data, error %s\n", mysql_error(&_db_conn)); exit(1); } //得到结果集 MYSQL_RES *result = mysql_store_result(&_db_conn); //得到行数 long line_num = mysql_num_rows(result); MYSQL_ROW row; for (long i = 0; i < line_num; i++) { row = mysql_fetch_row(result); int modID = atoi(row[1]); int cmdID = atoi(row[2]); unsigned ip = atoi(row[3]); int port = atoi(row[4]); //组装map的key,有modID/cmdID组合 uint64_t key = ((uint64_t)modID << 32) + cmdID; uint64_t value = ((uint64_t)ip << 32) + port; printf("modID = %d, cmdID = %d, ip = %lu, port = %d\n", modID, cmdID, ip, port); //插入到RouterDataMap_A中 (*_data_pointer)[key].insert(value); } mysql_free_result(result); } ``` ### 4.4 测试Route的构造及map加载 完成lars dns-service V0.1版本测试 我们在`Lars/base/sql`加入几个简单插入数据的sql语句,方便数据库里有一些测试数据,我们之后应该会提供一个web管理端来操作数据库。 > Lars/base/sql/dns_route_insert.sql ```sql USE lars_dns; INSERT INTO RouteData(modid, cmdid, serverip, serverport) VALUES(1, 1, 3232235953, 7777); INSERT INTO RouteData(modid, cmdid, serverip, serverport) VALUES(1, 2, 3232235954, 7776); INSERT INTO RouteData(modid, cmdid, serverip, serverport) VALUES(1, 2, 3232235955, 7778); INSERT INTO RouteData(modid, cmdid, serverip, serverport) VALUES(1, 2, 3232235956, 7779); UPDATE RouteVersion SET version = UNIX_TIMESTAMP(NOW()) WHERE id = 1; ``` > Lars/base/sql/dns_route_drop.sql ```sql USE lars_dns; DELETE FROM RouteData; UPDATE RouteVersion SET version = UNIX_TIMESTAMP(NOW()) WHERE id = 1; ``` ​ 先将测试数据导入数据库。然后回到`lars_dns`下编译。执行 ```bash $./bin/lars_dns msg_router init... create 0 thread create 1 thread create 2 thread create 3 thread create 4 thread modID = 1, cmdID = 1, ip = 3232235953, port = 7777 modID = 1, cmdID = 2, ip = 3232235954, port = 7776 modID = 1, cmdID = 2, ip = 3232235955, port = 7778 modID = 1, cmdID = 2, ip = 3232235956, port = 7779 lars dns service .... ``` --- ### 关于作者: 作者:`Aceld(刘丹冰)` mail: [danbing.at@gmail.com](mailto:danbing.at@gmail.com) github: [https://github.com/aceld](https://github.com/aceld) 原创书籍: [https://www.kancloud.cn/@aceld](https://www.kancloud.cn/@aceld) ![](https://img.kancloud.cn/b0/d1/b0d11a21ba62e96aef1c11d5bfff2cf8_227x227.jpg) >**原创声明:未经作者允许请勿转载, 如果转载请注明出处**