File size: 60,547 Bytes
c574d3a |
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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 |
//APIQA
package com.thomsonreuters.upa.valueadd.examples.watchlistconsumer;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import com.thomsonreuters.upa.codec.AckMsg;
import com.thomsonreuters.upa.codec.Buffer;
import com.thomsonreuters.upa.codec.CloseMsg;
import com.thomsonreuters.upa.codec.Codec;
import com.thomsonreuters.upa.codec.CodecFactory;
import com.thomsonreuters.upa.codec.CodecReturnCodes;
import com.thomsonreuters.upa.codec.DataDictionary;
import com.thomsonreuters.upa.codec.DataStates;
import com.thomsonreuters.upa.codec.DataTypes;
import com.thomsonreuters.upa.codec.MapEntryActions;
import com.thomsonreuters.upa.codec.Msg;
import com.thomsonreuters.upa.codec.MsgClasses;
import com.thomsonreuters.upa.codec.RefreshMsg;
import com.thomsonreuters.upa.codec.RequestMsg;
import com.thomsonreuters.upa.codec.StatusMsg;
import com.thomsonreuters.upa.codec.StreamStates;
import com.thomsonreuters.upa.codec.UpdateMsg;
import com.thomsonreuters.upa.codec.State;
import com.thomsonreuters.upa.rdm.Dictionary;
import com.thomsonreuters.upa.rdm.DomainTypes;
import com.thomsonreuters.upa.rdm.Login;
import com.thomsonreuters.upa.rdm.Dictionary.VerbosityValues;
import com.thomsonreuters.upa.transport.ConnectOptions;
import com.thomsonreuters.upa.transport.ConnectionTypes;
import com.thomsonreuters.upa.transport.Error;
import com.thomsonreuters.upa.transport.TransportFactory;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.dictionary.DictionaryMsgType;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.dictionary.DictionaryRefresh;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.dictionary.DictionaryStatus;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.directory.DirectoryMsgType;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.directory.DirectoryRefresh;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.directory.DirectoryStatus;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.directory.DirectoryUpdate;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.directory.Service;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.login.LoginMsgType;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.login.LoginRefresh;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.login.LoginRequest;
import com.thomsonreuters.upa.valueadd.domainrep.rdm.login.LoginStatus;
import com.thomsonreuters.upa.valueadd.examples.common.ItemArg;
import com.thomsonreuters.upa.valueadd.examples.watchlistconsumer.WatchlistConsumerConfig.ItemInfo;
import com.thomsonreuters.upa.valueadd.examples.watchlistconsumer.WatchlistConsumerConfig;
import com.thomsonreuters.upa.valueadd.reactor.ConsumerCallback;
import com.thomsonreuters.upa.valueadd.reactor.RDMDictionaryMsgEvent;
import com.thomsonreuters.upa.valueadd.reactor.RDMDirectoryMsgEvent;
import com.thomsonreuters.upa.valueadd.reactor.RDMLoginMsgEvent;
import com.thomsonreuters.upa.valueadd.reactor.Reactor;
import com.thomsonreuters.upa.valueadd.reactor.ReactorCallbackReturnCodes;
import com.thomsonreuters.upa.valueadd.reactor.ReactorChannel;
import com.thomsonreuters.upa.valueadd.reactor.ReactorChannelEvent;
import com.thomsonreuters.upa.valueadd.reactor.ReactorChannelEventTypes;
import com.thomsonreuters.upa.valueadd.reactor.ReactorDispatchOptions;
import com.thomsonreuters.upa.valueadd.reactor.ReactorErrorInfo;
import com.thomsonreuters.upa.valueadd.reactor.ReactorFactory;
import com.thomsonreuters.upa.valueadd.reactor.ReactorMsgEvent;
import com.thomsonreuters.upa.valueadd.reactor.ReactorOptions;
import com.thomsonreuters.upa.valueadd.reactor.ReactorReturnCodes;
import com.thomsonreuters.upa.valueadd.reactor.ReactorSubmitOptions;
/**
* <p>
* This interface implements callbacks that process information
* received by the provider. It creates the Reactor, creates the desired
* connections, then dispatches from the Reactor for events and messages.
* Once it has received the event indicating that the channel is ready,
* it will trigger ConsumerRequestThread to send the desired item requests (snapshot or streaming) to a
* provider. The resulting decoded responses from the provided are displayed on the console.
* </p>
*/
public class ConsumerCallbackThread implements Runnable, ConsumerCallback
{
private final String FIELD_DICTIONARY_DOWNLOAD_NAME = "RWFFld";
private final String ENUM_TABLE_DOWNLOAD_NAME = "RWFEnum";
private final String FIX_FIELD_DICTIONARY_FILE_NAME = "FDMFixFieldDictionary";
private final String FIX_ENUM_TABLE_FILE_NAME = "FDMenumtypes.def";
private Reactor reactor;
private ReactorOptions reactorOptions = ReactorFactory.createReactorOptions();
private ReactorErrorInfo errorInfo = ReactorFactory.createReactorErrorInfo();
private ReactorDispatchOptions dispatchOptions = ReactorFactory.createReactorDispatchOptions();
WatchlistConsumerConfig watchlistConsumerConfig;
private Selector selector;
private Error error; // error information
private DataDictionary fixdictionary;
ArrayList<ChannelInfo> chnlInfoList = new ArrayList<ChannelInfo>();
TunnelStreamHandler tunnelStreamHandler;
private String tsServiceName;
StringBuilder cacheDisplayStr;
Buffer cacheEntryBuffer;
boolean _finalStatusEvent;
boolean closeHandled;
private int FIELD_DICTIONARY_STREAM_ID = 3;
private int ENUM_DICTIONARY_STREAM_ID = 4;
ItemDecoder itemDecoder;
boolean itemsRequested = false;
public static final int MAX_MSG_SIZE = 1024;
public static int TRANSPORT_BUFFER_SIZE_CLOSE = MAX_MSG_SIZE;
boolean fieldDictionaryLoaded;
boolean enumDictionaryLoaded;
private ReactorSubmitOptions submitOptions = ReactorFactory.createReactorSubmitOptions();
private CloseMsg closeMsg = (CloseMsg)CodecFactory.createMsg();
boolean shutDown = false;
ChannelInfo chnlInfo;
WatchlistConsumer mtWatchlistConsumer;
ConsumerRequestThread consumerReqeustThread;
public ConsumerCallbackThread(ChannelInfo channelInfo, WatchlistConsumer consumer)
{
fixdictionary = CodecFactory.createDataDictionary();
error = TransportFactory.createError();
dispatchOptions.maxMessages(1);
_finalStatusEvent = true;
itemDecoder = new ItemDecoder();
itemsRequested = false;
chnlInfo = channelInfo;
mtWatchlistConsumer = consumer;
consumerReqeustThread = new ConsumerRequestThread(chnlInfo, this);
watchlistConsumerConfig = mtWatchlistConsumer.watchlistConsumerConfig();
try
{
selector = Selector.open();
}
catch (Exception e)
{
System.out.println("Selector.open() failed: " + e.getLocalizedMessage());
System.exit(ReactorReturnCodes.FAILURE);
}
}
/* Initializes the Value Add consumer application. */
private void init()
{
// display product version information
System.out.println(Codec.queryVersion().toString());
System.out.println("ConsumerCallbackThread " + this.hashCode() + " initializing...");
// enable Reactor XML tracing if specified
if (watchlistConsumerConfig.enableXmlTracing())
{
reactorOptions.enableXmlTracing();
}
itemDecoder.init();
// create reactor
reactor = ReactorFactory.createReactor(reactorOptions, errorInfo);
if (errorInfo.code() != ReactorReturnCodes.SUCCESS)
{
System.out.println("createReactor() failed: " + errorInfo.toString());
System.exit(ReactorReturnCodes.FAILURE);
}
// register selector with reactor's reactorChannel.
try
{
reactor.reactorChannel().selectableChannel().register(selector,
SelectionKey.OP_READ,
reactor.reactorChannel());
}
catch (ClosedChannelException e)
{
System.out.println("selector register failed: " + e.getLocalizedMessage());
System.exit(ReactorReturnCodes.FAILURE);
}
// initialize channel info
initChannelInfo(chnlInfo);
// connect channel
int ret;
if ((ret = reactor.connect(chnlInfo.connectOptions, chnlInfo.consumerRole, errorInfo)) < ReactorReturnCodes.SUCCESS)
{
System.out.println("Reactor.connect failed with return code: " + ret + " error = " + errorInfo.error().text());
System.exit(ReactorReturnCodes.FAILURE);
}
new Thread(consumerReqeustThread).start();
}
/* Runs the Value Add consumer application. */
public void run()
{
// initialize the test data from configuration and xml files
init();
int selectRetVal, selectTime = 1000;
while (true)
{
Set<SelectionKey> keySet = null;
try
{
selectRetVal = selector.select(selectTime);
if (selectRetVal > 0)
{
keySet = selector.selectedKeys();
}
}
catch (IOException e)
{
System.out.println("select failed: " + e.getLocalizedMessage());
System.exit(ReactorReturnCodes.FAILURE);
}
// nothing to read
if (keySet != null)
{
Iterator<SelectionKey> iter = keySet.iterator();
int ret = ReactorReturnCodes.SUCCESS;
while (iter.hasNext())
{
SelectionKey key = iter.next();
iter.remove();
try
{
if (key.isReadable())
{
// retrieve associated reactor channel and dispatch on that channel
ReactorChannel reactorChnl = (ReactorChannel)key.attachment();
// dispatch until no more messages
while ((ret = reactorChnl.dispatch(dispatchOptions, errorInfo)) > 0) {}
if (ret == ReactorReturnCodes.FAILURE)
{
if (reactorChnl.state() != ReactorChannel.State.CLOSED &&
reactorChnl.state() != ReactorChannel.State.DOWN_RECONNECTING)
{
System.out.println("ReactorChannel dispatch failed: " + ret + "(" + errorInfo.error().text() + ")");
uninitialize();
System.exit(ReactorReturnCodes.FAILURE);
}
}
}
}
catch (CancelledKeyException e)
{
} // key can be canceled during shutdown
}
}
// Handle run-time
if (shutDown && !closeHandled)
{
System.out.println("ConsumerCallbackThread " + this.hashCode() + " closing now...");
mtWatchlistConsumer.shutDown = true;
handleClose();
closeHandled = true;
}
if (!closeHandled)
{
handlePosting();
handleTunnelStream();
// send login reissue if login reissue time has passed
if (chnlInfo.reactorChannel == null ||
(chnlInfo.reactorChannel.state() != ReactorChannel.State.UP &&
chnlInfo.reactorChannel.state() != ReactorChannel.State.READY))
{
continue;
}
if (chnlInfo.canSendLoginReissue &&
System.currentTimeMillis() >= chnlInfo.loginReissueTime)
{
LoginRequest loginRequest = chnlInfo.consumerRole.rdmLoginRequest();
submitOptions.clear();
if (chnlInfo.reactorChannel.submit(loginRequest, submitOptions, errorInfo) != CodecReturnCodes.SUCCESS)
{
System.out.println("Login reissue failed. Error: " + errorInfo.error().text());
}
else
{
System.out.println("Login reissue sent");
}
chnlInfo.canSendLoginReissue = false;
}
}
if(closeHandled && tunnelStreamHandler != null && tunnelStreamHandler._chnlInfo != null &&
!tunnelStreamHandler._chnlInfo.isTunnelStreamUp)
break;
}
try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{
System.out.printf("Thread.sleep(1000) failed\n");
System.exit(-1);
}
if (closeHandled)
uninitialize();
}
private void requestDictionaries(ReactorChannel channel, ChannelInfo chnlInfo)
{
RequestMsg msg = (RequestMsg) CodecFactory.createMsg();
/* set-up message */
msg.msgClass(MsgClasses.REQUEST);
msg.applyStreaming();
msg.streamId(FIELD_DICTIONARY_STREAM_ID);
chnlInfo.fieldDictionaryStreamId = FIELD_DICTIONARY_STREAM_ID;
msg.domainType(DomainTypes.DICTIONARY);
msg.containerType(DataTypes.NO_DATA);
msg.msgKey().applyHasNameType();
msg.msgKey().applyHasName();
msg.msgKey().applyHasFilter();
msg.msgKey().filter(VerbosityValues.NORMAL);
msg.msgKey().name().data(FIELD_DICTIONARY_DOWNLOAD_NAME);
ReactorSubmitOptions submitOptions = ReactorFactory.createReactorSubmitOptions();
submitOptions.serviceName(watchlistConsumerConfig.serviceName());
channel.submit(msg, submitOptions, errorInfo);
msg.streamId(ENUM_DICTIONARY_STREAM_ID);
chnlInfo.enumDictionaryStreamId = ENUM_DICTIONARY_STREAM_ID;
msg.msgKey().name().data(ENUM_TABLE_DOWNLOAD_NAME);
channel.submit(msg, submitOptions, errorInfo);
}
@Override
public int reactorChannelEventCallback(ReactorChannelEvent event)
{
ChannelInfo chnlInfo = (ChannelInfo)event.reactorChannel().userSpecObj();
switch(event.eventType())
{
case ReactorChannelEventTypes.CHANNEL_UP:
{
if (event.reactorChannel().selectableChannel() != null)
System.out.println("Channel Up Event: " + event.reactorChannel().selectableChannel());
else
System.out.println("Channel Up Event");
// register selector with channel event's reactorChannel
try
{
event.reactorChannel().selectableChannel().register(selector,
SelectionKey.OP_READ,
event.reactorChannel());
}
catch (ClosedChannelException e)
{
System.out.println("selector register failed: " + e.getLocalizedMessage());
return ReactorCallbackReturnCodes.SUCCESS;
}
break;
}
case ReactorChannelEventTypes.FD_CHANGE:
{
System.out.println("Channel Change - Old Channel: "
+ event.reactorChannel().oldSelectableChannel() + " New Channel: "
+ event.reactorChannel().selectableChannel());
// cancel old reactorChannel select
SelectionKey key = event.reactorChannel().oldSelectableChannel().keyFor(selector);
if (key != null)
key.cancel();
// register selector with channel event's new reactorChannel
try
{
event.reactorChannel().selectableChannel().register(selector,
SelectionKey.OP_READ,
event.reactorChannel());
}
catch (Exception e)
{
System.out.println("selector register failed: " + e.getLocalizedMessage());
return ReactorCallbackReturnCodes.SUCCESS;
}
break;
}
case ReactorChannelEventTypes.CHANNEL_READY:
{
if (event.reactorChannel().selectableChannel() != null)
System.out.println("Channel Ready Event: " + event.reactorChannel().selectableChannel());
else
System.out.println("Channel Ready Event");
if (isRequestedServiceUp(chnlInfo))
{
checkAndInitPostingSupport(chnlInfo);
}
break;
}
case ReactorChannelEventTypes.CHANNEL_OPENED:
{
// set ReactorChannel on ChannelInfo, again need this?
chnlInfo.reactorChannel = event.reactorChannel();
if (!(fieldDictionaryLoaded && enumDictionaryLoaded) &&
!(itemDecoder.fieldDictionaryLoadedFromFile && itemDecoder.enumTypeDictionaryLoadedFromFile))
requestDictionaries(event.reactorChannel(), chnlInfo);
else
itemsRequested = false;
break;
}
case ReactorChannelEventTypes.CHANNEL_DOWN_RECONNECTING:
{
if (event.reactorChannel().selectableChannel() != null)
System.out.println("\nConnection down reconnecting: Channel " + event.reactorChannel().selectableChannel());
else
System.out.println("\nConnection down reconnecting");
if (event.errorInfo() != null && event.errorInfo().error().text() != null)
System.out.println(" Error text: " + event.errorInfo().error().text() + "\n");
// unregister selectableChannel from Selector
if (event.reactorChannel().selectableChannel() != null)
{
SelectionKey key = event.reactorChannel().selectableChannel().keyFor(selector);
if (key != null)
key.cancel();
}
// reset dictionary if not loaded from file
if (itemDecoder.fieldDictionaryLoadedFromFile == false &&
itemDecoder.enumTypeDictionaryLoadedFromFile == false)
{
if (chnlInfo.dictionary != null)
{
chnlInfo.dictionary.clear();
}
}
itemsRequested = false;
chnlInfo.hasServiceInfo = false;
chnlInfo.hasTunnelStreamServiceInfo = false;
break;
}
case ReactorChannelEventTypes.CHANNEL_DOWN:
{
if (event.reactorChannel().selectableChannel() != null)
System.out.println("\nConnection down: Channel " + event.reactorChannel().selectableChannel());
else
System.out.println("\nConnection down");
if (event.errorInfo() != null && event.errorInfo().error().text() != null)
System.out.println(" Error text: " + event.errorInfo().error().text() + "\n");
// unregister selectableChannel from Selector
if (event.reactorChannel().selectableChannel() != null)
{
SelectionKey key = event.reactorChannel().selectableChannel().keyFor(selector);
if (key != null)
key.cancel();
}
// close ReactorChannel
if (chnlInfo.reactorChannel != null)
{
chnlInfo.reactorChannel.close(errorInfo);
}
break;
}
case ReactorChannelEventTypes.WARNING:
System.out.println("Received ReactorChannel WARNING event\n");
break;
default:
{
System.out.println("Unknown channel event!\n");
return ReactorCallbackReturnCodes.SUCCESS;
}
}
return ReactorCallbackReturnCodes.SUCCESS;
}
@Override
public int defaultMsgCallback(ReactorMsgEvent event)
{
String itemName = null;
State itemState = null;
ItemInfo item = null;
ChannelInfo chnlInfo = (ChannelInfo)event.reactorChannel().userSpecObj();
Msg msg = event.msg();
if (msg == null)
{
/* The message is not present because an error occurred while decoding it. Print
* the error and close the channel. If desired, the un-decoded message buffer
* is available in event.transportBuffer(). */
System.out.printf("defaultMsgCallback: %s(%s)\n", event.errorInfo().error().text(), event.errorInfo().location());
// unregister selectableChannel from Selector
if (event.reactorChannel().selectableChannel() != null)
{
SelectionKey key = event.reactorChannel().selectableChannel().keyFor(selector);
if (key != null)
key.cancel();
}
// close ReactorChannel
if (chnlInfo.reactorChannel != null)
{
chnlInfo.reactorChannel.close(errorInfo);
}
return ReactorCallbackReturnCodes.SUCCESS;
}
item = watchlistConsumerConfig.getItemInfo(msg.streamId());
switch (msg.msgClass())
{
case MsgClasses.REFRESH:
RefreshMsg refreshMsg = (RefreshMsg)msg;
if ( refreshMsg.checkHasMsgKey())
{
if (refreshMsg.msgKey().checkHasName())
{
itemName = refreshMsg.msgKey().name().toString(); // Buffer
if ( item == null && refreshMsg.streamId() < 0)
{
watchlistConsumerConfig.addProvidedItemInfo(refreshMsg.streamId(), refreshMsg.msgKey(),
refreshMsg.domainType());
}
}
}
else if (item != null)
{
itemName = item.name();
}
System.out.println("DefaultMsgCallback Refresh ItemName: " + itemName + " Domain: " + DomainTypes.toString(refreshMsg.domainType()) + ", StreamId: " + refreshMsg.streamId());
System.out.println(" State: " + refreshMsg.state());
itemState = refreshMsg.state();
/* Decode data body according to its domain. */
itemDecoder.decodeDataBody(event.reactorChannel(), refreshMsg);
break;
case MsgClasses.UPDATE:
UpdateMsg updateMsg = (UpdateMsg)msg;
if (updateMsg.checkHasMsgKey() && updateMsg.msgKey().checkHasName())
{
itemName = updateMsg.msgKey().name().toString();
}
else if (item != null)
{
itemName = item.name();
}
System.out.println("DefaultMsgCallback Update ItemName: " + itemName + " Domain: " + DomainTypes.toString(updateMsg.domainType()) + ", StreamId: " + updateMsg.streamId());
/* Decode data body according to its domain. */
itemDecoder.decodeDataBody(event.reactorChannel(), updateMsg);
break;
case MsgClasses.STATUS:
StatusMsg statusMsg = (StatusMsg)msg;
if (statusMsg.checkHasMsgKey())
{
if (statusMsg.msgKey().checkHasName())
{
itemName = statusMsg.msgKey().name().toString();
if ( item != null && statusMsg.streamId() < 0)
{
watchlistConsumerConfig.addProvidedItemInfo(statusMsg.streamId(), statusMsg.msgKey(),
statusMsg.domainType());
}
}
}
else if (item != null)
{
itemName = item.name();
}
System.out.println("DefaultMsgCallback Status -- ItemName: " + itemName + " Domain: " + DomainTypes.toString(statusMsg.domainType()) + ", StreamId: " + statusMsg.streamId());
if ( statusMsg.checkHasState())
{
System.out.println(statusMsg.state());
itemState = statusMsg.state();
}
break;
case MsgClasses.ACK:
AckMsg ackMsg = (AckMsg)msg;
if (ackMsg.checkHasMsgKey())
{
if (ackMsg.msgKey().checkHasName())
{
itemName = ackMsg.msgKey().name().toString();
}
}
else if (item != null)
{
itemName = item.name();
}
System.out.println("DefaultMsgCallback Ack -- ItemName: " + itemName + " Domain: " + DomainTypes.toString(ackMsg.domainType()) + ", StreamId: " + ackMsg.streamId());
System.out.println(" ackId: " + ackMsg.ackId());
if ( ackMsg.checkHasSeqNum())
{
System.out.println(" seqNum: " + ackMsg.seqNum());
}
if ( ackMsg.checkHasNakCode())
{
System.out.println(" nakCode: " + ackMsg.nakCode());
}
if ( ackMsg.checkHasText())
{
System.out.println(" text: " + ackMsg.text());
}
break;
default:
System.out.println("Received Unhandled Item Msg Class: " + msg.msgClass());
break;
}
if (itemState != null && item != null)
{
/* Check state of any provider-driven streams.
* If the state indicates the item was closed, remove it from our list. */
if (msg.streamId() < 0 && itemState.streamState() != StreamStates.OPEN)
watchlistConsumerConfig.removeProvidedItemInfo(item);
/* Update item state. */
else
itemState.copy(item.state());
}
return ReactorCallbackReturnCodes.SUCCESS;
}
@Override
public int rdmLoginMsgCallback(RDMLoginMsgEvent event)
{
ChannelInfo chnlInfo = (ChannelInfo)event.reactorChannel().userSpecObj();
LoginMsgType msgType = event.rdmLoginMsg().rdmMsgType();
switch (msgType)
{
case REFRESH:
System.out.println("Received Login Refresh for Username: " + ((LoginRefresh)event.rdmLoginMsg()).userName());
System.out.println(event.rdmLoginMsg().toString());
// save loginRefresh
((LoginRefresh)event.rdmLoginMsg()).copy(chnlInfo.loginRefresh);
System.out.println("Domain: " + DomainTypes.toString(DomainTypes.LOGIN) + ", StreamId: " + event.rdmLoginMsg().streamId());
System.out.println(" State: " + chnlInfo.loginRefresh.state());
if ( chnlInfo.loginRefresh.checkHasUserName())
System.out.println(" UserName: " + chnlInfo.loginRefresh.userName().toString());
// get login reissue time from authenticationTTReissue
if (chnlInfo.loginRefresh.checkHasAuthenticationTTReissue())
{
chnlInfo.loginReissueTime = chnlInfo.loginRefresh.authenticationTTReissue() * 1000;
chnlInfo.canSendLoginReissue = true;
}
break;
case STATUS:
LoginStatus loginStatus = (LoginStatus)event.rdmLoginMsg();
System.out.println("Domain: " + DomainTypes.toString(DomainTypes.LOGIN) + ", StreamId: " + event.rdmLoginMsg().streamId());
System.out.println("Received Login StatusMsg");
if (loginStatus.checkHasState())
{
System.out.println(" " + loginStatus.state());
}
if (loginStatus.checkHasUserName())
System.out.println(" UserName: " + loginStatus.userName().toString());
break;
default:
System.out.println("Received Unhandled Login Msg Type: " + msgType);
break;
}
System.out.println("");
return ReactorCallbackReturnCodes.SUCCESS;
}
@Override
public int rdmDirectoryMsgCallback(RDMDirectoryMsgEvent event)
{
ChannelInfo chnlInfo = (ChannelInfo)event.reactorChannel().userSpecObj();
DirectoryMsgType msgType = event.rdmDirectoryMsg().rdmMsgType();
List<Service> serviceList = null;
switch (msgType)
{
case REFRESH:
DirectoryRefresh directoryRefresh = (DirectoryRefresh)event.rdmDirectoryMsg();
System.out.println("Domain: " + DomainTypes.toString(DomainTypes.SOURCE));
System.out.println("Stream: " + event.rdmDirectoryMsg().streamId() + " Msg Class: " + MsgClasses.toString(MsgClasses.REFRESH));
System.out.println(directoryRefresh.state().toString());
serviceList = directoryRefresh.serviceList();
String serviceName = chnlInfo.connectionArg.service();
for (Service service : serviceList)
{
if(service.info().serviceName().toString() != null)
{
if (service.info().serviceName().toString().equals(serviceName))
{
// save serviceInfo associated with requested service name
if (service.copy(chnlInfo.serviceInfo) < CodecReturnCodes.SUCCESS)
{
System.out.println("Service.copy() failure");
uninitialize();
System.exit(ReactorReturnCodes.FAILURE);
}
chnlInfo.hasServiceInfo = true;
}
if (service.info().serviceName().toString().equals(tsServiceName))
{
// save serviceInfo associated with requested service name
if (service.copy(chnlInfo.tsServiceInfo) < CodecReturnCodes.SUCCESS)
{
System.out.println("Service.copy() failure");
uninitialize();
System.exit(ReactorReturnCodes.FAILURE);
}
chnlInfo.hasTunnelStreamServiceInfo = true;
}
}
}
break;
case UPDATE:
DirectoryUpdate directoryUpdate = (DirectoryUpdate)event.rdmDirectoryMsg();
serviceName = chnlInfo.connectionArg.service();
String tsServiceName = chnlInfo.connectionArg.tsService();
System.out.println("Received Source Directory Update");
System.out.println(directoryUpdate.toString());
System.out.println("Domain: " + DomainTypes.toString(DomainTypes.SOURCE));
System.out.println("Stream: " + event.rdmDirectoryMsg().streamId() + " Msg Class: " + MsgClasses.toString(MsgClasses.UPDATE));
serviceList = directoryUpdate.serviceList();
for (Service service : serviceList)
{
if (service.action() == MapEntryActions.DELETE && service.serviceId() == chnlInfo.serviceInfo.serviceId() )
{
chnlInfo.serviceInfo.action(MapEntryActions.DELETE);
}
if (service.action() == MapEntryActions.DELETE && service.serviceId() == chnlInfo.tsServiceInfo.serviceId() )
{
chnlInfo.tsServiceInfo.action(MapEntryActions.DELETE);
}
boolean updateServiceInfo = false, updateQServiceInfo = false;
if(service.info().serviceName().toString() != null)
{
System.out.println("Received serviceName: " + service.info().serviceName() + "\n");
// update service cache - assume cache is built with previous refresh message
if (service.info().serviceName().toString().equals(serviceName) ||
service.serviceId() == chnlInfo.serviceInfo.serviceId())
{
updateServiceInfo = true;
}
if (service.info().serviceName().toString().equals(tsServiceName) ||
service.serviceId() == chnlInfo.tsServiceInfo.serviceId())
{
updateQServiceInfo = true;
}
}
else
{
if (service.serviceId() == chnlInfo.serviceInfo.serviceId())
{
updateServiceInfo = true;
}
if (service.serviceId() == chnlInfo.tsServiceInfo.serviceId())
{
updateQServiceInfo = true;
}
}
if (updateServiceInfo)
{
// update serviceInfo associated with requested service name
if (service.copy(chnlInfo.serviceInfo) < CodecReturnCodes.SUCCESS)
{
System.out.println("Service.copy() failure");
uninitialize();
System.exit(ReactorReturnCodes.FAILURE);
}
chnlInfo.hasServiceInfo = true;
}
if (updateQServiceInfo)
{
// update serviceInfo associated with requested service name
if (service.copy(chnlInfo.tsServiceInfo) < CodecReturnCodes.SUCCESS)
{
System.out.println("Service.copy() failure");
uninitialize();
System.exit(ReactorReturnCodes.FAILURE);
}
chnlInfo.hasTunnelStreamServiceInfo = true;
}
}
break;
case CLOSE:
System.out.println("Received Source Directory Close");
break;
case STATUS:
DirectoryStatus directoryStatus = (DirectoryStatus)event.rdmDirectoryMsg();
System.out.println("Received Source Directory StatusMsg");
System.out.println("Domain: " + DomainTypes.toString(DomainTypes.SOURCE));
System.out.println("Stream: " + event.rdmDirectoryMsg().streamId() + " Msg Class: " + MsgClasses.toString(MsgClasses.STATUS));
System.out.println(directoryStatus.state().toString());
if (directoryStatus.checkHasState())
{
System.out.println(" " + directoryStatus.state());
}
break;
default:
System.out.println("Received Unhandled Source Directory Msg Type: " + msgType);
break;
}
/* Refresh and update messages contain updates to service information. */
if ( serviceList != null )
{
for (Service service : serviceList)
{
System.out.println(" Service = " + service.serviceId() + " Action: " + MapEntryActions.toString(service.action()));
if (chnlInfo.connectionArg.tunnel())
{
tunnelStreamHandler.processServiceUpdate(chnlInfo.connectionArg.tsService(), service);
}
}
}
if (chnlInfo.connectionArg.tunnel())
{
if (!tunnelStreamHandler.isServiceFound())
{
System.out.println(" Directory response does not contain service name for tunnel streams: \n "
+ chnlInfo.connectionArg.tsService());
}
else if (!tunnelStreamHandler.isServiceSupported())
{
System.out.println(" Service in use for tunnel streams does not support them: \n"
+ chnlInfo.connectionArg.tsService());
}
else if (isRequestedTunnelStreamServiceUp(chnlInfo))
{
if (tunnelStreamHandler.openStream(chnlInfo, errorInfo) != ReactorReturnCodes.SUCCESS)
{
if (chnlInfo.reactorChannel.state() != ReactorChannel.State.CLOSED &&
chnlInfo.reactorChannel.state() != ReactorChannel.State.DOWN_RECONNECTING)
{
uninitialize();
System.exit(ReactorReturnCodes.FAILURE);
}
}
}
}
System.out.println("");
return ReactorCallbackReturnCodes.SUCCESS;
}
@Override
public int rdmDictionaryMsgCallback(RDMDictionaryMsgEvent event)
{
ChannelInfo chnlInfo = (ChannelInfo)event.reactorChannel().userSpecObj();
DictionaryMsgType msgType = event.rdmDictionaryMsg().rdmMsgType();
// initialize dictionary
if (chnlInfo.dictionary == null)
{
chnlInfo.dictionary = CodecFactory.createDataDictionary();
}
switch (msgType)
{
case REFRESH:
DictionaryRefresh dictionaryRefresh = (DictionaryRefresh)event.rdmDictionaryMsg();
if (dictionaryRefresh.checkHasInfo())
{
/* The first part of a dictionary refresh should contain information about its type.
* Save this information and use it as subsequent parts arrive. */
switch(dictionaryRefresh.dictionaryType())
{
case Dictionary.Types.FIELD_DEFINITIONS:
fieldDictionaryLoaded = false;
chnlInfo.fieldDictionaryStreamId = dictionaryRefresh.streamId();
break;
case Dictionary.Types.ENUM_TABLES:
enumDictionaryLoaded = false;
chnlInfo.enumDictionaryStreamId = dictionaryRefresh.streamId();
break;
default:
System.out.println("Unknown dictionary type " + dictionaryRefresh.dictionaryType() + " from message on stream " + dictionaryRefresh.streamId());
chnlInfo.reactorChannel.close(errorInfo);
return ReactorCallbackReturnCodes.SUCCESS;
}
}
/* decode dictionary response */
// clear decode iterator
chnlInfo.dIter.clear();
// set buffer and version info
chnlInfo.dIter.setBufferAndRWFVersion(dictionaryRefresh.dataBody(),
event.reactorChannel().majorVersion(),
event.reactorChannel().minorVersion());
System.out.println("Received Dictionary Response: " + dictionaryRefresh.dictionaryName());
if (dictionaryRefresh.streamId() == chnlInfo.fieldDictionaryStreamId)
{
if (chnlInfo.dictionary.decodeFieldDictionary(chnlInfo.dIter, Dictionary.VerbosityValues.VERBOSE, error) == CodecReturnCodes.SUCCESS)
{
if (dictionaryRefresh.checkRefreshComplete())
{
fieldDictionaryLoaded = true;
itemDecoder.fieldDictionaryDownloadedFromNetwork = true;
itemDecoder.dictionary = chnlInfo.dictionary;
System.out.println("Field Dictionary complete.");
}
}
else
{
System.out.println("Decoding Field Dictionary failed: " + error.text());
chnlInfo.reactorChannel.close(errorInfo);
}
}
else if (dictionaryRefresh.streamId() == chnlInfo.enumDictionaryStreamId)
{
if (chnlInfo.dictionary.decodeEnumTypeDictionary(chnlInfo.dIter, Dictionary.VerbosityValues.VERBOSE, error) == CodecReturnCodes.SUCCESS)
{
if (dictionaryRefresh.checkRefreshComplete())
{
enumDictionaryLoaded = true;
itemDecoder.enumTypeDictionaryDownloadedFromNetwork = true;
itemDecoder.dictionary = chnlInfo.dictionary;
System.out.println("EnumType Dictionary complete.");
}
}
else
{
System.out.println("Decoding EnumType Dictionary failed: " + error.text());
chnlInfo.reactorChannel.close(errorInfo);
}
}
else
{
System.out.println("Received unexpected dictionary message on stream " + dictionaryRefresh.streamId());
}
if (fieldDictionaryLoaded && enumDictionaryLoaded)
itemsRequested = false;
break;
case STATUS:
DictionaryStatus dictionaryStatus = (DictionaryStatus)event.rdmDictionaryMsg();
if (dictionaryStatus.streamId() == chnlInfo.fieldDictionaryStreamId)
{
System.out.println("Received Dictionary StatusMsg for RWFFld, streamId: " + chnlInfo.fieldDictionaryStreamId);
}
else if (dictionaryStatus.streamId() == chnlInfo.enumDictionaryStreamId)
{
System.out.println("Received Dictionary StatusMsg for RWFEnum, streamId: " + chnlInfo.enumDictionaryStreamId);
}
if (dictionaryStatus.checkHasState())
{
System.out.println(dictionaryStatus.state());
}
break;
default:
System.out.println("Received Unhandled Dictionary Msg Type: " + msgType);
break;
}
System.out.println("");
return ReactorCallbackReturnCodes.SUCCESS;
}
public boolean isRequestedServiceUp(ChannelInfo chnlInfo)
{
return chnlInfo.hasServiceInfo &&
chnlInfo.serviceInfo.checkHasState() && (!chnlInfo.serviceInfo.state().checkHasAcceptingRequests() ||
chnlInfo.serviceInfo.state().acceptingRequests() == 1) && chnlInfo.serviceInfo.state().serviceState() == 1;
}
public boolean isRequestedTunnelStreamServiceUp(ChannelInfo chnlInfo)
{
return chnlInfo.hasTunnelStreamServiceInfo &&
chnlInfo.tsServiceInfo.checkHasState() && (!chnlInfo.tsServiceInfo.state().checkHasAcceptingRequests() ||
chnlInfo.tsServiceInfo.state().acceptingRequests() == 1) && chnlInfo.tsServiceInfo.state().serviceState() == 1;
}
private void checkAndInitPostingSupport(ChannelInfo chnlInfo)
{
if (!(chnlInfo.shouldOnStreamPost || chnlInfo.shouldOffStreamPost))
return;
// set up posting if its enabled
// ensure that provider supports posting - if not, disable posting
if (!chnlInfo.loginRefresh.checkHasFeatures() ||
!chnlInfo.loginRefresh.features().checkHasSupportPost() ||
chnlInfo.loginRefresh.features().supportOMMPost() == 0)
{
// provider does not support posting, disable it
chnlInfo.shouldOffStreamPost = false;
chnlInfo.shouldOnStreamPost = false;
chnlInfo.postHandler.enableOnstreamPost(false);
chnlInfo.postHandler.enableOffstreamPost(false);
System.out.println("Connected Provider does not support OMM Posting. Disabling Post functionality.");
return;
}
if ( watchlistConsumerConfig.publisherId() != null && watchlistConsumerConfig.publisherAddress() != null)
chnlInfo.postHandler.setPublisherInfo(watchlistConsumerConfig.publisherId(), watchlistConsumerConfig.publisherAddress());
}
// on and off stream posting if enabled
private void handlePosting()
{
for (ChannelInfo chnlInfo : chnlInfoList)
{
if (chnlInfo.loginRefresh == null ||
chnlInfo.serviceInfo == null ||
chnlInfo.reactorChannel == null ||
chnlInfo.reactorChannel.state() != ReactorChannel.State.READY)
{
continue;
}
chnlInfo.postItemName.clear();
if (chnlInfo.postHandler.enableOnstreamPost())
{
ItemInfo postingItem = null;
// Find a first MarketPrice item
// If found, send on-stream posts on it.
for (int i = 0; i < watchlistConsumerConfig.itemCount(); i++)
{
if (watchlistConsumerConfig.itemList().get(i).domain() == DomainTypes.MARKET_PRICE)
{
postingItem = watchlistConsumerConfig.itemList().get(i);
if(watchlistConsumerConfig.itemList().get(i).state().streamState() != StreamStates.OPEN ||
watchlistConsumerConfig.itemList().get(i).state().dataState() != DataStates.OK)
{
System.out.println("No currently available Market Price streams to on-stream post to. Will retry shortly.");
return;
}
break;
}
}
if (postingItem == null)
{
System.out.println("No currently available Market Price streams to on-stream post to. Will retry shortly.\n");
return;
}
chnlInfo.postHandler.streamId(postingItem.streamId());
chnlInfo.postHandler.postItemName().data(postingItem.name());
chnlInfo.postHandler.serviceId(chnlInfo.serviceInfo.serviceId());
chnlInfo.postHandler.dictionary(chnlInfo.dictionary);
int ret = chnlInfo.postHandler.handlePosts(chnlInfo.reactorChannel, errorInfo);
if (ret < CodecReturnCodes.SUCCESS)
System.out.println("Error posting onstream: " + errorInfo.error().text());
}
if (chnlInfo.postHandler.enableOffstreamPost())
{
chnlInfo.postHandler.streamId(chnlInfo.loginRefresh.streamId());
chnlInfo.postHandler.postItemName().data("OFFPOST");
chnlInfo.postHandler.serviceId(chnlInfo.serviceInfo.serviceId());
chnlInfo.postHandler.dictionary(chnlInfo.dictionary);
int ret = chnlInfo.postHandler.handlePosts(chnlInfo.reactorChannel, errorInfo);
if (ret < CodecReturnCodes.SUCCESS)
System.out.println("Error posting offstream: " + errorInfo.error().text());
}
}
}
private void handleTunnelStream()
{
for (ChannelInfo chnlInfo : chnlInfoList)
{
if (chnlInfo.loginRefresh == null ||
chnlInfo.serviceInfo == null ||
chnlInfo.reactorChannel == null ||
chnlInfo.reactorChannel.state() != ReactorChannel.State.READY)
{
continue;
}
if (tunnelStreamHandler != null)
{
tunnelStreamHandler.sendMsg(chnlInfo.reactorChannel);
}
}
}
private void initChannelInfo(ChannelInfo chnlInfo)
{
// set up consumer role
chnlInfo.consumerRole.defaultMsgCallback(this);
chnlInfo.consumerRole.channelEventCallback(this);
chnlInfo.consumerRole.loginMsgCallback(this);
chnlInfo.consumerRole.directoryMsgCallback(this);
chnlInfo.consumerRole.watchlistOptions().enableWatchlist(true);
chnlInfo.consumerRole.watchlistOptions().itemCountHint(4);
chnlInfo.consumerRole.watchlistOptions().maxOutstandingPosts(5);
chnlInfo.consumerRole.watchlistOptions().obeyOpenWindow(true);
chnlInfo.consumerRole.watchlistOptions().channelOpenCallback(this);
if (itemDecoder.fieldDictionaryLoadedFromFile == false &&
itemDecoder.enumTypeDictionaryLoadedFromFile == false)
{
chnlInfo.consumerRole.dictionaryMsgCallback(this);
}
// initialize consumer role to default
chnlInfo.consumerRole.initDefaultRDMLoginRequest();
chnlInfo.consumerRole.initDefaultRDMDirectoryRequest();
// use command line login user name if specified
if (watchlistConsumerConfig.userName() != null && !watchlistConsumerConfig.userName().equals(""))
{
chnlInfo.consumerRole.rdmLoginRequest().userName().data(watchlistConsumerConfig.userName());
}
// use command line authentication token and extended authentication information if specified
if (watchlistConsumerConfig.authenticationToken() != null && !watchlistConsumerConfig.authenticationToken().equals(""))
{
chnlInfo.consumerRole.rdmLoginRequest().userNameType(Login.UserIdTypes.AUTHN_TOKEN);
chnlInfo.consumerRole.rdmLoginRequest().userName().data(watchlistConsumerConfig.authenticationToken());
if (watchlistConsumerConfig.authenticationExtended() != null && !watchlistConsumerConfig.authenticationExtended().equals(""))
{
chnlInfo.consumerRole.rdmLoginRequest().applyHasAuthenticationExtended();
chnlInfo.consumerRole.rdmLoginRequest().authenticationExtended().data(watchlistConsumerConfig.authenticationExtended());
}
}
// use command line application id if specified
if (watchlistConsumerConfig.applicationId() != null && !watchlistConsumerConfig.applicationId().equals(""))
{
chnlInfo.consumerRole.rdmLoginRequest().attrib().applicationId().data(watchlistConsumerConfig.applicationId());
}
chnlInfo.consumerRole.rdmLoginRequest().attrib().applyHasSingleOpen();
chnlInfo.consumerRole.rdmLoginRequest().attrib().singleOpen(1);
chnlInfo.consumerRole.rdmLoginRequest().attrib().applyHasAllowSuspectData();
chnlInfo.consumerRole.rdmLoginRequest().attrib().allowSuspectData(1);
if (itemDecoder.fieldDictionaryLoadedFromFile == true &&
itemDecoder.enumTypeDictionaryLoadedFromFile == true)
{
chnlInfo.dictionary = itemDecoder.getDictionary();
}
chnlInfo.shouldOffStreamPost = watchlistConsumerConfig.enableOffpost();
chnlInfo.shouldOnStreamPost = watchlistConsumerConfig.enablePost();
if (chnlInfo.shouldOnStreamPost)
{
boolean mpItemFound = false;
if (chnlInfo.connectionArg.itemList() != null)
{
for (ItemArg itemArg : chnlInfo.connectionArg.itemList())
{
if (itemArg.domain() == DomainTypes.MARKET_PRICE)
{
mpItemFound = true;
break;
}
}
}
if (mpItemFound == false)
{
System.out.println("\nPosting will not be performed for this channel as no Market Price items were requested");
chnlInfo.shouldOnStreamPost = false;
}
}
chnlInfo.postHandler.enableOnstreamPost(chnlInfo.shouldOnStreamPost);
chnlInfo.postHandler.enableOffstreamPost(chnlInfo.shouldOffStreamPost);
// This sets up our basic timing so post messages will be sent
// periodically
chnlInfo.postHandler.initPostHandler();
// set up reactor connect options
chnlInfo.connectOptions.reconnectAttemptLimit(-1); // attempt to recover forever
chnlInfo.connectOptions.reconnectMinDelay(500); // 0.5 second minimum
chnlInfo.connectOptions.reconnectMaxDelay(3000); // 3 second maximum
chnlInfo.connectOptions.connectionList().get(0).connectOptions().majorVersion(Codec.majorVersion());
chnlInfo.connectOptions.connectionList().get(0).connectOptions().minorVersion(Codec.minorVersion());
chnlInfo.connectOptions.connectionList().get(0).connectOptions().connectionType(chnlInfo.connectionArg.connectionType());
chnlInfo.connectOptions.connectionList().get(0).connectOptions().unifiedNetworkInfo().serviceName(chnlInfo.connectionArg.port());
chnlInfo.connectOptions.connectionList().get(0).connectOptions().unifiedNetworkInfo().address(chnlInfo.connectionArg.hostname());
chnlInfo.connectOptions.connectionList().get(0).connectOptions().unifiedNetworkInfo().interfaceName(chnlInfo.connectionArg.interfaceName());
chnlInfo.connectOptions.connectionList().get(0).connectOptions().userSpecObject(chnlInfo);
chnlInfo.connectOptions.connectionList().get(0).connectOptions().guaranteedOutputBuffers(1000);
// handler encrypted or http connection
chnlInfo.shouldEnableEncrypted = watchlistConsumerConfig.enableEncrypted();
chnlInfo.shouldEnableHttp = watchlistConsumerConfig.enableHttp();
if (chnlInfo.shouldEnableEncrypted)
{
ConnectOptions cOpt = chnlInfo.connectOptions.connectionList().get(0).connectOptions();
cOpt.connectionType(ConnectionTypes.ENCRYPTED);
cOpt.tunnelingInfo().tunnelingType("encrypted");
setEncryptedConfiguration(cOpt);
}
else if (chnlInfo.shouldEnableHttp)
{
ConnectOptions cOpt = chnlInfo.connectOptions.connectionList().get(0).connectOptions();
cOpt.connectionType(ConnectionTypes.HTTP);
cOpt.tunnelingInfo().tunnelingType("http");
setHTTPConfiguration(cOpt);
}
// handle basic tunnel stream configuration
if (chnlInfo.connectionArg.tunnel() && tunnelStreamHandler == null)
{
tsServiceName = chnlInfo.connectionArg.tsService();
tunnelStreamHandler = new TunnelStreamHandler(chnlInfo.connectionArg.tunnelAuth(), chnlInfo.connectionArg.tunnelDomain());
}
}
// load FIX dictionary to support FIX Protocol
public void loadFixDictionary()
{
fixdictionary.clear();
if (fixdictionary.loadFieldDictionary(FIX_FIELD_DICTIONARY_FILE_NAME, error) < 0)
{
System.out.println("\nUnable to load FIX field dictionary. \n\tText: "
+ error.text() + "\n");
uninitialize();
System.exit(ReactorReturnCodes.FAILURE);
}
if (fixdictionary.loadEnumTypeDictionary(FIX_ENUM_TABLE_FILE_NAME, error) < 0)
{
System.out.println("\nUnable to load FIX enum dictionary. \n\tText: "
+ error.text() + "\n");
uninitialize();
System.exit(ReactorReturnCodes.FAILURE);
}
}
private void closeItemStreams(ChannelInfo chnlInfo)
{
// have offstream posting post close status
if (chnlInfo.shouldOffStreamPost)
{
chnlInfo.postHandler.streamId(chnlInfo.loginRefresh.streamId());
chnlInfo.postHandler.postItemName().data("OFFPOST");
chnlInfo.postHandler.serviceId(chnlInfo.serviceInfo.serviceId());
chnlInfo.postHandler.dictionary(chnlInfo.dictionary);
chnlInfo.postHandler.closeOffStreamPost(chnlInfo.reactorChannel, errorInfo);
}
for(int itemListIndex = 0; itemListIndex < watchlistConsumerConfig.itemCount(); ++itemListIndex)
{
int domainType = watchlistConsumerConfig.itemList().get(itemListIndex).domain();
int streamId = watchlistConsumerConfig.itemList().get(itemListIndex).streamId;
/* encode item close */
closeMsg.clear();
closeMsg.msgClass(MsgClasses.CLOSE);
closeMsg.streamId(streamId);
closeMsg.domainType(domainType);
closeMsg.containerType(DataTypes.NO_DATA);
if ( (chnlInfo.reactorChannel.submit(closeMsg, submitOptions, errorInfo)) != CodecReturnCodes.SUCCESS)
{
System.out.println("Close itemStream of " + streamId + " Failed.");
}
}
}
/* Uninitializes the Value Add consumer application. */
private void uninitialize()
{
System.out.println("ConsumerCallbackThread " + this.hashCode() + " unitializing and exiting...");
for (ChannelInfo chnlInfo : chnlInfoList)
{
// close items streams
closeItemStreams(chnlInfo);
// close tunnel streams
if (tunnelStreamHandler != null &&
chnlInfo.reactorChannel != null)
{
if (tunnelStreamHandler.closeStreams(chnlInfo, _finalStatusEvent, errorInfo) != ReactorReturnCodes.SUCCESS)
{
System.out.println("tunnelStreamHandler.closeStream() failed with errorText: " + errorInfo.error().text());
}
}
// close ReactorChannel
if (chnlInfo.reactorChannel != null)
{
chnlInfo.reactorChannel.close(errorInfo);
}
}
// shutdown reactor
if (reactor != null)
{
reactor.shutdown(errorInfo);
}
}
private void handleClose()
{
System.out.println("ConsumerCallbackThread " + this.hashCode() + " closing streams...");
for (ChannelInfo chnlInfo : chnlInfoList)
{
closeItemStreams(chnlInfo);
// close tunnel streams
if (tunnelStreamHandler != null && chnlInfo.reactorChannel != null)
{
if (tunnelStreamHandler.closeStreams(chnlInfo, _finalStatusEvent, errorInfo) != ReactorReturnCodes.SUCCESS)
{
System.out.println("tunnelStreamHandler.closeStream() failed with errorText: " + errorInfo.error().text());
}
}
}
}
private void setEncryptedConfiguration(ConnectOptions options)
{
setHTTPConfiguration(options);
String keyFile = watchlistConsumerConfig.keyStoreFile();
String keyPasswd = watchlistConsumerConfig.keystorePassword();
if (keyFile == null)
{
System.err.println("Error: Keystore file not provided.");
System.exit(CodecReturnCodes.FAILURE);
}
if (keyPasswd == null)
{
System.err.println("Error: Keystore password not provided.");
System.exit(CodecReturnCodes.FAILURE);
}
options.tunnelingInfo().KeystoreFile(keyFile);
options.tunnelingInfo().KeystorePasswd(keyPasswd);
}
private void setHTTPConfiguration(ConnectOptions options)
{
options.tunnelingInfo().objectName("");
options.tunnelingInfo().KeystoreType("JKS");
options.tunnelingInfo().SecurityProtocol("TLS");
options.tunnelingInfo().SecurityProvider("SunJSSE");
options.tunnelingInfo().KeyManagerAlgorithm("SunX509");
options.tunnelingInfo().TrustManagerAlgorithm("PKIX");
if (watchlistConsumerConfig.enableProxy())
{
String proxyHostName = watchlistConsumerConfig.proxyHostname();
if ( proxyHostName == null)
{
System.err.println("Error: Proxy hostname not provided.");
System.exit(CodecReturnCodes.FAILURE);
}
String proxyPort = watchlistConsumerConfig.proxyPort();
if ( proxyPort == null)
{
System.err.println("Error: Proxy port number not provided.");
System.exit(CodecReturnCodes.FAILURE);
}
options.tunnelingInfo().HTTPproxy(true);
options.tunnelingInfo().HTTPproxyHostName(proxyHostName);
try
{
options.tunnelingInfo().HTTPproxyPort(Integer.parseInt(proxyPort));
}
catch(Exception e)
{
System.err.println("Error: Proxy port number not provided.");
System.exit(CodecReturnCodes.FAILURE);
}
}
// credentials
if (options.tunnelingInfo().HTTPproxy())
{
setCredentials(options);
}
}
/*
* For BASIC authentication we need: HTTPproxyUsername, HTTPproxyPasswd For
* NTLM authentication we need: HTTPproxyUsername, HTTPproxyPasswd,
* HTTPproxyDomain, HTTPproxyLocalHostname For Negotiate/Kerberos we need:
* HTTPproxyUsername, HTTPproxyPasswd, HTTPproxyDomain,
* HTTPproxyKRB5configFile
*/
private void setCredentials(ConnectOptions options)
{
String localIPaddress = null;
String localHostName = null;
String proxyUsername = watchlistConsumerConfig.proxyUsername();
if ( proxyUsername == null)
{
System.err.println("Error: Proxy username not provided.");
System.exit(CodecReturnCodes.FAILURE);
}
String proxyPasswd = watchlistConsumerConfig.proxyPassword();
if ( proxyPasswd == null)
{
System.err.println("Error: Proxy password not provided.");
System.exit(CodecReturnCodes.FAILURE);
}
String proxyDomain = watchlistConsumerConfig.proxyDomain();
if ( proxyDomain == null)
{
System.err.println("Error: Proxy domain not provided.");
System.exit(CodecReturnCodes.FAILURE);
}
options.credentialsInfo().HTTPproxyUsername(proxyUsername);
options.credentialsInfo().HTTPproxyPasswd(proxyPasswd);
options.credentialsInfo().HTTPproxyDomain(proxyDomain);
try
{
localIPaddress = InetAddress.getLocalHost().getHostAddress();
localHostName = InetAddress.getLocalHost().getHostName();
}
catch (UnknownHostException e)
{
localHostName = localIPaddress;
}
options.credentialsInfo().HTTPproxyLocalHostname(localHostName);
String proxyKrbfile = watchlistConsumerConfig.krbFile();
if (proxyKrbfile == null)
{
System.err.println("Error: Proxy krbfile not provided.");
System.exit(CodecReturnCodes.FAILURE);
}
options.credentialsInfo().HTTPproxyKRB5configFile(proxyKrbfile);
}
String mapServiceActionString(int action)
{
if (action == MapEntryActions.DELETE) return "DELETE";
else if ( action == MapEntryActions.ADD) return "ADD";
else if (action == MapEntryActions.UPDATE) return "UPDATE";
else
return null;
}
/** Signals thread to shutdown. */
public void shutDown(boolean shutDown)
{
this.shutDown = shutDown;
mtWatchlistConsumer.shutDown = true;
}
boolean isDictionaryReady()
{
return (fieldDictionaryLoaded && enumDictionaryLoaded) || (itemDecoder.fieldDictionaryLoadedFromFile && itemDecoder.enumTypeDictionaryLoadedFromFile);
}
}
|