wsproxy/server/wsproxy/jsondatamgr.cc
aozhiwei 3120257ff9 1
2019-05-31 09:53:20 +08:00

128 lines
5.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "precompile.h"
#include <mutex>
#include "jsondatamgr.h"
#include "app.h"
void JsonDataMgr::Init()
{
#if MASTER_MODE
node_host_mutex_ = new std::mutex();
#endif
std::string wsproxyserver_cluster_json_file;
std::string masterserver_cluster_json_file;
std::string targetserver_cluster_json_file;
std::string routing_tables_json_file;
if (f8::IsOnlineEnv()) {
#if MASTER_MODE
wsproxyserver_cluster_json_file = a8::Format("../config/node%d/game%d.wsproxy.cluster.json",
{
App::Instance()->node_id,
GAME_ID
});
masterserver_cluster_json_file = a8::Format("../config/node%d/game%d.masterserver.cluster.json",
{
App::Instance()->node_id,
GAME_ID
});
routing_tables_json_file = a8::Format("../config/routing_tables.json",
{
});
#else
wsproxyserver_cluster_json_file = a8::Format("../config/game%d.wsproxy.cluster.json", {GAME_ID});
masterserver_cluster_json_file = a8::Format("../config/game%d.masterserver.cluster.json", {GAME_ID});
#endif
} else {
#ifdef MASTER_MODE
wsproxyserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/node%d/game%d.wsproxy.cluster.json",
{
GAME_ID,
App::Instance()->node_id,
GAME_ID
});
masterserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/node%d/game%d.masterserver.cluster.json",
{
GAME_ID,
App::Instance()->node_id,
GAME_ID
});
routing_tables_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/routing_tables.json",
{
GAME_ID
});
#else
wsproxyserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/game%d.wsproxy.cluster.json",
{GAME_ID, GAME_ID});
masterserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/game%d.masterserver.cluster.json",
{GAME_ID, GAME_ID});
targetserver_cluster_json_file = a8::Format("/var/data/conf_test/game%d/wsproxy/game%d.gameserver.cluster.json",
{GAME_ID, GAME_ID});
#endif
}
wsproxyserver_cluster_json_.ReadFromFile(wsproxyserver_cluster_json_file);
#if MASTER_MODE
masterserver_cluster_json_.ReadFromFile(masterserver_cluster_json_file);
routing_tables_json_.ReadFromFile(routing_tables_json_file);
node_host_mutex_->lock();
for (int i = 0; i < routing_tables_json_.Size(); ++i) {
int node_id = routing_tables_json_.At("node_id")->AsXValue();
std::string host = routing_tables_json_.At("host")->AsXValue();
if (node_host_hash_.find(node_id) != node_host_hash_.end()) {
abort();
}
node_host_hash_[node_id] = host;
}
node_host_mutex_->unlock();
#else
targetserver_cluster_json_.ReadFromFile(targetserver_cluster_json_file);
#endif
}
void JsonDataMgr::UnInit()
{
#if MASTER_MODE
delete node_host_mutex_;
node_host_mutex_ = nullptr;
#endif
}
std::shared_ptr<a8::XObject> JsonDataMgr::GetConf()
{
if (App::Instance()->instance_id < 1 || App::Instance()->instance_id > wsproxyserver_cluster_json_.Size()) {
abort();
}
return wsproxyserver_cluster_json_[App::Instance()->instance_id - 1];
}
std::shared_ptr<a8::XObject> JsonDataMgr::GetMasterServerClusterConf()
{
return std::make_shared<a8::XObject>(masterserver_cluster_json_);
}
#if MASTER_MODE
bool JsonDataMgr::GetNodeHost(int node_id, std::string& host)
{
bool found = false;
node_host_mutex_->lock();
auto itr = node_host_hash_.find(node_id);
if (itr != node_host_hash_.end()) {
//!!!因为std::string基于引用计数多线程下直接复制的话会有问题所以要用这种方式赋值
host = itr->second.c_str();
found = true;
}
node_host_mutex_->unlock();
return found;
}
#else
std::shared_ptr<a8::XObject> JsonDataMgr::GetTargetServerClusterConf()
{
return std::make_shared<a8::XObject>(targetserver_cluster_json_);
}
#endif