-
Notifications
You must be signed in to change notification settings - Fork 637
/
Copy pathSessionState.java
575 lines (473 loc) · 16.2 KB
/
SessionState.java
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
/*******************************************************************************
* Copyright (c) quickfixengine.org All rights reserved.
*
* This file is part of the QuickFIX FIX Engine
*
* This file may be distributed under the terms of the quickfixengine.org
* license as defined by quickfixengine.org and appearing in the file
* LICENSE included in the packaging of this file.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
* THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* See http://www.quickfixengine.org/LICENSE for licensing information.
*
* Contact [email protected] if any conditions of this licensing
* are not clear to you.
******************************************************************************/
package quickfix;
import java.io.IOException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Used by the session communications code. Not intended to be used by applications. All dynamic data is protected by
* the session's intrinsic lock. The log and message store implementation must be thread safe.
*/
public final class SessionState {
private final Object lock;
private final Log log;
// MessageStore implementation must be thread safe
private final MessageStore messageStore;
private final Lock senderMsgSeqNumLock = new ReentrantLock();
private final Lock targetMsgSeqNumLock = new ReentrantLock();
private final boolean initiator;
private long logonTimeoutMs = 10000L;
private long logoutTimeoutMs = 2000L;
private boolean logonSent;
private boolean logonReceived;
private boolean logoutSent;
private boolean logoutReceived = false;
private int testRequestCounter;
private long lastSentTime;
private long lastReceivedTime;
private final double testRequestDelayMultiplier;
private long heartBeatMillis = Long.MAX_VALUE;
private int heartBeatInterval;
private final ResendRange resendRange = new ResendRange();
private boolean resetSent;
private boolean resetReceived;
private String logoutReason;
/*
* If this is anything other than zero it's the value of the 789/NextExpectedMsgSeqNum tag in the last Logon message sent.
* It is used to determine if the recipient has enough information (assuming they support 789) to avoid the need
* for a resend request i.e. they should be resending any necessary missing messages already. This value is used
* to populate the resendRange if necessary.
*/
private final AtomicInteger nextExpectedMsgSeqNum = new AtomicInteger(0);
// The messageQueue should be accessed from a single thread
private final Map<Integer, Message> messageQueue = new LinkedHashMap<>();
public SessionState(Object lock, Log log, int heartBeatInterval, boolean initiator, MessageStore messageStore,
double testRequestDelayMultiplier) {
this.lock = lock;
this.initiator = initiator;
this.messageStore = messageStore;
setHeartBeatInterval(heartBeatInterval);
this.log = log == null ? new NullLog() : log;
this.testRequestDelayMultiplier = testRequestDelayMultiplier;
}
public int getHeartBeatInterval() {
synchronized (lock) {
return heartBeatInterval;
}
}
public void setHeartBeatInterval(int heartBeatInterval) {
synchronized (lock) {
this.heartBeatInterval = heartBeatInterval;
}
setHeartBeatMillis(heartBeatInterval * 1000L);
}
private void setHeartBeatMillis(long heartBeatMillis) {
synchronized (lock) {
this.heartBeatMillis = heartBeatMillis;
}
}
long getHeartBeatMillis() {
synchronized (lock) {
return heartBeatMillis;
}
}
public boolean isHeartBeatNeeded() {
long millisSinceLastSentTime = SystemTime.currentTimeMillis() - getLastSentTime();
// QFJ-448: allow 10 ms leeway since exact comparison causes skipped heartbeats occasionally
return millisSinceLastSentTime + 10 > getHeartBeatMillis() && getTestRequestCounter() == 0;
}
public boolean isInitiator() {
return initiator;
}
public long getLastReceivedTime() {
synchronized (lock) {
return lastReceivedTime;
}
}
public void setLastReceivedTime(long lastReceivedTime) {
synchronized (lock) {
this.lastReceivedTime = lastReceivedTime;
}
}
public long getLastSentTime() {
synchronized (lock) {
return lastSentTime;
}
}
public void setLastSentTime(long lastSentTime) {
synchronized (lock) {
this.lastSentTime = lastSentTime;
}
}
public Log getLog() {
return log;
}
public boolean isLogonAlreadySent() {
return isInitiator() && isLogonSent();
}
public boolean isLogonReceived() {
synchronized (lock) {
return logonReceived;
}
}
public void setLogonReceived(boolean logonReceived) {
synchronized (lock) {
this.logonReceived = logonReceived;
}
}
public boolean isLogonSendNeeded() {
return isInitiator() && !isLogonSent();
}
public boolean isLogonSent() {
synchronized (lock) {
return logonSent;
}
}
public void setLogonSent(boolean logonSent) {
synchronized (lock) {
this.logonSent = logonSent;
}
}
public boolean isLogonTimedOut() {
synchronized (lock) {
return isLogonSent() && SystemTime.currentTimeMillis() - getLastReceivedTime() >= getLogonTimeoutMs();
}
}
public void setLogonTimeout(int logonTimeout) {
setLogonTimeoutMs(logonTimeout * 1000L);
}
public int getLogonTimeout() {
return (int) (getLogonTimeoutMs() / 1000L);
}
public void setLogoutTimeout(int logoutTimeout) {
setLogoutTimeoutMs(logoutTimeout * 1000L);
}
public int getLogoutTimeout() {
return (int) (getLogoutTimeoutMs() / 1000L);
}
private void setLogoutTimeoutMs(long logoutTimeoutMs) {
synchronized (lock) {
this.logoutTimeoutMs = logoutTimeoutMs;
}
}
private long getLogoutTimeoutMs() {
synchronized (lock) {
return logoutTimeoutMs;
}
}
private void setLogonTimeoutMs(long logonTimeoutMs) {
synchronized (lock) {
this.logonTimeoutMs = logonTimeoutMs;
}
}
private long getLogonTimeoutMs() {
synchronized (lock) {
return logonTimeoutMs;
}
}
public boolean isLogoutSent() {
synchronized (lock) {
return logoutSent;
}
}
public void setLogoutSent(boolean logoutSent) {
synchronized (lock) {
this.logoutSent = logoutSent;
}
}
public boolean isLogoutReceived() {
synchronized (lock) {
return logoutReceived;
}
}
public void setLogoutReceived(boolean logoutReceived) {
synchronized (lock) {
this.logoutReceived = logoutReceived;
}
}
public boolean isLogoutTimedOut() {
return isLogoutSent() && ((SystemTime.currentTimeMillis() - getLastSentTime()) >= getLogoutTimeoutMs());
}
public MessageStore getMessageStore() {
return messageStore;
}
private int getTestRequestCounter() {
synchronized (lock) {
return testRequestCounter;
}
}
public double getTestRequestDelayMultiplier() {
return testRequestDelayMultiplier;
}
public void clearTestRequestCounter() {
synchronized (lock) {
testRequestCounter = 0;
}
}
public void incrementTestRequestCounter() {
synchronized (lock) {
testRequestCounter++;
}
}
public boolean isTestRequestNeeded() {
long millisSinceLastReceivedTime = timeSinceLastReceivedMessage();
return millisSinceLastReceivedTime >= ((1 + testRequestDelayMultiplier) * (getTestRequestCounter() + 1))
* getHeartBeatMillis();
}
private long timeSinceLastReceivedMessage() {
return SystemTime.currentTimeMillis() - getLastReceivedTime();
}
public boolean isTimedOut() {
long millisSinceLastReceivedTime = timeSinceLastReceivedMessage();
return millisSinceLastReceivedTime >= 2.4 * getHeartBeatMillis();
}
public boolean set(int sequence, String message) throws IOException {
return messageStore.set(sequence, message);
}
public void get(int first, int last, Collection<String> messages) throws IOException {
messageStore.get(first, last, messages);
}
public void enqueue(int sequence, Message message) {
messageQueue.put(sequence, message);
}
public Message dequeue(int sequence) {
return messageQueue.remove(sequence);
}
/**
* Remove messages from messageQueue up to a given sequence number.
*
* @param seqnum up to which sequence number messages should be deleted
*/
public void dequeueMessagesUpTo(int seqnum) {
for (int i = 1; i < seqnum; i++) {
dequeue(i);
}
}
public Message getNextQueuedMessage() {
return !messageQueue.isEmpty() ? messageQueue.values().iterator().next() : null;
}
public Collection<Integer> getQueuedSeqNums() {
return messageQueue.keySet();
}
public void clearQueue() {
messageQueue.clear();
}
public void lockSenderMsgSeqNum() {
senderMsgSeqNumLock.lock();
}
public void unlockSenderMsgSeqNum() {
senderMsgSeqNumLock.unlock();
}
public void lockTargetMsgSeqNum() {
targetMsgSeqNumLock.lock();
}
public void unlockTargetMsgSeqNum() {
targetMsgSeqNumLock.unlock();
}
public int getNextSenderMsgSeqNum() throws IOException {
return messageStore.getNextSenderMsgSeqNum();
}
public int getNextTargetMsgSeqNum() throws IOException {
return messageStore.getNextTargetMsgSeqNum();
}
public void setNextTargetMsgSeqNum(int sequence) throws IOException {
messageStore.setNextTargetMsgSeqNum(sequence);
}
public void incrNextSenderMsgSeqNum() throws IOException {
messageStore.incrNextSenderMsgSeqNum();
}
public void incrNextTargetMsgSeqNum() throws IOException {
messageStore.incrNextTargetMsgSeqNum();
}
public Date getCreationTime() throws IOException {
return messageStore.getCreationTime();
}
public boolean isResetNeeded() throws IOException {
return getNextSenderMsgSeqNum() != 1 || getNextTargetMsgSeqNum() != 1;
}
public void reset() {
try {
messageStore.reset();
} catch (IOException e) {
throw new RuntimeError(e);
}
}
public void setResendRange(int low, int high) {
synchronized (lock) {
resendRange.setBeginSeqNo(low);
resendRange.setEndSeqNo(high);
}
}
public void setResendRange(int low, int high, int currentResend) {
synchronized (lock) {
resendRange.setBeginSeqNo(low);
resendRange.setEndSeqNo(high);
resendRange.setCurrentEndSeqNo(currentResend);
}
}
public boolean isResendRequested() {
synchronized (lock) {
return !(resendRange.getBeginSeqNo() == 0 && resendRange.getEndSeqNo() == 0);
}
}
public ResendRange getResendRange() {
synchronized (lock) {
return resendRange;
}
}
public boolean isResetReceived() {
synchronized (lock) {
return resetReceived;
}
}
public void setResetReceived(boolean resetReceived) {
synchronized (lock) {
this.resetReceived = resetReceived;
}
}
public boolean isResetSent() {
synchronized (lock) {
return resetSent;
}
}
public void setResetSent(boolean resetSent) {
synchronized (lock) {
this.resetSent = resetSent;
}
}
/**
* No actual resend request has occurred but at logon we populated tag 789 so that the other side knows we
* are missing messages without an explicit resend request and should immediately reply with the missing
* messages.
*
* This is expected to be called only in the scenario where target is too high on logon and tag 789 is supported.
*/
public void setResetRangeFromLastExpectedLogonNextSeqNumLogon() {
synchronized (lock) {
// we have already requested all msgs from nextExpectedMsgSeqNum to infinity
setResendRange(getLastExpectedLogonNextSeqNum(), 0);
// clean up the variable (not really needed)
setLastExpectedLogonNextSeqNum(0);
}
}
/**
* @param lastExpectedLogonNextSeqNum
*
* This method is thread safe (atomic set).
*/
public void setLastExpectedLogonNextSeqNum(int lastExpectedLogonNextSeqNum) {
this.nextExpectedMsgSeqNum.set(lastExpectedLogonNextSeqNum);
}
/**
* @return nextExpectedMsgSeqNum
*
* This method is thread safe (atomic get).
*/
public int getLastExpectedLogonNextSeqNum() {
return this.nextExpectedMsgSeqNum.get();
}
/**
* @return true if we populated tag 789 at logon and our sequence
* numbers don't line up we are in an implicit resend mode.
*
* This method is thread safe (atomic get).
*/
public boolean isExpectedLogonNextSeqNumSent() {
return this.nextExpectedMsgSeqNum.get() != 0;
}
public void setLogoutReason(String reason) {
synchronized (lock) {
logoutReason = reason;
}
}
public String getLogoutReason() {
synchronized (lock) {
return logoutReason;
}
}
public void clearLogoutReason() {
synchronized (lock) {
logoutReason = "";
}
}
public Object getLock() {
return lock;
}
public Calendar getCreationTimeCalendar() throws IOException {
return messageStore.getCreationTimeCalendar();
}
private final static class NullLog implements Log {
public void onOutgoing(String message) {
}
public void onIncoming(String message) {
}
public void onEvent(String text) {
}
public void onErrorEvent(String text) {
}
public void clear() {
}
}
/**
* The resend range when sending a resend request.
* If a gap is detected and messages from x to y are needed, the received messages are checked against the values x and y that are stored in the resendRange.
* Some FIX engines do not support resendRequest range greater than a given value. There, in this case, the ResendRequests have to be splitted.
* E.g.: CME will reject any resend request for more than 2500 messages.
* The solution is to send resend requests with smaller range until the global range has been requested.
*
* The resendRange contains 3 values:
* <ol>
* <li>the begin index of the global resend request</li>
* <li>the last index of the global resend request</li>
* <li>the current last index of the splitted sub resend request</li>
* </ol>
*/
public static class ResendRange {
int beginSeqNo = 0;
int endSeqNo = 0;
int currentEndSeqNo = 0;
public int getBeginSeqNo() {
return beginSeqNo;
}
public void setBeginSeqNo(int beginSeqNo) {
this.beginSeqNo = beginSeqNo;
}
public int getEndSeqNo() {
return endSeqNo;
}
public void setEndSeqNo(int endSeqNo) {
this.endSeqNo = endSeqNo;
}
public int getCurrentEndSeqNo() {
return currentEndSeqNo;
}
public void setCurrentEndSeqNo(int currentEndSeqNo) {
this.currentEndSeqNo = currentEndSeqNo;
}
public boolean isChunkedResendRequest() {
return getCurrentEndSeqNo() > 0;
}
}
}