diff --git a/src/FlowControl.h b/src/FlowControl.h index f64bf82..abd6a7d 100644 --- a/src/FlowControl.h +++ b/src/FlowControl.h @@ -1,6 +1,6 @@ // ********************************************************************** // This file was generated by a TARS parser! -// TARS version 2.0.0. +// TARS version 2.4.2. // ********************************************************************** #ifndef __FLOWCONTROL_H_ @@ -228,11 +228,20 @@ namespace Base UniAttribute tarsAttr; tarsAttr.setVersion(current->getRequestVersion()); tarsAttr.put("", _ret); + tarsAttr.put("tars_ret", _ret); vector sTupResponseBuffer; tarsAttr.encode(sTupResponseBuffer); current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); } + else if (current->getRequestVersion() == JSONVERSION) + { + tars::JsonValueObjPtr _p = new tars::JsonValueObj(); + _p->value["tars_ret"] = tars::JsonOutput::writeJson(_ret); + vector sJsonResponseBuffer; + tars::TC_Json::writeValue(_p, sJsonResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sJsonResponseBuffer); + } else { tars::TarsOutputStream _os; @@ -282,11 +291,12 @@ namespace Base tars::Int32 _ret = report(flow,ip, _current); if(_current->isResponse()) { - if (_current->getRequestVersion() == TUPVERSION ) + if (_current->getRequestVersion() == TUPVERSION) { UniAttribute tarsAttr; tarsAttr.setVersion(_current->getRequestVersion()); tarsAttr.put("", _ret); + tarsAttr.put("tars_ret", _ret); tarsAttr.encode(_sResponseBuffer); } else if (_current->getRequestVersion() == JSONVERSION) diff --git a/src/httpproxy/StationManager.cpp b/src/httpproxy/StationManager.cpp index 0200d4a..eac19f7 100644 --- a/src/httpproxy/StationManager.cpp +++ b/src/httpproxy/StationManager.cpp @@ -1,15 +1,14 @@ #include "StationManager.h" -#include "util/tc_mysql.h" -#include "util/tc_md5.h" #include "HttpProxy.h" - +#include "util/tc_md5.h" +#include "util/tc_mysql.h" StationManager::StationManager() { _isLoading = false; } -bool StationManager::load(TC_Config& conf) +bool StationManager::load(TC_Config &conf) { _conf = conf; // 这里用个状态位进行简单处理,不是很严谨 @@ -28,13 +27,13 @@ bool StationManager::load(TC_Config& conf) if (ret) { - flushObj(); + flushObj(); } return ret; } -bool StationManager::loadHttp(TC_Config& conf) +bool StationManager::loadHttp(TC_Config &conf) { _conf = conf; // 这里用个状态位进行简单处理,不是很严谨 @@ -51,9 +50,11 @@ bool StationManager::loadHttp(TC_Config& conf) if (ret) { - flushObj(); + flushObj(); } + return ret; } + bool StationManager::loadComm() { bool ret = loadBlackList(); @@ -61,7 +62,7 @@ bool StationManager::loadComm() return ret; } -bool StationManager::init(TC_Config& conf) +bool StationManager::init(TC_Config &conf) { map m = conf.getDomainMap("/main/db"); TC_DBConf tcDBConf; @@ -71,7 +72,7 @@ bool StationManager::init(TC_Config& conf) return load(conf); } -bool StationManager::loadHttpRouterConf(vector& paramList) +bool StationManager::loadHttpRouterConf(vector ¶mList) { try { @@ -95,7 +96,7 @@ bool StationManager::loadHttpRouterConf(vector& paramList) return true; } - catch(const std::exception& e) + catch (const std::exception &e) { TLOGERROR(e.what() << endl); } @@ -115,8 +116,8 @@ bool StationManager::loadRouter() { RouterParam rp; rp.id = TC_Common::strto(data[i]["f_id"]); - rp.serverName = TC_Common::trim(data[i]["f_server_name"]); - rp.location = TC_Common::trim(data[i]["f_path_rule"]); + rp.serverName = TC_Common::trim(data[i]["f_server_name"]); + rp.location = TC_Common::trim(data[i]["f_path_rule"]); rp.proxyPass = TC_Common::trim(data[i]["f_proxy_pass"]); rp.stationId = TC_Common::trim(data[i]["f_station_id"]); if (rp.location.empty() || rp.proxyPass.empty()) @@ -124,7 +125,7 @@ bool StationManager::loadRouter() TLOGERROR("error db conf:" << rp.id << "|" << rp.serverName << "|" << rp.location << "|" << rp.proxyPass << "|" << rp.stationId << endl); continue; } - + paramList.push_back(rp); } } @@ -136,7 +137,8 @@ bool StationManager::loadRouter() // 合并配置文件内容 loadHttpRouterConf(paramList); - return Router->reload(paramList);; + return Router->reload(paramList); + ; } void StationManager::terminate() @@ -167,7 +169,7 @@ bool StationManager::loadMonitor() TLOGERROR("exception:" << e.what() << endl); } - return false; + return false; } bool StationManager::loadBlackList() @@ -177,16 +179,16 @@ bool StationManager::loadBlackList() string sql = "select f_station_id, f_ip from t_blacklist where f_valid = 1 limit 10000"; TC_Mysql::MysqlData data = _mysql.queryRecord(sql); TLOGDEBUG(sql << " ===> result size=" << data.size() << endl); - + unordered_map> blackList; - unordered_map> blackListPat; + unordered_map> blackListPat; for (size_t i = 0; i < data.size(); i++) { string stationId = TC_Common::trim(data[i]["f_station_id"]); string ip = TC_Common::trim(data[i]["f_ip"]); if (ip.find("*") != string::npos) { - blackListPat[stationId].insert(ip); + blackListPat[stationId].insert(ip); } else { @@ -206,7 +208,7 @@ bool StationManager::loadBlackList() TLOGERROR("exception:" << e.what() << endl); } - return false; + return false; } bool StationManager::loadWhiteList() { @@ -216,14 +218,14 @@ bool StationManager::loadWhiteList() TC_Mysql::MysqlData data = _mysql.queryRecord(sql); TLOGDEBUG(sql << " ===> result size=" << data.size() << endl); unordered_map> whiteList; - unordered_map> whiteListPat; + unordered_map> whiteListPat; for (size_t i = 0; i < data.size(); i++) { string stationId = TC_Common::trim(data[i]["f_station_id"]); string ip = TC_Common::trim(data[i]["f_ip"]); if (ip.find("*") != string::npos) { - whiteListPat[stationId].insert(ip); + whiteListPat[stationId].insert(ip); } else { @@ -243,32 +245,32 @@ bool StationManager::loadWhiteList() TLOGERROR("exception:" << e.what() << endl); } - return false; + return false; } void StationManager::run() { - while(!_terminate) + while (!_terminate) { try { flushObj(); } - catch(exception &ex) + catch (exception &ex) { TLOGERROR("exception:" << ex.what() << endl); } - catch(...) + catch (...) { TLOGERROR("exception unknown error." << endl); } TC_ThreadLock::Lock lock(*this); timedWait(1000 * 30); - } + } } -string StationManager::getMonitorUrl(const string& stationId) +string StationManager::getMonitorUrl(const string &stationId) { TC_ThreadRLock r(_rwLock); auto it = _stationMonitorUrl.find(stationId); @@ -292,7 +294,7 @@ string StationManager::getMonitorUrl(const string& stationId) // return TC_MD5::md5str(ss.str()); // } -string StationManager::genAddrVer(const vector& addrList) +string StationManager::genAddrVer(const vector &addrList) { stringstream ss; for (auto itv = addrList.begin(); itv != addrList.end(); ++itv) @@ -303,7 +305,7 @@ string StationManager::genAddrVer(const vector& addrList) return TC_MD5::md5str(ss.str()); } -void StationManager::addObjUpstream(const string& obj) +void StationManager::addObjUpstream(const string &obj) { TC_ThreadWLock w(_rwLock); _stationObj.insert(obj); @@ -357,9 +359,9 @@ void StationManager::flushObj() _ObjProxy[*it].ver = ver; } - HttpProxyFactory::getInstance()->getHttpProxy(*it)->setAddr(addrList, ver); + HttpProxyFactory::getInstance()->getHttpProxy(*it)->setAddr(addrList, ver); } - else + else { TLOGERROR(*it << ", has no valid tars enpoint." << endl); } @@ -389,8 +391,8 @@ bool StationManager::isInBlackList(const string &stationId, const string &ip) } } } - - return false; + + return false; } bool StationManager::checkWhiteList(const string &stationId, const string &ip) @@ -421,8 +423,8 @@ bool StationManager::checkWhiteList(const string &stationId, const string &ip) } } } - - return ret; + + return ret; } bool StationManager::loadUpstream() @@ -439,7 +441,7 @@ bool StationManager::loadUpstream() UpstreamInfo info; info.addr = TC_Common::trim(data[i]["f_addr"]); info.weight = TC_Common::strto(data[i]["f_weight"]); - info.fusingOnOff = (TC_Common::strto(data[i]["f_fusing_onoff"]) == 1 ? true:false); + info.fusingOnOff = (TC_Common::strto(data[i]["f_fusing_onoff"]) == 1 ? true : false); proxy[TC_Common::trim(data[i]["f_upstream"])].addrList.push_back(info); //proxy[TC_Common::trim(data[i]["f_upstream"])].addrList.push_back(make_pair(TC_Common::trim(data[i]["f_addr"]), TC_Common::strto(data[i]["f_weight"]))); } @@ -447,7 +449,7 @@ bool StationManager::loadUpstream() for (auto it = proxy.begin(); it != proxy.end(); ++it) { it->second.ver = genAddrVer(it->second.addrList); - HttpProxyFactory::getInstance()->getHttpProxy(it->first)->setAddr(it->second.addrList, it->second.ver); + HttpProxyFactory::getInstance()->getHttpProxy(it->first)->setAddr(it->second.addrList, it->second.ver); } return true; diff --git a/src/tupproxy/TupCallback.cpp b/src/tupproxy/TupCallback.cpp index bb91e26..812d7c9 100755 --- a/src/tupproxy/TupCallback.cpp +++ b/src/tupproxy/TupCallback.cpp @@ -41,7 +41,7 @@ int TupCallback::onDispatch(ReqMessagePtr msg) } else if (getType() == "json") { - if (!msg->response->sBuffer.empty()) + //if (!msg->response->sBuffer.empty()) { ReportHelper::reportProperty("response_iVersion_json"); doResponse_json(msg->response); @@ -287,10 +287,10 @@ void TupCallback::doResponseException(int ret, const vector &buffer) void TupCallback::handleResponse() { - if (_rspBuffer.size() == 0) - { - return; - } + // if (_rspBuffer.size() == 0) + // { + // return; + // } TLOGDEBUG("rsp size:" << _rspBuffer.size() << endl); bool bGzipOk = !_stParam.pairAcceptZip.first.empty(); diff --git a/test/HelloServer/Hello.h b/test/HelloServer/Hello.h index 02e6e97..7bceb11 100644 --- a/test/HelloServer/Hello.h +++ b/test/HelloServer/Hello.h @@ -1,6 +1,6 @@ // ********************************************************************** // This file was generated by a TARS parser! -// TARS version 2.0.0. +// TARS version 2.4.2. // ********************************************************************** #ifndef __HELLO_H_ @@ -344,12 +344,22 @@ namespace TestApp UniAttribute tarsAttr; tarsAttr.setVersion(current->getRequestVersion()); tarsAttr.put("", _ret); + tarsAttr.put("tars_ret", _ret); tarsAttr.put("r", r); vector sTupResponseBuffer; tarsAttr.encode(sTupResponseBuffer); current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); } + else if (current->getRequestVersion() == JSONVERSION) + { + tars::JsonValueObjPtr _p = new tars::JsonValueObj(); + _p->value["r"] = tars::JsonOutput::writeJson(r); + _p->value["tars_ret"] = tars::JsonOutput::writeJson(_ret); + vector sJsonResponseBuffer; + tars::TC_Json::writeValue(_p, sJsonResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sJsonResponseBuffer); + } else { tars::TarsOutputStream _os; @@ -369,12 +379,22 @@ namespace TestApp UniAttribute tarsAttr; tarsAttr.setVersion(current->getRequestVersion()); tarsAttr.put("", _ret); + tarsAttr.put("tars_ret", _ret); tarsAttr.put("sRsp", sRsp); vector sTupResponseBuffer; tarsAttr.encode(sTupResponseBuffer); current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); } + else if (current->getRequestVersion() == JSONVERSION) + { + tars::JsonValueObjPtr _p = new tars::JsonValueObj(); + _p->value["sRsp"] = tars::JsonOutput::writeJson(sRsp); + _p->value["tars_ret"] = tars::JsonOutput::writeJson(_ret); + vector sJsonResponseBuffer; + tars::TC_Json::writeValue(_p, sJsonResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sJsonResponseBuffer); + } else { tars::TarsOutputStream _os; @@ -431,11 +451,12 @@ namespace TestApp tars::Int32 _ret = testHello(index,s,r, _current); if(_current->isResponse()) { - if (_current->getRequestVersion() == TUPVERSION ) + if (_current->getRequestVersion() == TUPVERSION) { UniAttribute tarsAttr; tarsAttr.setVersion(_current->getRequestVersion()); tarsAttr.put("", _ret); + tarsAttr.put("tars_ret", _ret); tarsAttr.put("r", r); tarsAttr.encode(_sResponseBuffer); } @@ -485,11 +506,12 @@ namespace TestApp tars::Int32 _ret = testSHello(sReq,sRsp, _current); if(_current->isResponse()) { - if (_current->getRequestVersion() == TUPVERSION ) + if (_current->getRequestVersion() == TUPVERSION) { UniAttribute tarsAttr; tarsAttr.setVersion(_current->getRequestVersion()); tarsAttr.put("", _ret); + tarsAttr.put("tars_ret", _ret); tarsAttr.put("sRsp", sRsp); tarsAttr.encode(_sResponseBuffer); } diff --git a/test/HttpClient/main.cpp b/test/HttpClient/main.cpp index 92275f5..92127d2 100644 --- a/test/HttpClient/main.cpp +++ b/test/HttpClient/main.cpp @@ -54,7 +54,7 @@ void wupSyncCall(int excut_num) int ret = stHttpReq.doRequest(stHttpRsp, 3000); - // cout << ret << ":" << recvLen << endl; + cout << ret << ":" << stHttpRsp.getContent().size() << endl; UniPacket<> rsp; @@ -89,6 +89,7 @@ class AsyncWupHttpCallback : public TC_HttpAsync::RequestCallback string retStr = ""; int ret = rsp.getTarsResultCode(); + cout << "tars code:" << ret << endl; rsp.get("sRsp", retStr); @@ -109,7 +110,7 @@ TC_HttpAsync ast; void wupAsyncCall(int excut_num) { - uint64_t i = TC_Common::now2ms(); + //uint64_t i = TC_Common::now2ms(); string buffer(param.buffersize, 'a'); @@ -175,7 +176,7 @@ class AsyncHttpCallback : public TC_HttpAsync::RequestCallback void httpAsyncCall(int excut_num) { - uint64_t i = TC_Common::now2ms(); + //uint64_t i = TC_Common::now2ms(); string buffer(param.buffersize, 'a');