This repository has been archived by the owner on May 29, 2020. It is now read-only.
forked from libtrading/libtrading
/
fix_client.c
598 lines (481 loc) · 12.5 KB
/
fix_client.c
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#include "libtrading/proto/fix_message.h"
#include "libtrading/proto/fix_session.h"
#include "libtrading/compat.h"
#include "libtrading/array.h"
#include "libtrading/time.h"
#include "libtrading/die.h"
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <inttypes.h>
#include <libgen.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <float.h>
#include <netdb.h>
#include <stdio.h>
#include <math.h>
#include "fix_client.h"
#include "test.h"
static const char *program;
static sig_atomic_t stop;
static struct fix_client_function fix_client_functions[] = {
[FIX_CLIENT_SCRIPT] = {
.fix_session_initiate = fix_client_script,
.mode = FIX_CLIENT_SCRIPT,
},
[FIX_CLIENT_SESSION] = {
.fix_session_initiate = fix_client_session,
.mode = FIX_CLIENT_SESSION,
},
[FIX_CLIENT_ORDER] = {
.fix_session_initiate = fix_client_order,
.mode = FIX_CLIENT_ORDER,
},
};
static void signal_handler(int signum)
{
if (signum == SIGINT)
stop = 1;
}
static int fix_logout_send(struct fix_session *session, const char *text)
{
struct fix_field fields[] = {
FIX_STRING_FIELD(Text, text),
};
long nr_fields = ARRAY_SIZE(fields);
struct fix_message logout_msg;
if (!text)
nr_fields--;
logout_msg = (struct fix_message) {
.type = FIX_MSG_TYPE_LOGOUT,
.nr_fields = nr_fields,
.fields = fields,
};
return fix_session_send(session, &logout_msg, 0);
}
static int fix_client_logout(struct fix_session *session, const char *text, bool grace)
{
int ret;
if (grace)
ret = fix_session_logout(session, text);
else
ret = fix_logout_send(session, text);
if (ret)
fprintf(stderr, "Client Logout FAILED\n");
else
fprintf(stdout, "Client Logout OK\n");
return ret;
}
static int fix_client_script(struct fix_session_cfg *cfg, struct fix_client_arg *arg)
{
struct fcontainer *s_container = NULL;
struct fcontainer *c_container = NULL;
struct fix_session *session = NULL;
struct felem *expected_elem;
struct felem *tosend_elem;
struct fix_message *msg;
FILE *stream = NULL;
int ret = -1;
if (!arg->script) {
fprintf(stderr, "No script is specified\n");
goto exit;
}
stream = fopen(arg->script, "r");
if (!stream) {
fprintf(stderr, "Opening %s failed: %s\n",
arg->script, strerror(errno));
goto exit;
}
session = fix_session_new(cfg);
if (!session) {
fprintf(stderr, "FIX session cannot be created\n");
goto exit;
}
s_container = fcontainer_new();
if (!s_container) {
fprintf(stderr, "Cannot allocate container\n");
goto exit;
}
c_container = fcontainer_new();
if (!c_container) {
fprintf(stderr, "Cannot allocate container\n");
goto exit;
}
if (script_read(stream, s_container, c_container)) {
fprintf(stderr, "Invalid script: %s\n", arg->script);
goto exit;
}
ret = fix_session_logon(session);
if (ret) {
fprintf(stderr, "Client Logon FAILED\n");
goto exit;
}
fprintf(stdout, "Client Logon OK\n");
expected_elem = cur_elem(s_container);
tosend_elem = cur_elem(c_container);
while (tosend_elem) {
if (tosend_elem->msg.msg_seq_num)
fix_session_send(session, &tosend_elem->msg, FIX_SEND_FLAG_PRESERVE_MSG_NUM);
else
fix_session_send(session, &tosend_elem->msg, 0);
if (!expected_elem)
goto next;
retry:
if (fix_session_recv(session, &msg, FIX_RECV_FLAG_MSG_DONTWAIT) <= 0)
goto retry;
fprintf(stdout, "< ");
fprintmsg(stdout, msg);
if (fmsgcmp(&expected_elem->msg, msg)) {
fprintf(stderr, "Client: messages differ\n");
fprintmsg(stderr, &expected_elem->msg);
fprintmsg(stderr, msg);
break;
}
if (fix_message_type_is(msg, FIX_MSG_TYPE_LOGOUT)) {
ret = fix_client_logout(session, NULL, false);
goto exit;
}
next:
expected_elem = next_elem(s_container);
tosend_elem = next_elem(c_container);
}
ret = tosend_elem ? -1 : 0;
if (ret)
goto exit;
ret = fix_client_logout(session, NULL, true);
exit:
fcontainer_free(c_container);
fcontainer_free(s_container);
fix_session_free(session);
if (stream)
fclose(stream);
return ret;
}
static int fix_client_session(struct fix_session_cfg *cfg, struct fix_client_arg *arg)
{
struct fix_session *session = NULL;
struct timespec cur, prev;
struct fix_message *msg;
int ret = -1;
int diff;
if (signal(SIGINT, signal_handler) == SIG_ERR) {
fprintf(stderr, "Unable to register a signal handler\n");
goto exit;
}
session = fix_session_new(cfg);
if (!session) {
fprintf(stderr, "FIX session cannot be created\n");
goto exit;
}
ret = fix_session_logon(session);
if (ret) {
fprintf(stderr, "Client Logon FAILED\n");
goto exit;
}
fprintf(stdout, "Client Logon OK\n");
clock_gettime(CLOCK_MONOTONIC, &prev);
while (!stop && session->active) {
clock_gettime(CLOCK_MONOTONIC, &cur);
diff = cur.tv_sec - prev.tv_sec;
if (diff > 0.1 * session->heartbtint) {
prev = cur;
if (!fix_session_keepalive(session, &cur)) {
stop = 1;
break;
}
}
if (fix_session_time_update(session)) {
stop = 1;
break;
}
if (fix_session_recv(session, &msg, FIX_RECV_FLAG_MSG_DONTWAIT) <= 0) {
fprintmsg(stdout, msg);
if (fix_session_admin(session, msg))
continue;
switch (msg->type) {
case FIX_MSG_TYPE_LOGOUT:
stop = 1;
break;
default:
stop = 1;
break;
}
}
}
if (session->active) {
ret = fix_session_logout(session, NULL);
if (ret) {
fprintf(stderr, "Client Logout FAILED\n");
goto exit;
}
}
fprintf(stdout, "Client Logout OK\n");
exit:
fix_session_free(session);
return ret;
}
static unsigned long fix_new_order_single_fields(struct fix_session *session, struct fix_field *fields)
{
unsigned long nr = 0;
fields[nr++] = FIX_STRING_FIELD(TransactTime, session->str_now);
fields[nr++] = FIX_STRING_FIELD(ClOrdID, "ClOrdID");
fields[nr++] = FIX_STRING_FIELD(Symbol, "Symbol");
fields[nr++] = FIX_FLOAT_FIELD(OrderQty, 100);
fields[nr++] = FIX_STRING_FIELD(OrdType, "2");
fields[nr++] = FIX_STRING_FIELD(Side, "1");
fields[nr++] = FIX_FLOAT_FIELD(Price, 100);
return nr;
}
static int fix_client_order(struct fix_session_cfg *cfg, struct fix_client_arg *arg)
{
double min_usec, avg_usec, max_usec, total_usec;
struct fix_session *session = NULL;
struct fix_field *fields = NULL;
struct fix_message *msg;
FILE *file = NULL;
unsigned long nr;
int ret = -1;
int orders;
int i;
if (!arg)
goto exit;
orders = arg->orders;
if (arg->output) {
file = fopen(arg->output, "w");
if (!file) {
fprintf(stderr, "Cannot open a file %s\n", arg->output);
goto exit;
}
}
session = fix_session_new(cfg);
if (!session) {
fprintf(stderr, "FIX session cannot be created\n");
goto exit;
}
ret = fix_session_logon(session);
if (ret) {
fprintf(stderr, "Client Logon FAILED\n");
goto exit;
}
fprintf(stdout, "Client Logon OK\n");
ret = -1;
fields = calloc(FIX_MAX_FIELD_NUMBER, sizeof(struct fix_field));
if (!fields) {
fprintf(stderr, "Cannot allocate memory\n");
goto exit;
}
nr = fix_new_order_single_fields(session, fields);
min_usec = DBL_MAX;
max_usec = 0;
total_usec = 0;
for (i = 0; i < arg->warmup_orders; i++) {
fix_session_new_order_single(session, fields, nr);
retry_warmup:
if (fix_session_recv(session, &msg, MSG_DONTWAIT) <= 0)
goto retry_warmup;
if (!fix_message_type_is(msg, FIX_MSG_TYPE_EXECUTION_REPORT))
goto retry_warmup;
}
for (i = 0; i < orders; i++) {
struct timespec before, after;
uint64_t elapsed_usec;
clock_gettime(CLOCK_MONOTONIC, &before);
fix_session_new_order_single(session, fields, nr);
retry:
if (fix_session_recv(session, &msg, FIX_RECV_FLAG_MSG_DONTWAIT) <= 0)
goto retry;
if (!fix_message_type_is(msg, FIX_MSG_TYPE_EXECUTION_REPORT))
goto retry;
clock_gettime(CLOCK_MONOTONIC, &after);
elapsed_usec = timespec_delta(&before, &after) / 1000;
total_usec += elapsed_usec;
min_usec = fmin(min_usec, elapsed_usec);
max_usec = fmax(max_usec, elapsed_usec);
if (file)
fprintf(file, "%" PRIu64 "\n", elapsed_usec);
}
avg_usec = total_usec / orders;
fprintf(stdout, "Messages sent: %d\n", orders);
fprintf(stdout, "Round-trip time: min/avg/max = %.1lf/%.1lf/%.1lf μs\n", min_usec, avg_usec, max_usec);
if (session->active) {
ret = fix_session_logout(session, NULL);
if (ret) {
fprintf(stderr, "Client Logout FAILED\n");
goto exit;
}
}
fprintf(stdout, "Client Logout OK\n");
exit:
fix_session_free(session);
free(fields);
if (file)
fclose(file);
return ret;
}
static void usage(void)
{
printf("\n usage: %s [-m mode] [-d dialect] [-f filename] [-n orders] [-s sender-comp-id] [-t target-comp-id] [-r password] [-w warmup orders] -h hostname -p port\n\n", program);
exit(EXIT_FAILURE);
}
static int socket_setopt(int sockfd, int level, int optname, int optval)
{
return setsockopt(sockfd, level, optname, (void *) &optval, sizeof(optval));
}
static enum fix_client_mode strclientmode(const char *mode)
{
enum fix_client_mode m;
if (!strcmp(mode, "session"))
return FIX_CLIENT_SESSION;
else if (!strcmp(mode, "script"))
return FIX_CLIENT_SCRIPT;
else if (!strcmp(mode, "order"))
return FIX_CLIENT_ORDER;
if (sscanf(mode, "%u", &m) != 1)
return FIX_CLIENT_SCRIPT;
switch (m) {
case FIX_CLIENT_SESSION:
case FIX_CLIENT_SCRIPT:
case FIX_CLIENT_ORDER:
return m;
default:
break;
}
return FIX_CLIENT_SCRIPT;
}
static enum fix_version strversion(const char *dialect)
{
if (!strcmp(dialect, "fix42"))
return FIX_4_2;
else if (!strcmp(dialect, "fix43"))
return FIX_4_3;
else if (!strcmp(dialect, "fix44"))
return FIX_4_4;
return FIX_4_4;
}
int main(int argc, char *argv[])
{
enum fix_client_mode mode = FIX_CLIENT_SCRIPT;
enum fix_version version = FIX_4_4;
const char *target_comp_id = NULL;
const char *sender_comp_id = NULL;
struct fix_client_arg arg = {0};
const char *password = NULL;
struct fix_session_cfg cfg;
const char *host = NULL;
struct sockaddr_in sa;
int saved_errno = 0;
struct hostent *he;
int port = 0;
int ret = 0;
char **ap;
int opt;
program = basename(argv[0]);
while ((opt = getopt(argc, argv, "f:h:p:d:s:t:m:n:o:r:w:")) != -1) {
switch (opt) {
case 'd':
version = strversion(optarg);
break;
case 'n':
arg.orders = atoi(optarg);
break;
case 's':
sender_comp_id = optarg;
break;
case 't':
target_comp_id = optarg;
break;
case 'r':
password = optarg;
break;
case 'm':
mode = strclientmode(optarg);
break;
case 'p':
port = atoi(optarg);
break;
case 'f':
arg.script = optarg;
break;
case 'o':
arg.output = optarg;
break;
case 'h':
host = optarg;
break;
case 'w':
arg.warmup_orders = atoi(optarg);
break;
default: /* '?' */
usage();
}
}
if (!port || !host)
usage();
fix_session_cfg_init(&cfg);
cfg.dialect = &fix_dialects[version];
if (!password) {
memset(cfg.password, 0, ARRAY_SIZE(cfg.password));
} else {
strncpy(cfg.password, password, ARRAY_SIZE(cfg.password));
}
if (!sender_comp_id) {
strncpy(cfg.sender_comp_id, "BUYSIDE", ARRAY_SIZE(cfg.sender_comp_id));
} else {
strncpy(cfg.sender_comp_id, sender_comp_id, ARRAY_SIZE(cfg.sender_comp_id));
}
if (!target_comp_id) {
strncpy(cfg.target_comp_id, "SELLSIDE", ARRAY_SIZE(cfg.target_comp_id));
} else {
strncpy(cfg.target_comp_id, target_comp_id, ARRAY_SIZE(cfg.target_comp_id));
}
he = gethostbyname(host);
if (!he)
error("Unable to look up %s (%s)", host, hstrerror(h_errno));
for (ap = he->h_addr_list; *ap; ap++) {
cfg.sockfd = socket(he->h_addrtype, SOCK_STREAM, IPPROTO_TCP);
if (cfg.sockfd < 0) {
saved_errno = errno;
continue;
}
sa = (struct sockaddr_in) {
.sin_family = he->h_addrtype,
.sin_port = htons(port),
};
memcpy(&sa.sin_addr, *ap, he->h_length);
if (connect(cfg.sockfd, (const struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
saved_errno = errno;
close(cfg.sockfd);
cfg.sockfd = -1;
continue;
}
break;
}
if (cfg.sockfd < 0)
error("Unable to connect to a socket (%s)", strerror(saved_errno));
if (socket_setopt(cfg.sockfd, IPPROTO_TCP, TCP_NODELAY, 1) < 0)
die("cannot set socket option TCP_NODELAY");
switch (mode) {
case FIX_CLIENT_SCRIPT:
ret = fix_client_functions[mode].fix_session_initiate(&cfg, &arg);
break;
case FIX_CLIENT_ORDER:
ret = fix_client_functions[mode].fix_session_initiate(&cfg, &arg);
break;
case FIX_CLIENT_SESSION:
cfg.heartbtint = 15;
ret = fix_client_functions[mode].fix_session_initiate(&cfg, NULL);
break;
default:
error("Invalid mode");
}
shutdown(cfg.sockfd, SHUT_RDWR);
if (close(cfg.sockfd) < 0)
die("close");
return ret;
}