#include <vector>
#include <tuple>
#include "comserver_control.h"
#include "aaaaaa_com_interface.h"
#include "business_engine_comserver.h"
#include "comserver_define.h"
#include "aaaaaa_com_factory.h"
#include "http_pool_factory.h"
CComServerControl::CComServerControl() {
CComServerControl::Init();
}
CComServerControl::~CComServerControl() {
}
bool CComServerControl::Init() {
if (_bInit) return true;
std::vector<std::tuple<int, VBusinessComFactoryFuncSPtr>> comFactoryConfigs = {
// 业务组件
std::make_tuple(ComServer_Basic_AAAAAA, (VBusinessComFactoryFuncSPtr)CAAAAAAComFactory::CreateInstanceSPtr),
// 通用组件
std::make_tuple(ComServer_Common_HttpPool, (VBusinessComFactoryFuncSPtr)CIHttpClientPoolFactory::CreateInstanceSPtr),
};
for (const auto &t : comFactoryConfigs) {
_compFactoryMap.insert(CompFactoryMap::value_type(std::get<0>(t), std::get<1>(t)));
}
// 初始化组件
_bInit = InitComponents();
return _bInit;
}
void CComServerControl::UnInit() {
}
bool CComServerControl::InitComponents() {
bool bRet = true;
// 此处动态控制对应默认组件是否被创建
std::vector<std::tuple<int, bool>> comConfigs = {
std::make_tuple(ComServer_Basic_AAAAAA, m_clDyConfig.DY_ENABLE_AAAAAA),
std::make_tuple(ComServer_Common_HttpPool, m_clDyConfig.DY_ENABLE_HTTPPOOL),
};
CompSPtr compSPtr;
for(const auto &t : comConfigs)
{
int type = std::get<0>(t);
if (std::get<1>(t) && _compFactoryMap.find(type) != _compFactoryMap.end())
{
if (!AddDefaultComponent(type, _compFactoryMap[type], compSPtr))
{
bRet = false;
break;
}
}
}
return bRet;
}
void CComServerControl::UnInitComponents() {
}
bool CComServerControl::LoadDynamicConfig(const std::string& clDyJson) {
return true;
}
bool CComServerControl::SetDefaultCompControl(BusinessControlSPtr pBusinessControl) {
// 第一个创建的
if (_pDefaultBusinessControl) return true;
for (auto &kv : _compMapDefault) {
for (auto &sub : kv.second) {
sub.second->SetBusinessControl(pBusinessControl);
}
}
_pDefaultBusinessControl = pBusinessControl;
return true;
}
bool CComServerControl::RegistComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const bool& isComRegist) {
return RegistComponent(type, factoryFunc, CompClsID(type), isComRegist);
}
bool CComServerControl::AddDefaultComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, CompSPtr& compSPtr) {
return AddDefaultComponent(type, factoryFunc, CompClsID(type), CompVID(type), compSPtr);
}
bool CComServerControl::GetDefaultComServerSPtr(const int& type, CompSPtr& compSPtr) {
if (!_bInit)
{
CComServerControl::Init();
}
bool bRet = false;
if (type <= 0 || type == ComServer_Basic_Cnt || type >= ComServer_Common_Cnt)
{
return bRet;
}
std::string clsID = CompClsID(type);
std::string vID = CompVID(type);
do {
// defalut 中查询
if (_compMapDefault.find(clsID) == _compMapDefault.end()) break;
if (_compMapDefault[clsID].find(vID) == _compMapDefault[clsID].end()) break;
compSPtr = _compMapDefault[clsID][vID];
bRet = true;
} while (false);
if (!bRet && (_compFactoryMap.find(type) != _compFactoryMap.end()))
{
bRet = AddDefaultComponent(type, _compFactoryMap[type], compSPtr);
}
return bRet;
}
bool CComServerControl::CreateCompSPtr(const int& type, const std::string& tag, CompSPtr& compSPtr) {
return true;
}
std::string CComServerControl::CompClsID(const int& type) {
std::string strID = "";
strID += "comp_";
strID += std::to_string(type);
return strID;
}
std::string CComServerControl::CompVID(const int& type) {
return CompVID(type, "");
}
std::string CComServerControl::CompVID(const int& type, const std::string& tag) {
std::string strID = "";
strID += CompClsID(type);
if (!tag.empty()) {
strID.append("_");
strID.append(tag);
}
strID += "_instance";
return strID;
}
bool CComServerControl::AddComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, CompSPtr& compSPtr) {
std::string clsID = CompClsID(type);
std::string tag = "";
if (_pBusinessControl)
{
tag = _pBusinessControl->ID().c_str();
}
std::string vID = CompVID(type, tag);
return AddComponent(type, factoryFunc, clsID, vID, compSPtr);
}
bool CComServerControl::AddComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& tag, CompSPtr& compSPtr) {
std::string clsID = CompClsID(type);
std::string vID = CompVID(type, tag);
return AddComponent(type, factoryFunc, clsID, vID, compSPtr);
}
bool CComServerControl::SetBusinessControl(BusinessControlSPtr pBusinessControl) {
_pBusinessControl = pBusinessControl;
SetDefaultCompControl(pBusinessControl);
return true;
}
bool CComServerControl::GetComServerSPtr(const int& type, CompSPtr& compSPtr) {
bool bRet = GetComServerSPtr(type,
CompVID(type, _pBusinessControl->ID().c_str()),
compSPtr
);
if (!bRet) {
bRet = GetComServerSPtr(type,
CompVID(type),
compSPtr
);
}
return bRet;
}
bool CComServerControl::GetComServerSPtr(const int& type, const std::string& vID, CompSPtr& compSPtr) {
bool bRet = false;
std::string clsID = CompClsID(type);
do{
if (vID == CompVID(type))
{
if (_compMapDefault.find(clsID) == _compMapDefault.end()) break;
if (_compMapDefault[clsID].find(vID) == _compMapDefault[clsID].end()) break;
compSPtr = _compMapDefault[clsID][vID];
}
else
{
if (_compMap.find(clsID) == _compMap.end()) break;
if (_compMap[clsID].find(vID) == _compMap[clsID].end()) break;
compSPtr = _compMap[clsID][vID];
}
bRet = true;
}while (0);
return bRet;
}
bool CComServerControl::AddDefaultComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& clsID, const std::string& vID, CompSPtr& compSPtr) {
bool bRet = false;
if (!RegistComponent(type, factoryFunc, clsID)) return bRet;
if (clsID == "" || vID == "") return bRet;
if (_compMapDefault.find(clsID) == _compMapDefault.end())
{
CompSPtrMap subCompMap;
_compMapDefault.insert(CompMap::value_type(clsID, subCompMap));
}
std::map<std::string, CompSPtr> &subCompMap = _compMapDefault[clsID];
if (subCompMap.find(vID) == subCompMap.end())
{
CompSPtr compSptr;
if (CVBusinessComServer::ComCreateInstanceSPtr(clsID, vID, compSptr) == BUSINESS_VS_OK)
{
compSptr->SetBusinessControl(_pDefaultBusinessControl);
subCompMap.insert(CompSPtrMap::value_type(vID, compSptr));
bRet = true;
}
}
return bRet;
}
bool CComServerControl::RegistComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& clsID, const bool& isComRegist) {
_compFactoryMap[type] = factoryFunc;
if (isComRegist)
{
return CVBusinessComServer::ComRegist(clsID, factoryFunc) == BUSINESS_VS_OK;
}
return true;
}
bool CComServerControl::AddComponent(const int& type, const VBusinessComFactoryFuncSPtr& factoryFunc, const std::string& clsID, const std::string& vID, CompSPtr& compSPtr) {
bool bRet = false;
if (!RegistComponent(type, factoryFunc, clsID)) return bRet;
if (clsID