Commit 2f638ccc by guanghui.cui

短连接增加超时,异常情况返回数据判断,删除不用代码

parent 2f3d0d98
...@@ -725,17 +725,29 @@ std::string JsonModule::_convertToOrderOperationReponseJson(IN const char* json) ...@@ -725,17 +725,29 @@ std::string JsonModule::_convertToOrderOperationReponseJson(IN const char* json)
return buffer.GetString(); return buffer.GetString();
} }
bool JsonModule::getOdsResponseData(int status_code, const std::string &msg, std::string &result) bool JsonModule::getOdsResponseData(int status_code, const std::string &msg, IN const std::string& odsData, std::string &result)
{ {
/* rapidjson::Document document;
document.Parse(odsData.c_str());
std::string order_id, channel;
int status;
if (!document.HasMember("pk_id"))
{ {
"fm_cmd" : 2, LOG(INFO) << "Don't have needed parames";
"order_id" : "xxxxx", return false;
"status" : 1, }
"status_code" : 100,
"msg" : "接收订单成功" int pk_id = GetJsonIntSafe(document, "pk_id");
rapidjson::Value& orderContent_obj = document["orderContent"];
if (orderContent_obj.HasMember("orders")) {
rapidjson::Value& orders_obj = orderContent_obj["orders"];
order_id = GetJsonStringSafe(orders_obj, "orderId");
status = atoi(GetJsonStringSafe(orders_obj, "status"));
} }
*/ channel = GetJsonStringSafe(document, "channel");
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
writer.StartObject(); writer.StartObject();
...@@ -744,10 +756,13 @@ bool JsonModule::getOdsResponseData(int status_code, const std::string &msg, std ...@@ -744,10 +756,13 @@ bool JsonModule::getOdsResponseData(int status_code, const std::string &msg, std
writer.Int(2); writer.Int(2);
writer.Key("order_id"); writer.Key("order_id");
writer.String(""); writer.String(order_id.c_str());
writer.Key("channel");
writer.String(channel.c_str());
writer.Key("status"); writer.Key("status");
writer.Int(0); writer.Int(status);
writer.Key("status_code"); writer.Key("status_code");
writer.Int(status_code); writer.Int(status_code);
...@@ -755,6 +770,9 @@ bool JsonModule::getOdsResponseData(int status_code, const std::string &msg, std ...@@ -755,6 +770,9 @@ bool JsonModule::getOdsResponseData(int status_code, const std::string &msg, std
writer.Key("msg"); writer.Key("msg");
writer.String(msg.c_str()); writer.String(msg.c_str());
writer.Key("pk_id");
writer.Int(pk_id);
writer.EndObject(); writer.EndObject();
result = buffer.GetString(); result = buffer.GetString();
......
#ifndef JSON_MODULE_H #ifndef JSON_MODULE_H
#define JSON_MODULE_H #define JSON_MODULE_H
#include <stdlib.h> #include <stdlib.h>
...@@ -74,7 +74,7 @@ public: ...@@ -74,7 +74,7 @@ public:
* 重载:[1]POS返回数据[2]订单数据[3]ods发送数据[4]转换后数据 * 重载:[1]POS返回数据[2]订单数据[3]ods发送数据[4]转换后数据
* 返回:是否转换成功 * 返回:是否转换成功
* */ * */
bool getOdsResponseData(IN int status_code, IN const std::string& msg, OUT std::string& result); bool getOdsResponseData(IN int status_code, IN const std::string& msg, IN const std::string& odsData, OUT std::string& result);
bool getOdsResponseData(IN const std::string& posResponse, IN const std::string& orderData,IN const std::string& odsData, OUT std::string& result); bool getOdsResponseData(IN const std::string& posResponse, IN const std::string& orderData,IN const std::string& odsData, OUT std::string& result);
/* 功能:转换POS数据格式到中台数据格式 /* 功能:转换POS数据格式到中台数据格式
......
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include "JsonModule.h" #include "JsonModule.h"
#include "SocketModule.h" #include "SocketModule.h"
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
INITIALIZE_EASYLOGGINGPP INITIALIZE_EASYLOGGINGPP
#define VERSION "1.0.5 Beta1" //版本号 #define VERSION "1.0.5 Beta2" //版本号
std::string g_init_data; std::string g_init_data;
std::string g_init_data_ods_back; std::string g_init_data_ods_back;
...@@ -220,39 +220,6 @@ void* listen_pos_func(void* arg) ...@@ -220,39 +220,6 @@ void* listen_pos_func(void* arg)
#endif // WIN32 #endif // WIN32
} }
// //发送POS订单失败,重新发送线程
// void* retry_send_pos_func(void* arg)
// {
// bRetryThreadRunning=true;
// JsonModule jsonTool;
// std::vector<orderSendFailedObj>::iterator Iter;
// for(Iter = vecFailedOrders.begin(); Iter != vecFailedOrders.end();) {
// Iter = vecFailedOrders.begin(); //一直发送第一个,直到发送成功
// std::string back_json;
// std::string ods_json=(*Iter).order_json;
// std::string order_json;
// if( jsonTool.convertDataOds2Pos(ods_json, order_json) ){
// if(order_send_to_pos(order_json,ods_json,back_json)){
// char lpSql[200] = {0};
// sprintf(lpSql, "delete from fmOrderFailed where id = %lld",(*Iter).timestamp);
// LOG(INFO)<<"sql:"<<lpSql;
// if(!sqlite.remove(lpSql)){
// LOG(ERROR) << "remove failed:"<<lpSql;
// }
// vecFailedOrders.erase(Iter); //Iter为删除元素的下一个元素的迭代器
// }
// }
// sleep(1); //等待1s,再次尝试发送
// }
// LOG(INFO)<<"retry_send_pos_func thread done";
// bRetryThreadRunning=false;
// pthread_detach(pthread_self());
// }
int main(int argc,char *argv[]) int main(int argc,char *argv[])
{ {
if (2 == argc && 0 == strcmp("-v", argv[1])) { if (2 == argc && 0 == strcmp("-v", argv[1])) {
...@@ -368,21 +335,6 @@ int main(int argc,char *argv[]) ...@@ -368,21 +335,6 @@ int main(int argc,char *argv[])
} }
} }
break; break;
// //如果连接非法,跳出循环重新连接
// if( !ods.isValid() ){
// break;
// }
// //检测是否有发送失败的订单,如果有的话,启动线程,先发送原先失败的订单
// sqlite.query("select * from fmOrderFailed",vecFailedOrders);
// LOG(INFO)<<"vecFailedOrders size:"<<vecFailedOrders.size();
// if(vecFailedOrders.size()>0){
// pthread_t retry_thread_id;
// if(pthread_create(&retry_thread_id,NULL,retry_send_pos_func,NULL))
// LOG(INFO) << "create retry_send_pos_func thread failed";
// }
} }
} }
else{ else{
...@@ -419,22 +371,13 @@ int main(int argc,char *argv[]) ...@@ -419,22 +371,13 @@ int main(int argc,char *argv[])
{ {
LOG(INFO) << "----------- PLUGIN ===>> POS ------------"; LOG(INFO) << "----------- PLUGIN ===>> POS ------------";
LOG(INFO) << pushPosData; LOG(INFO) << pushPosData;
order_send_to_pos(pushPosData,odsPushData,responseData); if (!order_send_to_pos(pushPosData, odsPushData, responseData)) {
// if(!order_send_to_pos(pushPosData,odsPushData,responseData)){ jsonTool.getOdsResponseData(101, "send to pos failed or receive data illegal!", odsPushData, responseData);
// write_failed_order(odsPushData); }
// if(!bRetryThreadRunning){ //如果线程没有在运行
// pthread_t retry_thread_id;
// /*创建 retry send 线程*/
// if(pthread_create(&retry_thread_id,NULL,retry_send_pos_func,NULL))
// LOG(INFO) << "create retry_send_pos_func thread failed";
// }
// }
}else }else
{ {
jsonTool.getOdsResponseData(101, "convert data to [POS] format failed!", responseData); jsonTool.getOdsResponseData(101, "convert data to [POS] format failed!", odsPushData, responseData);
} }
// TODO待加入重试机制
LOG(INFO) << "PLUGIN ===>> ODS:"<<responseData.data(); LOG(INFO) << "PLUGIN ===>> ODS:"<<responseData.data();
//test //test
//responseData="{\"fm_cmd\": 2,\"order_id\" : \"1234\",\"status\" : 1,\"channel\" : \"1234\",\"status_code\" : 100,\"msg\" : \"\"}"; //responseData="{\"fm_cmd\": 2,\"order_id\" : \"1234\",\"status\" : 1,\"channel\" : \"1234\",\"status_code\" : 100,\"msg\" : \"\"}";
...@@ -476,6 +419,7 @@ bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT s ...@@ -476,6 +419,7 @@ bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT s
std::string tmp="{\"status_code\": 102, \"msg\": \"connect pos failed!\"}"; std::string tmp="{\"status_code\": 102, \"msg\": \"connect pos failed!\"}";
if( pos.doConnect(pos_listen_port, pos_ip.c_str()) ) if( pos.doConnect(pos_listen_port, pos_ip.c_str()) )
{ {
pos.setSocketTimeout(60); //设置超时
if( pos.write(order_json.c_str()) ) if( pos.write(order_json.c_str()) )
{ {
char tmpBuf[FM_BUF_SIZE] = {0}; char tmpBuf[FM_BUF_SIZE] = {0};
...@@ -506,24 +450,6 @@ bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT s ...@@ -506,24 +450,6 @@ bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT s
return rlt; return rlt;
} }
// void write_failed_order(IN std::string &order_json)
// {
// orderSendFailedObj orderFailedObj;
// orderFailedObj.timestamp=timestamps_milliseconds();
// orderFailedObj.order_json=order_json;
// vecFailedOrders.push_back(orderFailedObj);
// LOG(INFO)<<"---------------create timestamp:"<<orderFailedObj.timestamp<<" vector size:"<<vecFailedOrders.size();
// char* lpSql = new char[BUF_SIZE];
// sprintf(lpSql, "insert into fmOrderFailed(id,msg) values(%lld,'%s')",orderFailedObj.timestamp,orderFailedObj.order_json.c_str());
// if(!sqlite.insert(lpSql)){
// LOG(ERROR) << "write sqlite failed:"<<lpSql;
// }
// delete[] lpSql;
// }
//实现逻辑:向本地socket发送一个命令,程序接到命令后自杀,如果没有自杀成功,根据名称杀掉 //实现逻辑:向本地socket发送一个命令,程序接到命令后自杀,如果没有自杀成功,根据名称杀掉
void kill_origin_process() void kill_origin_process()
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment