File size: 68,722 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 1551 1552 1553 1554 1555 1556 |
package com.thomsonreuters.upa.examples.provider;
import java.nio.ByteBuffer;
import java.util.EnumSet;
import com.thomsonreuters.upa.codec.AckMsg;
import com.thomsonreuters.upa.codec.AckMsgFlags;
import com.thomsonreuters.upa.codec.Array;
import com.thomsonreuters.upa.codec.ArrayEntry;
import com.thomsonreuters.upa.codec.Buffer;
import com.thomsonreuters.upa.codec.CodecFactory;
import com.thomsonreuters.upa.codec.CodecReturnCodes;
import com.thomsonreuters.upa.codec.DataStates;
import com.thomsonreuters.upa.codec.DataTypes;
import com.thomsonreuters.upa.codec.DecodeIterator;
import com.thomsonreuters.upa.codec.ElementEntry;
import com.thomsonreuters.upa.codec.ElementList;
import com.thomsonreuters.upa.codec.EncodeIterator;
import com.thomsonreuters.upa.codec.Msg;
import com.thomsonreuters.upa.codec.MsgClasses;
import com.thomsonreuters.upa.codec.MsgKey;
import com.thomsonreuters.upa.codec.MsgKeyFlags;
import com.thomsonreuters.upa.codec.NakCodes;
import com.thomsonreuters.upa.codec.PostMsg;
import com.thomsonreuters.upa.codec.Qos;
import com.thomsonreuters.upa.codec.QosRates;
import com.thomsonreuters.upa.codec.QosTimeliness;
import com.thomsonreuters.upa.codec.RefreshMsg;
import com.thomsonreuters.upa.codec.RefreshMsgFlags;
import com.thomsonreuters.upa.codec.RequestMsg;
import com.thomsonreuters.upa.codec.RequestMsgFlags;
import com.thomsonreuters.upa.codec.StateCodes;
import com.thomsonreuters.upa.codec.StatusMsg;
import com.thomsonreuters.upa.codec.StatusMsgFlags;
import com.thomsonreuters.upa.codec.StreamStates;
import com.thomsonreuters.upa.codec.UpdateMsg;
import com.thomsonreuters.upa.codec.UpdateMsgFlags;
import com.thomsonreuters.upa.shared.LoginRequestInfo;
import com.thomsonreuters.upa.shared.ProviderDirectoryHandler;
import com.thomsonreuters.upa.shared.ProviderSession;
import com.thomsonreuters.upa.shared.provider.ItemInfo;
import com.thomsonreuters.upa.shared.provider.ItemInfoList;
import com.thomsonreuters.upa.shared.provider.ItemRejectReason;
import com.thomsonreuters.upa.shared.provider.ItemRequestInfo;
import com.thomsonreuters.upa.shared.provider.ItemRequestInfoList;
import com.thomsonreuters.upa.shared.provider.MarketByOrderItems;
import com.thomsonreuters.upa.shared.provider.MarketByPriceItems;
import com.thomsonreuters.upa.shared.provider.MarketPriceItems;
import com.thomsonreuters.upa.shared.provider.SymbolListItems;
import com.thomsonreuters.upa.shared.rdm.marketbyprice.MarketByPriceItem;
import com.thomsonreuters.upa.shared.rdm.marketprice.MarketPriceItem;
import com.thomsonreuters.upa.shared.rdm.marketprice.MarketPriceStatus;
import com.thomsonreuters.upa.examples.common.ProviderDictionaryHandler;
import com.thomsonreuters.upa.examples.common.ProviderLoginHandler;
import com.thomsonreuters.upa.rdm.DomainTypes;
import com.thomsonreuters.upa.transport.Channel;
import com.thomsonreuters.upa.transport.ChannelState;
import com.thomsonreuters.upa.transport.Error;
import com.thomsonreuters.upa.transport.TransportBuffer;
import com.thomsonreuters.upa.transport.TransportFactory;
import com.thomsonreuters.upa.transport.TransportReturnCodes;
/**
* This is the implementation of handling item requests for the interactive
* provider application.
* <p>
* It provides methods for processing item requests from consumers and sending
* back the refresh/update messages. Methods for sending request reject/close
* status messages, initializing the item handler, checking if the item count
* per channel has been reached, checking if an item is already opened on a
* channel, checking if a stream is already in use, and closing item streams are
* also provided.
* <p>
* This handler provides data for MarketPrice, MarketByPrice and MarketByOrder
* item requests.
*/
public class ItemHandler
{
private static final int REJECT_MSG_SIZE = 1024;
private static final int ACK_MSG_SIZE = 1024;
private static final int ITEM_MSG_SIZE = 1024;
private static final int MAX_REFRESH_PARTS = 3;
private static final int POST_MSG_SIZE = 1024;
private static final Buffer _triItemName = CodecFactory.createBuffer();
private static final Buffer _privateStreamItemName = CodecFactory.createBuffer();
private static final Buffer _slNameBuf = CodecFactory.createBuffer();
private static final Buffer _batchReqName = CodecFactory.createBuffer();
private MarketPriceStatus _marketPriceStatus;
private ProviderSession _providerSession;
private SymbolListItems _symbolListItemWatchList;
private MarketByOrderItems _marketByOrderItemWatchList;
private MarketByPriceItems _marketByPriceItemWatchList;
private MarketPriceItems _marketPriceItemWatchList;
private ItemInfoList _itemInfoWatchList;
private ItemRequestInfoList _itemRequestWatchList;
private ProviderDictionaryHandler _dictionaryHandler;
private ProviderLoginHandler _loginHandler;
private int _serviceId;
private int _itemCount = 0;
private Error _error = TransportFactory.createError();
private ElementList _elementList = CodecFactory.createElementList();
private ElementEntry _elementEntry = CodecFactory.createElementEntry();
private Array _array = CodecFactory.createArray();
private ArrayEntry _arrayEntry = CodecFactory.createArrayEntry();
private EncodeIterator _encodeIter = CodecFactory.createEncodeIterator();
private AckMsg _ackMsg = (AckMsg)CodecFactory.createMsg();
private Msg _nestedMsg = CodecFactory.createMsg();
private UpdateMsg _updateMsg = (UpdateMsg)CodecFactory.createMsg();
private StatusMsg _statusMsg = (StatusMsg)CodecFactory.createMsg();
private Qos _providerQos = CodecFactory.createQos();
/**
* Instantiates a new item handler.
*
* @param providerSession the provider session
* @param dictionaryHandler the dictionary handler
* @param loginHandler the login handler
*/
public ItemHandler(ProviderSession providerSession, ProviderDictionaryHandler dictionaryHandler, ProviderLoginHandler loginHandler)
{
_providerSession = providerSession;
_marketByOrderItemWatchList = new MarketByOrderItems();
_marketByPriceItemWatchList = new MarketByPriceItems();
_marketPriceItemWatchList = new MarketPriceItems();
_marketPriceStatus = new MarketPriceStatus();
_symbolListItemWatchList = new SymbolListItems();
_itemInfoWatchList = new ItemInfoList();
_itemRequestWatchList = new ItemRequestInfoList();
_dictionaryHandler = dictionaryHandler;
_loginHandler = loginHandler;
_updateMsg.msgClass(MsgClasses.UPDATE);
_ackMsg.msgClass(MsgClasses.ACK);
_statusMsg.msgClass(MsgClasses.STATUS);
_triItemName.data("TRI");
_privateStreamItemName.data("RES-DS");
_slNameBuf.data("_UPA_ITEM_LIST");
_batchReqName.data(":ItemList");
//set Qos for provider
_providerQos.dynamic(false);
_providerQos.rate(QosRates.TICK_BY_TICK);
_providerQos.timeliness(QosTimeliness.REALTIME);
}
/**
* Initializes item handler internal structures.
*/
public void init()
{
_itemRequestWatchList.init();
_itemInfoWatchList.init();
_marketByOrderItemWatchList.init();
_marketByPriceItemWatchList.init();
_marketPriceItemWatchList.init();
_symbolListItemWatchList.init();
}
/**
* Processes an item request. This consists of storing the request
* information, then calling sendItemResponse() to send the response.
*
* @param chnl - The channel of the response
* @param msg - The partially decoded message
* @param dIter - The decode iterator
* @param error the error
* @return returns success if decoding of request message and sending of
* response message succeeds or failure if it fails.
*/
public int processRequest(Channel chnl, Msg msg, DecodeIterator dIter, Error error)
{
switch (msg.msgClass())
{
case MsgClasses.REQUEST:
if (!msg.msgKey().checkHasServiceId() || msg.msgKey().serviceId() != serviceId())
{
return sendItemRequestReject(chnl, msg.streamId(), msg.domainType(), ItemRejectReason.INVALID_SERVICE_ID, false, error);
}
//check if QoS supported
if ((msg.flags() & RequestMsgFlags.HAS_WORST_QOS) != 0 &&
(msg.flags() & RequestMsgFlags.HAS_QOS) != 0)
{
if (!((RequestMsg)msg).qos().isInRange(((RequestMsg)msg).worstQos(), _providerQos))
{
return sendItemRequestReject(chnl, msg.streamId(),
msg.domainType(), ItemRejectReason.QOS_NOT_SUPPORTED, false, error);
}
}
else if ((msg.flags() & RequestMsgFlags.HAS_QOS) != 0)
{
if (!((RequestMsg)msg).qos().equals(_providerQos))
{
return sendItemRequestReject(chnl, msg.streamId(),
msg.domainType(), ItemRejectReason.QOS_NOT_SUPPORTED, false, error);
}
}
//check for unsupported key attribute information
if (msg.msgKey().checkHasAttrib())
{
return sendItemRequestReject(chnl, msg.streamId(), msg.domainType(), ItemRejectReason.KEY_ENC_ATTRIB_NOT_SUPPORTED, false, error);
}
if (((RequestMsg)msg).checkHasBatch())
{
return processBatchRequest(chnl, msg, dIter, ((RequestMsg)msg).checkPrivateStream(), error);
}
else
{
return processSingleItemRequest(chnl, msg, dIter, ((RequestMsg)msg).checkPrivateStream(), error);
}
case MsgClasses.CLOSE:
System.out.println("Received Item close for streamId " + msg.streamId());
closeStream(chnl, msg.streamId());
break;
case MsgClasses.POST:
return processPost(chnl, msg, dIter, error);
default:
error.text("Received Unhandled Item Msg Class: " + MsgClasses.toString(msg.msgClass()));
return CodecReturnCodes.FAILURE;
}
return CodecReturnCodes.SUCCESS;
}
private int processBatchRequest(Channel chnl, Msg msg, DecodeIterator dIter, boolean isPrivateStream, Error error)
{
System.out.println("Received batch item request (streamId=" + msg.streamId() + ") on domain " + DomainTypes.toString(msg.domainType()));
/* check if batch stream already in use with a different key */
if (isStreamInUse(chnl, msg.streamId(), msg.msgKey()))
{
return sendItemRequestReject(chnl, msg.streamId(), msg.domainType(), ItemRejectReason.STREAM_ALREADY_IN_USE, isPrivateStream, error);
}
// The payload of a batch request contains an elementList
int ret = _elementList.decode(dIter, null);
if (ret < CodecReturnCodes.SUCCESS)
{
return sendItemRequestReject(chnl, msg.streamId(), msg.domainType(), ItemRejectReason.REQUEST_DECODE_ERROR, isPrivateStream, error);
}
EnumSet<ItemRejectReason> rejectReasonSet = EnumSet.noneOf(ItemRejectReason.class);
int dataState = DataStates.NO_CHANGE;
// The list of items being requested is in an elementList entry with the
// element name of ":ItemList"
while ((ret = _elementEntry.decode(dIter)) != CodecReturnCodes.END_OF_CONTAINER)
{
if (_elementEntry.name().equals(_batchReqName))
{
// The list of items names is in an array
ret = _array.decode(dIter);
if (ret < CodecReturnCodes.SUCCESS)
{
error.text("Array.decode() for batch request failed with return code: " + CodecReturnCodes.toString(ret));
break;
}
// Get each requested item name
// We will assign consecutive stream IDs for each item
// starting with the stream following the one the batch request
// was made on
int itemStream = msg.streamId();
while ((ret = _arrayEntry.decode(dIter)) != CodecReturnCodes.END_OF_CONTAINER)
{
if (ret < CodecReturnCodes.SUCCESS)
{
error.text("ArrayEntry.decode() failed with return code: " + CodecReturnCodes.toString(ret));
dataState = DataStates.SUSPECT;
continue;
}
//check if stream already in use with a different key
itemStream++; // increment stream ID with each item we find
// in the batch request
//check for private stream special item name without
//private stream flag set
if (!isPrivateStream && _arrayEntry.encodedData().equals(_privateStreamItemName))
{
sendItemRequestReject(chnl, itemStream, msg.domainType(), ItemRejectReason.PRIVATE_STREAM_REDIRECT, isPrivateStream, error);
dataState = DataStates.SUSPECT;
continue;
}
//all of the items requested have the same key. They use
//the key of the batch request.
//The only difference is the name
msg.msgKey().flags(msg.msgKey().flags() | MsgKeyFlags.HAS_NAME);
msg.msgKey().name(_arrayEntry.encodedData());
ItemRequestInfo itemReqInfo = getMatchingItemReqInfo(chnl, msg, itemStream, rejectReasonSet);
ItemRejectReason rejectReason = rejectReasonSet.iterator().hasNext() ? rejectReasonSet.iterator().next() : ItemRejectReason.NONE;
if (itemReqInfo == null && rejectReason != ItemRejectReason.NONE)
{
sendItemRequestReject(chnl, itemStream, msg.domainType(), rejectReason, isPrivateStream, error);
dataState = DataStates.SUSPECT;
continue;
}
else if (itemReqInfo != null)
{
// Batch requests should not be used to reissue item
// requests.
sendItemRequestReject(chnl, itemStream, msg.domainType(), ItemRejectReason.BATCH_ITEM_REISSUE, isPrivateStream, error);
dataState = DataStates.SUSPECT;
continue;
}
rejectReasonSet.clear();
itemReqInfo = getNewItemReqInfo(chnl, msg, itemStream, rejectReasonSet);
if (itemReqInfo == null)
{
// Batch requests should not be used to reissue item
// requests.
sendItemRequestReject(chnl, itemStream, msg.domainType(), rejectReasonSet.iterator().next(), isPrivateStream, error);
dataState = DataStates.SUSPECT;
continue;
}
if (((RequestMsg)msg).checkPrivateStream())
{
System.out.println("Received Private Stream Item Request for " + itemReqInfo.itemName + "(streamId=" + itemStream + ") on domain " + DomainTypes.toString(itemReqInfo.domainType));
}
else
{
System.out.println("Received Item Request for " + itemReqInfo.itemName + "(streamId=" + itemStream + ") on domain " + DomainTypes.toString(itemReqInfo.domainType));
}
//send item response/refresh if required
if (!((RequestMsg)msg).checkNoRefresh())
sendItemResponse(itemReqInfo, error);
if (!itemReqInfo.isStreamingRequest)
{
//snapshot request - so we dont have to send updates
//free item request info
freeItemReqInfo(itemReqInfo);
}
else
{
itemReqInfo.itemInfo.isRefreshRequired = false;
}
}
}
}
//now that we have processed the batch request and sent responses for
//all the items, send a response for the batch request itself
//get a buffer for the batch status close
TransportBuffer msgBuf = chnl.getBuffer(ACK_MSG_SIZE, false, error);
if (msgBuf == null)
{
return CodecReturnCodes.FAILURE;
}
//we close the stream the batch request was made on (and later send the
//item responses on different streams)
ret = encodeBatchCloseStatus(chnl, msg.domainType(), msgBuf, msg.streamId(), dataState, error);
if (ret != CodecReturnCodes.SUCCESS)
{
return ret;
}
//send batch status close
return _providerSession.write(chnl, msgBuf, error);
}
private int encodeBatchCloseStatus(Channel chnl, int domainType, TransportBuffer msgBuf, int streamId, int dataState, Error error)
{
_statusMsg.clear();
/* set-up message */
_statusMsg.msgClass(MsgClasses.STATUS);
_statusMsg.streamId(streamId);
_statusMsg.domainType(domainType);
_statusMsg.containerType(DataTypes.NO_DATA);
_statusMsg.flags(StatusMsgFlags.HAS_STATE);
_statusMsg.state().streamState(StreamStates.CLOSED);
_statusMsg.state().dataState(dataState);
_statusMsg.state().code(StateCodes.NONE);
_statusMsg.state().text().data("Stream closed for batch");
/* clear encode iterator */
_encodeIter.clear();
/* encode message */
int ret = _encodeIter.setBufferAndRWFVersion(msgBuf, chnl.majorVersion(), chnl.minorVersion());
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("EncodeIterator.setBufferAndRWFVersion() failed with return code: " + CodecReturnCodes.toString(ret));
return ret;
}
ret = _statusMsg.encode(_encodeIter);
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("StatusMsg.encode() failed");
return ret;
}
return CodecReturnCodes.SUCCESS;
}
private int processSingleItemRequest(Channel chnl, Msg msg, DecodeIterator dIter, boolean isPrivateStream, Error error)
{
int domainType = msg.domainType();
//check for private stream special item name without private stream
//flag set
if (!isPrivateStream && _privateStreamItemName.equals(msg.msgKey().name()))
{
return sendItemRequestReject(chnl, msg.streamId(), domainType, ItemRejectReason.PRIVATE_STREAM_REDIRECT, isPrivateStream, error);
}
//check for invalid symbol list request
if ((domainType == DomainTypes.SYMBOL_LIST) && (msg.msgKey().name() != null))
{
//if the consumer specified symbol list name isn't
//"_UPA_ITEM_LIST", reject it
if (!msg.msgKey().name().equals(_slNameBuf))
{
return sendItemRequestReject(chnl, msg.streamId(), domainType, ItemRejectReason.ITEM_NOT_SUPPORTED, isPrivateStream, error);
}
}
//get request info structure
//check if item already opened with exact same key on another stream
EnumSet<ItemRejectReason> rejectReasonSet = EnumSet.noneOf(ItemRejectReason.class);
//Check for reissue request
ItemRequestInfo itemReqInfo = getMatchingItemReqInfo(chnl, msg, msg.streamId(), rejectReasonSet);
ItemRejectReason itemReject = (rejectReasonSet.iterator().hasNext() ? rejectReasonSet.iterator().next() : ItemRejectReason.NONE);
if (itemReqInfo == null && itemReject == ItemRejectReason.NONE)
{
//No matching items. This is a new request.
rejectReasonSet.clear();
itemReqInfo = getNewItemReqInfo(chnl, msg, msg.streamId(), rejectReasonSet);
}
if (itemReqInfo == null)
{
return sendItemRequestReject(chnl, msg.streamId(), domainType, rejectReasonSet.iterator().next(), isPrivateStream, error);
}
if (((RequestMsg)msg).checkPrivateStream())
{
System.out.println("Received Private Stream Item Request for " + itemReqInfo.itemName + "(streamId=" + msg.streamId() + ") on domain " + DomainTypes.toString(itemReqInfo.domainType));
}
else
{
System.out.println("Received Item Request for " + itemReqInfo.itemName + "(streamId=" + msg.streamId() + ") on domain " + DomainTypes.toString(itemReqInfo.domainType));
}
//send item refresh
if (!((RequestMsg)msg).checkNoRefresh())
{
itemReqInfo.itemInfo.isRefreshRequired = true;
int ret = sendItemResponse(itemReqInfo, error);
if (ret != CodecReturnCodes.SUCCESS)
return ret;
}
if (!itemReqInfo.isStreamingRequest)
{
//snapshot request - so we dont have to send updates
//free item request info
freeItemReqInfo(itemReqInfo);
}
else
{
itemReqInfo.itemInfo.isRefreshRequired = false;
}
return CodecReturnCodes.SUCCESS;
}
private int sendItemRequestReject(Channel channel, int streamId, int domainType, ItemRejectReason reason, boolean isPrivateStream, Error error)
{
//get a buffer for the item request reject status
TransportBuffer msgBuf = channel.getBuffer(REJECT_MSG_SIZE, false, error);
if (msgBuf == null)
return CodecReturnCodes.FAILURE;
_marketPriceStatus.clear();
_marketPriceStatus.applyHasState();
_marketPriceStatus.state().streamState(StreamStates.CLOSED_RECOVER);
_marketPriceStatus.state().dataState(DataStates.SUSPECT);
if (isPrivateStream)
_marketPriceStatus.applyPrivateStream();
//encode request reject status
switch (reason)
{
case ITEM_COUNT_REACHED:
_marketPriceStatus.state().code(StateCodes.TOO_MANY_ITEMS);
_marketPriceStatus.state().text().data("Item request rejected for stream id " + streamId + "- item count reached for this channel");
break;
case INVALID_SERVICE_ID:
_marketPriceStatus.state().code(StateCodes.USAGE_ERROR);
_marketPriceStatus.state().streamState(StreamStates.CLOSED);
_marketPriceStatus.state().text().data("Item request rejected for stream id " + streamId + "- service id invalid");
break;
case QOS_NOT_SUPPORTED:
_marketPriceStatus.state().code(StateCodes.USAGE_ERROR);
_marketPriceStatus.state().streamState(StreamStates.CLOSED);
_marketPriceStatus.state().text().data("Item request rejected for stream id " + streamId + "- QoS not supported");
break;
case ITEM_ALREADY_OPENED:
_marketPriceStatus.state().code(StateCodes.ALREADY_OPEN);
_marketPriceStatus.state().streamState(StreamStates.CLOSED);
_marketPriceStatus.state().text().data("Item request rejected for stream id " + streamId + "- item already open with exact same key on another stream");
break;
case STREAM_ALREADY_IN_USE:
_marketPriceStatus.state().code(StateCodes.USAGE_ERROR);
_marketPriceStatus.state().streamState(StreamStates.CLOSED);
_marketPriceStatus.state().text().data("Item request rejected for stream id " + streamId + "- stream already in use with a different key");
break;
case KEY_ENC_ATTRIB_NOT_SUPPORTED:
_marketPriceStatus.state().code(StateCodes.USAGE_ERROR);
_marketPriceStatus.state().streamState(StreamStates.CLOSED);
_marketPriceStatus.state().text().data("Item request rejected for stream id " + streamId + "- this provider does not support key attribute information");
break;
case ITEM_NOT_SUPPORTED:
_marketPriceStatus.state().code(StateCodes.USAGE_ERROR);
_marketPriceStatus.state().streamState(StreamStates.CLOSED);
_marketPriceStatus.state().text().data("Item request rejected for stream id " + streamId + "- item not supported");
break;
case PRIVATE_STREAM_REDIRECT:
_marketPriceStatus.applyPrivateStream();
_marketPriceStatus.state().code(StateCodes.NONE);
_marketPriceStatus.state().streamState(StreamStates.REDIRECTED);
_marketPriceStatus.state().text().data("Standard stream redirect to private for stream id " + streamId + " - this item must be requested via private stream");
break;
case PRIVATE_STREAM_MISMATCH:
_marketPriceStatus.applyPrivateStream();
_marketPriceStatus.state().code(StateCodes.USAGE_ERROR);
_marketPriceStatus.state().streamState(StreamStates.CLOSED);
_marketPriceStatus.state().text().data("Rejected request for stream id " + streamId + " - reissue via batch request is not allowed");
break;
//APIQA
case TEMPORARY_REJECTED:
_marketPriceStatus.applyPrivateStream();
_marketPriceStatus.state().code(StateCodes.ERROR);
_marketPriceStatus.state().streamState(StreamStates.CLOSED);
_marketPriceStatus.state().text().data("Rejected request for stream id " + streamId + " - temporary for testing");
break;
default:
break;
}
_encodeIter.clear();
_marketPriceStatus.streamId(streamId);
_marketPriceStatus.domainType(domainType);
int ret = _encodeIter.setBufferAndRWFVersion(msgBuf, channel.majorVersion(), channel.minorVersion());
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("EncodeIterator.setBufferAndRWFVersion() failed with return code: " + CodecReturnCodes.toString(ret));
return ret;
}
ret = _marketPriceStatus.encode(_encodeIter);
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("MarketPriceStatus.encode() failed");
return ret;
}
System.out.println("Rejecting Item Request with streamId=" + streamId + " and domain " + DomainTypes.toString(domainType) + ". Reason: " + ItemRejectReason.toString(reason));
return _providerSession.write(channel, msgBuf, error);
}
private void closeStream(Channel chnl, int streamId)
{
ItemRequestInfo itemRequestInfo = _itemRequestWatchList.get(chnl, streamId);
//remove original item request information
if (itemRequestInfo != null)
{
System.out.println("Closing item stream id " + itemRequestInfo.streamId + " with item name: " + itemRequestInfo.itemName);
freeItemReqInfo(itemRequestInfo);
}
else
{
System.out.println("No item found for StreamId: " + streamId);
}
}
private void deleteSymbolListItem(ItemRequestInfo itemReqInfo)
{
Buffer delItem = itemReqInfo.itemName;
// TRI and RES-DS are always present in our symbol list and should never
// be deleted
if (_triItemName.equals(delItem) || _privateStreamItemName.equals(delItem))
{
return;
}
//search the symbol list, and delete the item if the interest count is 0
for (int i = 2; i < SymbolListItems.MAX_SYMBOL_LIST_SIZE; i++)
{
if (delItem.equals(_symbolListItemWatchList.symbolListItemName(i)))
{
_symbolListItemWatchList.decrementInterestCount(i);
//no more interest in the item, so remove it from the symbol list
if (_symbolListItemWatchList.interestCount(i) == 0)
{
_symbolListItemWatchList.clear(i);
_symbolListItemWatchList.decrementItemCount();
//find all consumers using the symbol list domain and send them updates
for (ItemRequestInfo itemReqInfoL : _itemRequestWatchList)
{
//Only send Symbol List updates to active channels that have made requests
if (itemReqInfoL.domainType == DomainTypes.SYMBOL_LIST && itemReqInfoL.channel.state() == ChannelState.ACTIVE)
{
sendSLItemUpdates(itemReqInfoL.channel, itemReqInfo, SymbolListItems.SYMBOL_LIST_UPDATE_DELETE, itemReqInfoL.streamId);
}
}
}
break;
}
}
}
private void freeItemReqInfo(ItemRequestInfo itemReqInfo)
{
if (itemReqInfo != null)
{
//decrement item interest count
if (itemReqInfo.itemInfo != null && itemReqInfo.itemInfo.interestCount > 0)
{
itemReqInfo.itemInfo.interestCount--;
}
if (itemReqInfo.domainType != DomainTypes.SYMBOL_LIST && itemReqInfo.itemInfo.interestCount == 0)
{
deleteSymbolListItem(itemReqInfo);
}
//free item information if no more interest
if (itemReqInfo.itemInfo.interestCount == 0)
{
itemReqInfo.itemInfo.clear();
}
//free item request information
itemReqInfo.clear();
}
}
/**
* Sends the item close status message(s) for a channel. This consists of
* finding all request information for this channel and sending the close
* status messages to the channel.
*
* @param channel - The channel to send close status message(s) to
* @param error the error
* @return the int
*/
public int sendCloseStatusMsgs(Channel channel, Error error)
{
int ret = 0;
for (ItemRequestInfo itemRequestInfo : _itemRequestWatchList)
{
if (itemRequestInfo.isInUse && itemRequestInfo.channel == channel)
{
ret = sendCloseStatus(channel, itemRequestInfo, error);
if (ret != CodecReturnCodes.SUCCESS)
return ret;
}
}
return CodecReturnCodes.SUCCESS;
}
private int sendCloseStatus(Channel channel, ItemRequestInfo itemReqInfo, Error error)
{
//get a buffer for the close status
TransportBuffer msgBuf = channel.getBuffer(1024, false, error);
if (msgBuf == null)
return CodecReturnCodes.FAILURE;
//encode close status
_marketPriceStatus.clear();
_marketPriceStatus.streamId(itemReqInfo.streamId);
_marketPriceStatus.domainType(itemReqInfo.domainType);
if (itemReqInfo.isPrivateStreamRequest)
_marketPriceStatus.applyPrivateStream();
_marketPriceStatus.applyHasState();
_marketPriceStatus.state().streamState(StreamStates.CLOSED);
_marketPriceStatus.state().dataState(DataStates.SUSPECT);
_marketPriceStatus.state().text().data("Stream closed for item: " + itemReqInfo.itemName);
_encodeIter.clear();
int ret = _encodeIter.setBufferAndRWFVersion(msgBuf, channel.majorVersion(), channel.minorVersion());
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("EncodeIterator.setBufferAndRWFVersion() failed with return code: " + CodecReturnCodes.toString(ret));
return ret;
}
ret = _marketPriceStatus.encode(_encodeIter);
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("MarketPriceStatus.encode() failed");
return ret;
}
return _providerSession.write(channel, msgBuf, error);
}
private int encodeAck(TransportBuffer msgBuf, Channel chnl, PostMsg postMsg, int nakCode, String text, Error error)
{
//set-up message
_ackMsg.msgClass(MsgClasses.ACK);
_ackMsg.streamId(postMsg.streamId());
_ackMsg.domainType(postMsg.domainType());
_ackMsg.containerType(DataTypes.NO_DATA);
_ackMsg.flags(AckMsgFlags.NONE);
_ackMsg.nakCode(nakCode);
_ackMsg.ackId(postMsg.postId());
_ackMsg.seqNum(postMsg.seqNum());
if (nakCode != NakCodes.NONE)
_ackMsg.applyHasNakCode();
if (postMsg.checkHasSeqNum())
_ackMsg.applyHasSeqNum();
if (text != null)
{
_ackMsg.applyHasText();
_ackMsg.text().data(text);
}
//encode message
_encodeIter.clear();
int ret = _encodeIter.setBufferAndRWFVersion(msgBuf, chnl.majorVersion(), chnl.minorVersion());
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("EncodeIterator.setBufferAndRWFVersion() failed with return code: " + CodecReturnCodes.toString(ret));
return ret;
}
ret = _ackMsg.encode(_encodeIter);
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("AckMsg.encode() failed");
return ret;
}
return CodecReturnCodes.SUCCESS;
}
private ItemRequestInfo getMatchingItemReqInfo(Channel channel, Msg msg, int streamId, EnumSet<ItemRejectReason> rejectReason)
{
for (ItemRequestInfo itemRequestInfo : _itemRequestWatchList)
{
if (itemRequestInfo.isInUse && itemRequestInfo.channel == channel)
{
if (itemRequestInfo.domainType == msg.domainType()
&& (itemRequestInfo.msgKey.equals(msg.msgKey())))
{
//The request has the same domain and key as one currently
//open for this channel.
if (itemRequestInfo.streamId != streamId)
{
//The request has a different stream ID, meaning it
//would open the same item on another stream. This is
//not allowed(except for private streams).
if (!((RequestMsg)msg).checkPrivateStream())
{
rejectReason.add(ItemRejectReason.ITEM_ALREADY_OPENED);
return null;
}
//Otherwise continue checking the list
}
else
{
//Check that the private stream flag matches correctly.
if (((RequestMsg)msg).checkPrivateStream() && !itemRequestInfo.isPrivateStreamRequest
|| !(((RequestMsg)msg).checkPrivateStream()) && itemRequestInfo.isPrivateStreamRequest)
{
//This item would be a match except that the
//private stream flag does not match.
rejectReason.add(ItemRejectReason.PRIVATE_STREAM_MISMATCH);
return null;
}
//The domain, key, stream ID, and private stream flag
//all match, so this item is a match, and the request
//is a reissue.
return itemRequestInfo;
}
}
else if (itemRequestInfo.streamId == streamId)
{
//This stream ID is already in use for a different item.
rejectReason.add(ItemRejectReason.STREAM_ALREADY_IN_USE);
return null;
}
}
}
rejectReason.add(ItemRejectReason.NONE);
return null;
}
private int sendAck(Channel chnl, PostMsg postMsg, int nakCode, String errText, Error error)
{
//send an ack if it was requested
if (postMsg.checkAck())
{
TransportBuffer msgBuf = chnl.getBuffer(ACK_MSG_SIZE, false, error);
if (msgBuf == null)
{
return CodecReturnCodes.FAILURE;
}
int ret = encodeAck(msgBuf, chnl, postMsg, nakCode, errText, error);
if (ret != CodecReturnCodes.SUCCESS)
return ret;
return _providerSession.write(chnl, msgBuf, error);
}
return CodecReturnCodes.SUCCESS;
}
//APIQA
// Add count variable to reject every 5th request.
public int rejectItemCount = 0;
/*
* Initializes a new item request information structure for a channel (or
* rejects the request if its channel has too many items open).
*/
private ItemRequestInfo getNewItemReqInfo(Channel channel, Msg msg, int stream, EnumSet<ItemRejectReason> rejectReasons)
{
//APIQA
if (++rejectItemCount%5 == 0) {
rejectReasons.add(ItemRejectReason.TEMPORARY_REJECTED);
return null;
}
ItemRequestInfo itemRequestInfo = null;
int count = 0;
//Find an available item request info structure to use, and check that
//the channel has not reached its allowed limit of open items.
for (ItemRequestInfo itemReqInfo : _itemRequestWatchList)
{
if (itemReqInfo.isInUse)
{
if (itemReqInfo.channel == channel)
{
++count;
if (count >= ProviderDirectoryHandler.OPEN_LIMIT)
{
//Consumer has requested too many items.
rejectReasons.add(ItemRejectReason.ITEM_COUNT_REACHED);
return null;
}
}
}
else if (itemRequestInfo == null)
{
itemRequestInfo = itemReqInfo;
break;
}
}
if (itemRequestInfo == null)
{
rejectReasons.add(ItemRejectReason.ITEM_COUNT_REACHED);
return null;
}
itemRequestInfo.channel = channel;
itemRequestInfo.isInUse = true;
if (copyMsgKey(itemRequestInfo.msgKey, msg.msgKey()) != CodecReturnCodes.SUCCESS)
{
rejectReasons.add(ItemRejectReason.ITEM_NOT_SUPPORTED);
return null;
}
itemRequestInfo.domainType = msg.domainType();
//copy item name buffer
ByteBuffer byteBuffer = ByteBuffer.allocate(itemRequestInfo.msgKey.name().length());
itemRequestInfo.msgKey.name().copy(byteBuffer);
itemRequestInfo.itemName.data(byteBuffer);
int msgFlags = msg.flags();
if ( (msgFlags & RequestMsgFlags.PRIVATE_STREAM) != 0)
{
itemRequestInfo.isPrivateStreamRequest(true);
}
/* get IsStreamingRequest */
if ((msgFlags & RequestMsgFlags.STREAMING) != 0)
{
itemRequestInfo.isStreamingRequest(true);
}
/* get IncludeKeyInUpdates */
if ((msgFlags & RequestMsgFlags.MSG_KEY_IN_UPDATES) != 0)
{
itemRequestInfo.includeKeyInUpdates(true);
}
//get item information
itemRequestInfo.itemInfo = _itemInfoWatchList.get(channel, itemRequestInfo.itemName,
itemRequestInfo.domainType, itemRequestInfo.isPrivateStreamRequest());
if (itemRequestInfo.itemInfo == null)
{
rejectReasons.add(ItemRejectReason.ITEM_NOT_SUPPORTED);
}
else
{
switch (itemRequestInfo.domainType)
{
case DomainTypes.MARKET_PRICE:
itemRequestInfo.itemInfo.itemData = _marketPriceItemWatchList.get(itemRequestInfo.itemName.toString());
break;
case DomainTypes.MARKET_BY_ORDER:
itemRequestInfo.itemInfo.itemData = _marketByOrderItemWatchList.get(itemRequestInfo.itemName.toString());
break;
case DomainTypes.MARKET_BY_PRICE:
itemRequestInfo.itemInfo.itemData = _marketByPriceItemWatchList.get(itemRequestInfo.itemName.toString());
break;
case DomainTypes.SYMBOL_LIST:
break;
default:
break;
}
if ((itemRequestInfo.itemInfo.itemData == null) && (itemRequestInfo.domainType != DomainTypes.SYMBOL_LIST))
{
rejectReasons.add(ItemRejectReason.ITEM_COUNT_REACHED);
return null;
}
if (itemRequestInfo.itemInfo.domainType != DomainTypes.SYMBOL_LIST)
{
addSymbolListItem(channel, itemRequestInfo);
}
}
//get IsStreamingRequest
if (((RequestMsg)msg).checkStreaming())
{
itemRequestInfo.isStreamingRequest = true;
}
//IsPrivateStreamRequest
if (((RequestMsg)msg).checkPrivateStream())
{
itemRequestInfo.isPrivateStreamRequest = true;
}
//IncludeKeyInUpdates
if (((RequestMsg)msg).checkMsgKeyInUpdates())
{
itemRequestInfo.includeKeyInUpdates = true;
}
//increment item interest count if new request
itemRequestInfo.itemInfo.interestCount++;
itemRequestInfo.streamId = stream;
//provide a refresh if one was requested.
itemRequestInfo.itemInfo.isRefreshRequired = (((RequestMsg)msg).checkNoRefresh()) ? false : true;
return itemRequestInfo;
}
private void addSymbolListItem(Channel channel, ItemRequestInfo itemReqInfo)
{
int itemVacancy = 0;
Buffer newItem = itemReqInfo.itemName;
boolean foundVacancy = false;
//TRI and RES-DS are added to our symbol list at initialization, and
//are always present so they never need to be added again
if (_triItemName.equals(newItem) || _privateStreamItemName.equals(newItem) || _itemCount >= SymbolListItems.MAX_SYMBOL_LIST_SIZE)
{
return;
}
//check to see if this item is already in the item list
for (int i = 2; i < SymbolListItems.MAX_SYMBOL_LIST_SIZE; i++)
{
//if the item is already present, increment the interest count
if (newItem.equals(_symbolListItemWatchList.symbolListItemName(i)))
{
_symbolListItemWatchList.incrementInterestCount(i);
return;
}
if ((_symbolListItemWatchList.getStatus(i) == false) && foundVacancy == false)
{
//store the index of the first vacancy in the symbol list
foundVacancy = true;
itemVacancy = i;
}
}
//add the new item name to the symbol list
_symbolListItemWatchList.symbolListItemName(newItem, itemVacancy);
_symbolListItemWatchList.incrementInterestCount(itemVacancy);
//find all consumers currently using the symbol list domain, and send
//them updates
for (ItemRequestInfo itemReqInfoL : _itemRequestWatchList)
{
if (itemReqInfoL.domainType == DomainTypes.SYMBOL_LIST)
{
sendSLItemUpdates(itemReqInfoL.channel, itemReqInfo, SymbolListItems.SYMBOL_LIST_UPDATE_ADD, itemReqInfoL.streamId);
}
}
}
private void sendSLItemUpdates(Channel chnl, ItemRequestInfo itemReqInfo, int responseType, int streamId)
{
//get a buffer for the response
TransportBuffer msgBuf = chnl.getBuffer(SymbolListItems.MAX_SYMBOL_LIST_SIZE, false, _error);
if (msgBuf == null)
{
System.out.println("chnl.getBuffer(): Failed " + _error.text());
return;
}
int ret = _symbolListItemWatchList.encodeResponse(chnl, itemReqInfo.itemInfo, msgBuf, streamId, true, serviceId(), itemReqInfo.isStreamingRequest, _dictionaryHandler.dictionary(), responseType, _error);
if (ret != CodecReturnCodes.SUCCESS)
{
System.out.println("encodeSymbolListResponse() failed");
}
if (_providerSession.write(chnl, msgBuf, _error) == TransportReturnCodes.FAILURE)
System.out.println("Error writing message: " + _error.text());
}
/**
* Sends the item update(s) for a channel. This consists of finding all
* request information for this channel, and sending the responses to the
* channel.
*
* @param channel - The channel of the response
* @param error - error information in case of failure.
* @return returns success if sending of item updates succeeds or failure if
* it fails.
*/
public int sendItemUpdates(Channel channel, Error error)
{
int ret;
for (ItemRequestInfo itemReqInfo : _itemRequestWatchList)
{
if (itemReqInfo.isInUse && itemReqInfo.channel == channel)
{
ret = sendItemResponse(itemReqInfo, error);
if (ret != CodecReturnCodes.SUCCESS)
return ret;
}
}
return CodecReturnCodes.SUCCESS;
}
private int sendItemResponse(ItemRequestInfo itemReqInfo, Error error)
{
//market by price is handled separately due to multi-part refresh
if (itemReqInfo.domainType == DomainTypes.MARKET_BY_PRICE)
{
return sendMBPItemResponse(itemReqInfo, error);
}
//get a buffer for the response
TransportBuffer msgBuf = itemReqInfo.channel.getBuffer(ITEM_MSG_SIZE, false, error);
if (msgBuf == null)
return CodecReturnCodes.FAILURE;
//Encode the message with data appopriate for the domain
switch (itemReqInfo.domainType)
{
case DomainTypes.MARKET_PRICE:
//encode market price response
int ret = _marketPriceItemWatchList.encodeResponse(itemReqInfo.channel, itemReqInfo.itemInfo, msgBuf, true, itemReqInfo.streamId, itemReqInfo.isStreamingRequest, itemReqInfo.isPrivateStreamRequest, serviceId(), _dictionaryHandler.dictionary(), error);
if (ret != CodecReturnCodes.SUCCESS)
{
return ret;
}
break;
case DomainTypes.MARKET_BY_ORDER:
//encode market by order response
ret = _marketByOrderItemWatchList.encodeResponse(itemReqInfo.channel, itemReqInfo.itemInfo, msgBuf, true, itemReqInfo.streamId, itemReqInfo.isStreamingRequest, itemReqInfo.isPrivateStreamRequest, serviceId(), _dictionaryHandler.dictionary(), error);
if (ret != CodecReturnCodes.SUCCESS)
{
return ret;
}
break;
case DomainTypes.SYMBOL_LIST:
//encode symbol list response
//only encode refresh responses for the symbol list from this
//method. symbol list update responses are handled separately
if (itemReqInfo.itemInfo.isRefreshRequired)
{
ret = _symbolListItemWatchList.encodeResponse(itemReqInfo.channel, itemReqInfo.itemInfo, msgBuf, itemReqInfo.streamId, true, serviceId(), itemReqInfo.isStreamingRequest, _dictionaryHandler.dictionary(), SymbolListItems.SYMBOL_LIST_REFRESH, error);
if (ret != CodecReturnCodes.SUCCESS)
{
return ret;
}
}
else
{
itemReqInfo.channel.releaseBuffer(msgBuf, error);
return CodecReturnCodes.SUCCESS;
}
break;
default:
error.text("Received unhandled domain " + itemReqInfo.domainType + " for item ");
return CodecReturnCodes.FAILURE;
}
//send item response
return _providerSession.write(itemReqInfo.channel, msgBuf, error);
}
private int sendMBPItemResponse(ItemRequestInfo itemReqInfo, Error error)
{
MarketByPriceItem mbpItem = new MarketByPriceItem();
if (itemReqInfo.itemInfo.isRefreshRequired)
{
mbpItem = (MarketByPriceItem)itemReqInfo.itemInfo.itemData;
for (int i = 0; i < MAX_REFRESH_PARTS; i++)
{
//get a buffer for the response
TransportBuffer msgBuf = itemReqInfo.channel.getBuffer(ITEM_MSG_SIZE, false, error);
if (msgBuf == null)
return CodecReturnCodes.FAILURE;
if (msgBuf != null)
{
//encode market by price refresh
int ret = _marketByPriceItemWatchList.encodeRefresh(itemReqInfo.channel, itemReqInfo.itemInfo, msgBuf, true, itemReqInfo.streamId, itemReqInfo.isStreamingRequest, itemReqInfo.isPrivateStreamRequest, serviceId(), _dictionaryHandler.dictionary(), i, error);
if (ret != CodecReturnCodes.SUCCESS)
{
return ret;
}
//send item response
ret = _providerSession.write(itemReqInfo.channel, msgBuf, error);
if (ret != TransportReturnCodes.SUCCESS)
return ret;
}
//send an update between each part of the refresh
if (i < MAX_REFRESH_PARTS - 1)
{
mbpItem.priceInfoList.get(0).ORDER_SIZE.value(mbpItem.priceInfoList.get(0).ORDER_SIZE.toDouble() + i + 1, mbpItem.priceInfoList.get(0).ORDER_SIZE.hint()); // change
// order
//size for update
mbpItem.priceInfoList.get(1).ORDER_SIZE.value(mbpItem.priceInfoList.get(1).ORDER_SIZE.toDouble() + i + 1, mbpItem.priceInfoList.get(1).ORDER_SIZE.hint()); // change
mbpItem.priceInfoList.get(2).ORDER_SIZE.value(mbpItem.priceInfoList.get(2).ORDER_SIZE.toDouble() + i + 1, mbpItem.priceInfoList.get(2).ORDER_SIZE.hint()); // change
// order
//get a buffer for the response
msgBuf = itemReqInfo.channel.getBuffer(ITEM_MSG_SIZE, false, error);
if (msgBuf == null)
return CodecReturnCodes.FAILURE;
//encode market by price update
int ret = _marketByPriceItemWatchList.encodeUpdate(itemReqInfo.channel, itemReqInfo.itemInfo, msgBuf, true, itemReqInfo.streamId, itemReqInfo.isStreamingRequest, itemReqInfo.isPrivateStreamRequest, serviceId(), _dictionaryHandler.dictionary(), error);
if (ret != CodecReturnCodes.SUCCESS)
{
return ret;
}
//send item response
ret = _providerSession.write(itemReqInfo.channel, msgBuf, error);
if (ret != TransportReturnCodes.SUCCESS)
{
return ret;
}
mbpItem.priceInfoList.get(0).ORDER_SIZE.value(mbpItem.priceInfoList.get(0).ORDER_SIZE.toDouble() - (i + 1), mbpItem.priceInfoList.get(0).ORDER_SIZE.hint()); // change
// order
// size for update
mbpItem.priceInfoList.get(1).ORDER_SIZE.value(mbpItem.priceInfoList.get(1).ORDER_SIZE.toDouble() - (i + 1), mbpItem.priceInfoList.get(1).ORDER_SIZE.hint()); // change
// order
mbpItem.priceInfoList.get(2).ORDER_SIZE.value(mbpItem.priceInfoList.get(2).ORDER_SIZE.toDouble() - (i + 1), mbpItem.priceInfoList.get(2).ORDER_SIZE.hint()); // change
}
}
}
else
//update
{
//get a buffer for the response
TransportBuffer msgBuf = itemReqInfo.channel.getBuffer(ITEM_MSG_SIZE, false, error);
if (msgBuf == null)
return CodecReturnCodes.FAILURE;
//encode market by price update
int ret = _marketByPriceItemWatchList.encodeUpdate(itemReqInfo.channel, itemReqInfo.itemInfo, msgBuf, true, itemReqInfo.streamId, itemReqInfo.isStreamingRequest, itemReqInfo.isPrivateStreamRequest, serviceId(), _dictionaryHandler.dictionary(), error);
if (ret != CodecReturnCodes.SUCCESS)
{
return ret;
}
//send item response
return _providerSession.write(itemReqInfo.channel, msgBuf, error);
}
return CodecReturnCodes.SUCCESS;
}
private int copyMsgKey(MsgKey destKey, MsgKey sourceKey)
{
destKey.flags(sourceKey.flags());
destKey.nameType(sourceKey.nameType());
if (sourceKey.checkHasName() && sourceKey.name() != null)
{
destKey.name().data(ByteBuffer.allocate(sourceKey.name().length()));
sourceKey.name().copy(destKey.name());
}
destKey.serviceId(sourceKey.serviceId());
destKey.filter(sourceKey.filter());
destKey.identifier(sourceKey.identifier());
destKey.attribContainerType(sourceKey.attribContainerType());
if (sourceKey.checkHasAttrib() && sourceKey.encodedAttrib() != null)
{
int ret = sourceKey.encodedAttrib().copy(destKey.encodedAttrib());
if (ret != CodecReturnCodes.SUCCESS)
return ret;
}
return CodecReturnCodes.SUCCESS;
}
private boolean isStreamInUse(Channel chnl, int streamId, MsgKey key)
{
boolean streamInUse = false;
for (ItemRequestInfo itemReqInfo : _itemRequestWatchList)
{
if (itemReqInfo.isInUse &&
itemReqInfo.channel == chnl &&
itemReqInfo.streamId == streamId)
{
if (itemReqInfo.msgKey.equals(key))
{
streamInUse = true;
break;
}
}
}
return streamInUse;
}
/*
* Processes a posting request. This consists of decoding the
* status/update/refresh information and sending it out to all clients the
* posting request may be on-stream or off-stream
*
* In this example, we will send the post to any stream which has the item
* open. We will also update the item's field values with the post values.
* if the item name in an off-steam post is not open on any streams, then no
* updates will be sent or made. a more complete implementation might choose
* to add unknown(or new) items to the item cache if the client has
* sufficient postUserRights a more complete implementation might also
* choose to add unknown(or new) fields on a posting refresh to the item
* cache.
*/
private int processPost(Channel chnl, Msg msg, DecodeIterator dIter, Error error)
{
LoginRequestInfo loginRequestInfo;
PostMsg postMsg = (PostMsg)msg;
// get the login stream so that we can see if the post was an off-stream
// post
if ((loginRequestInfo = _loginHandler.findLoginRequestInfo(chnl)) == null)
{
return sendAck(chnl, postMsg, NakCodes.INVALID_CONTENT, "Received a post message request from client before login", error);
}
ItemInfo itemInfo;
// if the post is on the login stream, then it's an off-stream post
if (loginRequestInfo.loginRequest().streamId() == msg.streamId())
{
// the msg key must be specified to provide the item name
if (!postMsg.checkHasMsgKey())
{
return sendAck(chnl, postMsg, NakCodes.INVALID_CONTENT, "Received an off-stream post message request from client without a msgkey", error);
}
System.out.println("Received an off-stream item post (item=" + postMsg.msgKey().name() + ")");
// look up the item name
// for this example, we will treat an unknown item as an error
// However, other providers may choose to add the item to their
// cache
if ((itemInfo = _itemInfoWatchList.get(postMsg.msgKey().name(), postMsg.domainType(), false)) == null)
{
return sendAck(chnl, postMsg, NakCodes.SYMBOL_UNKNOWN, "Received an off-stream post message for an unknown item", error);
}
}
else
{
ItemRequestInfo itemReqInfo = null;
// the msgkey is not required for on-stream post
// get the item request associated with this on-stream post
if ((itemReqInfo = _itemRequestWatchList.get(chnl, postMsg.streamId())) == null)
{
return sendAck(chnl, postMsg, NakCodes.INVALID_CONTENT, "Received an on-stream post message on a stream that does not have an item open", error);
}
itemInfo = itemReqInfo.itemInfo;
System.out.println("Received an on-stream post for item= " + itemInfo.itemName);
}
// if the post message contains another message, then use the
// "contained" message as the update/refresh/status
if (postMsg.containerType() == DataTypes.MSG)
{
_nestedMsg.clear();
int ret = _nestedMsg.decode(dIter);
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("Unable to decode msg");
return ret;
}
switch (_nestedMsg.msgClass())
{
case MsgClasses.REFRESH:
_nestedMsg.msgClass(MsgClasses.REFRESH);
int flags = _nestedMsg.flags();
flags |= RefreshMsgFlags.HAS_POST_USER_INFO;
flags &= ~RefreshMsgFlags.SOLICITED;
_nestedMsg.flags(flags);
((RefreshMsg)_nestedMsg).postUserInfo().userAddr(postMsg.postUserInfo().userAddr());
((RefreshMsg)_nestedMsg).postUserInfo().userId(postMsg.postUserInfo().userId());
if (updateItemInfoFromPost(itemInfo, _nestedMsg, dIter, error) != CodecReturnCodes.SUCCESS)
{
ret = sendAck(chnl, postMsg, NakCodes.INVALID_CONTENT, error.text(), error);
if (ret != CodecReturnCodes.SUCCESS)
{
return ret;
}
}
break;
case MsgClasses.UPDATE:
_nestedMsg.msgClass(MsgClasses.UPDATE);
((UpdateMsg)_nestedMsg).flags(_nestedMsg.flags() | UpdateMsgFlags.HAS_POST_USER_INFO);
((UpdateMsg)_nestedMsg).postUserInfo().userAddr(postMsg.postUserInfo().userAddr());
((UpdateMsg)_nestedMsg).postUserInfo().userId(postMsg.postUserInfo().userId());
if (updateItemInfoFromPost(itemInfo, _nestedMsg, dIter, error) != CodecReturnCodes.SUCCESS)
{
ret = sendAck(chnl, postMsg, NakCodes.INVALID_CONTENT, error.text(), error);
if (ret != CodecReturnCodes.SUCCESS)
{
return ret;
}
}
break;
case MsgClasses.STATUS:
_nestedMsg.msgClass(MsgClasses.STATUS);
((StatusMsg)_nestedMsg).flags(_nestedMsg.flags() | StatusMsgFlags.HAS_POST_USER_INFO);
((StatusMsg)_nestedMsg).postUserInfo().userAddr(postMsg.postUserInfo().userAddr());
((StatusMsg)_nestedMsg).postUserInfo().userId(postMsg.postUserInfo().userId());
if (((StatusMsg)_nestedMsg).checkHasState() && ((StatusMsg)_nestedMsg).state().streamState() == StreamStates.CLOSED)
{
// check if the user has the rights to send a post that
// closes an item
if (postMsg.checkHasPostUserRights() || postMsg.postUserRights() == 0)
{
ret = sendAck(chnl, postMsg, NakCodes.INVALID_CONTENT, "client has insufficient rights to close/delete an item", error);
if (ret != CodecReturnCodes.SUCCESS)
return ret;
}
}
break;
default:
break;
}
}
else
{
//It's a container(e.g. field list). Add an update header for reflecting.
_updateMsg.clear();
_updateMsg.msgClass(MsgClasses.UPDATE);
_updateMsg.domainType(postMsg.domainType());
_updateMsg.containerType(postMsg.containerType());
if (msg.encodedDataBody() != null && msg.encodedDataBody().length() > 0)
_updateMsg.encodedDataBody(msg.encodedDataBody());
_updateMsg.flags(UpdateMsgFlags.HAS_POST_USER_INFO);
((UpdateMsg)_updateMsg).postUserInfo().userAddr(postMsg.postUserInfo().userAddr());
((UpdateMsg)_updateMsg).postUserInfo().userId(postMsg.postUserInfo().userId());
if (postMsg.checkHasMsgKey())
{
_updateMsg.flags(_updateMsg.flags() | UpdateMsgFlags.HAS_MSG_KEY);
_updateMsg.msgKey().copy(postMsg.msgKey());
}
if (updateItemInfoFromPost(itemInfo, msg, dIter, error) != CodecReturnCodes.SUCCESS)
{
int ret = sendAck(chnl, postMsg, NakCodes.INVALID_CONTENT, error.text(), error);
if (ret != CodecReturnCodes.SUCCESS)
{
return ret;
}
}
}
int ret = sendAck(chnl, postMsg, NakCodes.NONE, null, error);
if (ret != CodecReturnCodes.SUCCESS)
{
return ret;
}
// send the post to all public streams with this item open
for (ItemRequestInfo itemReqInfoL : _itemRequestWatchList)
{
if (itemReqInfoL.itemInfo == itemInfo)
{
_encodeIter.clear();
TransportBuffer sendBuf = itemReqInfoL.channel.getBuffer(POST_MSG_SIZE, false, error);
if (sendBuf == null)
{
return CodecReturnCodes.FAILURE;
}
ret = _encodeIter.setBufferAndRWFVersion(sendBuf, itemReqInfoL.channel.majorVersion(), itemReqInfoL.channel.minorVersion());
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("EncodeIterator.setBufferAndRWFVersion() failed with return code: " + CodecReturnCodes.toString(ret));
return CodecReturnCodes.FAILURE;
}
if (postMsg.containerType() == DataTypes.MSG)
{
// send the contained/embedded message if there was one.
_nestedMsg.streamId(itemReqInfoL.streamId);
if(_nestedMsg.msgClass() == MsgClasses.REFRESH)
{
((RefreshMsg)_nestedMsg).applyHasMsgKey();
}
ret = _nestedMsg.encode(_encodeIter);
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("nestedMsg.encode() failed");
return CodecReturnCodes.FAILURE;
}
ret = _providerSession.write(itemReqInfoL.channel, sendBuf, error);
if (ret != TransportReturnCodes.SUCCESS)
return CodecReturnCodes.FAILURE;
// check if its a status close and close any open streams if
// it is
if (_nestedMsg.msgClass() == MsgClasses.STATUS && ((StatusMsg)_nestedMsg).checkHasState() && ((StatusMsg)_nestedMsg).state().streamState() == StreamStates.CLOSED)
closeStream(itemReqInfoL.channel, _nestedMsg.streamId());
}
else
{
// send an update message if the post contained data
_updateMsg.streamId(itemReqInfoL.streamId);
ret = _updateMsg.encode(_encodeIter);
if (ret != CodecReturnCodes.SUCCESS)
{
error.text("nestedMsg.encode() failed");
return CodecReturnCodes.FAILURE;
}
ret = _providerSession.write(itemReqInfoL.channel, sendBuf, error);
if (ret != TransportReturnCodes.SUCCESS)
return CodecReturnCodes.FAILURE;
}
}
}
return CodecReturnCodes.SUCCESS;
}
/*
* update the item info from the post based on market domain This example
* supports posting on the market Price domain a more fully functional
* implementation would support additional domains
*/
private int updateItemInfoFromPost(ItemInfo itemInfo, Msg msg, DecodeIterator dIter, Error error)
{
int ret;
switch (itemInfo.domainType)
{
case DomainTypes.MARKET_PRICE:
ret = _marketPriceItemWatchList.updateFieldsFromPost((MarketPriceItem)itemInfo.itemData, dIter, error);
break;
case DomainTypes.MARKET_BY_ORDER:
case DomainTypes.MARKET_BY_PRICE:
default:
error.text("Unsupported domain" + itemInfo.domainType + " in post message update/refresh");
ret = CodecReturnCodes.FAILURE;
}
return ret;
}
/**
* Closes all item requests for the closed channel.
*
* @param channel - channel for which channel close has received.
*/
public void closeRequests(Channel channel)
{
//find original item request information associated with channel
for (ItemRequestInfo itemRequestInfoL : _itemRequestWatchList)
{
if (itemRequestInfoL.channel == channel && itemRequestInfoL.isInUse)
{
freeItemReqInfo(itemRequestInfoL);
}
}
}
/**
* Updates item information for all items in the watch list.
*/
public void updateItemInfo()
{
_itemInfoWatchList.update();
}
/**
* Service id.
*
* @return the int
*/
public int serviceId()
{
return _serviceId;
}
/**
* Service id.
*
* @param serviceId the service id
*/
public void serviceId(int serviceId)
{
this._serviceId = serviceId;
}
}
|