-
Notifications
You must be signed in to change notification settings - Fork 13
/
draft.cpp
253 lines (219 loc) · 7.27 KB
/
draft.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <converter.h>
#include <json/json.h>
#include <comhelper.h>
// 配置数据
static Configure config;
/// 读取配置数据
void loadConfig() {
config.load();
printf("FrontAddress=%s\n",config.FrontAddress);
printf("BrokerID=%s\n",config.BrokerID);
printf("UserID=%s\n",config.UserID);
printf("Password=%s\n",config.Password);
printf("RequestPipe=%s\n",config.RequestPipe);
printf("PushbackPipe=%s\n",config.PushbackPipe);
printf("PublishPipe=%s\n",config.PublishPipe);
}
void test01() {
zmq::context_t * pContext = new zmq::context_t(1);
zmq::socket_t * pReceiver = new zmq::socket_t(*pContext, ZMQ_PULL);
pReceiver->connect(config.PushbackPipe);
//pReceiver->connect("ipc://ipc/pushback");
//zmq::socket_t * pSender = new zmq::socket_t(*pContext, ZMQ_PUSH);
////pSender->bind("tcp://*:10000");
//pSender->bind("ipc://pushback");
/*
while(1){
std::cout << "-----------begin-----------" << std::endl;
do {
std::cout << "接收到来自[tcp://localhost:10000]的消息:" << s_recv(*pReceiver) << std::endl;
} while(s_more(*pReceiver));
std::cout << "------------end------------" << std::endl;
sleep(5);
}
*/
RequestMessage requestMessage;
//requestMessage.send(*pSender);
while(1) {
try {
requestMessage.recv(*pReceiver);
std::cout << "requestMessage.header=" << requestMessage.header << std::endl;
std::cout << "requestMessage.apiName=" << requestMessage.apiName << std::endl;
std::cout << "requestMessage.reqInfo=" << requestMessage.reqInfo << std::endl;
std::cout << "requestMessage.metaData=" << requestMessage.metaData << std::endl;
} catch(std::exception & e) {
std::cout << e.what() << std::endl;
}
}
}
void test02() {
loadConfig();
CApiWrapper api(&config);
api.init();
}
void writeJson() {
using namespace std;
Json::Value root;
Json::Value arrayObj;
Json::Value item;
item["cpp"] = "jsoncpp";
item["java"] = "jsoninjava";
item["php"] = "support";
arrayObj.append(item);
root["name"] = "json";
root["array"] = arrayObj;
root.toStyledString();
std::string out = root.toStyledString();
std::cout << out << std::endl;
}
void readJson() {
using namespace std;
std::string strValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\",\"cpp1\":\"jxx\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}";
Json::Reader reader;
Json::Value value;
if (reader.parse(strValue, value))
{
std::string out = value["name"].asString();
//std::cout << out << std::endl;
const Json::Value arrayObj = value["array"];
for (int i=0; i<arrayObj.size(); i++) {
const Json::Value::Members memberNames = arrayObj[i].getMemberNames();
for(int j=0; j<memberNames.size(); j++) {
std::cout << memberNames[j] << std::endl;
}
}
//for (unsigned int i = 0; i < arrayObj.size(); i++)
//{
//if (!arrayObj[i].isMember("cpp"))
// continue;
//out = arrayObj[i].asString();
//std::cout << out;
//if (i != (arrayObj.size() - 1))
// std::cout << std::endl;
//std::cout << "1111" << std::endl;
//}
}
}
void readJsonDict() {
std::string strValue = "{\"a\":\"1\",\"b\":\"2\",\"c\":\"3\"}";
Json::Reader reader;
Json::Value value;
reader.parse(strValue, value);
std::cout << "value.isObject=" << value.isObject() << std::endl;
const Json::Value::Members memberNames = value.getMemberNames();
for(int i=0; i<memberNames.size(); i++) {
std::cout << memberNames[i] << ":" << value[memberNames[i]] << std::endl;
}
}
void test03() {
config.load();
CThostFtdcInstrumentField pInstrument;
CThostFtdcRspInfoField pRspInfo;
int nRequestID;
bool bIsLast;
CTraderHandler traderHandler = CTraderHandler(&config);
while(1) {
traderHandler.OnRspQryInstrument(&pInstrument,&pRspInfo,nRequestID,bIsLast);
//traderHandler.OnRspQryInstrument(0,&pRspInfo,nRequestID,bIsLast);
std::cout << "message send ..." << std::endl;
getchar();
}
}
void test04() {
std::string jsonString = "\
{\
\"ResponseMethod\" : \"ReqQryTradingAccount\",\
\"Parameters\" : {\
\"Data\" : {\
\"string\" : \"1234567890\",\
\"char\" : \"c\",\
\"int\" : 123456,\
\"double\" : 3.1415926\
}\
}\
}\
";
char str[10];
char c = '0';
int i = 0;
double d = 0.0;
try {
Json::Reader jsonReader;
Json::Value jsonData;
if (!jsonReader.parse(jsonString, jsonData)) {
throw std::exception();
}
Json::Value Parameters = jsonData["Parameters"];
Assert<std::exception>(!Parameters.empty());
Json::Value Data = Parameters["Data"];
Assert<std::exception>(!Data.empty());
// TODO : 处理没有提供的字段并设置默认值
////////////// 字符处理过程 ////////////////
if ( !Data["char"].empty()) {
c = Data["char"].asString()[0];
} else {
c = '0';
}
///////////// 整型处理过程 ////////////////
if (!Data["int"].empty()) {
i = Data["int"].asInt();
} else {
i = 0;
}
///////////// 浮点处理过程 ////////////////
if (!Data["double"].empty()) {
d = Data["double"].asDouble();
} else {
d = 0;
}
///////////// 字符串处理过程 ////////////////
if (!Data["string"].empty()) {
str[sizeof(str)-1] = 0;
strncpy(str,Data["string"].asCString(),sizeof(str)-1);
} else {
strcpy(str,"");
}
// 查看输出结果
std::cout << "c=" << c << std::endl;
std::cout << "i=" << i << std::endl;
std::cout << "d=" << d << std::endl;
std::cout << "str=" << str << '|' << std::endl;
for(i=0; i<sizeof(str); i++) {
printf("%d:%d\n",i,(int)str[i]);
}
//}catch (std::exception & e){
} catch (...) {
//std::cout << "exception:" << e.what() << std::endl;
std::cout << "json数据格式错误" << std::endl;
}
}
void test05(){
std::string jsonString = "\
{\
\"ResponseMethod\" : \"ReqQryTradingAccount\",\
\"Parameters\" : {\
\"Data\" : {\
\"a\":1 \
}\
}\
}\
";
loadConfig();
CApiWrapper api(&config);
api.init();
int result ;
for( int i=0; i<10; i++ ) {
result = api.ReqQryInvestor(jsonString);
std::cout << "result=" << result << std::endl;
// 读取Api响应数据
zmq::context_t context(1);
zmq::socket_t receiver(context, ZMQ_PULL);
PushbackMessage message;
receiver.connect(config.PushbackPipe);
message.recv(receiver);
std::cout << "message.requestID = " << message.requestID << std::endl;
std::cout << "message.apiName = " << message.apiName << std::endl;
std::cout << "message.respInfo = " << message.respInfo << std::endl;
sleep(1);
}
}