<commit_before>#include "servermanage.h"

#include "utility.h"
#include "NetworkEvent.hpp"
#include "univ/Base64.h"
#include <jsoncpp/json/json.h>
using namespace Sloong::Events;


CResult Sloong::CServerManage::Initialize( int template_id )
{
	m_listFuncHandler["AddTemplate"]=std::bind(&CServerManage::AddTemplateHandler, this, std::placeholders::_1,std::placeholders::_2);
	m_listFuncHandler["DeleteTemplate"]=std::bind(&CServerManage::DeleteTemplateHandler, this, std::placeholders::_1,std::placeholders::_2);
	m_listFuncHandler["SetTemplate"]=std::bind(&CServerManage::SetTemplateHandler, this, std::placeholders::_1,std::placeholders::_2);
	m_listFuncHandler["QueryTemplate"]=std::bind(&CServerManage::QueryTemplateHandler, this, std::placeholders::_1,std::placeholders::_2);

	auto res = m_pAllConfig->Initialize("/data/configuation.db");
	if (res.IsFialed()) return res;

	// Initialize template list
	auto list = m_pAllConfig->GetTemplateList();
	for (auto item : list)
	{
		TemplateItem addItem(item);
		m_oTemplateList[addItem.ID] = addItem;
	}

	res = m_pAllConfig->GetTemplate(template_id);
	if (res.IsFialed())
		return CResult::Succeed();
	else
		return res;
}


int Sloong::CServerManage::SearchNeedCreateTemplate()
{
	for (auto item : m_oTemplateList)
	{
		if (item.second.Replicas == 0)
			continue;

		if (item.second.Created.size() == item.second.Replicas)
			continue;

		if (item.second.Created.size() == 0)
			return item.first;
	}

	for (auto item : m_oTemplateList)
	{
		if (item.second.Replicas == 0)
			continue;

		if (item.second.Created.size() == item.second.Replicas)
			continue;

		if (item.second.Created.size() < item.second.Replicas)
			return item.first;
	}
	return -1;
}


bool Sloong::CServerManage::RegisterServerHandler(Functions func, string sender, CDataTransPackage* pack)
{
	auto index = SearchNeedCreateTemplate();
	if (index == -1)
	{
		pack->ResponsePackage(ResultType::Retry,"Retry");
		return true;
	}

	if( sender.length() == 0 )
	{
		sender = CUtility::GenUUID();
	}

	auto sender_info = m_oServerList.try_get(sender);
	if( sender_info == nullptr)
	{
		ServerItem item;
		item.Address = pack->GetSocketIP();
		item.Port = pack->GetSocketPort();
		item.UUID = sender;
		m_oServerList[sender] = item;
		m_pLog->Verbos(CUniversal::Format("Module[%s:%d] regist to system. Allocating uuid [%s].", item.Address, item.Port, item.UUID));
		sender_info = m_oServerList.try_get(sender);
	}

	if( sender_info == nullptr)
	{
		pack->ResponsePackage(ResultType::Error,"Add server info to ServerList fialed.");
		return true;
	}
	
	auto tpl = m_oTemplateList[index];
	sender_info->TemplateName = tpl.Name;
	sender_info->TemplateID = tpl.ID;
	m_pLog->Verbos(CUniversal::Format("Allocating module[%s] Type to [%s]", sender_info->UUID, sender_info->TemplateName));
	pack->ResponsePackage(sender_info->UUID,sender_info->TemplateID==-1?"": CBase64::Decode(m_oTemplateList[sender_info->TemplateID].Configuation));
	
	return true;
}


bool Sloong::CServerManage::ProcessHandler(Functions func, string sender, CDataTransPackage* pack)
{
	Json::Reader reader;
	Json::Value root;
	auto jreq = pack->GetRecvMessage();
	if (!reader.parse(jreq, root))
	{
		pack->ResponsePackage(ResultType::Error, CUniversal::Format("Parser json[% s] error.", jreq));
		return true;
	}
	if (root["Function"].isNull())
	{
		pack->ResponsePackage(ResultType::Error, "Request no set [Function] node.");
		return true;
	}
	auto function = root["Function"].asString();
	if(!m_listFuncHandler.exist(function))
	{
		pack->ResponsePackage(ResultType::Error, CUniversal::Format("Function [%s] no handler.",function));
		return true;
	}

	return m_listFuncHandler[function](sender,pack);
}


bool Sloong::CServerManage::AddTemplateHandler(const Json::Value& jRequest,CDataTransPackage* pack)
{
	if(jRequest["Name"].isNull()||jRequest["Replicas"].isNull()||jRequest["Configuation"].isNull())
	{
		pack->ResponsePackage(ResultType::Error, "The required parameter is null.");
		return true;
	}

	TemplateItem item;
	item.Name = jRequest["Name"].asString();
	item.Note = jRequest["Note"].asString();
	item.Replicas = jRequest["Replicas"].asInt();
	item.Configuation = jRequest["Configuation"].asString();
	int id = 0;<commit_msg>Fix the request error.<commit_after>#include "servermanage.h"

#include "utility.h"
#include "NetworkEvent.hpp"
#include "univ/Base64.h"
#include <jsoncpp/json/json.h>
using namespace Sloong::Events;


CResult Sloong::CServerManage::Initialize( int template_id )
{
	m_listFuncHandler["AddTemplate"]=std::bind(&CServerManage::AddTemplateHandler, this, std::placeholders::_1,std::placeholders::_2);
	m_listFuncHandler["DeleteTemplate"]=std::bind(&CServerManage::DeleteTemplateHandler, this, std::placeholders::_1,std::placeholders::_2);
	m_listFuncHandler["SetTemplate"]=std::bind(&CServerManage::SetTemplateHandler, this, std::placeholders::_1,std::placeholders::_2);
	m_listFuncHandler["QueryTemplate"]=std::bind(&CServerManage::QueryTemplateHandler, this, std::placeholders::_1,std::placeholders::_2);

	auto res = m_pAllConfig->Initialize("/data/configuation.db");
	if (res.IsFialed()) return res;

	// Initialize template list
	auto list = m_pAllConfig->GetTemplateList();
	for (auto item : list)
	{
		TemplateItem addItem(item);
		m_oTemplateList[addItem.ID] = addItem;
	}

	res = m_pAllConfig->GetTemplate(template_id);
	if (res.IsFialed())
		return CResult::Succeed();
	else
		return res;
}


int Sloong::CServerManage::SearchNeedCreateTemplate()
{
	for (auto item : m_oTemplateList)
	{
		if (item.second.Replicas == 0)
			continue;

		if (item.second.Created.size() == item.second.Replicas)
			continue;

		if (item.second.Created.size() == 0)
			return item.first;
	}

	for (auto item : m_oTemplateList)
	{
		if (item.second.Replicas == 0)
			continue;

		if (item.second.Created.size() == item.second.Replicas)
			continue;

		if (item.second.Created.size() < item.second.Replicas)
			return item.first;
	}
	return -1;
}


bool Sloong::CServerManage::RegisterServerHandler(Functions func, string sender, CDataTransPackage* pack)
{
	auto index = SearchNeedCreateTemplate();
	if (index == -1)
	{
		pack->ResponsePackage(ResultType::Retry,"Retry");
		return true;
	}

	if( sender.length() == 0 )
	{
		sender = CUtility::GenUUID();
	}

	auto sender_info = m_oServerList.try_get(sender);
	if( sender_info == nullptr)
	{
		ServerItem item;
		item.Address = pack->GetSocketIP();
		item.Port = pack->GetSocketPort();
		item.UUID = sender;
		m_oServerList[sender] = item;
		m_pLog->Verbos(CUniversal::Format("Module[%s:%d] regist to system. Allocating uuid [%s].", item.Address, item.Port, item.UUID));
		sender_info = m_oServerList.try_get(sender);
	}

	if( sender_info == nullptr)
	{
		pack->ResponsePackage(ResultType::Error,"Add server info to ServerList fialed.");
		return true;
	}
	
	auto tpl = m_oTemplateList[index];
	sender_info->TemplateName = tpl.Name;
	sender_info->TemplateID = tpl.ID;
	m_pLog->Verbos(CUniversal::Format("Allocating module[%s] Type to [%s]", sender_info->UUID, sender_info->TemplateName));
	pack->ResponsePackage(sender_info->UUID,sender_info->TemplateID==-1?"": CBase64::Decode(m_oTemplateList[sender_info->TemplateID].Configuation));
	
	return true;
}


bool Sloong::CServerManage::ProcessHandler(Functions func, string sender, CDataTransPackage* pack)
{
	Json::Reader reader;
	Json::Value jReq;
	auto str_req = pack->GetRecvMessage();
	if (!reader.parse(str_req, jReq))
	{
		pack->ResponsePackage(ResultType::Error, CUniversal::Format("Parser json[% s] error.", str_req));
		return true;
	}
	if (jReq["Function"].isNull())
	{
		pack->ResponsePackage(ResultType::Error, "Request no set [Function] node.");
		return true;
	}
	auto function = jReq["Function"].asString();
	if(!m_listFuncHandler.exist(function))
	{
		pack->ResponsePackage(ResultType::Error, CUniversal::Format("Function [%s] no handler.",function));
		return true;
	}

	return m_listFuncHandler[function](jReq,pack);
}


bool Sloong::CServerManage::AddTemplateHandler(const Json::Value& jRequest,CDataTransPackage* pack)
{
	if(jRequest["Name"].isNull()||jRequest["Replicas"].isNull()||jRequest["Configuation"].isNull())
	{
		pack->ResponsePackage(ResultType::Error, "The required parameter is null.");
		return true;
	}

	TemplateItem item;
	item.Name = jRequest["Name"].asString();
	item.Note = jRequest["Note"].asString();
	item.Replicas = jRequest["Replicas"].asInt();
	item.Configuation = jRequest["Configuation"].asString();
	int id = 0;
{'fraction_non_alphanumeric': 0.16079867674858223, 'fraction_numerical': 0.004725897920604915, 'mean_word_length': 6.179813401187447, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '662671', 'n_tokens_mistral': 3042, 'n_tokens_neox': 2922, 'n_words': 502}