62 lines
2.0 KiB
C++
62 lines
2.0 KiB
C++
|
#include "tinypb_dispatcher.hpp"
|
||
|
#include "logger.hpp"
|
||
|
#include "tinypb_data.hpp"
|
||
|
#include "error_code.hpp"
|
||
|
#include <sstream>
|
||
|
|
||
|
namespace tinyrpc {
|
||
|
TinypbDispatcher::TinypbDispatcher() {}
|
||
|
// TODO
|
||
|
TinypbDispatcher::~TinypbDispatcher() {
|
||
|
|
||
|
}
|
||
|
void TinypbDispatcher::dispatcher(TcpConnection& conn, AbstractData& data, AbstractData& resp) {
|
||
|
logger() << "dispatcher";
|
||
|
TinypbData& pbdata = dynamic_cast<TinypbData&>(data);
|
||
|
TinypbData& respond = dynamic_cast<TinypbData&>(resp);
|
||
|
std::string service_name;
|
||
|
std::string method_name;
|
||
|
|
||
|
respond.service_full_name = pbdata.service_full_name;
|
||
|
respond.msg_req = pbdata.msg_req;
|
||
|
bool ret = parseServiceFullName(pbdata.service_full_name, service_name, method_name);
|
||
|
if(ret == false) {
|
||
|
respond.err_code = ERROR_PARSE_SERVICE_NAME;
|
||
|
std::stringstream ss;
|
||
|
ss << "not found service_name:[" << service_name << "]";
|
||
|
respond.err_info = ss.str();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
auto it = m_service_map.find(service_name);
|
||
|
|
||
|
if (it == m_service_map.end() || !((*it).second)) {
|
||
|
respond.err_code = ERROR_SERVICE_NOT_FOUND;
|
||
|
std::stringstream ss;
|
||
|
ss << "not found service_name:[" << service_name << "]";
|
||
|
respond.err_info = ss.str();
|
||
|
return;
|
||
|
|
||
|
}
|
||
|
|
||
|
Service* service = it->second;
|
||
|
|
||
|
const google::protobuf::MethodDescriptor* method = service->GetDescriptor()->FindMethodByName(method_name);
|
||
|
}
|
||
|
|
||
|
bool TinypbDispatcher::parseServiceFullName(const std::string& name, std::string& serviceName, std::string& methodName) {
|
||
|
if(name.empty()) return false;
|
||
|
|
||
|
auto pos = name.find(".");
|
||
|
if(pos == std::string::npos) return false;
|
||
|
serviceName = name.substr(0, pos);
|
||
|
methodName = name.substr(pos + 1);
|
||
|
|
||
|
logger() << "serviceName=" << serviceName;
|
||
|
logger() << "methodName=" << methodName;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|