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 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
|
static const char *RcsId = "$Id: dserverpoll.cpp 15556 2011-02-11 08:25:58Z taurel $\n$Name$";
//+=============================================================================
//
// file : DServer.cpp
//
// description : C++ source for the DServer class and its commands.
// The class is derived from Device. It represents the
// CORBA servant object which will be accessed from the
// network. All commands which can be executed on a
// DServer object are implemented in this file.
//
// 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.36 2010/09/09 13:45:22 taurel
// - Add year 2010 in Copyright notice
//
// Revision 3.35 2010/07/16 10:53:13 taurel
// - Even with a minimun polling period defined, i is possible to ask for a
// polling period of 0 (externally trigged or polling buffer externally filled in)
//
// Revision 3.34 2010/06/18 13:57:09 taurel
// - Add a way (using properties) to define a minimum polling period
//
// Revision 3.33 2010/06/18 07:45:47 taurel
// - In case of locked device, polling and logging related commands are
// allowed only for the locker process
//
// Revision 3.32 2010/04/27 07:36:42 taurel
// - Merge with the bugfixes branch
//
// Revision 3.31.2.1 2010/03/31 06:42:43 taurel
// - Fix a case dependency bug when updating polling related property.
//
// Revision 3.31 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.30 2009/04/01 06:31:25 taurel
// - Only some changes in printed messages when using -v5
//
// Revision 3.29 2009/03/30 15:03:44 taurel
// - Fix last bugs before Tango 7 ??
//
// Revision 3.28 2009/03/13 09:33:29 taurel
// - Small changes to fix Windows VC8 warnings in Warning level 3
//
// Revision 3.27 2009/02/27 13:26:46 taurel
// - Small changes for Solaris
//
// Revision 3.26 2009/01/21 12:49:04 taurel
// - Change CopyRights for 2009
//
// Revision 3.25 2009/01/15 13:57:20 taurel
// - Fix bugs found by Jens
//
// Revision 3.24 2008/12/17 09:50:59 taurel
// - First implementation of attributes sent on the wire using IDL Union
// instead of IDL Any
//
// Revision 3.23 2008/10/06 15:01:09 taurel
// - Changed the licensing info from GPL to LGPL
//
// Revision 3.22 2008/10/03 06:52:31 taurel
// - Add some licensing info in each files
//
// Revision 3.21 2008/10/02 09:09:47 taurel
// - First implementation of multiple polling thread(s)
//
// Revision 3.20 2008/01/08 14:38:18 taurel
// - strcasecmp() is not named like this on Windows !!
//
// Revision 3.19 2007/11/08 12:03:44 taurel
// - Start implementing user interceptors
// - Fix bug in poll thread pproperty management when removing polling object
// - Set a database timeout to 6 sec
//
// Revision 3.18 2007/10/26 11:35:44 taurel
// - Fix bug in synchronization with polling thread for start_polling, stop_polling and rem_obj_polling
// - String comparaison now case insensitive when storing polling properties in db
//
// Revision 3.17 2007/10/16 08:23:37 taurel
// - Add management of the TC connection establishment timeout for DB access
// - Add DB server cache in DS used during DS startup sequence
// - Comment out the sleep time during DS startup sequence
//
// Revision 3.16 2007/05/17 07:59:07 taurel
// - The polling is not configured via a separate thread any more. The polling thread add_obj_polling method has been modified to support a parameter telling to the polling thread when it has to polled the object.
// Add device name in monitor print message
// Add device_destroyer method in DeviceClass class
//
// Revision 3.15 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
//
//-=============================================================================
#if HAVE_CONFIG_H
#include <ac_config.h>
#endif
#include <tango.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
namespace Tango
{
//+----------------------------------------------------------------------------
//
// method : DServer::polled_device()
//
// description : command to read all the devices actually polled by the
// device server
//
// out : The device name list in a strings sequence
//
//-----------------------------------------------------------------------------
Tango::DevVarStringArray *DServer::polled_device()
{
NoSyncModelTangoMonitor mon(this);
cout4 << "In polled_device command" << endl;
long nb_class = class_list.size();
vector<string> dev_name;
try
{
for (int i = 0;i < nb_class;i++)
{
long nb_dev = class_list[i]->get_device_list().size();
for (long j = 0;j < nb_dev;j++)
{
if ((class_list[i]->get_device_list())[j]->is_polled() == true)
{
dev_name.push_back((class_list[i]->get_device_list())[j]->get_name().c_str());
}
}
}
}
catch (bad_alloc)
{
Except::throw_exception((const char *)"API_MemoryAllocation",
(const char *)"Can't allocate memory in server",
(const char *)"DServer::polled_device");
}
//
// Return an empty sequence if no devices are polled
//
if (dev_name.size() == 0)
{
Tango::DevVarStringArray *ret = new Tango::DevVarStringArray();
ret->length(0);
return ret;
}
//
// Returned device name list to caller (sorted)
//
sort(dev_name.begin(),dev_name.end());
long nb_dev = dev_name.size();
Tango::DevVarStringArray *ret = new Tango::DevVarStringArray(nb_dev);
ret->length(nb_dev);
for (long k = 0;k < nb_dev;k++)
(*ret)[k] = dev_name[k].c_str();
return(ret);
}
//+----------------------------------------------------------------------------
//
// method : DServer::dev_polled_status()
//
// description : command to read device polling status
//
// out : The device polling status as a string (multiple lines)
//
//-----------------------------------------------------------------------------
Tango::DevVarStringArray *DServer::dev_poll_status(string &dev_name)
{
NoSyncModelTangoMonitor mon(this);
cout4 << "In dev_poll_status method" << endl;
//
// Find the device
//
Tango::Util *tg = Tango::Util::instance();
DeviceImpl *dev;
dev = tg->get_device_by_name(dev_name);
vector<PollObj *> &poll_list = dev->get_poll_obj_list();
long nb_poll_obj = poll_list.size();
//
// Return an empty sequence if nothing is polled for this device
//
if (nb_poll_obj == 0)
{
Tango::DevVarStringArray *ret;
ret = new DevVarStringArray();
ret->length(0);
return ret;
}
long i,nb_cmd,nb_attr;
//
// Compute how many cmds and/or attributes are polled
// Since IDL V3, state and status are polled as attributes
// For compatibility, if one of these "attributes" is polled,
// also returned the info as the command is polled
//
nb_cmd = nb_attr = 0;
long nb_to_add = 0;
for (i = 0;i < nb_poll_obj;i++)
{
if (poll_list[i]->get_type() == Tango::POLL_CMD)
nb_cmd++;
else
{
nb_attr++;
if ((poll_list[i]->get_name() == "state") ||
(poll_list[i]->get_name() == "status"))
{
nb_cmd++;
nb_to_add++;
}
}
}
//
// Allocate memory for returned strings
//
Tango::DevVarStringArray *ret;
ret = new DevVarStringArray(nb_poll_obj + nb_to_add);
ret->length(nb_poll_obj + nb_to_add);
//
// Populate returned strings
//
long cmd_ind = 0;
long attr_ind = nb_cmd;
string returned_info;
for(i = 0;i < nb_poll_obj;i++)
{
bool duplicate = false;
//
// First, the name
//
Tango::PollObjType type = poll_list[i]->get_type();
if (type == Tango::POLL_CMD)
{
returned_info = "Polled command name = ";
long k;
long nb_cmd = dev->get_device_class()->get_command_list().size();
for (k = 0;k < nb_cmd;k++)
{
if (dev->get_device_class()->get_command_list()[k]->get_lower_name() == poll_list[i]->get_name())
{
returned_info = returned_info + dev->get_device_class()->get_command_list()[k]->get_name();
break;
}
}
}
else
{
returned_info = "Polled attribute name = ";
if (poll_list[i]->get_name() == "state")
{
duplicate = true;
returned_info = returned_info + "State";
}
else if (poll_list[i]->get_name() == "status")
{
duplicate = true;
returned_info = returned_info + "Status";
}
else
{
Attribute &att = dev->get_device_attr()->get_attr_by_name(poll_list[i]->get_name().c_str());
returned_info = returned_info + att.get_name();
}
}
//
// Add update period
//
// TangoSys_MemStream s;
stringstream s;
string tmp_str;
long po = poll_list[i]->get_upd();
if (po == 0)
{
returned_info = returned_info + "\nPolling externally triggered";
}
else
{
returned_info = returned_info + "\nPolling period (mS) = ";
s << po;
s >> tmp_str;
returned_info = returned_info + tmp_str;
s.clear(); // clear the stream eof flag
}
s.str(""); // clear the underlying string
//
// Add ring buffer depth
//
returned_info = returned_info + "\nPolling ring buffer depth = ";
long depth;
if (type == Tango::POLL_CMD)
depth = dev->get_cmd_poll_ring_depth(poll_list[i]->get_name());
else
depth = dev->get_attr_poll_ring_depth(poll_list[i]->get_name());
s << depth;
returned_info = returned_info + s.str();
s.str(""); // Clear the underlying string
//
// Add a message if the data ring is empty
//
if (poll_list[i]->is_ring_empty() == true)
{
returned_info = returned_info + "\nNo data recorded yet";
}
else
{
//
// Take polled object ownership in order to have coherent info returned to caller
// We don't want the polling thread to insert a new elt in polling ring while
// we are getting these data. Therefore, use the xxx_i methods
//
{
omni_mutex_lock sync(*(poll_list[i]));
//
// Add needed time to execute last command
//
double tmp_db = poll_list[i]->get_needed_time_i();
if (tmp_db == 0.0)
{
returned_info = returned_info + "\nThe polling buffer is externally filled in";
}
else
{
if (po != 0)
{
returned_info = returned_info + "\nTime needed for the last ";
if (type == Tango::POLL_CMD)
returned_info = returned_info + "command execution (mS) = ";
else
returned_info = returned_info + "attribute reading (mS) = ";
s.setf(ios::fixed);
s << setprecision(3) << tmp_db;
returned_info = returned_info + s.str();
s.str("");
//
// Add not updated since... info
//
returned_info = returned_info + "\nData not updated since ";
double since = poll_list[i]->get_last_insert_date_i();
struct timeval now;
#ifdef _TG_WINDOWS_
struct _timeb now_win;
_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;
double now_d = (double)now.tv_sec + ((double)now.tv_usec / 1000000);
double diff_t = now_d - since;
diff_t = diff_t - (tmp_db / 1000);
if (diff_t < 1.0)
{
long nb_msec = (long)(diff_t * 1000);
s << nb_msec;
returned_info = returned_info + s.str() + " mS";
s.str("");
}
else if (diff_t < 60.0)
{
long nb_sec = (long)diff_t;
long nb_msec = (long)((diff_t - nb_sec) * 1000);
s << nb_sec;
returned_info = returned_info + s.str() + " S and ";
s.str("");
s << nb_msec;
returned_info = returned_info + s.str() + " mS";
s.str("");
}
else
{
long nb_min = (long)(diff_t / 60);
long nb_sec = (long)(diff_t - (60 * nb_min));
long nb_msec = (long)((diff_t - (long)diff_t) * 1000);
s << nb_min;
returned_info = returned_info + s.str() + " MN";
s.str("");
if (nb_sec != 0)
{
s << nb_sec ;
returned_info = returned_info + " ," + s.str() + " S";
s.str("");
}
if (nb_msec != 0)
{
s << nb_msec;
returned_info = returned_info + " and " + s.str() + " mS";
s.str("");
}
}
}
}
//
// Add delta_t between last record(s)
//
try
{
vector<double> delta;
poll_list[i]->get_delta_t_i(delta,4);
returned_info = returned_info + "\nDelta between last records (in mS) = ";
for (unsigned long j = 0;j < delta.size();j++)
{
long nb_msec = (long)(delta[j] * 1000);
s << nb_msec;
returned_info = returned_info + s.str();
s.str("");
if (j != (delta.size() - 1))
returned_info = returned_info + ", ";
}
}
catch (Tango::DevFailed)
{
}
//
// Add last polling exception fields (if any)
//
long dev_vers;
bool last_err;
dev_vers = dev->get_dev_idl_version();
if (dev_vers < 3)
last_err = poll_list[i]->is_last_an_error_i();
else
last_err = poll_list[i]->is_last_an_error_i_3();
if (last_err == true)
{
if (type == Tango::POLL_CMD)
returned_info = returned_info + "\nLast command execution FAILED :";
else
returned_info = returned_info + "\nLast attribute read FAILED :";
Tango::DevFailed *exe_ptr = poll_list[i]->get_last_except_i();
returned_info = returned_info + "\n\tReason = " + exe_ptr->errors[0].reason.in();
returned_info = returned_info + "\n\tDesc = " + exe_ptr->errors[0].desc.in();
returned_info = returned_info + "\n\tOrigin = " + exe_ptr->errors[0].origin.in();
}
//
// Release polled object monitor (only a compiler block end)
//
}
}
//
// Init. string in sequence
//
if (type == Tango::POLL_CMD)
{
(*ret)[cmd_ind] = CORBA::string_dup(returned_info.c_str());
cmd_ind++;
}
else
{
(*ret)[attr_ind] = CORBA::string_dup(returned_info.c_str());
attr_ind++;
//
// If the attribute is state or status, also add the string in command list
// after replacing all occurences of "attributes" by "command" in the returned
// string
//
if (duplicate == true)
{
string::size_type pos = returned_info.find("attribute");
while (pos != string::npos)
{
returned_info.replace(pos,9,"command");
string::size_type npos;
npos = returned_info.find("attribute",pos);
pos = npos;
}
(*ret)[cmd_ind] = CORBA::string_dup(returned_info.c_str());
cmd_ind++;
}
}
}
return(ret);
}
//+----------------------------------------------------------------------------
//
// method : DServer::add_obj_polling()
//
// description : command to add one object to be polled
//
// in : The polling parameters :
// device name
// object type (command or attribute)
// object name
// update period in mS (in the long array)
//
//-----------------------------------------------------------------------------
void DServer::add_obj_polling(const Tango::DevVarLongStringArray *argin,
bool with_db_upd,int delta_ms)
{
NoSyncModelTangoMonitor nosyn_mon(this);
cout4 << "In add_obj_polling method" << endl;
unsigned long i;
for (i = 0;i < argin->svalue.length();i++)
cout4 << "Input string = " << (argin->svalue)[i].in() << endl;
for (i = 0;i < argin->lvalue.length();i++)
cout4 << "Input long = " << (argin->lvalue)[i] << endl;
//
// Check that parameters number is correct
//
if ((argin->svalue.length() != 3) || (argin->lvalue.length() != 1))
{
Except::throw_exception((const char *)"API_WrongNumberOfArgs",
(const char *)"Incorrect number of inout arguments",
(const char *)"DServer::add_obj_polling");
}
//
// Find the device
//
Tango::Util *tg = Tango::Util::instance();
DeviceImpl *dev;
try
{
dev = tg->get_device_by_name((argin->svalue)[0]);
}
catch (Tango::DevFailed &e)
{
TangoSys_OMemStream o;
o << "Device " << (argin->svalue)[0] << " not found" << ends;
Except::re_throw_exception(e,(const char *)"API_DeviceNotFound",o.str(),
(const char *)"DServer::add_obj_polling");
}
//
// If the device is locked and if the client is not the lock owner,
// refuse to do the job
//
check_lock_owner(dev,"add_obj_polling",(argin->svalue)[0]);
//
// Check that the command (or the attribute) exists. For command, also checks
// that it does not need input value.
//
string obj_type((argin->svalue)[1]);
transform(obj_type.begin(),obj_type.end(),obj_type.begin(),::tolower);
string obj_name((argin->svalue)[2]);
transform(obj_name.begin(),obj_name.end(),obj_name.begin(),::tolower);
PollObjType type;
if (obj_type == PollCommand)
{
dev->check_command_exists(obj_name);
type = Tango::POLL_CMD;
}
else if (obj_type == PollAttribute)
{
dev->get_device_attr()->get_attr_by_name((argin->svalue)[2]);
type = Tango::POLL_ATTR;
}
else
{
TangoSys_OMemStream o;
o << "Object type " << obj_type << " not supported" << ends;
Except::throw_exception((const char *)"API_NotSupported",o.str(),
(const char *)"DServer::add_obj_polling");
}
//
// If it's for the Init command, refuse to poll it
//
if (obj_type == PollCommand)
{
if (obj_name == "init")
{
TangoSys_OMemStream o;
o << "It's not possible to poll the Init command!" << ends;
Except::throw_exception((const char *)"API_NotSupported",o.str(),
(const char *)"DServer::add_obj_polling");
}
//
// Since IDl release 3, state and status command must be polled
// as attributes to be able to generate event on state or
// status.
//
else if ((dev->get_dev_idl_version() >= 3) && ((obj_name == "state") || (obj_name == "status")))
type = Tango::POLL_ATTR;
}
//
// Check that the object is not already polled
//
vector<PollObj *> &poll_list = dev->get_poll_obj_list();
for (i = 0;i < poll_list.size();i++)
{
if (poll_list[i]->get_type() == type)
{
string name_lower = poll_list[i]->get_name();
transform(name_lower.begin(),name_lower.end(),name_lower.begin(),::tolower);
if (name_lower == obj_name)
{
TangoSys_OMemStream o;
if (type == Tango::POLL_CMD)
o << "Command ";
else
o << "Attribute ";
o << obj_name << " already polled" << ends;
Except::throw_exception((const char *)"API_AlreadyPolled",
o.str(),
(const char *)"DServer::add_obj_polling");
}
}
}
//
// Check that the update period is not to small
//
int upd = (argin->lvalue)[0];
if ((upd != 0) && (upd < MIN_POLL_PERIOD))
{
TangoSys_OMemStream o;
o << (argin->lvalue)[0] << " is below the min authorized period (" << MIN_POLL_PERIOD << " mS)" << ends;
Except::throw_exception((const char *)"API_NotSupported",o.str(),
(const char *)"DServer::add_obj_polling");
}
//
// Check that the requested polling period is not below the one authorized
// (if defined)
// 0 as polling period is always authorized for polling buffer externally filled
//
if (upd != 0)
check_upd_authorized(dev,upd,type,obj_name);
//
// Round the update period (which is in mS) to the next tenth of a second
//
/* double upd_db = (double)upd;
double remain = fmod(upd_db,(double)MIN_POLL_PERIOD);
double base = floor(upd_db) - remain;
if (remain >= 50)
upd = (long)base + MIN_POLL_PERIOD;
else
upd = (long)base;
cout4 << "Rounded polling period = " << upd << " mS" << endl;*/
//
// Create a new PollObj instance for this object
// Protect this code by a monitor in case of the polling thread using one of
// the vector element.
//
long depth;
if (obj_type == PollCommand)
depth = dev->get_cmd_poll_ring_depth(obj_name);
else
depth = dev->get_attr_poll_ring_depth(obj_name);
dev->get_poll_monitor().get_monitor();
poll_list.push_back(new PollObj(dev,type,obj_name,upd,depth));
dev->get_poll_monitor().rel_monitor();
//
// Find out which thread is in charge of the device.
// If none exists already, create one
//
PollingThreadInfo *th_info;
int thread_created = -2;
int poll_th_id = tg->get_polling_thread_id_by_name((argin->svalue)[0]);
if (poll_th_id == 0)
{
cout4 << "POLLING: Creating a thread to poll device " << (argin->svalue)[0] << endl;
thread_created = tg->create_poll_thread((argin->svalue)[0],false);
poll_th_id = tg->get_polling_thread_id_by_name((argin->svalue)[0]);
}
cout4 << "POLLING: Thread in charge of device " << (argin->svalue)[0] << " is thread " << poll_th_id << endl;
th_info = tg->get_polling_thread_info_by_id(poll_th_id);
//
// Send command to the polling thread but wait in case of previous cmd
// still not executed
//
cout4 << "Sending cmd to polling thread" << endl;
int interupted;
TangoMonitor &mon = th_info->poll_mon;
PollThCmd &shared_cmd = th_info->shared_data;
{
omni_mutex_lock sync(mon);
if (shared_cmd.cmd_pending == true)
{
mon.wait();
}
shared_cmd.cmd_pending = true;
shared_cmd.cmd_code = POLL_ADD_OBJ;
shared_cmd.dev = dev;
shared_cmd.index = poll_list.size() - 1;
shared_cmd.new_upd = delta_ms;
mon.signal();
cout4 << "Cmd sent to polling thread" << endl;
//
// Wait for thread to execute command except if the command is
// requested by the polling thread itself
//
omni_thread *th = omni_thread::self();
int th_id = th->id();
if (th_id != poll_th_id)
{
while (shared_cmd.cmd_pending == true)
{
interupted = mon.wait(DEFAULT_TIMEOUT);
if ((shared_cmd.cmd_pending == true) && (interupted == false))
{
cout4 << "TIME OUT" << endl;
delete poll_list.back();
poll_list.pop_back();
//
// If the thread has been created by this request, try to kill it
//
if (thread_created == -1)
{
shared_cmd.cmd_pending = true;
shared_cmd.cmd_code = POLL_EXIT;
mon.signal();
}
Except::throw_exception((const char *)"API_CommandTimedOut",
(const char *)"Polling thread blocked !!!",
(const char *)"DServer::add_obj_polling");
}
}
}
}
cout4 << "Thread cmd normally executed" << endl;
th_info->nb_polled_objects++;
//
// Update polling parameters in database (if wanted and possible)
// If the property is already there (it should not but...), only update its
// polling period
//
if ((with_db_upd == true) && (Tango::Util::_UseDb == true))
{
TangoSys_MemStream s;
string upd_str;
s << upd;
s >> upd_str;
bool found = false;
DbDatum db_info("polled_cmd");
if (type == Tango::POLL_CMD)
{
vector<string> &non_auto_list = dev->get_non_auto_polled_cmd();
vector<string>::iterator ite;
for (ite = non_auto_list.begin();ite < non_auto_list.end();++ite)
{
if (TG_strcasecmp((*ite).c_str(),obj_name.c_str()) == 0)
{
non_auto_list.erase(ite);
db_info.name = "non_auto_polled_cmd";
db_info << non_auto_list;
found = true;
break;
}
}
if (found == false)
{
vector<string> &cmd_list = dev->get_polled_cmd();
for (i = 0;i < cmd_list.size();i = i+2)
{
if (TG_strcasecmp(cmd_list[i].c_str(),obj_name.c_str()) == 0)
{
cmd_list[i + 1] = upd_str;
break;
}
}
if (i == cmd_list.size())
{
cmd_list.push_back(obj_name);
cmd_list.push_back(upd_str);
}
db_info << cmd_list;
}
}
else
{
vector<string> &non_auto_list = dev->get_non_auto_polled_attr();
vector<string>::iterator ite;
for (ite = non_auto_list.begin();ite < non_auto_list.end();++ite)
{
if (TG_strcasecmp((*ite).c_str(),obj_name.c_str()) == 0)
{
non_auto_list.erase(ite);
db_info.name = "non_auto_polled_attr";
db_info << non_auto_list;
found = true;
break;
}
}
if (found == false)
{
db_info.name = "polled_attr";
vector<string> &attr_list = dev->get_polled_attr();
for (i = 0;i < attr_list.size();i = i+2)
{
if (TG_strcasecmp(attr_list[i].c_str(),obj_name.c_str()) == 0)
{
attr_list[i + 1] = upd_str;
break;
}
}
if (i == attr_list.size())
{
attr_list.push_back(obj_name);
attr_list.push_back(upd_str);
}
db_info << attr_list;
}
}
DbData send_data;
send_data.push_back(db_info);
dev->get_db_device()->put_property(send_data);
}
//
// If a polling thread has just been created, ask it to poll
//
if (thread_created == -1)
{
start_polling(th_info);
}
//
// Also update the polling threads pool conf if one thread has been created by this call
//
if (thread_created != -2)
{
string dev_name((argin->svalue)[0]);
transform(dev_name.begin(),dev_name.end(),dev_name.begin(),::tolower);
cout4 << "thread_created = " << thread_created << endl;
if (thread_created == -1)
{
tg->get_poll_pool_conf().push_back(dev_name);
}
else if (thread_created >= 0)
{
string &conf_entry = (tg->get_poll_pool_conf())[thread_created];
conf_entry = conf_entry + ',' + dev_name;
}
if ((with_db_upd == true) && (Tango::Util::_UseDb == true))
{
DbData send_data;
send_data.push_back(DbDatum("polling_threads_pool_conf"));
send_data[0] << tg->get_poll_pool_conf();
tg->get_dserver_device()->get_db_device()->put_property(send_data);
}
}
cout4 << "Polling properties updated" << endl;
//
// Mark the device as polled
//
dev->is_polled(true);
}
//+----------------------------------------------------------------------------
//
// method : DServer::upd_obj_polling_period()
//
// description : command to upadte an already polled object update period
//
// in : The polling parameters :
// device name
// object type (command or attribute)
// object name
// update period in mS (in the long array)
//
//-----------------------------------------------------------------------------
void DServer::upd_obj_polling_period(const Tango::DevVarLongStringArray *argin,
bool with_db_upd)
{
NoSyncModelTangoMonitor nosync_mon(this);
cout4 << "In upd_obj_polling_period method" << endl;
unsigned long i;
for (i = 0;i < argin->svalue.length();i++)
cout4 << "Input string = " << (argin->svalue)[i].in() << endl;
for (i = 0;i < argin->lvalue.length();i++)
cout4 << "Input long = " << (argin->lvalue)[i] << endl;
//
// Check that parameters number is correct
//
if ((argin->svalue.length() != 3) || (argin->lvalue.length() != 1))
{
Except::throw_exception((const char *)"API_WrongNumberOfArgs",
(const char *)"Incorrect number of inout arguments",
(const char *)"DServer::upd_obj_polling_period");
}
//
// Find the device
//
Tango::Util *tg = Tango::Util::instance();
DeviceImpl *dev;
try
{
dev = tg->get_device_by_name((argin->svalue)[0]);
}
catch (Tango::DevFailed &e)
{
TangoSys_OMemStream o;
o << "Device " << (argin->svalue)[0] << " not found" << ends;
Except::re_throw_exception(e,(const char *)"API_DeviceNotFound",o.str(),
(const char *)"DServer::upd_obj_polling_period");
}
//
// Check that the device is polled
//
if (dev->is_polled() == false)
{
TangoSys_OMemStream o;
o << "Device " << (argin->svalue)[0] << " is not polled" << ends;
Except::throw_exception((const char *)"API_DeviceNotPolled",o.str(),
(const char *)"DServer::upd_obj_polling_period");
}
//
// If the device is locked and if the client is not the lock owner,
// refuse to do the job
//
check_lock_owner(dev,"upd_obj_polling_period",(argin->svalue)[0]);
//
// Find the wanted object in the list of device polled object
//
string obj_type((argin->svalue)[1]);
transform(obj_type.begin(),obj_type.end(),obj_type.begin(),::tolower);
string obj_name((argin->svalue)[2]);
transform(obj_name.begin(),obj_name.end(),obj_name.begin(),::tolower);
PollObjType type;
if (obj_type == PollCommand)
{
type = Tango::POLL_CMD;
//
// Since IDl release 3, state and status command must be polled
// as attributes to be able to generate event on state or
// status.
//
if ((obj_name == "state") || (obj_name == "status"))
type = Tango::POLL_ATTR;
}
else if (obj_type == PollAttribute)
{
type = Tango::POLL_ATTR;
}
else
{
TangoSys_OMemStream o;
o << "Object type " << obj_type << " not supported" << ends;
Except::throw_exception((const char *)"API_NotSupported",o.str(),
(const char *)"DServer::upd_obj_polling_period");
}
vector<PollObj *>::iterator ite = dev->get_polled_obj_by_type_name(type,obj_name);
//
// Check that the requested polling period is not below the one authorized
// (if defined)
//
int upd = (argin->lvalue)[0];
check_upd_authorized(dev,upd,type,obj_name);
//
// Check that it is not an externally triggered polling object. In this
// case, throw exception
//
/* long tmp_upd = (*ite)->get_upd();
if (tmp_upd == 0)
{
TangoSys_OMemStream o;
o << "Polling for ";
if (type == Tango::POLL_CMD)
o << "command ";
else
o << "attribute ";
o << (argin->svalue)[2];
o << " (device " << (argin->svalue)[0] << ") ";
o << " is externally triggered. Remove and add object to change its polling period";
o << ends;
Except::throw_exception((const char *)"API_NotSupported",o.str(),
(const char *)"DServer::upd_obj_polling_period");
}*/
//
// Check that the update period is not to small
//
if ((upd != 0) && (upd < MIN_POLL_PERIOD))
{
TangoSys_OMemStream o;
o << (argin->lvalue)[0] << " is below the min authorized period (" << MIN_POLL_PERIOD << " mS)" << ends;
Except::throw_exception((const char *)"API_NotSupported",o.str(),
(const char *)"DServer::upd_obj_polling");
}
//
// Find out which thread is in charge of the device.
// If none exists already, create one
//
PollingThreadInfo *th_info;
int poll_th_id = tg->get_polling_thread_id_by_name((argin->svalue)[0]);
if (poll_th_id == 0)
{
TangoSys_OMemStream o;
o << "Can't find a polling thread for device " << (argin->svalue)[0] << ends;
Except::throw_exception((const char *)"API_NotSupported",o.str(),
(const char *)"DServer::upd_obj_polling");
}
th_info = tg->get_polling_thread_info_by_id(poll_th_id);
//
// Update polling period
//
(*ite)->update_upd((argin->lvalue)[0]);
//
// Send command to the polling thread
//
TangoMonitor &mon = th_info->poll_mon;
PollThCmd &shared_cmd = th_info->shared_data;
int th_id = omni_thread::self()->id();
if (th_id != poll_th_id)
{
omni_mutex_lock sync(mon);
if (shared_cmd.cmd_pending == true)
{
mon.wait();
}
shared_cmd.cmd_pending = true;
shared_cmd.cmd_code = POLL_UPD_PERIOD;
shared_cmd.dev = dev;
shared_cmd.name = obj_name;
shared_cmd.type = type;
shared_cmd.new_upd = (argin->lvalue)[0];
#ifdef __SUNPRO_CC
distance(dev->get_poll_obj_list().begin(),ite,shared_cmd.index);
#else
shared_cmd.index = distance(dev->get_poll_obj_list().begin(),ite);
#endif
mon.signal();
}
else
{
shared_cmd.cmd_pending = true;
shared_cmd.cmd_code = POLL_UPD_PERIOD;
shared_cmd.dev = dev;
shared_cmd.name = obj_name;
shared_cmd.type = type;
shared_cmd.new_upd = (argin->lvalue)[0];
#ifdef __SUNPRO_CC
distance(dev->get_poll_obj_list().begin(),ite,shared_cmd.index);
#else
shared_cmd.index = distance(dev->get_poll_obj_list().begin(),ite);
#endif
PollThread *poll_th = th_info->poll_th;
poll_th->set_local_cmd(shared_cmd);
poll_th->execute_cmd();
}
//
// Update database property --> Update polling period if this object is already
// defined in the polling property. Add object name and update period if the
// object is not known in the property
//
if ((with_db_upd == true) && (Tango::Util::_UseDb == true))
{
TangoSys_MemStream s;
string upd_str;
s << (argin->lvalue)[0] << ends;
s >> upd_str;
DbDatum db_info("polled_attr");
if (type == Tango::POLL_CMD)
{
db_info.name = "polled_cmd";
vector<string> &cmd_list = dev->get_polled_cmd();
for (i = 0;i < cmd_list.size();i = i+2)
{
if (TG_strcasecmp(cmd_list[i].c_str(),obj_name.c_str()) == 0)
{
cmd_list[i + 1] = upd_str;
break;
}
}
if (i == cmd_list.size())
{
cmd_list.push_back(obj_name);
cmd_list.push_back(upd_str);
}
db_info << cmd_list;
}
else
{
vector<string> &attr_list = dev->get_polled_attr();
for (i = 0;i < attr_list.size();i = i+2)
{
if (TG_strcasecmp(attr_list[i].c_str(),obj_name.c_str()) == 0)
{
attr_list[i + 1] = upd_str;
break;
}
}
if (i == attr_list.size())
{
attr_list.push_back(obj_name);
attr_list.push_back(upd_str);
}
db_info << attr_list;
}
DbData send_data;
send_data.push_back(db_info);
dev->get_db_device()->put_property(send_data);
}
}
//+----------------------------------------------------------------------------
//
// method : DServer::rem_obj_polling()
//
// description : command to remove an already polled object from the device
// polled object list
//
// in : The polling parameters :
// device name
// object type (command or attribute)
// object name
//
//-----------------------------------------------------------------------------
void DServer::rem_obj_polling(const Tango::DevVarStringArray *argin,
bool with_db_upd)
{
NoSyncModelTangoMonitor nosync_mon(this);
cout4 << "In rem_obj_polling method" << endl;
unsigned long i;
for (i = 0;i < argin->length();i++)
cout4 << "Input string = " << (*argin)[i].in() << endl;
//
// Check that parameters number is correct
//
if (argin->length() != 3)
{
Except::throw_exception((const char *)"API_WrongNumberOfArgs",
(const char *)"Incorrect number of inout arguments",
(const char *)"DServer::rem_obj_polling");
}
//
// Find the device
//
Tango::Util *tg = Tango::Util::instance();
DeviceImpl *dev;
try
{
dev = tg->get_device_by_name((*argin)[0]);
}
catch (Tango::DevFailed &e)
{
TangoSys_OMemStream o;
o << "Device " << (*argin)[0] << " not found" << ends;
Except::re_throw_exception(e,(const char *)"API_DeviceNotFound",o.str(),
(const char *)"DServer::rem_obj_polling");
}
//
// Check that the device is polled
//
if (dev->is_polled() == false)
{
TangoSys_OMemStream o;
o << "Device " << (*argin)[0] << " is not polled" << ends;
Except::throw_exception((const char *)"API_DeviceNotPolled",o.str(),
(const char *)"DServer::rem_obj_polling");
}
//
// If the device is locked and if the client is not the lock owner,
// refuse to do the job
//
check_lock_owner(dev,"rem_obj_polling",(*argin)[0]);
//
// Find the wanted object in the list of device polled object
//
string obj_type((*argin)[1]);
transform(obj_type.begin(),obj_type.end(),obj_type.begin(),::tolower);
string obj_name((*argin)[2]);
transform(obj_name.begin(),obj_name.end(),obj_name.begin(),::tolower);
PollObjType type;
if (obj_type == PollCommand)
{
type = Tango::POLL_CMD;
if ((obj_name == "state") || (obj_name == "status"))
type = Tango::POLL_ATTR;
}
else if (obj_type == PollAttribute)
{
type = Tango::POLL_ATTR;
}
else
{
TangoSys_OMemStream o;
o << "Object type " << obj_type << " not supported" << ends;
Except::throw_exception((const char *)"API_NotSupported",o.str(),
(const char *)"DServer::rem_obj_polling");
}
vector<PollObj *>::iterator ite = dev->get_polled_obj_by_type_name(type,obj_name);
long tmp_upd = (*ite)->get_upd();
//
// Find out which thread is in charge of the device.
//
PollingThreadInfo *th_info;
int poll_th_id = tg->get_polling_thread_id_by_name((*argin)[0]);
if (poll_th_id == 0)
{
TangoSys_OMemStream o;
o << "Can't find a polling thread for device " << (*argin)[0] << ends;
Except::throw_exception((const char *)"API_NotSupported",o.str(),
(const char *)"DServer::rem_obj_polling");
}
cout4 << "Thread in charge of device " << (*argin)[0] << " is thread " << poll_th_id << endl;
th_info = tg->get_polling_thread_info_by_id(poll_th_id);
//
// Test whether the polling thread is still running!
//
if ( th_info->poll_th != NULL )
{
//
// Send command to the polling thread
//
cout4 << "Sending cmd to polling thread" << endl;
int interupted;
TangoMonitor &mon = th_info->poll_mon;
PollThCmd &shared_cmd = th_info->shared_data;
{
omni_mutex_lock sync(mon);
if (shared_cmd.cmd_pending == true)
{
mon.wait();
}
shared_cmd.cmd_pending = true;
if (tmp_upd == 0)
shared_cmd.cmd_code = POLL_REM_EXT_TRIG_OBJ;
else
shared_cmd.cmd_code = POLL_REM_OBJ;
shared_cmd.dev = dev;
shared_cmd.name = obj_name;
shared_cmd.type = type;
mon.signal();
cout4 << "Cmd sent to polling thread" << endl;
//
// Wait for thread to execute command except if the command is
// requested by the polling thread itself
//
int th_id = omni_thread::self()->id();
if (th_id != poll_th_id)
{
while (shared_cmd.cmd_pending == true)
{
interupted = mon.wait(DEFAULT_TIMEOUT);
if ((shared_cmd.cmd_pending == true) && (interupted == false))
{
cout4 << "TIME OUT" << endl;
Except::throw_exception((const char *)"API_CommandTimedOut",
(const char *)"Polling thread blocked !!!",
(const char *)"DServer::rem_obj_polling");
}
}
}
}
cout4 << "Thread cmd normally executed" << endl;
}
else
{
cout4 << "Polling thread is no longer running!!!!" << endl;
}
//
// Remove the object from the polled object list
//
vector<PollObj *> &poll_list = dev->get_poll_obj_list();
dev->get_poll_monitor().get_monitor();
delete(*ite);
poll_list.erase(ite);
dev->get_poll_monitor().rel_monitor();
//
// Mark the device as non polled if this was the last polled object
//
if (poll_list.empty() == true)
dev->is_polled(false);
//
// Update database property. This means remove object entry in the polling
// properties if they exist or add it to the list of device not polled
// for automatic polling defined at command/attribute level.
// Do this if possible and wanted.
//
if ((with_db_upd == true) && (Tango::Util::_UseDb == true))
{
DbData send_data;
DbDatum db_info("polled_attr");
bool update_needed = false;
if (type == Tango::POLL_CMD)
{
db_info.name = "polled_cmd";
vector<string> &cmd_list = dev->get_polled_cmd();
vector<string>::iterator s_ite;
for (s_ite = cmd_list.begin();s_ite < cmd_list.end();++s_ite)
{
if (TG_strcasecmp((*s_ite).c_str(),obj_name.c_str()) == 0)
{
s_ite = cmd_list.erase(s_ite);
cmd_list.erase(s_ite);
db_info << cmd_list;
update_needed = true;
break;
}
++s_ite;
}
if (update_needed == false)
{
vector<string> &non_auto_cmd = dev->get_non_auto_polled_cmd();
for (s_ite = non_auto_cmd.begin();s_ite < non_auto_cmd.end();s_ite++)
{
if (TG_strcasecmp((*s_ite).c_str(),obj_name.c_str()) == 0)
break;
}
if (s_ite == non_auto_cmd.end())
{
non_auto_cmd.push_back(obj_name);
db_info.name = "non_auto_polled_cmd";
db_info << non_auto_cmd;
update_needed = true;
}
}
}
else
{
vector<string> &attr_list = dev->get_polled_attr();
vector<string>::iterator s_ite;
for (s_ite = attr_list.begin();s_ite < attr_list.end();++s_ite)
{
if (TG_strcasecmp((*s_ite).c_str(),obj_name.c_str()) == 0)
{
s_ite = attr_list.erase(s_ite);
attr_list.erase(s_ite);
db_info << attr_list;
update_needed = true;
break;
}
++s_ite;
}
if (update_needed == false)
{
vector<string> &non_auto_attr = dev->get_non_auto_polled_attr();
for (s_ite = non_auto_attr.begin();s_ite < non_auto_attr.end();s_ite++)
{
if (TG_strcasecmp((*s_ite).c_str(),obj_name.c_str()) == 0)
break;
}
if (s_ite == non_auto_attr.end())
{
non_auto_attr.push_back(obj_name);
db_info.name = "non_auto_polled_attr";
db_info << non_auto_attr;
update_needed = true;
}
}
}
if (update_needed == true)
{
DbData send_data;
send_data.push_back(db_info);
if (db_info.size() == 0)
dev->get_db_device()->delete_property(send_data);
else
dev->get_db_device()->put_property(send_data);
cout4 << "Database polling properties updated" << endl;
}
}
//
// If the device is not polled any more, update the pool conf first locally.
// Also update the map<device name,thread id>
// If this device was the only one for a polling thread, kill the thread
// Then in Db if possible
//
bool kill_thread = false;
if (poll_list.empty() == true)
{
int ind;
string dev_name((*argin)[0]);
transform(dev_name.begin(),dev_name.end(),dev_name.begin(),::tolower);
if ((ind = tg->get_dev_entry_in_pool_conf(dev_name)) == -1)
{
TangoSys_OMemStream o;
o << "Can't find entry for device " << (*argin)[0] << " in polling threads pool configuration !"<< ends;
Except::throw_exception((const char *)"API_NotSupported",o.str(),
(const char *)"DServer::rem_obj_polling");
}
vector<string> &pool_conf = tg->get_poll_pool_conf();
string &conf_entry = pool_conf[ind];
string::size_type pos;
if ((pos = conf_entry.find(',')) != string::npos)
{
pos = conf_entry.find(dev_name);
if ((pos + dev_name.size()) != conf_entry.size())
conf_entry.erase(pos,dev_name.size() + 1);
else
conf_entry.erase(pos - 1);
}
else
{
vector<string>::iterator iter = pool_conf.begin() + ind;
pool_conf.erase(iter);
kill_thread = true;
}
tg->remove_dev_from_polling_map(dev_name);
//
// Kill the thread if needed and join
//
if (kill_thread == true)
{
TangoMonitor &mon = th_info->poll_mon;
PollThCmd &shared_cmd = th_info->shared_data;
{
omni_mutex_lock sync(mon);
shared_cmd.cmd_pending = true;
shared_cmd.cmd_code = POLL_EXIT;
mon.signal();
}
void *dummy_ptr;
cout4 << "POLLING: Joining with one polling thread" << endl;
th_info->poll_th->join(&dummy_ptr);
tg->remove_polling_thread_info_by_id(poll_th_id);
}
//
// Update db
//
if ((with_db_upd == true) && (Tango::Util::_UseDb == true))
{
DbData send_data;
send_data.push_back(DbDatum("polling_threads_pool_conf"));
send_data[0] << tg->get_poll_pool_conf();
tg->get_dserver_device()->get_db_device()->put_property(send_data);
}
}
}
//+----------------------------------------------------------------------------
//
// method : DServer::stop_polling()
//
// description : command to stop the polling thread
//
//-----------------------------------------------------------------------------
void DServer::stop_polling()
{
NoSyncModelTangoMonitor nosync_mon(this);
cout4 << "In stop_polling method" << endl;
//
// Send command to the polling thread and wait for its execution
//
int interupted;
Tango::Util *tg = Tango::Util::instance();
vector<PollingThreadInfo *> &th_info = tg->get_polling_threads_info();
vector<PollingThreadInfo *>:: iterator iter;
for (iter = th_info.begin();iter != th_info.end();++iter)
{
TangoMonitor &mon = (*iter)->poll_mon;
PollThCmd &shared_cmd = (*iter)->shared_data;
{
omni_mutex_lock sync(mon);
if (shared_cmd.cmd_pending == true)
{
mon.wait();
}
shared_cmd.cmd_pending = true;
shared_cmd.cmd_code = POLL_STOP;
mon.signal();
while (shared_cmd.cmd_pending == true)
{
interupted = mon.wait(DEFAULT_TIMEOUT);
if ((shared_cmd.cmd_pending == true) && (interupted == false))
{
cout4 << "TIME OUT" << endl;
Except::throw_exception((const char *)"API_CommandTimedOut",
(const char *)"Polling thread blocked !!!",
(const char *)"DServer::stop_polling");
}
}
}
}
//
// Update polling status
//
tg->poll_status(false);
string &str = get_status();
str = "The device is ON\nThe polling is OFF";
}
//+----------------------------------------------------------------------------
//
// method : DServer::start_polling()
//
// description : command to start the polling thread
//
//-----------------------------------------------------------------------------
void DServer::start_polling()
{
NoSyncModelTangoMonitor nosync_mon(this);
cout4 << "In start_polling method" << endl;
//
// Send command to the polling thread(s) and wait for its execution
//
int interupted;
Tango::Util *tg = Tango::Util::instance();
vector<PollingThreadInfo *> &th_info = tg->get_polling_threads_info();
vector<PollingThreadInfo *>:: iterator iter;
for (iter = th_info.begin();iter != th_info.end();++iter)
{
TangoMonitor &mon = (*iter)->poll_mon;
PollThCmd &shared_cmd = (*iter)->shared_data;
{
omni_mutex_lock sync(mon);
if (shared_cmd.cmd_pending == true)
{
mon.wait();
}
shared_cmd.cmd_pending = true;
shared_cmd.cmd_code = POLL_START;
mon.signal();
while (shared_cmd.cmd_pending == true)
{
interupted = mon.wait(DEFAULT_TIMEOUT);
if ((shared_cmd.cmd_pending == true) && (interupted == false))
{
cout4 << "TIME OUT" << endl;
Except::throw_exception((const char *)"API_CommandTimedOut",
(const char *)"Polling thread blocked !!!",
(const char *)"DServer::start_polling");
}
}
}
}
//
// Update polling status
//
tg->poll_status(true);
string &str = get_status();
str = "The device is ON\nThe polling is ON";
}
void DServer::start_polling(PollingThreadInfo *th_info)
{
TangoMonitor &mon = th_info->poll_mon;
PollThCmd &shared_cmd = th_info->shared_data;
int interupted;
{
omni_mutex_lock sync(mon);
if (shared_cmd.cmd_pending == true)
{
mon.wait();
}
shared_cmd.cmd_pending = true;
shared_cmd.cmd_code = POLL_START;
mon.signal();
while (shared_cmd.cmd_pending == true)
{
interupted = mon.wait(DEFAULT_TIMEOUT);
if ((shared_cmd.cmd_pending == true) && (interupted == false))
{
cout4 << "TIME OUT" << endl;
Except::throw_exception((const char *)"API_CommandTimedOut",
(const char *)"Polling thread blocked while trying to start thread polling!!!",
(const char *)"DServer::start_polling");
}
}
}
}
//+----------------------------------------------------------------------------
//
// method : DServer::add_event_heartbeat()
//
// description : command to ask the heartbeat thread to send
// the event heartbeat every 10 seconds
//
//-----------------------------------------------------------------------------
void DServer::add_event_heartbeat()
{
NoSyncModelTangoMonitor nosyn_mon(this);
cout4 << "In add_event_heartbeat method" << endl;
//
// Send command to the heartbeat thread but wait in case of previous cmd
// still not executed
//
cout4 << "Sending cmd to polling thread" << endl;
int interupted;
Tango::Util *tg = Tango::Util::instance();
TangoMonitor &mon = tg->get_heartbeat_monitor();
PollThCmd &shared_cmd = tg->get_heartbeat_shared_cmd();
{
omni_mutex_lock sync(mon);
if (shared_cmd.cmd_pending == true)
{
mon.wait();
}
shared_cmd.cmd_pending = true;
shared_cmd.cmd_code = POLL_ADD_HEARTBEAT;
mon.signal();
cout4 << "Cmd sent to polling thread" << endl;
//
// Wait for thread to execute command except if the command is
// requested by the polling thread itself
//
int th_id = omni_thread::self()->id();
if (th_id != tg->get_heartbeat_thread_id())
{
while (shared_cmd.cmd_pending == true)
{
interupted = mon.wait(DEFAULT_TIMEOUT);
if ((shared_cmd.cmd_pending == true) && (interupted == false))
{
cout4 << "TIME OUT" << endl;
Except::throw_exception((const char *)"API_CommandTimedOut",
(const char *)"Polling thread blocked !!!",
(const char *)"DServer::add_event_heartbeat");
}
}
}
}
cout4 << "Thread cmd normally executed" << endl;
}
//+----------------------------------------------------------------------------
//
// method : DServer::rem_event_heartbeat()
//
// description : command to ask the heartbeat thread to stop sending
// the event heartbeat
//
//-----------------------------------------------------------------------------
void DServer::rem_event_heartbeat()
{
NoSyncModelTangoMonitor nosyn_mon(this);
cout4 << "In rem_event_heartbeat method" << endl;
//
// Send command to the heartbeat thread but wait in case of previous cmd
// still not executed
//
cout4 << "Sending cmd to polling thread" << endl;
int interupted;
Tango::Util *tg = Tango::Util::instance();
TangoMonitor &mon = tg->get_heartbeat_monitor();
PollThCmd &shared_cmd = tg->get_heartbeat_shared_cmd();
{
omni_mutex_lock sync(mon);
if (shared_cmd.cmd_pending == true)
{
mon.wait();
}
shared_cmd.cmd_pending = true;
shared_cmd.cmd_code = POLL_REM_HEARTBEAT;
mon.signal();
cout4 << "Cmd sent to polling thread" << endl;
//
// Wait for thread to execute command except if the command is
// requested by the polling thread itself
//
int th_id = omni_thread::self()->id();
if (th_id != tg->get_heartbeat_thread_id())
{
while (shared_cmd.cmd_pending == true)
{
interupted = mon.wait(DEFAULT_TIMEOUT);
if ((shared_cmd.cmd_pending == true) && (interupted == false))
{
cout4 << "TIME OUT" << endl;
Except::throw_exception((const char *)"API_CommandTimedOut",
(const char *)"Polling thread blocked !!!",
(const char *)"DServer::rem_event_heartbeat");
}
}
}
}
cout4 << "Thread cmd normally executed" << endl;
}
//+----------------------------------------------------------------------------
//
// method : DServer::check_upd_authorized()
//
// description : In case a minimun update polling period is defined (via the
// min_poll_period, cmd_min_poll_period,attr_min_poll_period)
// check if the requested period is not smaller
//
// argin: dev : The device
// upd : The requested update period
// type : The polled object type (cmd / attr)
// obj_name : The polled object name
//
//-----------------------------------------------------------------------------
void DServer::check_upd_authorized(DeviceImpl *dev,int upd,PollObjType obj_type,string &obj_name)
{
int min_upd = 0;
//
// Get first the xxx_min_poll_period then if not defined the min_poll_period
//
vector<string> *v_ptr;
if (obj_type == Tango::POLL_CMD)
v_ptr = &(dev->get_cmd_min_poll_period());
else
v_ptr = &(dev->get_attr_min_poll_period());
vector<string>::iterator ite = find(v_ptr->begin(),v_ptr->end(),obj_name);
if (ite != v_ptr->end())
{
cout << obj_name << " found in vector" << endl;
ite++;
TangoSys_MemStream s;
s << *ite;
#if ((defined _TG_WINDOWS_) || (defined __SUNPRO_CC) || (defined GCC_STD))
if ((s >> min_upd) == false)
#else
s >> min_upd;
if (!s)
#endif
{
TangoSys_OMemStream o;
o << "System property ";
if (obj_type == Tango::POLL_CMD)
o << "cmd_min_poll_period";
else
o << "attr_min_poll_period";
o << " for device " << dev->get_name() << " has wrong syntax" << ends;
Except::throw_exception((const char *)"API_BadConfigurationProperty",
o.str(),
(const char *)"DServer::check_upd_uthorized()");
}
}
else
{
cout << "min_upd from min_poll_period" << endl;
min_upd = dev->get_min_poll_period();
}
//
// Check with user request
//
cout << "min_upd = " << min_upd << ", upd = " << upd << endl;
if ((min_upd != 0) && (upd < min_upd))
{
TangoSys_OMemStream o;
o << "Polling period for ";
if (obj_type == Tango::POLL_CMD)
o << "command ";
else
o << "attribute ";
o << obj_name << " is below the min authorized (" << min_upd << ")" << ends;
Except::throw_exception((const char *)"API_MethodArgument",o.str(),
(const char *)"DServer::check_upd_authorized");
}
}
} // End of Tango namespace
|