Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
familyMart_takeaway
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
guanghui.cui
familyMart_takeaway
Commits
2f638ccc
Commit
2f638ccc
authored
Jun 20, 2018
by
guanghui.cui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
短连接增加超时,异常情况返回数据判断,删除不用代码
parent
2f3d0d98
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
93 deletions
+37
-93
src/JsonModule.cpp
+28
-10
src/JsonModule.h
+2
-2
src/main.cpp
+7
-81
No files found.
src/JsonModule.cpp
View file @
2f638ccc
...
...
@@ -725,17 +725,29 @@ std::string JsonModule::_convertToOrderOperationReponseJson(IN const char* json)
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,
"order_id" : "xxxxx",
"status" : 1,
"status_code" : 100,
"msg" : "接收订单成功"
LOG
(
INFO
)
<<
"Don't have needed parames"
;
return
false
;
}
*/
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
::
Writer
<
rapidjson
::
StringBuffer
>
writer
(
buffer
);
writer
.
StartObject
();
...
...
@@ -744,10 +756,13 @@ bool JsonModule::getOdsResponseData(int status_code, const std::string &msg, std
writer
.
Int
(
2
);
writer
.
Key
(
"order_id"
);
writer
.
String
(
""
);
writer
.
String
(
order_id
.
c_str
());
writer
.
Key
(
"channel"
);
writer
.
String
(
channel
.
c_str
());
writer
.
Key
(
"status"
);
writer
.
Int
(
0
);
writer
.
Int
(
status
);
writer
.
Key
(
"status_code"
);
writer
.
Int
(
status_code
);
...
...
@@ -755,6 +770,9 @@ bool JsonModule::getOdsResponseData(int status_code, const std::string &msg, std
writer
.
Key
(
"msg"
);
writer
.
String
(
msg
.
c_str
());
writer
.
Key
(
"pk_id"
);
writer
.
Int
(
pk_id
);
writer
.
EndObject
();
result
=
buffer
.
GetString
();
...
...
src/JsonModule.h
View file @
2f638ccc
#ifndef JSON_MODULE_H
#
ifndef
JSON_MODULE_H
#define JSON_MODULE_H
#include <stdlib.h>
...
...
@@ -74,7 +74,7 @@ public:
* 重载:[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
);
/* 功能:转换POS数据格式到中台数据格式
...
...
src/main.cpp
View file @
2f638ccc
#include <iostream>
#
include
<
iostream
>
#include <fstream>
#include "JsonModule.h"
#include "SocketModule.h"
...
...
@@ -18,7 +18,7 @@
INITIALIZE_EASYLOGGINGPP
#define VERSION "1.0.5 Beta
1
" //版本号
#define VERSION "1.0.5 Beta
2
" //版本号
std
::
string
g_init_data
;
std
::
string
g_init_data_ods_back
;
...
...
@@ -220,39 +220,6 @@ void* listen_pos_func(void* arg)
#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
[])
{
if
(
2
==
argc
&&
0
==
strcmp
(
"-v"
,
argv
[
1
]))
{
...
...
@@ -368,21 +335,6 @@ int main(int argc,char *argv[])
}
}
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
{
...
...
@@ -419,22 +371,13 @@ int main(int argc,char *argv[])
{
LOG
(
INFO
)
<<
"----------- PLUGIN ===>> POS ------------"
;
LOG
(
INFO
)
<<
pushPosData
;
order_send_to_pos
(
pushPosData
,
odsPushData
,
responseData
);
// if(!order_send_to_pos(pushPosData,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";
// }
// }
if
(
!
order_send_to_pos
(
pushPosData
,
odsPushData
,
responseData
))
{
jsonTool
.
getOdsResponseData
(
101
,
"send to pos failed or receive data illegal!"
,
odsPushData
,
responseData
);
}
}
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
();
//test
//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
std
::
string
tmp
=
"{
\"
status_code
\"
: 102,
\"
msg
\"
:
\"
connect pos failed!
\"
}"
;
if
(
pos
.
doConnect
(
pos_listen_port
,
pos_ip
.
c_str
())
)
{
pos
.
setSocketTimeout
(
60
);
//设置超时
if
(
pos
.
write
(
order_json
.
c_str
())
)
{
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
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发送一个命令,程序接到命令后自杀,如果没有自杀成功,根据名称杀掉
void
kill_origin_process
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment