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 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
|
static const char *RcsId = "$Id: pollthread.cpp 15556 2011-02-11 08:25:58Z taurel $\n$Name$";
//+============================================================================
//
// file : PollThread.cpp
//
// description : C++ source code for the PollThread class.
// This class is used for the polling thread. The rule of
// this thread is to regulary exceute command on device or
// read attribute and store result in a ring buffer.
//
// project : TANGO
//
// author(s) : E.Taurel
//
// Copyright (C) : 2004,2005,2006,2007,2008,2009,2010,2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This file is part of Tango.
//
// Tango is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tango is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Tango. If not, see <http://www.gnu.org/licenses/>.
//
// $Revision: 15556 $
//
// $Log$
// Revision 3.31 2011/01/10 13:55:07 taurel
// - For periodic and archive/periodic, take time got before the attribute
// is read to decide if it is time to store data. This time is much
// more stable than time got after the attribute is read. Reading attribute
// on some device takes a long and unstabe time.
//
// Revision 3.30 2010/09/09 13:46:45 taurel
// - Add year 2010 in Copyright notice
//
// Revision 3.29 2010/09/08 12:32:53 taurel
// - Miscellaneous changes to implement a better timeout management
// (now manage a user connect timeout with the env. variable TANGOconnectTimeout)
//
// Revision 3.28 2010/01/20 07:53:02 taurel
// - Commit after merge with the Release_7_1_1-bugfixes branch
//
// Revision 3.27.2.1 2010/01/18 16:12:40 taurel
// - Fix polling thread tuning algorithum bug in case of object with very slow polling period (> 100 sec)
//
// Revision 3.27 2009/10/23 14:36:27 taurel
// - Tango 7.1.1
// - Fix bugs 2880372 and 2881841
// - Now support event in case of Tango system with multi db server
// - The polling threads start with polling inactive
//
// Revision 3.26 2009/02/16 09:30:37 jensmeyer
// Modified print_list() method to handle sub device diagnostics.
//
// Revision 3.25 2009/02/03 15:17:11 jensmeyer
// Added the storage of sub device properties on a regular basis of
// 30 minutes to the heartbeat thread of a device server.
//
// Revision 3.24 2009/01/21 12:47:15 taurel
// - Change CopyRights for 2009
//
// Revision 3.23 2009/01/08 14:58:03 taurel
// - The read_attribute_4 also transfer the client authentification
//
// Revision 3.22 2008/10/06 15:01:36 taurel
// - Changed the licensing info from GPL to LGPL
//
// Revision 3.21 2008/10/03 06:52:31 taurel
// - Add some licensing info in each files
//
// Revision 3.20 2008/10/02 09:09:47 taurel
// - First implementation of multiple polling thread(s)
//
// Revision 3.19 2008/07/01 07:38:40 taurel
// - Some more code for a proper implementation of the DevEncoded data type with the new IDL release 4
//
// Revision 3.18 2008/05/20 12:44:12 taurel
// - Commit after merge with release 7 branch
//
// Revision 3.17.2.1 2008/05/20 06:17:46 taurel
// - Last commit before merge with trunk
// (start the implementation of the new DevEncoded data type)
//
// Revision 3.17 2007/05/15 07:46:59 taurel
// - The polling thread is not configured by a separate thread any more.
// The Add_obj_polling command now support a delta_t to start the first polling
//
// Revision 3.16 2007/04/20 14:41:33 taurel
// - Ported to Windows 64 bits x64 architecture
//
// Revision 3.15 2007/04/16 14:57:42 taurel
// - Added 3 new attributes data types (DevULong, DevULong64 and DevState)
// - Ported to omniORB4.1
// - Increased the MAX_TRANSFER_SIZE to 256 MBytes
// - Added a new filterable field in the archive event
//
// Revision 3.14 2007/03/29 07:09:25 taurel
// - Change some data types for 64 bits compatibility
//
// Revision 3.13 2007/03/02 09:44:03 jensmeyer
// Corrected the use of detect_and_push event methods. Uses now the _3 methods
// for idl_vers>3 everywhere.
//
// Revision 3.12 2006/05/18 08:51:56 taurel
// - Miscellaneous changes due to Python device server ported to Windows
// - Fix some bugs discovered by Windows VC8 using the test suite
// - Update Windows resource file include path
// - Fix some Windows VC8 warnings
//
// Revision 3.11 2006/04/13 13:31:44 taurel
// Several changes:
// - A new DeviceImpl::push_event() method to push USER event with State as data
// - Fix bug in polling thread POLL_IUPD_PERIOD. The new polling period was applied only after the following poll still done with the old polling period
// - It is now possible to update polling period of polled objects from a polled attribute or command
//
// Revision 3.10 2006/01/27 14:27:10 taurel
// - Fix a severe incompatibility problem introduced by all the modifs done for
// PY DS
// - Duplicate some EventSupplier class methods (instead of using template) in order to be able to generate Tango shared library on Suse 9.3
//
// Revision 3.9 2006/01/20 08:22:29 taurel
// - Added necessary changes to support Device server written in Python
//
// Revision 3.8 2005/07/20 14:40:47 taurel
// - Fix bug in polling thread out of sync error. Time threshold to generate
// event heartbeat error changed from 10 to 11. Fix some comments
//
// Revision 3.7 2005/06/29 08:31:19 taurel
// - Last commit before release 5.2 ?
//
// Revision 3.6 2005/01/13 08:30:00 taurel
// - Merge trunk with Release_5_0 from brach Release_5_branch
//
// Revision 3.5.2.8 2004/12/06 14:39:29 taurel
// - Polling starts in a separate thread
// - For Windows : Polling thread cmd/attr measurement used PerformanceCounter
// - Fix bug in pollext.h in methods to externally fill polling buffer
//
// Revision 3.5.2.7 2004/11/09 09:57:49 taurel
// - Fix one memory leak and some minor changes
// - Force database file writing when server exit
// - Some minor changes for the -file option for WIN32
//
// Revision 3.5.2.6 2004/11/04 09:46:39 taurel
// - Add a tuning method in the polling thread
// - Some minor fixes to pass test suite
//
// Revision 3.5.2.5 2004/10/27 05:59:47 taurel
// - Some minor changes to compile on all our supported platforms
//
// Revision 3.5.2.4 2004/10/22 11:26:33 taurel
// Added warning alarm
// Change attribute config. It now includes alarm and event parameters
// Array attribute property now supported
// subscribe_event throws exception for change event if they are not correctly configured
// Change in the polling thread: The event heartbeat has its own work in the work list
// Also add some event_unregister
// Fix order in which classes are destructed
// Fix bug in asynchronous mode (PUSH_CALLBACK). The callback thread ate all the CPU
// Change in the CORBA info call for the device type
//
// Revision 3.5.2.3 2004/09/27 09:10:06 taurel
// - Changes to allow reading state and/or status as attributes
//
// Revision 3.5.2.2 2004/08/26 07:34:45 taurel
// - Implement a way to directly fills command or attribute polling buffer
//
// Revision 3.5.2.1 2004/08/19 07:44:59 taurel
// - Replace server low level database access call by Database class method call
// - Split device monitor in 3 : 1 to protect harware access, 1 to protect cache access and one mutex for device black box
//
// Revision 3.5 2004/07/07 08:40:12 taurel
//
// - Fisrt commit after merge between Trunk and release 4 branch
// - Add EventData copy ctor, asiignement operator and dtor
// - Add Database and DeviceProxy::get_alias() method
// - Add AttributeProxy ctor from "device_alias/attribute_name"
// - Exception thrown when subscribing two times for exactly yhe same event
//
// Revision 3.4 2003/08/21 07:24:37 taurel
// - End of the implementation of the new way to transfer data for read and
// write attributes (better use of exception)
// - Added Attribute::set_date() and Attribute::set_value_date_quality() methods
// - Added DeviceAttribute ctors from "const char *"
// - Enable writing of spectrum and image attributes
// - Many new DeviceAttribute ctors/inserters to enable easy image and spectrums
// attribute writing
// - Attribute date automatically set in case of attribute quality factor set to INVALID
// - Change in the polling thread discarding element algo. to support case of polling
// several cmd/atts at the same polling period with cmd/attr having a long response time
// - Take cmd/attr execution time into account in the "Data not updated since" polling
// status string
// - Split "str().c_str()" code in two lines of code. It was the reason of some problem
// on Windows device server
// - Add the possibility to set a cmd/attr polling as "externally triggered". Add method
// to send trigger to the polling thread
//
// Revision 3.3 2003/07/03 07:40:51 taurel
// - Change in Tango IDL file : Implement a new way to tranfer data for read_attribute and write_attribute CORBA operation
// - Handle this new IDL release in DeviceProxy class
// - New exception methods in DeviceAttribute class
// - New way to get data out of DeviceAttribute object
// - Fix bugs in DeviceProxy copy constructor and assignement operator
// - Change some method names in DeviceDataHistory and DeviceAttributeHistory classes
// - Change the implementation of the DeviceProxy::write_attribute() method to avoid DeviceAttribute copying
// - Clean-up how a server is killed via a CTRL-C or a dserver device kill command
// - Add a server_cleanup() method in the Util class
// - Win32 : Update debug menu in the server graphical window to support logging feature
// - Win32 : Display library CVS tag in the "Help->About" sub-window
//
// Revision 3.2.2.7 2004/04/23 09:24:55 taurel
// - Just a change in comment
//
// Revision 3.2.2.6 2004/03/09 16:36:37 taurel
// - Added HP aCC port (thanks to Claudio from Elettra)
// - Some last small bugs fixes
//
// Revision 3.2.2.5 2004/02/06 11:58:51 taurel
// - Many changes in the event system
//
// Revision 3.2.2.4 2004/01/20 08:32:37 taurel
// -First commit after merge with the event branch and work on the AttributeProxy class
// - Fix bug in the stream "clear()" method usage when used with gcc 3.3
//
// Revision 3.2.2.3 2003/12/10 16:08:56 taurel
// Last commit before merging with the event branch.
// Revision 3.0.2.4 2003/11/16 22:10:43 andy_gotz
// New version which defines 4 types of events - change, quality, periodic and
// archive. Code has been factorised to reduce redundancy. Minimum and maximum
// changes are supported. Event period is taken into account. Relative and
// absolute changes are detected. Whole sequence is taken into account when
// determining change.
//
// Revision 3.2.2.2 2003/10/03 13:34:26 taurel
// - Fix bug for device server started without database, with device name specified on command line using mix of upper and lower cases
// - It's now possible to send a command to the polling thread from itself
// - Add a release lock if the insert into the polling buffer failed
//
// Revision 3.2.2.1 2003/09/30 11:49:25 taurel
// Add some changes foreseen for release 4.1 and already implemented on
// the trunck into this release 4.0 branch
//
// Revision 3.2 2003/05/28 14:55:10 taurel
// Add the include (conditionally) of the include files generated by autoconf
//
// Revision 3.1 2003/05/16 08:46:16 taurel
// Many changes for release 3.0.1. The most important ones are :
// - Timeout are backs
// - Multiple db servers (change in TANGO_HOST syntax)
// - Added methods to print DeviceData, DeviceDataHistory, DeviceAttribute and DeviceAttributeHistory instances
// - Attributes name stored in blackbox
// - Remove check if a class is created without any device
// - It's now possible to create a DeviceProxy from its alias name
// - Command, attribute names are case insensitive
// - Change parameters of some DeviceProxy logging methods
// - Change parameters of DeviceProxy asynchronous replies calls
// - New serialization model in device server (no serialization model)
// - Win32 (2000) device server service does not exit at loggoff anymore
// - Miscellaneous bug fixes
// Revision 3.0.2.3 2003/04/15 19:01:55 andy_gotz
// added heartbeat on client and server side; cleaned up cout's
//
// Revision 3.0.2.2 2003/04/13 22:12:17 andy_gotz
// added heartbeat; polling starts automatically on subscription
//
// Revision 3.0.2.1 2003/04/08 13:12:52 andy_gotz
// first version of TANGO events
//
// Revision 3.0 2003/03/25 16:44:10 taurel
// Many changes for Tango release 3.0 including
// - Added full logging features
// - Added asynchronous calls
// - Host name of clients now stored in black-box
// - Three serialization model in DS
// - Fix miscellaneous bugs
// - Ported to gcc 3.2
// - Added ApiUtil::cleanup() and destructor methods
// - Some internal cleanups
// - Change the way how TangoMonitor class is implemented. It's a recursive
// mutex
//
//
//-============================================================================
#if HAVE_CONFIG_H
#include <ac_config.h>
#endif
#include <tango.h>
#include <eventsupplier.h>
#include <math.h>
#ifdef _TG_WINDOWS_
#include <sys/timeb.h>
#else
#include <sys/time.h>
#endif
#if ((defined _TG_WINDOWS_) || (defined __SUNPRO_CC) || (defined GCC_STD))
#include <iomanip>
#else
#include <iomanip.h>
#endif
extern omni_thread::key_t key_py_data;
namespace Tango
{
DeviceImpl *PollThread::dev_to_del = NULL;
string PollThread::name_to_del = "";
PollObjType PollThread::type_to_del = Tango::POLL_CMD;
//+-------------------------------------------------------------------------
//
// method : PollThread::Pollthread
//
// description : The polling thread constructor.
//
//--------------------------------------------------------------------------
PollThread::PollThread(PollThCmd &cmd,TangoMonitor &m,bool heartbeat): shared_cmd(cmd),p_mon(m),
sleep(1),polling_stop(true),
attr_names(1),tune_ctr(1),
need_two_tuning(false),auto_upd(-1),send_heartbeat(heartbeat)
{
attr_names.length(1);
cci = 0;
dummy_cl_id.cpp_clnt(cci);
if (heartbeat == true)
polling_stop = false;
#ifdef _TG_WINDOWS_
LARGE_INTEGER f;
BOOL is_ctr;
is_ctr = QueryPerformanceFrequency(&f);
if (is_ctr != 0)
ctr_frequency = 1000000.0 / (double)f.QuadPart;
else
ctr_frequency = 0.0;
#endif
}
//+-------------------------------------------------------------------------
//
// method : PollThread::run_undetached
//
// description : The polling thread main code
//
//--------------------------------------------------------------------------
void *PollThread::run_undetached(void *ptr)
{
PollCmdType received;
bool per_thread_data_created = false;
//
// If the thread is the event heartbeat thread,
// use it also for the storage of sub device properties.
// Declare a work item to check the for new sub devices
// regularly.
//
if ( send_heartbeat == true )
{
WorkItem wo;
wo.dev = NULL;
wo.poll_list = NULL;
wo.type = STORE_SUBDEV;
wo.update = 30*60*1000; // check ervery 30 minutes
wo.name = "Sub device property storage";
wo.needed_time.tv_sec = 0;
wo.needed_time.tv_usec = 0;
#ifdef _TG_WINDOWS_
_ftime(&now_win);
now.tv_sec = (unsigned long)now_win.time;
now.tv_usec = (long)now_win.millitm * 1000;
#else
gettimeofday(&now,NULL);
#endif
now.tv_sec = now.tv_sec - DELTA_T;
wo.wake_up_date = now;
insert_in_list(wo);
}
//
// The infinite loop
//
while(1)
{
try
{
if (sleep != 0)
received = get_command(sleep);
else
received = POLL_TIME_OUT;
//
// Create the per thread data if it is not already done (For Python DS)
//
if (per_thread_data_created == false)
{
omni_thread::self()->set_value(key_py_data,new PyData());
per_thread_data_created = true;
}
#ifdef _TG_WINDOWS_
_ftime(&now_win);
now.tv_sec = (unsigned long)now_win.time;
now.tv_usec = (long)now_win.millitm * 1000;
#else
gettimeofday(&now,NULL);
#endif
now.tv_sec = now.tv_sec - DELTA_T;
switch (received)
{
case POLL_COMMAND:
execute_cmd();
break;
case POLL_TIME_OUT:
one_more_poll();
break;
case POLL_TRIGGER:
one_more_trigg();
break;
}
#ifdef _TG_WINDOWS_
_ftime(&after_win);
after.tv_sec = (unsigned long)after_win.time;
after.tv_usec = (long)after_win.millitm * 1000;
#else
gettimeofday(&after,NULL);
#endif
after.tv_sec = after.tv_sec - DELTA_T;
if (tune_ctr <= 0)
{
tune_list(true,0);
if (need_two_tuning == true)
{
unsigned long nb_works = works.size();
tune_ctr = (nb_works << 2);
need_two_tuning = false;
}
else
tune_ctr = POLL_LOOP_NB;
}
compute_sleep_time();
}
catch (omni_thread_fatal &)
{
cerr << "OUPS !! A omni thread fatal exception received by a polling thread !!!!!!!!" << endl;
#ifndef _TG_WINDOWS_
time_t t = time(NULL);
cerr << ctime(&t);
#endif
cerr << "Trying to re-enter the main loop" << endl;
}
catch (const std::exception &ex)
{
cerr << "OUPS !! An unforeseen standard exception has been received by a polling thread !!!!!!" << endl;
cerr << ex.what() << endl;
#ifndef _TG_WINDOWS_
time_t t = time(NULL);
cerr << ctime(&t);
#endif
cerr << "Trying to re-enter the main loop" << endl;
}
}
return NULL;
}
//+-------------------------------------------------------------------------
//
// method : PollThread::get_command
//
// description : This method wait on the shared monitor for a new
// command to be sent to the polling thread. The
// thread waits with a timeout. If the thread is
// awaken due to the timeout, false is returned.
// If the work list is empty, the thread waits for ever.
//
// argument : in : tout : The timeout in mS
//
// The method returns true if the thread has been awaken due to a new
// command sent by the main thread
//
//--------------------------------------------------------------------------
PollCmdType PollThread::get_command(long tout)
{
omni_mutex_lock sync(p_mon);
PollCmdType ret;
//
// Wait on monitor
//
if ((shared_cmd.cmd_pending == false) && (shared_cmd.trigger == false))
{
if (works.empty() == true)
p_mon.wait();
else
{
if (tout != -1)
p_mon.wait(tout);
}
}
//
// Test if it is a new command. If yes, copy its data locally
//
if (shared_cmd.cmd_pending == true)
{
local_cmd = shared_cmd;
ret = POLL_COMMAND;
}
else if (shared_cmd.trigger == true)
{
local_cmd = shared_cmd;
ret = POLL_TRIGGER;
}
else
ret = POLL_TIME_OUT;
return ret;
}
//+-------------------------------------------------------------------------
//
// method : PollThread::execute_cmd and two unary predicates
//
// description : This method is called when a command has been received
// It exceute the command!
//
//--------------------------------------------------------------------------
bool pred(const WorkItem &w)
{
if (w.dev == PollThread::dev_to_del)
{
if (w.type == PollThread::type_to_del)
return w.name == PollThread::name_to_del;
else
return false;
}
else
return false;
}
bool pred_dev(const WorkItem &w)
{
return w.dev == PollThread::dev_to_del;
}
void PollThread::execute_cmd()
{
WorkItem wo;
list<WorkItem>::iterator ite;
vector<WorkItem>::iterator et_ite;
switch (local_cmd.cmd_code)
{
//
// Add a new object
//
case Tango::POLL_ADD_OBJ :
cout5 << "Received a Add object command" << endl;
wo.dev = local_cmd.dev;
wo.poll_list = &(wo.dev->get_poll_obj_list());
wo.type = (*wo.poll_list)[local_cmd.index]->get_type();
wo.update = (*wo.poll_list)[local_cmd.index]->get_upd();
wo.name = (*wo.poll_list)[local_cmd.index]->get_name().c_str();
wo.needed_time.tv_sec = 0;
wo.needed_time.tv_usec = 0;
if (wo.update != 0)
{
wo.wake_up_date = now;
if (local_cmd.new_upd != 0)
{
cout5 << "Received a delta from now of " << local_cmd.new_upd << endl;
T_ADD(wo.wake_up_date,local_cmd.new_upd * 1000);
}
// add_random_delay(wo.wake_up_date);
insert_in_list(wo);
unsigned long nb_works = works.size();
tune_ctr = (nb_works << 2);
need_two_tuning = true;
}
else
{
wo.wake_up_date.tv_sec = 0;
wo.wake_up_date.tv_usec = 0;
ext_trig_works.push_back(wo);
}
break;
//
// Remove an already polled object
//
case Tango::POLL_REM_OBJ :
cout5 << "Received a Rem object command" << endl;
dev_to_del = local_cmd.dev;
name_to_del = local_cmd.name;
type_to_del = local_cmd.type;
#ifdef _TG_WINDOWS_
unsigned int i,nb_elt;
nb_elt = works.size();
ite = works.begin();
for (i = 0;i < nb_elt;i++)
{
if (ite->dev == PollThread::dev_to_del)
{
if (ite->type == PollThread::type_to_del)
{
if (ite->name == PollThread::name_to_del)
{
works.erase(ite);
break;
}
}
}
ite++;
}
#else
works.remove_if(pred);
#endif
break;
//
// Remove an already externally triggered polled object
//
case Tango::POLL_REM_EXT_TRIG_OBJ :
cout5 << "Received a Ext Trig Rem object command" << endl;
for (et_ite = ext_trig_works.begin();
et_ite != ext_trig_works.end();++et_ite)
{
if (et_ite->dev == local_cmd.dev)
{
if (et_ite->type == local_cmd.type)
{
if (et_ite->name == local_cmd.name)
{
ext_trig_works.erase(et_ite);
break;
}
}
}
}
break;
//
// Remove all objects belonging to a device.
// Take care, the same device could have several objects --> No break after
// the successfull if in loop
//
case Tango::POLL_REM_DEV :
cout5 << "Received a Rem device command" << endl;
dev_to_del = local_cmd.dev;
#ifdef _TG_WINDOWS_
nb_elt = works.size();
ite = works.begin();
for (i = 0;i < nb_elt;i++)
{
if (ite->dev == PollThread::dev_to_del)
{
ite = works.erase(ite);
}
else
ite++;
}
nb_elt = ext_trig_works.size();
et_ite = ext_trig_works.begin();
for (i = 0;i < nb_elt;i++)
{
if (et_ite->dev == PollThread::dev_to_del)
{
et_ite = ext_trig_works.erase(et_ite);
}
else
et_ite++;
}
#else
works.remove_if(pred_dev);
ext_trig_works.erase(remove_if(ext_trig_works.begin(),
ext_trig_works.end(),
pred_dev),
ext_trig_works.end());
#endif
break;
//
// Update polling period
// Several cases has to be implemented here.
// 1 - A classical command from the external world. In this case updating
// polling period means removing the already inserted object from the work list,
// compute its new polling time and insert it in the work list with its new
// polling time and polling period
// 2 - This is executed by the polling thread itself
// 2-1 - The command updates polling period for another object: idem than previous
// 2-2 - The commands updates polling period for the object it is actually polled.
// In this case, the object is not in the work list. It has been removed from the
// work list at the beginning of the "one_more_poll" method and is memorized there
// Therefore, simply stores new polling period in a data member. The "one_more_poll"
// method will get its new polling period before re-inserting the object in the work
// list with the new update period.
// We detect this case because the object is not in any work list (either the work
// list or the trigger list)
//
case Tango::POLL_UPD_PERIOD :
cout5 << "Received a update polling period command" << endl;
dev_to_del = local_cmd.dev;
name_to_del = local_cmd.name;
type_to_del = local_cmd.type;
ite = find_if(works.begin(),works.end(),pred);
if (ite != works.end())
{
if (local_cmd.new_upd != 0)
{
WorkItem tmp_work = *ite;
#ifdef _TG_WINDOWS_
unsigned int i,nb_elt;
nb_elt = works.size();
ite = works.begin();
for (i = 0;i < nb_elt;i++)
{
if (ite->dev == PollThread::dev_to_del)
{
if (ite->type == PollThread::type_to_del)
{
if (ite->name == PollThread::name_to_del)
{
works.erase(ite);
break;
}
}
}
ite++;
}
#else
works.remove_if(pred);
#endif
tmp_work.update = local_cmd.new_upd;
compute_new_date(now,local_cmd.new_upd);
tmp_work.wake_up_date = now;
insert_in_list(tmp_work);
}
else
{
//
// First, remove object from polling list and insert it in externally
// triggered list
//
#ifdef _TG_WINDOWS_
unsigned int i,nb_elt;
nb_elt = works.size();
ite = works.begin();
for (i = 0;i < nb_elt;i++)
{
if (ite->dev == PollThread::dev_to_del)
{
if (ite->type == PollThread::type_to_del)
{
if (ite->name == PollThread::name_to_del)
{
works.erase(ite);
break;
}
}
}
ite++;
}
#else
works.remove_if(pred);
#endif
wo.dev = local_cmd.dev;
wo.poll_list = &(wo.dev->get_poll_obj_list());
wo.type = (*wo.poll_list)[local_cmd.index]->get_type();
wo.update = (*wo.poll_list)[local_cmd.index]->get_upd();
wo.name = (*wo.poll_list)[local_cmd.index]->get_name().c_str();
wo.wake_up_date.tv_sec = 0;
wo.wake_up_date.tv_usec = 0;
ext_trig_works.push_back(wo);
}
}
else
{
//
// If not found in work list, it should be in the externally
// triggered object. Therefore, remove it from externally
// triggered list and insert it in work list
// If not found in work list and in trig list, we are in case
// 2-2 as desribed above (polling thread updateing itself polling
// period of the object it actually polls)
//
bool found = false;
for (et_ite = ext_trig_works.begin();
et_ite != ext_trig_works.end();++et_ite)
{
if (et_ite->dev == local_cmd.dev)
{
if (et_ite->type == local_cmd.type)
{
if (et_ite->name == local_cmd.name)
{
ext_trig_works.erase(et_ite);
found = true;
break;
}
}
}
}
if (found == true)
{
wo.dev = local_cmd.dev;
wo.poll_list = &(wo.dev->get_poll_obj_list());
wo.type = type_to_del;
wo.update = local_cmd.new_upd;
wo.name = name_to_del;
wo.wake_up_date = now;
insert_in_list(wo);
}
else
auto_upd = local_cmd.new_upd;
}
break;
//
// Add the event heartbeat every 9 seconds
//
case Tango::POLL_ADD_HEARTBEAT:
cout5 << "Received a add heartbeat command" << endl;
wo.dev = NULL;
wo.poll_list = NULL;
wo.type = EVENT_HEARTBEAT;
wo.update = 9000;
wo.name = "Event heartbeat";
wo.needed_time.tv_sec = 0;
wo.needed_time.tv_usec = TIME_HEARTBEAT;
wo.wake_up_date = now;
insert_in_list(wo);
break;
//
// Remove the event heartbeat
//
case Tango::POLL_REM_HEARTBEAT:
cout5 << "Received a remove heartbeat command" << endl;
unsigned int ii,nb_elem;
nb_elem = works.size();
ite = works.begin();
for (ii = 0;ii < nb_elem;ii++)
{
if (ite->type == EVENT_HEARTBEAT)
{
works.erase(ite);
break;
}
ite++;
}
break;
//
// Start polling
//
case Tango::POLL_START :
cout5 << "Received a Start polling command" << endl;
polling_stop = false;
break;
//
// Stop polling
//
case Tango::POLL_STOP :
cout5 << "Received a Stop polling command" << endl;
polling_stop = true;
break;
//
// Ask polling thread to exit
//
case Tango::POLL_EXIT :
cout5 << "Received an exit command" << endl;
omni_thread::exit();
break;
}
//
// Inform requesting thread that the work is done
//
{
omni_mutex_lock sync(p_mon);
shared_cmd.cmd_pending = false;
p_mon.signal();
}
if (Tango::Util::_tracelevel >= 4)
print_list();
}
//+-------------------------------------------------------------------------
//
// method : PollThread::add_random_delay
//
// description : Add a random number of microsecond (between 0 and
// 500000) in order to spread the date where polling
// should happens. This is necessary especially at device
// process startup when each command in send to the
// polling thread in a loop
//
// argument : in : t : The timeval structure reference
//
//--------------------------------------------------------------------------
void PollThread::add_random_delay(struct timeval &t)
{
long add_usec;
add_usec = (long)(500000. * (float)rand() / (float)RAND_MAX);
t.tv_usec = t.tv_usec + add_usec;
if (t.tv_usec >= 1000000)
{
t.tv_sec++;
t.tv_usec = t.tv_usec - 1000000;
}
}
//+-------------------------------------------------------------------------
//
// method : PollThread::one_more_poll
//
// description :
//
// argument : in :
//
//--------------------------------------------------------------------------
void PollThread::one_more_poll()
{
WorkItem tmp = works.front();
works.pop_front();
if (polling_stop == false)
{
switch (tmp.type)
{
case Tango::POLL_CMD:
poll_cmd(tmp);
break;
case Tango::POLL_ATTR:
poll_attr(tmp);
break;
case Tango::EVENT_HEARTBEAT:
eve_heartbeat(tmp);
break;
case Tango::STORE_SUBDEV:
store_subdev(tmp);
break;
}
}
//
// For case where the plling thread itself modify the polling
// period of the object it already polls
//
if (auto_upd != -1)
{
tmp.update = auto_upd;
auto_upd = -1;
}
//
// Compute new polling date and insert work in list
//
compute_new_date(tmp.wake_up_date,tmp.update);
insert_in_list(tmp);
tune_ctr--;
}
//+-------------------------------------------------------------------------
//
// method : PollThread::one_more_trigg
//
// description : This method is called when a trigger command has been
// received
//
//--------------------------------------------------------------------------
void PollThread::one_more_trigg()
{
cout5 << "Polling thread has received a trigger" << endl;
//
// Check that the object is registered
//
dev_to_del = local_cmd.dev;
name_to_del = local_cmd.name;
type_to_del = local_cmd.type;
vector<WorkItem>::iterator et_ite;
et_ite = find_if(ext_trig_works.begin(),ext_trig_works.end(),pred);
//
// Check that the object to poll has been installed.
// If not, simply returns. This case should never happens because it is
// tested in the Util::trigger_polling() method before the trigger is
// effectively sent to this thread.
//
if (et_ite == ext_trig_works.end())
{
cout5 << "Object externally triggered not found !!!" << endl;
{
omni_mutex_lock sync(p_mon);
shared_cmd.trigger = false;
p_mon.signal();
}
return;
}
//
// Do the job
//
WorkItem tmp = *et_ite;
if (polling_stop == false)
{
if (tmp.type == Tango::POLL_CMD)
poll_cmd(tmp);
else
poll_attr(tmp);
}
//
// Inform requesting thread that the work is done
//
{
omni_mutex_lock sync(p_mon);
shared_cmd.trigger = false;
p_mon.signal();
}
}
//+-------------------------------------------------------------------------
//
// method : PollThread::print_list
//
// description : To print work list
//
//--------------------------------------------------------------------------
void PollThread::print_list()
{
list<WorkItem>::iterator ite;
long nb_elt,i;
nb_elt = works.size();
ite = works.begin();
for (i = 0;i < nb_elt;i++)
{
if (ite->type != EVENT_HEARTBEAT )
{
if ( ite->type != STORE_SUBDEV)
{
cout4 << "Dev name = " << ite->dev->get_name()
<< ", obj name = " << ite->name
<< ", next wake_up at " << + ite->wake_up_date.tv_sec
<< "," << setw(6) << setfill('0')
<< ite->wake_up_date.tv_usec << endl;
}
else
{
cout4 << ite->name
<< ", next wake_up at " << + ite->wake_up_date.tv_sec
<< "," << setw(6) << setfill('0')
<< ite->wake_up_date.tv_usec << endl;
}
}
else
{
cout4 << "Event heartbeat, next wake_up at " << + ite->wake_up_date.tv_sec
<< "," << setw(6) << setfill('0')
<< ite->wake_up_date.tv_usec << endl;
}
ite++;
}
}
//+-------------------------------------------------------------------------
//
// method : PollThread::insert_in_list
//
// description : To insert (at the correct place) a new Work Item in
// the work list
//
// argument: In : - new_work : The new work item
//
//--------------------------------------------------------------------------
void PollThread::insert_in_list(WorkItem &new_work)
{
list<WorkItem>::iterator ite;
for (ite = works.begin();ite != works.end();ite++)
{
if (ite->wake_up_date.tv_sec < new_work.wake_up_date.tv_sec)
continue;
else if (ite->wake_up_date.tv_sec == new_work.wake_up_date.tv_sec)
{
if (ite->wake_up_date.tv_usec < new_work.wake_up_date.tv_usec)
continue;
else
{
works.insert(ite,new_work);
return;
}
}
else
{
works.insert(ite,new_work);
return;
}
}
if (ite == works.end())
works.push_back(new_work);
}
//+-------------------------------------------------------------------------
//
// method : PollThread::tune_list
//
// description : This method tunes the work list.
//
// argument: In : - from_needed : Set to true if the delta between
// work should be at least equal to the
// time needed to execute the previous work
// - min_delta : Min. delta between polling works
// when from_needed is false
//
//--------------------------------------------------------------------------
void PollThread::tune_list(bool from_needed, long min_delta)
{
list<WorkItem>::iterator ite,ite_next,ite_prev;
unsigned long nb_works = works.size();
cout4 << "Entering tuning list. The list has " << nb_works << " item(s)" << endl;
//
// Nothing to do if only one let in list
//
if (nb_works < 2)
return;
//
// If we try to tune the list with respect to works needed
// time, compute works needed time sum and find minimun update
// period
//
unsigned long needed_sum = 0;
unsigned long min_upd = 0;
long max_delta_needed;
if (from_needed == true)
{
for (ite = works.begin();ite != works.end();++ite)
{
long needed_time_usec = (ite->needed_time.tv_sec * 1000000) + ite->needed_time.tv_usec;
needed_sum = needed_sum + (unsigned long)needed_time_usec;
unsigned long update_usec = (unsigned long)ite->update * 1000;
if (ite == works.begin())
{
min_upd = update_usec;
}
else
{
if (min_upd > update_usec)
min_upd = update_usec;
}
}
//
// In some cases, it is impossible to tune
//
if (needed_sum > min_upd)
return;
else
{
long sleeping = min_upd - needed_sum;
max_delta_needed = sleeping / (nb_works);
}
//
// Now build a new tuned list
//
list<WorkItem> new_works;
new_works.push_front(works.front());
ite = works.begin();
ite_prev = new_works.begin();
for (++ite;ite != works.end();++ite,++ite_prev)
{
long needed_time_usec = (ite_prev->needed_time.tv_sec * 1000000) + ite_prev->needed_time.tv_usec;
WorkItem wo = *ite;
wo.wake_up_date = ite_prev->wake_up_date;
T_ADD(wo.wake_up_date,needed_time_usec + max_delta_needed);
new_works.push_back(wo);
}
//
// Replace work list
//
works = new_works;
}
else
{
ite_next = works.begin();
ite = ite_next;
++ite_next;
for (unsigned int i = 1;i < nb_works;i++)
{
long diff;
T_DIFF(ite->wake_up_date,ite_next->wake_up_date,diff);
//
// If delta time between works is less than min,
// shift following work
//
if (diff < min_delta)
T_ADD(ite_next->wake_up_date,min_delta - diff);
++ite;
++ite_next;
}
}
cout4 << "Tuning list done" << endl;
print_list();
}
//+-------------------------------------------------------------------------
//
// method : PollThread::compute_new_date
//
// description : This method computes the new poll date.
//
// argument: In : - time : The actual date
// - upd : The polling update period (mS)
//
//--------------------------------------------------------------------------
void PollThread::compute_new_date(struct timeval &time,int upd)
{
double ori_d = (double)time.tv_sec + ((double)time.tv_usec / 1000000);
double new_d = ori_d + ((double)(upd) / 1000);
time.tv_sec = (long)new_d;
time.tv_usec = (long)((new_d - time.tv_sec) * 1000000);
}
void PollThread::time_diff(struct timeval &before,
struct timeval &after_t,
struct timeval &result)
{
double bef_d = (double)before.tv_sec + ((double)before.tv_usec / 1000000);
double aft_d = (double)after_t.tv_sec + ((double)after_t.tv_usec / 1000000);
double diff_d = aft_d - bef_d;
result.tv_sec = (long)diff_d;
result.tv_usec = (long)((diff_d - result.tv_sec) * 1000000);
}
//+-------------------------------------------------------------------------
//
// method : PollThread::compute_sleep_time
//
// description : This method computes how many mS the thread should
// sleep before the next poll time. If this time is
// negative and greater than a pre-defined threshold,
// the polling is discarded.
//
//--------------------------------------------------------------------------
void PollThread::compute_sleep_time()
{
print_list();
if (works.empty() == false)
{
double next,after_d,diff;
next = (double)works.front().wake_up_date.tv_sec + ((double)works.front().wake_up_date.tv_usec / 1000000);
after_d = (double)after.tv_sec + ((double)after.tv_usec / 1000000);
diff = next - after_d;
if (diff < 0)
{
if (fabs(diff) < DISCARD_THRESHOLD)
sleep = -1;
else
{
while((diff < 0) && (fabs(diff) > DISCARD_THRESHOLD))
{
cout5 << "Discard one elt !!!!!!!!!!!!!" << endl;
WorkItem tmp = works.front();
if (tmp.type == POLL_ATTR)
err_out_of_sync(tmp);
compute_new_date(tmp.wake_up_date,tmp.update);
insert_in_list(tmp);
works.pop_front();
tune_ctr--;
next = (double)works.front().wake_up_date.tv_sec + ((double)works.front().wake_up_date.tv_usec / 1000000);
diff = next - after_d;
}
if (fabs(diff) < DISCARD_THRESHOLD)
sleep = -1;
else
sleep = (long)(diff * 1000);
}
}
else
sleep = (long)(diff * 1000);
cout5 << "Sleep for : " << sleep << endl;
}
}
//+-------------------------------------------------------------------------
//
// method : PollThread::err_out_of_sync
//
// description : To force one event if the polling thread has
// discarded one work item because it is late
//
// argument : in : - to_do : The work item
//
//--------------------------------------------------------------------------
void PollThread::err_out_of_sync(WorkItem &to_do)
{
EventSupplier *event_supplier;
event_supplier = Util::instance()->get_event_supplier();
if (event_supplier != NULL)
{
Tango::DevErrorList errs;
errs.length(1);
errs[0].severity = Tango::ERR;
errs[0].reason = CORBA::string_dup("API_PollThreadOutOfSync");
errs[0].origin = CORBA::string_dup("PollThread::err_out_of_sync");
errs[0].desc = CORBA::string_dup("The polling thread is late and discard this object polling.\nAdvice: Tune device server polling");
Tango::DevFailed except(errs);
long idl_vers = to_do.dev->get_dev_idl_version();
if (idl_vers >= 3)
event_supplier->detect_and_push_events_3(to_do.dev,
idl_vers,
&dummy_att3,
NULL,
&except,
to_do.name,
(struct timeval *)NULL);
else
event_supplier->detect_and_push_events(to_do.dev,
idl_vers,
dummy_att,
&except,
to_do.name);
}
}
//+-------------------------------------------------------------------------
//
// method : PollThread::poll_cmd
//
// description : Execute a command and store the result in the device
// ring buffer
//
// argument : in : - to_do : The work item
//
//--------------------------------------------------------------------------
void PollThread::poll_cmd(WorkItem &to_do)
{
cout5 << "----------> Time = " << now.tv_sec << ","
<< setw(6) << setfill('0') << now.tv_usec
<< " Dev name = " << to_do.dev->get_name()
<< ", Cmd name = " << to_do.name << endl;
CORBA::Any *argout = NULL;
Tango::DevFailed *save_except = NULL;
struct timeval before_cmd,after_cmd,needed_time;
#ifdef _TG_WINDOWS_
struct _timeb before_win,after_win;
LARGE_INTEGER before,after;
#endif
vector<PollObj *>::iterator ite;
bool cmd_failed = false;
try
{
#ifdef _TG_WINDOWS_
if (ctr_frequency != 0)
QueryPerformanceCounter(&before);
_ftime(&before_win);
before_cmd.tv_sec = (unsigned long)before_win.time;
before_cmd.tv_usec = (long)before_win.millitm * 1000;
#else
gettimeofday(&before_cmd,NULL);
#endif
before_cmd.tv_sec = before_cmd.tv_sec - DELTA_T;
//
// Execute the command
//
argout = to_do.dev->command_inout(to_do.name.c_str(),in_any);
#ifdef _TG_WINDOWS_
if (ctr_frequency != 0)
{
QueryPerformanceCounter(&after);
needed_time.tv_sec = 0;
needed_time.tv_usec = (long)((double)(after.QuadPart - before.QuadPart) * ctr_frequency);
to_do.needed_time = needed_time;
}
else
{
_ftime(&after_win);
after_cmd.tv_sec = (unsigned long)after_win.time;
after_cmd.tv_usec = (long)after_win.millitm * 1000;
after_cmd.tv_sec = after_cmd.tv_sec - DELTA_T;
time_diff(before_cmd,after_cmd,needed_time);
to_do.needed_time = needed_time;
}
#else
gettimeofday(&after_cmd,NULL);
after_cmd.tv_sec = after_cmd.tv_sec - DELTA_T;
time_diff(before_cmd,after_cmd,needed_time);
to_do.needed_time = needed_time;
#endif
}
catch (Tango::DevFailed &e)
{
cmd_failed = true;
#ifdef _TG_WINDOWS_
if (ctr_frequency != 0)
{
QueryPerformanceCounter(&after);
needed_time.tv_sec = 0;
needed_time.tv_usec = (long)((double)(after.QuadPart - before.QuadPart) * ctr_frequency);
to_do.needed_time = needed_time;
}
else
{
_ftime(&after_win);
after_cmd.tv_sec = (unsigned long)after_win.time;
after_cmd.tv_usec = (long)after_win.millitm * 1000;
after_cmd.tv_sec = after_cmd.tv_sec - DELTA_T;
time_diff(before_cmd,after_cmd,needed_time);
to_do.needed_time = needed_time;
}
#else
gettimeofday(&after_cmd,NULL);
after_cmd.tv_sec = after_cmd.tv_sec - DELTA_T;
time_diff(before_cmd,after_cmd,needed_time);
to_do.needed_time = needed_time;
#endif
save_except = new Tango::DevFailed(e);
}
//
// Insert result in polling buffer and simply forget this command if it is
// not possible to insert the result in polling buffer
//
try
{
to_do.dev->get_poll_monitor().get_monitor();
ite = to_do.dev->get_polled_obj_by_type_name(to_do.type,to_do.name);
if (cmd_failed == false)
(*ite)->insert_data(argout,before_cmd,needed_time);
else
(*ite)->insert_except(save_except,before_cmd,needed_time);
to_do.dev->get_poll_monitor().rel_monitor();
}
catch (Tango::DevFailed &)
{
if (cmd_failed == false)
delete argout;
else
delete save_except;
to_do.dev->get_poll_monitor().rel_monitor();
}
}
//+-------------------------------------------------------------------------
//
// method : PollThread::poll_attr
//
// description : Read attribute and store the result in the device
// ring buffer
//
// argument : in : - to_do : The work item
//
//--------------------------------------------------------------------------
void PollThread::poll_attr(WorkItem &to_do)
{
cout5 << "----------> Time = " << now.tv_sec << ","
<< setw(6) << setfill('0') << now.tv_usec
<< " Dev name = " << to_do.dev->get_name()
<< ", Attr name = " << to_do.name << endl;
struct timeval before_cmd,after_cmd,needed_time;
#ifdef _TG_WINDOWS_
struct _timeb before_win,after_win;
LARGE_INTEGER before,after;
#endif
Tango::AttributeValueList *argout = NULL;
Tango::AttributeValueList_3 *argout_3 = NULL;
Tango::AttributeValueList_4 *argout_4 = NULL;
Tango::DevFailed *save_except = NULL;
bool attr_failed = false;
vector<PollObj *>::iterator ite;
long idl_vers = to_do.dev->get_dev_idl_version();
try
{
#ifdef _TG_WINDOWS_
if (ctr_frequency != 0)
QueryPerformanceCounter(&before);
_ftime(&before_win);
before_cmd.tv_sec = (unsigned long)before_win.time;
before_cmd.tv_usec = (long)before_win.millitm * 1000;
#else
gettimeofday(&before_cmd,NULL);
#endif
before_cmd.tv_sec = before_cmd.tv_sec - DELTA_T;
//
// Read the attributes
//
attr_names[0] = to_do.name.c_str();
if (idl_vers >= 4)
argout_4 = (static_cast<Device_4Impl *>(to_do.dev))->read_attributes_4(attr_names,Tango::DEV,dummy_cl_id);
else if (idl_vers == 3)
argout_3 = (static_cast<Device_3Impl *>(to_do.dev))->read_attributes_3(attr_names,Tango::DEV);
else
argout = to_do.dev->read_attributes(attr_names);
#ifdef _TG_WINDOWS_
if (ctr_frequency != 0)
{
QueryPerformanceCounter(&after);
needed_time.tv_sec = 0;
needed_time.tv_usec = (long)((double)(after.QuadPart - before.QuadPart) * ctr_frequency);
to_do.needed_time = needed_time;
}
else
{
_ftime(&after_win);
after_cmd.tv_sec = (unsigned long)after_win.time;
after_cmd.tv_usec = (long)after_win.millitm * 1000;
after_cmd.tv_sec = after_cmd.tv_sec - DELTA_T;
time_diff(before_cmd,after_cmd,needed_time);
to_do.needed_time = needed_time;
}
#else
gettimeofday(&after_cmd,NULL);
after_cmd.tv_sec = after_cmd.tv_sec - DELTA_T;
time_diff(before_cmd,after_cmd,needed_time);
to_do.needed_time = needed_time;
#endif
}
catch (Tango::DevFailed &e)
{
attr_failed = true;
#ifdef _TG_WINDOWS_
if (ctr_frequency != 0)
{
QueryPerformanceCounter(&after);
needed_time.tv_sec = 0;
needed_time.tv_usec = (long)((double)(after.QuadPart - before.QuadPart) * ctr_frequency);
to_do.needed_time = needed_time;
}
else
{
_ftime(&after_win);
after_cmd.tv_sec = (unsigned long)after_win.time;
after_cmd.tv_usec = (long)after_win.millitm * 1000;
after_cmd.tv_sec = after_cmd.tv_sec - DELTA_T;
time_diff(before_cmd,after_cmd,needed_time);
to_do.needed_time = needed_time;
}
#else
gettimeofday(&after_cmd,NULL);
after_cmd.tv_sec = after_cmd.tv_sec - DELTA_T;
time_diff(before_cmd,after_cmd,needed_time);
to_do.needed_time = needed_time;
#endif
save_except = new Tango::DevFailed(e);
}
//
// Starting with IDl release 3, an attribute in error is not an exception
// any more. Re-create one.
// Don't forget that it is still possible to receive classical exception
// (in case of Monitor timeout for instance)
//
if (idl_vers >= 3)
{
if (idl_vers == 4)
{
if ((attr_failed == false) && ((*argout_4)[0].err_list.length() != 0))
{
attr_failed = true;
save_except = new Tango::DevFailed((*argout_4)[0].err_list);
delete argout_4;
}
}
else
{
if ((attr_failed == false) && ((*argout_3)[0].err_list.length() != 0))
{
attr_failed = true;
save_except = new Tango::DevFailed((*argout_3)[0].err_list);
delete argout_3;
}
}
}
//
// Events - for each event call the detect_and_push() method
// this method will fire events if there are clients registered
// and if there is an event (on_change, on_alarm or periodic)
//
EventSupplier *event_supplier;
event_supplier = Util::instance()->get_event_supplier();
if (event_supplier != NULL)
{
if (attr_failed == true)
{
if (idl_vers >= 3)
event_supplier->detect_and_push_events_3(to_do.dev,
idl_vers,
&dummy_att3,
NULL,
save_except,
to_do.name,
&before_cmd);
else
event_supplier->detect_and_push_events(to_do.dev,
idl_vers,
dummy_att,
save_except,
to_do.name);
}
else
{
if (idl_vers >= 4)
event_supplier->detect_and_push_events_3(to_do.dev,
idl_vers,
NULL,
&((*argout_4)[0]),
save_except,
to_do.name,
&before_cmd);
else if (idl_vers == 3)
event_supplier->detect_and_push_events_3(to_do.dev,
idl_vers,
&((*argout_3)[0]),
NULL,
save_except,
to_do.name,
&before_cmd);
else
event_supplier->detect_and_push_events(to_do.dev,
idl_vers,
(*argout)[0],
save_except,
to_do.name);
}
//
// Heartbeat - check to see if it is time to send a heartbeat event
//
if (send_heartbeat == true)
event_supplier->push_heartbeat_event();
}
//
// Insert result in polling buffer and simply forget this attribute if it is
// not possible to insert the result in polling buffer
//
try
{
to_do.dev->get_poll_monitor().get_monitor();
ite = to_do.dev->get_polled_obj_by_type_name(to_do.type,to_do.name);
if (attr_failed == false)
{
if (idl_vers >= 4)
(*ite)->insert_data(argout_4,before_cmd,needed_time);
else if (idl_vers == 3)
(*ite)->insert_data(argout_3,before_cmd,needed_time);
else
(*ite)->insert_data(argout,before_cmd,needed_time);
}
else
(*ite)->insert_except(save_except,before_cmd,needed_time);
to_do.dev->get_poll_monitor().rel_monitor();
}
catch (Tango::DevFailed &)
{
if (attr_failed == false)
{
if (idl_vers >= 4)
delete argout_4;
else if (idl_vers == 3)
delete argout_3;
else
delete argout;
}
else
delete save_except;
to_do.dev->get_poll_monitor().rel_monitor();
}
}
//+-------------------------------------------------------------------------
//
// method : PollThread::eve_heartbeat
//
// description : Send the event heartbeat
//
// argument : in : - to_do : The work item
//
//--------------------------------------------------------------------------
void PollThread::eve_heartbeat(WorkItem &to_do)
{
cout5 << "----------> Time = " << now.tv_sec << ","
<< setw(6) << setfill('0') << now.tv_usec
<< " Sending event heartbeat" << endl;
EventSupplier *event_supplier;
event_supplier = Util::instance()->get_event_supplier();
if ((event_supplier != NULL) && (send_heartbeat == true))
{
event_supplier->push_heartbeat_event();
}
}
//+-------------------------------------------------------------------------
//
// method : PollThread::store_subdev
//
// description : Store the sub device properties when
// needed.
//
// argument : in : - to_do : The work item
//
//--------------------------------------------------------------------------
void PollThread::store_subdev(WorkItem &to_do)
{
static bool ignore_call = true;
cout5 << "----------> Time = " << now.tv_sec << ","
<< setw(6) << setfill('0') << now.tv_usec
<< " Store sub device property data if needed!" << endl;
if ( !ignore_call )
{
Tango::Util *tg = Tango::Util::instance();
tg->get_sub_dev_diag().store_sub_devices();
}
else
{
// ignore the first call to avoid storage during
// device server start-up.
ignore_call = false;
}
}
} // End of Tango namespace
|