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 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705
|
/******************************************************************************
* Copyright (c) 2000-2021 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
*
* Contributors:
* Baji, Laszlo
* Balasko, Jeno
* Baranyi, Botond
* Delic, Adam
* Feher, Csaba
* Forstner, Matyas
* Kovacs, Ferenc
* Pandi, Krisztian
* Raduly, Csaba
* Szabados, Kristof
* Szabo, Bence Janos
* Szabo, Janos Zoltan – initial implementation
* Szalai, Gabor
* Tatarka, Gabor
*
******************************************************************************/
#include "Port.hh"
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include "../common/platform.h"
#include "../common/memory.h"
#include "Component.hh"
#include "Error.hh"
#include "Logger.hh"
#include "Event_Handler.hh"
#include "Fd_And_Timeout_User.hh"
#include "Snapshot.hh"
#include "Communication.hh"
#include "Runtime.hh"
#include "Octetstring.hh"
#include "TitanLoggerApi.hh"
// maximum number of iterations for binding the UNIX server socket
#define UNIX_BIND_MAX_ITER 100
#include "../common/dbgnew.hh"
void Map_Params::init(unsigned int p_nof_params)
{
nof_params = p_nof_params;
params = new CHARSTRING[nof_params];
}
void Map_Params::clear()
{
delete[] params;
nof_params = 0;
params = NULL;
}
void Map_Params::copy(const Map_Params& p_other)
{
init(p_other.nof_params);
for (unsigned int i = 0; i < nof_params; ++i) {
params[i] = p_other.params[i];
}
}
Map_Params& Map_Params::operator=(const Map_Params& p_other)
{
clear();
copy(p_other);
return *this;
}
void Map_Params::reset(unsigned int p_nof_params)
{
clear();
init(p_nof_params);
}
void Map_Params::set_param(unsigned int p_index, const CHARSTRING& p_param)
{
if (p_index >= nof_params) {
TTCN_error("Map/unmap parameter index out of bounds");
}
params[p_index] = p_param;
}
unsigned int Map_Params::get_nof_params() const
{
return nof_params;
}
const CHARSTRING& Map_Params::get_param(unsigned int p_index) const
{
if (p_index >= nof_params) {
TTCN_error("Map/unmap parameter index out of bounds");
}
return params[p_index];
}
Map_Params map_params_cache(0);
PORT *PORT::list_head = NULL, *PORT::list_tail = NULL;
PORT *PORT::system_list_head = NULL, *PORT::system_list_tail = NULL;
void PORT::add_to_list(boolean system)
{
PORT **head = system == FALSE ? &list_head : &system_list_head;
PORT **tail = system == FALSE ? &list_tail : &system_list_tail;
// check for duplicate names
for (PORT *p = *head; p != NULL; p = p->list_next) {
// do nothing if this is already a member of the list
if (p == this) return;
else if (!strcmp(p->port_name, port_name))
TTCN_error("Internal error: There are more than one ports with "
"name %s.", port_name);
}
// append this to the list
if (*head == NULL) *head = this;
else if (*tail != NULL) (*tail)->list_next = this;
list_prev = *tail;
list_next = NULL;
*tail = this;
}
void PORT::remove_from_list(boolean system)
{
PORT **head = system == FALSE ? &list_head : &system_list_head;
PORT **tail = system == FALSE ? &list_tail : &system_list_tail;
if (list_prev != NULL) list_prev->list_next = list_next;
else if (*head == this) *head = list_next;
if (list_next != NULL) list_next->list_prev = list_prev;
else if (*tail == this) *tail = list_prev;
list_prev = NULL;
list_next = NULL;
}
PORT *PORT::lookup_by_name(const char *par_port_name, boolean system)
{
if (system == FALSE) {
for (PORT *port = list_head; port != NULL; port = port->list_next)
if (!strcmp(par_port_name, port->port_name)) return port;
return NULL;
} else {
for (PORT *port = system_list_head; port != NULL; port = port->list_next)
if (!strcmp(par_port_name, port->port_name)) return port;
return NULL;
}
}
struct PORT::port_parameter {
component_id_t component_id;
char *port_name;
char *parameter_name;
char *parameter_value;
struct port_parameter *next_par;
} *PORT::parameter_head = NULL, *PORT::parameter_tail = NULL;
void PORT::apply_parameter(port_parameter *par_ptr)
{
if (par_ptr->port_name != NULL) {
// the parameter refers to a specific port
PORT *port = lookup_by_name(par_ptr->port_name);
if (port != NULL) port->set_parameter(par_ptr->parameter_name,
par_ptr->parameter_value);
} else {
// the parameter refers to all ports (*)
for (PORT *port = list_head; port != NULL; port = port->list_next)
port->set_parameter(par_ptr->parameter_name,
par_ptr->parameter_value);
}
}
void PORT::set_system_parameters(const char *system_port)
{
for (port_parameter *par = parameter_head; par != NULL; par = par->next_par) {
if (par->component_id.id_selector == COMPONENT_ID_SYSTEM &&
(par->port_name == NULL || !strcmp(par->port_name, system_port))) {
set_parameter(par->parameter_name, par->parameter_value);
}
}
}
void PORT::add_parameter(const component_id_t& component_id,
const char *par_port_name, const char *parameter_name,
const char *parameter_value)
{
port_parameter *new_par = new port_parameter;
new_par->component_id.id_selector = component_id.id_selector;
switch (component_id.id_selector) {
case COMPONENT_ID_NAME:
new_par->component_id.id_name = mcopystr(component_id.id_name);
break;
case COMPONENT_ID_COMPREF:
new_par->component_id.id_compref = component_id.id_compref;
break;
default:
break;
}
if (par_port_name == NULL) new_par->port_name = NULL;
else new_par->port_name = mcopystr(par_port_name);
new_par->parameter_name = mcopystr(parameter_name);
new_par->parameter_value = mcopystr(parameter_value);
new_par->next_par = NULL;
if (parameter_head == NULL) parameter_head = new_par;
if (parameter_tail != NULL) parameter_tail->next_par = new_par;
parameter_tail = new_par;
}
void PORT::clear_parameters()
{
while (parameter_head != NULL) {
port_parameter *next_par = parameter_head->next_par;
if (parameter_head->component_id.id_selector == COMPONENT_ID_NAME)
Free(parameter_head->component_id.id_name);
Free(parameter_head->port_name);
Free(parameter_head->parameter_name);
Free(parameter_head->parameter_value);
delete parameter_head;
parameter_head = next_par;
}
}
void PORT::set_parameters(component component_reference,
const char *component_name)
{
for (port_parameter *par = parameter_head; par != NULL; par = par->next_par)
switch (par->component_id.id_selector) {
case COMPONENT_ID_NAME:
if (component_name != NULL &&
!strcmp(par->component_id.id_name, component_name))
apply_parameter(par);
break;
case COMPONENT_ID_COMPREF:
if (par->component_id.id_compref == component_reference)
apply_parameter(par);
break;
case COMPONENT_ID_ALL:
apply_parameter(par);
break;
default:
break;
}
}
enum connection_data_type_enum {
CONN_DATA_LAST = 0, CONN_DATA_MESSAGE = 1, CONN_DATA_CALL = 2,
CONN_DATA_REPLY = 3, CONN_DATA_EXCEPTION = 4
};
enum connection_state_enum {
CONN_IDLE, CONN_LISTENING, CONN_CONNECTED, CONN_LAST_MSG_SENT,
CONN_LAST_MSG_RCVD
};
struct port_connection : public Fd_Event_Handler {
PORT *owner_port;
connection_state_enum connection_state;
component remote_component;
char *remote_port;
transport_type_enum transport_type;
union {
struct {
PORT *port_ptr;
} local;
struct {
int comm_fd;
Text_Buf *incoming_buf;
} stream;
};
struct port_connection *list_prev, *list_next;
OCTETSTRING sliding_buffer;
virtual void Handle_Fd_Event(int fd,
boolean is_readable, boolean is_writeable, boolean is_error);
virtual ~port_connection();
virtual void log() const;
};
void port_connection::Handle_Fd_Event(int,
boolean is_readable, boolean /*is_writeable*/, boolean /*is_error*/)
{
// Note event for connection with TRANSPORT_LOCAL transport_type
// may not arrive.
if (transport_type == TRANSPORT_INET_STREAM
|| transport_type == TRANSPORT_UNIX_STREAM
) {
if (is_readable) {
if (connection_state == CONN_LISTENING)
owner_port->handle_incoming_connection(this);
else owner_port->handle_incoming_data(this);
}
} else
TTCN_error("Internal error: Invalid transport type (%d) in port "
"connection between %s and %d:%s.", transport_type,
owner_port->get_name(), remote_component, remote_port);
}
void port_connection::log() const
{
TTCN_Logger::log_event("port connection between ");
owner_port->log(); TTCN_Logger::log_event(" and ");
TTCN_Logger::log_event(remote_component); TTCN_Logger::log_event(":");
TTCN_Logger::log_event("%s", remote_port);
}
port_connection::~port_connection()
{
if (transport_type == TRANSPORT_INET_STREAM
|| transport_type == TRANSPORT_UNIX_STREAM
) {
if (stream.comm_fd != -1) {
TTCN_warning_begin("Internal Error: File descriptor %d not "
"closed/removed in ", stream.comm_fd); log();
TTCN_warning_end();
}
}
sliding_buffer.clean_up();
}
PORT::PORT(const char *par_port_name)
{
port_name = par_port_name != NULL ? par_port_name : "<unknown>";
is_active = FALSE;
is_started = FALSE;
is_halted = FALSE;
list_prev = NULL;
list_next = NULL;
connection_list_head = NULL;
connection_list_tail = NULL;
n_system_mappings = 0;
system_mappings = NULL;
}
PORT::~PORT()
{
if (is_active) deactivate_port(FALSE); //TODO false or true
}
void PORT::set_name(const char * name)
{
if (name == NULL) TTCN_error("Internal error: Setting an "
"invalid name for a single element of a port array.");
port_name = name;
}
void PORT::log() const
{
TTCN_Logger::log_event("port %s", port_name);
}
void PORT::activate_port(boolean system)
{
if (!is_active) {
add_to_list(system);
is_active = TRUE;
msg_head_count = 0;
msg_tail_count = 0;
proc_head_count = 0;
proc_tail_count = 0;
// Only has effect when the translation port has port variables with
// default values. Only call when it is activated.
if (n_system_mappings == 0) {
init_port_variables();
}
}
}
void PORT::deactivate_port(boolean system)
{
if (is_active) {
/* In order to proceed with the deactivation we must ignore the
* following errors:
* - errors in user code of Test Port (i.e. user_stop, user_unmap)
* - failures when sending messages to MC (the link may be down)
*/
boolean is_parallel = !TTCN_Runtime::is_single();
// terminate all connections
while (connection_list_head != NULL) {
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::removing__unterminated__connection,
port_name,
connection_list_head->remote_component, connection_list_head->remote_port);
if (is_parallel) {
try {
TTCN_Communication::send_disconnected(port_name,
connection_list_head->remote_component,
connection_list_head->remote_port);
} catch (const TC_Error&) { }
}
remove_connection(connection_list_head);
}
// terminate all mappings
while (n_system_mappings > 0) {
// we must make a copy of the string because unmap() will destroy it
char *system_port = mcopystr(system_mappings[0]);
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::removing__unterminated__mapping,
port_name, NULL_COMPREF, system_port);
Map_Params params(0);
try {
unmap(system_port, params, system);
} catch (const TC_Error&) { }
if (is_parallel) {
try {
TTCN_Communication::send_unmapped(port_name, system_port, params, system);
} catch (const TC_Error&) { }
}
Free(system_port);
}
// the previous disconnect/unmap operations may generate incoming events
// so we should stop and clear the queue after them
if (is_started || is_halted) {
try {
stop();
} catch (const TC_Error&) { }
}
clear_queue();
// deactivate all event handlers
Fd_And_Timeout_User::remove_all_fds(this);
Fd_And_Timeout_User::set_timer(this, 0.0);
// File descriptor events of port connections are removed
// in remove_connection
remove_from_list(system);
is_active = FALSE;
}
}
void PORT::deactivate_all()
{
while (list_head != NULL) list_head->deactivate_port(FALSE);
while (system_list_head != NULL) system_list_head->deactivate_port(TRUE);
}
void PORT::clear()
{
if (!is_active) TTCN_error("Internal error: Inactive port %s cannot "
"be cleared.", port_name);
if (!is_started && !is_halted) {
TTCN_warning("Performing clear operation on port %s, which is "
"already stopped. The operation has no effect.", port_name);
}
clear_queue();
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::port__was__cleared, port_name);
}
void PORT::all_clear()
{
for (PORT *port = list_head; port != NULL; port = port->list_next)
port->clear();
for (PORT *port = system_list_head; port != NULL; port = port->list_next)
port->clear();
}
void PORT::start()
{
if (!is_active) TTCN_error("Internal error: Inactive port %s cannot "
"be started.", port_name);
if (is_started) {
TTCN_warning("Performing start operation on port %s, which is "
"already started. The operation will clear the incoming queue.",
port_name);
clear_queue();
} else {
if (is_halted) {
// the queue might contain old messages which has to be discarded
clear_queue();
is_halted = FALSE;
}
user_start();
is_started = TRUE;
}
TTCN_Logger::log_port_state(TitanLoggerApi::Port__State_operation::started,
port_name);
}
void PORT::all_start()
{
for (PORT *port = list_head; port != NULL; port = port->list_next)
port->start();
for (PORT *port = system_list_head; port != NULL; port = port->list_next)
port->start();
}
void PORT::stop()
{
if (!is_active) TTCN_error("Internal error: Inactive port %s cannot "
"be stopped.", port_name);
if (is_started) {
is_started = FALSE;
is_halted = FALSE;
user_stop();
// dropping all messages from the queue because they cannot be
// extracted by receiving operations anymore
clear_queue();
} else if (is_halted) {
is_halted = FALSE;
clear_queue();
} else {
TTCN_warning("Performing stop operation on port %s, which is "
"already stopped. The operation has no effect.", port_name);
}
TTCN_Logger::log_port_state(TitanLoggerApi::Port__State_operation::stopped,
port_name);
}
void PORT::all_stop()
{
for (PORT *port = list_head; port != NULL; port = port->list_next)
port->stop();
for (PORT *port = system_list_head; port != NULL; port = port->list_next)
port->stop();
}
void PORT::halt()
{
if (!is_active) TTCN_error("Internal error: Inactive port %s cannot "
"be halted.", port_name);
if (is_started) {
is_started = FALSE;
is_halted = TRUE;
user_stop();
// keep the messages in the queue
} else if (is_halted) {
TTCN_warning("Performing halt operation on port %s, which is "
"already halted. The operation has no effect.", port_name);
} else {
TTCN_warning("Performing halt operation on port %s, which is "
"already stopped. The operation has no effect.", port_name);
}
TTCN_Logger::log_port_state(TitanLoggerApi::Port__State_operation::halted,
port_name);
}
void PORT::all_halt()
{
for (PORT *port = list_head; port != NULL; port = port->list_next)
port->halt();
for (PORT *port = system_list_head; port != NULL; port = port->list_next)
port->halt();
}
boolean PORT::port_is_started() {
return is_started;
}
void PORT::safe_start()
{
if (!is_started) {
activate_port(TRUE);
start();
}
}
void PORT::add_port(PORT* /*p*/)
{
TTCN_error("Internal error: Calling PORT::add_port");
}
void PORT::remove_port(PORT* /*p*/)
{
TTCN_error("Internal error: Calling PORT::remove_port");
}
PORT* PORT::get_provider_port() {
return NULL;
}
boolean PORT::incoming_message_handler(const void* message_ptr, const char* message_type,
component sender_component, const FLOAT& timestamp)
{
return FALSE;
}
alt_status PORT::receive(const COMPONENT_template&, COMPONENT *, FLOAT*, Index_Redirect*)
{
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::no__incoming__types,
TitanLoggerApi::MatchingProblemType_operation::receive__,
FALSE, FALSE, port_name);
return ALT_NO;
}
alt_status PORT::any_receive(const COMPONENT_template& sender_template,
COMPONENT *sender_ptr, FLOAT* timestamp_redirect)
{
if (list_head != NULL) {
alt_status ret_val = ALT_NO;
for (PORT *port = list_head; port != NULL; port = port->list_next) {
switch (port->receive(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Receive operation returned "
"unexpected status code on port %s while evaluating "
"`any port.receive'.", port->port_name);
}
}
return ret_val;
} else {
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::component__has__no__ports,
TitanLoggerApi::MatchingProblemType_operation::receive__,
TRUE, FALSE);
return ALT_NO;
}
}
alt_status PORT::check_receive(const COMPONENT_template&, COMPONENT *, FLOAT*,
Index_Redirect*)
{
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::no__incoming__types,
TitanLoggerApi::MatchingProblemType_operation::receive__,
FALSE, TRUE, port_name);
return ALT_NO;
}
alt_status PORT::any_check_receive(const COMPONENT_template& sender_template,
COMPONENT *sender_ptr, FLOAT* timestamp_redirect)
{
if (list_head != NULL) {
alt_status ret_val = ALT_NO;
for (PORT *port = list_head; port != NULL; port = port->list_next) {
switch (port->check_receive(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Check-receive operation returned "
"unexpected status code on port %s while evaluating "
"`any port.check(receive)'.", port->port_name);
}
}
return ret_val;
} else {
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::component__has__no__ports,
TitanLoggerApi::MatchingProblemType_operation::receive__,
TRUE, TRUE);
return ALT_NO;
}
}
alt_status PORT::trigger(const COMPONENT_template&, COMPONENT *, FLOAT*,
Index_Redirect*)
{
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::no__incoming__types,
TitanLoggerApi::MatchingProblemType_operation::trigger__,
FALSE, FALSE, port_name);
return ALT_NO;
}
alt_status PORT::any_trigger(const COMPONENT_template& sender_template,
COMPONENT *sender_ptr, FLOAT* timestamp_redirect)
{
if (list_head != NULL) {
alt_status ret_val = ALT_NO;
for (PORT *port = list_head; port != NULL; port = port->list_next) {
switch (port->trigger(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
case ALT_REPEAT:
return ALT_REPEAT;
default:
TTCN_error("Internal error: Trigger operation returned "
"unexpected status code on port %s while evaluating "
"`any port.trigger'.", port->port_name);
}
}
return ret_val;
} else {
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::component__has__no__ports,
TitanLoggerApi::MatchingProblemType_operation::trigger__,
TRUE, FALSE);
return ALT_NO;
}
}
alt_status PORT::getcall(const COMPONENT_template&, COMPONENT *, FLOAT*, Index_Redirect*)
{
// ToDo:Unnecessary log matching problem warning removed.
// Question: does it unnecessary?
// TTCN_Logger::log_matching_problem(
// TitanLoggerApi::MatchingProblemType_reason::no__incoming__signatures,
// TitanLoggerApi::MatchingProblemType_operation::getcall__,
// false, false, port_name);
return ALT_NO;
}
alt_status PORT::any_getcall(const COMPONENT_template& sender_template,
COMPONENT *sender_ptr, FLOAT* timestamp_redirect)
{
if (list_head != NULL) {
alt_status ret_val = ALT_NO;
for (PORT *port = list_head; port != NULL; port = port->list_next) {
switch (port->getcall(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Getcall operation returned "
"unexpected status code on port %s while evaluating "
"`any port.getcall'.", port->port_name);
}
}
return ret_val;
} else {
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::component__has__no__ports,
TitanLoggerApi::MatchingProblemType_operation::getcall__,
TRUE, FALSE);
return ALT_NO;
}
}
alt_status PORT::check_getcall(const COMPONENT_template&, COMPONENT *, FLOAT*, Index_Redirect*)
{
// ToDo:Unnecessary log matching problem warning removed.
// Question: does it unnecessary
// TTCN_Logger::log_matching_problem(
// TitanLoggerApi::MatchingProblemType_reason::no__incoming__signatures,
// TitanLoggerApi::MatchingProblemType_operation::getcall__,
// false, false, port_name);
return ALT_NO;
}
alt_status PORT::any_check_getcall(const COMPONENT_template& sender_template,
COMPONENT *sender_ptr, FLOAT* timestamp_redirect)
{
if (list_head != NULL) {
alt_status ret_val = ALT_NO;
for (PORT *port = list_head; port != NULL; port = port->list_next) {
switch (port->check_getcall(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Check-getcall operation returned "
"unexpected status code on port %s while evaluating "
"`any port.check(getcall)'.", port->port_name);
}
}
return ret_val;
} else {
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::component__has__no__ports,
TitanLoggerApi::MatchingProblemType_operation::getcall__,
TRUE, TRUE);
return ALT_NO;
}
}
alt_status PORT::getreply(const COMPONENT_template&, COMPONENT *, FLOAT*, Index_Redirect*)
{
// ToDo:Unnecessary log matching problem warning removed.
// Question: does it unnecessary
// TTCN_Logger::log_matching_problem(
// TitanLoggerApi::MatchingProblemType_reason::no__outgoing__blocking__signatures,
// TitanLoggerApi::MatchingProblemType_operation::getreply__,
// false, false, port_name);
return ALT_NO;
}
alt_status PORT::any_getreply(const COMPONENT_template& sender_template,
COMPONENT *sender_ptr, FLOAT* timestamp_redirect)
{
if (list_head != NULL) {
alt_status ret_val = ALT_NO;
for (PORT *port = list_head; port != NULL; port = port->list_next) {
switch (port->getreply(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Getreply operation returned "
"unexpected status code on port %s while evaluating "
"`any port.getreply'.", port->port_name);
}
}
return ret_val;
} else {
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::component__has__no__ports,
TitanLoggerApi::MatchingProblemType_operation::getreply__,
TRUE, FALSE);
return ALT_NO;
}
}
alt_status PORT::check_getreply(const COMPONENT_template&, COMPONENT *, FLOAT*, Index_Redirect*)
{
// ToDo:Unnecessary log matching problem warning removed.
// Question: does it unnecessary
// TTCN_Logger::log_matching_problem(
// TitanLoggerApi::MatchingProblemType_reason::no__outgoing__blocking__signatures,
// TitanLoggerApi::MatchingProblemType_operation::getreply__,
// false, true, port_name);
return ALT_NO;
}
alt_status PORT::any_check_getreply(const COMPONENT_template& sender_template,
COMPONENT *sender_ptr, FLOAT* timestamp_redirect)
{
if (list_head != NULL) {
alt_status ret_val = ALT_NO;
for (PORT *port = list_head; port != NULL; port = port->list_next) {
switch (port->check_getreply(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Check-getreply operation returned "
"unexpected status code on port %s while evaluating "
"`any port.check(getreply)'.", port->port_name);
}
}
return ret_val;
} else {
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::component__has__no__ports,
TitanLoggerApi::MatchingProblemType_operation::getreply__,
TRUE, TRUE);
return ALT_NO;
}
}
alt_status PORT::get_exception(const COMPONENT_template&, COMPONENT *, FLOAT*, Index_Redirect*)
{
// ToDo:Unnecessary log matching problem warning removed.
// Question: does it unnecessary
// TTCN_Logger::log_matching_problem(
// TitanLoggerApi::MatchingProblemType_reason::no__outgoing__blocking__signatures__that__support__exceptions,
// TitanLoggerApi::MatchingProblemType_operation::catch__,
// false, false, port_name);
return ALT_NO;
}
alt_status PORT::any_catch(const COMPONENT_template& sender_template,
COMPONENT *sender_ptr, FLOAT* timestamp_redirect)
{
if (list_head != NULL) {
alt_status ret_val = ALT_NO;
for (PORT *port = list_head; port != NULL; port = port->list_next) {
switch (port->get_exception(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Catch operation returned "
"unexpected status code on port %s while evaluating "
"`any port.catch'.", port->port_name);
}
}
return ret_val;
} else {
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::component__has__no__ports,
TitanLoggerApi::MatchingProblemType_operation::catch__,
TRUE, FALSE);
return ALT_NO;
}
}
alt_status PORT::check_catch(const COMPONENT_template& ,
COMPONENT *, FLOAT*, Index_Redirect*)
{
// ToDo:Unnecessary log matching problem warning removed.
// Question: does it unnecessary
// TTCN_Logger::log_matching_problem(
// TitanLoggerApi::MatchingProblemType_reason::no__outgoing__blocking__signatures__that__support__exceptions,
// TitanLoggerApi::MatchingProblemType_operation::catch__,
// false, TRUE, port_name);
return ALT_NO;
}
alt_status PORT::any_check_catch(const COMPONENT_template& sender_template,
COMPONENT *sender_ptr, FLOAT* timestamp_redirect)
{
if (list_head != NULL) {
alt_status ret_val = ALT_NO;
for (PORT *port = list_head; port != NULL; port = port->list_next) {
switch (port->check_catch(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Check-catch operation returned "
"unexpected status code on port %s while evaluating "
"`any port.check(catch)'.", port->port_name);
}
}
return ret_val;
} else {
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::component__has__no__ports,
TitanLoggerApi::MatchingProblemType_operation::catch__,
TRUE, TRUE);
return ALT_NO;
}
}
alt_status PORT::check(const COMPONENT_template& sender_template,
COMPONENT *sender_ptr, FLOAT* timestamp_redirect, Index_Redirect*)
{
alt_status ret_val = ALT_NO;
// the procedure-based queue must have the higher priority
switch (check_getcall(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Check-getcall operation returned "
"unexpected status code on port %s.", port_name);
}
if (ret_val != ALT_MAYBE) {
// don't try getreply if the procedure-based queue is empty
// (i.e. check_getcall() returned ALT_MAYBE)
switch (check_getreply(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Check-getreply operation returned "
"unexpected status code on port %s.", port_name);
}
}
if (ret_val != ALT_MAYBE) {
// don't try catch if the procedure-based queue is empty
// (i.e. check_getcall() or check_getreply() returned ALT_MAYBE)
switch (check_catch(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Check-catch operation returned "
"unexpected status code on port %s.", port_name);
}
}
switch (check_receive(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Check-receive operation returned "
"unexpected status code on port %s.", port_name);
}
return ret_val;
}
alt_status PORT::any_check(const COMPONENT_template& sender_template,
COMPONENT *sender_ptr, FLOAT* timestamp_redirect)
{
if (list_head != NULL) {
alt_status ret_val = ALT_NO;
for (PORT *port = list_head; port != NULL; port = port->list_next) {
switch (port->check(sender_template, sender_ptr, timestamp_redirect)) {
case ALT_YES:
return ALT_YES;
case ALT_MAYBE:
ret_val = ALT_MAYBE;
break;
case ALT_NO:
break;
default:
TTCN_error("Internal error: Check operation returned "
"unexpected status code on port %s while evaluating "
"`any port.check'.", port->port_name);
}
}
return ret_val;
} else {
TTCN_Logger::log_matching_problem(
TitanLoggerApi::MatchingProblemType_reason::component__has__no__ports,
TitanLoggerApi::MatchingProblemType_operation::check__,
TRUE, FALSE);
return ALT_NO;
}
}
void PORT::set_parameter(const char *parameter_name, const char *)
{
TTCN_warning("Test port parameter %s is not supported on port %s.",
parameter_name, port_name);
}
void PORT::append_to_msg_queue(msg_queue_item_base* new_item)
{
new_item->next_item = NULL;
if (msg_queue_tail == NULL) msg_queue_head = new_item;
else msg_queue_tail->next_item = new_item;
msg_queue_tail = new_item;
}
void PORT::Handle_Fd_Event(int fd, boolean is_readable, boolean is_writable,
boolean is_error)
{
// The port intends to use the finer granularity event handler functions
if (is_error) {
Handle_Fd_Event_Error(fd);
if (!is_writable && !is_readable) return;
fd_event_type_enum event = Fd_And_Timeout_User::getCurReceivedEvent();
if ((event & FD_EVENT_WR) == 0) is_writable = FALSE;
if ((event & FD_EVENT_RD) == 0) is_readable = FALSE;
}
if (is_writable) {
Handle_Fd_Event_Writable(fd);
if (!is_readable) return;
if ((Fd_And_Timeout_User::getCurReceivedEvent() & FD_EVENT_RD) == 0)
return;
}
if (is_readable)
Handle_Fd_Event_Readable(fd);
}
void PORT::Handle_Fd_Event_Error(int)
{
// Silently ignore
// A port need not wait for error events
// Note: error events always cause event handler invocation
}
void PORT::Handle_Fd_Event_Writable(int)
{
TTCN_error("There is no Handle_Fd_Event_Writable member function "
"implemented in port %s. "
"This method or the Handle_Fd_Event method has to be implemented in "
"the port if the port waits for any file descriptor to be writable - "
"unless the port uses Install_Handler to specify the file descriptor "
"and timeout events for which the port waits.", port_name);
}
void PORT::Handle_Fd_Event_Readable(int)
{
TTCN_error("There is no Handle_Fd_Event_Readable member function "
"implemented in port %s. "
"This method or the Handle_Fd_Event method has to be implemented in "
"the port if the port waits for any file descriptor to be readable - "
"unless the port uses Install_Handler to specify the file descriptor "
"and timeout events for which the port waits.", port_name);
}
void PORT::Handle_Timeout(double /*time_since_last_call*/)
{
TTCN_error("There is no Handle_Timeout member function implemented in "
"port %s. "
"This method has to be implemented in the port if the port waits for "
"timeouts unless the port uses Install_Handler to specify the timeout.",
port_name);
}
void PORT::Event_Handler(const fd_set * /*read_fds*/, const fd_set * /*write_fds*/,
const fd_set * /*error_fds*/, double /*time_since_last_call*/)
{
TTCN_error("There is no Event_Handler implemented in port %s. "
"Event_Handler has to be implemented in the port if "
"Install_Handler is used to specify the file descriptor and timeout "
"events for which the port waits.", port_name);
}
void PORT::Handler_Add_Fd(int fd, Fd_Event_Type event_mask)
{
Fd_And_Timeout_User::add_fd(fd, this,
static_cast<fd_event_type_enum>(
static_cast<int>(event_mask)));
}
void PORT::Handler_Add_Fd_Read(int fd)
{
Fd_And_Timeout_User::add_fd(fd, this, FD_EVENT_RD);
}
void PORT::Handler_Add_Fd_Write(int fd)
{
Fd_And_Timeout_User::add_fd(fd, this, FD_EVENT_WR);
}
void PORT::Handler_Remove_Fd(int fd, Fd_Event_Type event_mask)
{
Fd_And_Timeout_User::remove_fd(fd, this,
static_cast<fd_event_type_enum>(
static_cast<int>(event_mask)));
}
void PORT::Handler_Remove_Fd_Read(int fd)
{
Fd_And_Timeout_User::remove_fd(fd, this, FD_EVENT_RD);
}
void PORT::Handler_Remove_Fd_Write(int fd)
{
Fd_And_Timeout_User::remove_fd(fd, this, FD_EVENT_WR);
}
void PORT::Handler_Set_Timer(double call_interval, boolean is_timeout,
boolean call_anyway, boolean is_periodic)
{
Fd_And_Timeout_User::set_timer(this, call_interval, is_timeout, call_anyway,
is_periodic);
}
void PORT::Install_Handler(const fd_set *read_fds, const fd_set *write_fds,
const fd_set *error_fds, double call_interval)
{
if (!is_active) TTCN_error("Event handler cannot be installed for "
"inactive port %s.", port_name);
if ((long) FdMap::getFdLimit() > (long) FD_SETSIZE) {
static boolean once = TRUE;
if (once) {
TTCN_warning("The maximum number of open file descriptors (%i)"
" is greater than FD_SETSIZE (%li)."
" Ensure that Test Ports using Install_Handler do not try to"
" wait for events of file descriptors with values greater than"
" FD_SETSIZE (%li)."
" (Current caller of Install_Handler is \"%s\")",
FdMap::getFdLimit(), (long) FD_SETSIZE, (long) FD_SETSIZE,
port_name);
}
once = FALSE;
}
Fd_And_Timeout_User::set_fds_with_fd_sets(this, read_fds, write_fds,
error_fds);
Fd_And_Timeout_User::set_timer(this, call_interval);
}
void PORT::Uninstall_Handler()
{
Fd_And_Timeout_User::remove_all_fds(this);
Fd_And_Timeout_User::set_timer(this, 0.0);
}
void PORT::user_map(const char *system_port)
{
// call the new user_map function if the legacy version is not overridden in
// the port implementation
Map_Params dummy(0);
user_map(system_port, dummy);
}
void PORT::user_unmap(const char *system_port)
{
// call the new user_unmap function if the legacy version is not overridden in
// the port implementation
Map_Params dummy(0);
user_unmap(system_port, dummy);
}
void PORT::user_map(const char *, Map_Params&)
{
}
void PORT::user_unmap(const char *, Map_Params&)
{
}
void PORT::user_start()
{
}
void PORT::user_stop()
{
}
void PORT::clear_queue()
{
}
component PORT::get_default_destination()
{
if (connection_list_head != NULL) {
if (n_system_mappings > 0) TTCN_error("Port %s has both connection(s) "
"and mapping(s). Message can be sent on it only with explicit "
"addressing.", port_name);
else if (connection_list_head->list_next != NULL) TTCN_error("Port %s "
"has more than one active connections. Message can be sent on it "
"only with explicit addressing.", port_name);
return connection_list_head->remote_component;
} else {
if (n_system_mappings > 1) {
TTCN_error("Port %s has more than one mappings. Message cannot "
"be sent on it to system.", port_name);
} else if (n_system_mappings < 1) {
TTCN_error("Port %s has neither connections nor mappings. "
"Message cannot be sent on it.", port_name);
}
return SYSTEM_COMPREF;
}
}
void PORT::prepare_message(Text_Buf& outgoing_buf, const char *message_type)
{
outgoing_buf.push_int(CONN_DATA_MESSAGE);
outgoing_buf.push_string(message_type);
}
void PORT::prepare_call(Text_Buf& outgoing_buf, const char *signature_name)
{
outgoing_buf.push_int(CONN_DATA_CALL);
outgoing_buf.push_string(signature_name);
}
void PORT::prepare_reply(Text_Buf& outgoing_buf, const char *signature_name)
{
outgoing_buf.push_int(CONN_DATA_REPLY);
outgoing_buf.push_string(signature_name);
}
void PORT::prepare_exception(Text_Buf& outgoing_buf, const char *signature_name)
{
outgoing_buf.push_int(CONN_DATA_EXCEPTION);
outgoing_buf.push_string(signature_name);
}
void PORT::send_data(Text_Buf &outgoing_buf,
const COMPONENT& destination_component)
{
if (!destination_component.is_bound()) TTCN_error("Internal error: "
"The destination component reference is unbound when sending data on "
"port %s.", port_name);
component destination_compref = (component)destination_component;
boolean is_unique;
port_connection *conn_ptr =
lookup_connection_to_compref(destination_compref, &is_unique);
if (conn_ptr == NULL)
TTCN_error("Data cannot be sent on port %s to component %d "
"because there is no connection towards component %d.", port_name,
destination_compref, destination_compref);
else if (!is_unique)
TTCN_error("Data cannot be sent on port %s to component %d "
"because there are more than one connections towards component "
"%d.", port_name, destination_compref, destination_compref);
else if (conn_ptr->connection_state != CONN_CONNECTED)
TTCN_error("Data cannot be sent on port %s to component %d "
"because the connection is not in active state.",
port_name, destination_compref);
switch (conn_ptr->transport_type) {
case TRANSPORT_LOCAL:
send_data_local(conn_ptr, outgoing_buf);
break;
case TRANSPORT_INET_STREAM:
case TRANSPORT_UNIX_STREAM:
send_data_stream(conn_ptr, outgoing_buf, FALSE);
break;
default:
TTCN_error("Internal error: Invalid transport type (%d) in port "
"connection between %s and %d:%s.", conn_ptr->transport_type,
port_name, conn_ptr->remote_component, conn_ptr->remote_port);
}
}
void PORT::process_data(port_connection *conn_ptr, Text_Buf& incoming_buf)
{
connection_data_type_enum conn_data_type =
(connection_data_type_enum)incoming_buf.pull_int().get_val();
if (conn_data_type != CONN_DATA_LAST) {
switch (conn_ptr->connection_state) {
case CONN_CONNECTED:
case CONN_LAST_MSG_SENT:
break;
case CONN_LAST_MSG_RCVD:
case CONN_IDLE:
TTCN_warning("Data arrived after the indication of connection "
"termination on port %s from %d:%s. Data is ignored.",
port_name, conn_ptr->remote_component, conn_ptr->remote_port);
return;
default:
TTCN_error("Internal error: Connection of port %s with %d:%s has "
"invalid state (%d).", port_name, conn_ptr->remote_component,
conn_ptr->remote_port, conn_ptr->connection_state);
}
char *message_type = incoming_buf.pull_string();
try {
switch (conn_data_type) {
case CONN_DATA_MESSAGE:
if (!process_message(message_type, incoming_buf,
conn_ptr->remote_component, conn_ptr->sliding_buffer)) {
TTCN_error("Port %s does not support incoming message "
"type %s, which has arrived on the connection from "
"%d:%s.", port_name, message_type,
conn_ptr->remote_component, conn_ptr->remote_port);
}
break;
case CONN_DATA_CALL:
if (!process_call(message_type, incoming_buf,
conn_ptr->remote_component)) {
TTCN_error("Port %s does not support incoming call of "
"signature %s, which has arrived on the connection "
"from %d:%s.", port_name, message_type,
conn_ptr->remote_component, conn_ptr->remote_port);
}
break;
case CONN_DATA_REPLY:
if (!process_reply(message_type, incoming_buf,
conn_ptr->remote_component)) {
TTCN_error("Port %s does not support incoming reply of "
"signature %s, which has arrived on the connection "
"from %d:%s.", port_name, message_type,
conn_ptr->remote_component, conn_ptr->remote_port);
}
break;
case CONN_DATA_EXCEPTION:
if (!process_exception(message_type, incoming_buf,
conn_ptr->remote_component)) {
TTCN_error("Port %s does not support incoming exception "
"of signature %s, which has arrived on the connection "
"from %d:%s.", port_name, message_type,
conn_ptr->remote_component, conn_ptr->remote_port);
}
break;
default:
TTCN_error("Internal error: Data with invalid selector (%d) "
"was received on port %s from %d:%s.", conn_data_type,
port_name, conn_ptr->remote_component,
conn_ptr->remote_port);
}
} catch (...) {
// avoid memory leak
delete [] message_type;
throw;
}
delete [] message_type;
} else process_last_message(conn_ptr);
}
boolean PORT::process_message(const char *, Text_Buf&, component, OCTETSTRING&)
{
return FALSE;
}
boolean PORT::process_call(const char *, Text_Buf&, component)
{
return FALSE;
}
boolean PORT::process_reply(const char *, Text_Buf&, component)
{
return FALSE;
}
boolean PORT::process_exception(const char *, Text_Buf&, component)
{
return FALSE;
}
port_connection *PORT::add_connection(component remote_component,
const char *remote_port, transport_type_enum transport_type)
{
port_connection *conn_ptr;
for (conn_ptr = connection_list_head; conn_ptr != NULL;
conn_ptr = conn_ptr->list_next) {
if (conn_ptr->remote_component == remote_component) {
int ret_val = strcmp(conn_ptr->remote_port, remote_port);
if (ret_val == 0) return conn_ptr;
else if (ret_val > 0) break;
} else if (conn_ptr->remote_component > remote_component) break;
}
if (n_system_mappings > 0) {
TTCN_error("Connect operation cannot be performed on a mapped port (%s).", port_name);
}
port_connection *new_conn = new port_connection;
new_conn->owner_port = this;
new_conn->connection_state = CONN_IDLE;
new_conn->remote_component = remote_component;
new_conn->remote_port = mcopystr(remote_port);
new_conn->transport_type = transport_type;
new_conn->sliding_buffer = OCTETSTRING(0, 0);
switch (transport_type) {
case TRANSPORT_LOCAL:
new_conn->local.port_ptr = NULL;
break;
case TRANSPORT_INET_STREAM:
case TRANSPORT_UNIX_STREAM:
new_conn->stream.comm_fd = -1;
new_conn->stream.incoming_buf = NULL;
break;
default:
delete new_conn;
TTCN_error("Internal error: PORT::add_connection(): invalid transport "
"type (%d).", transport_type);
}
new_conn->list_next = conn_ptr;
if (conn_ptr != NULL) {
// new_conn will be inserted before conn_ptr in the ordered list
new_conn->list_prev = conn_ptr->list_prev;
conn_ptr->list_prev = new_conn;
if (new_conn->list_prev != NULL)
new_conn->list_prev->list_next = new_conn;
} else {
// new_conn will be inserted to the end of the list
new_conn->list_prev = connection_list_tail;
if (connection_list_tail != NULL)
connection_list_tail->list_next = new_conn;
connection_list_tail = new_conn;
}
if (conn_ptr == connection_list_head) connection_list_head = new_conn;
return new_conn;
}
void PORT::remove_connection(port_connection *conn_ptr)
{
Free(conn_ptr->remote_port);
switch (conn_ptr->transport_type) {
case TRANSPORT_LOCAL:
break;
case TRANSPORT_INET_STREAM:
case TRANSPORT_UNIX_STREAM:
if (conn_ptr->stream.comm_fd >= 0) {
Fd_And_Timeout_User::remove_fd(conn_ptr->stream.comm_fd, conn_ptr,
FD_EVENT_RD);
if (conn_ptr->connection_state == CONN_LISTENING &&
conn_ptr->transport_type == TRANSPORT_UNIX_STREAM)
unlink_unix_pathname(conn_ptr->stream.comm_fd);
close(conn_ptr->stream.comm_fd);
conn_ptr->stream.comm_fd = -1;
}
delete conn_ptr->stream.incoming_buf;
break;
default:
TTCN_error("Internal error: PORT::remove_connection(): invalid "
"transport type.");
}
if (conn_ptr->list_prev != NULL)
conn_ptr->list_prev->list_next = conn_ptr->list_next;
else if (connection_list_head == conn_ptr)
connection_list_head = conn_ptr->list_next;
if (conn_ptr->list_next != NULL)
conn_ptr->list_next->list_prev = conn_ptr->list_prev;
else if (connection_list_tail == conn_ptr)
connection_list_tail = conn_ptr->list_prev;
delete conn_ptr;
}
port_connection *PORT::lookup_connection_to_compref(
component remote_component, boolean *is_unique)
{
for (port_connection *conn = connection_list_head; conn != NULL;
conn = conn->list_next) {
if (conn->remote_component == remote_component) {
if (is_unique != NULL) {
port_connection *nxt = conn->list_next;
if (nxt != NULL && nxt->remote_component == remote_component)
*is_unique = FALSE;
else *is_unique = TRUE;
}
return conn;
} else if (conn->remote_component > remote_component) break;
}
return NULL;
}
port_connection *PORT::lookup_connection(component remote_component,
const char *remote_port)
{
for (port_connection *conn = connection_list_head; conn != NULL;
conn = conn->list_next) {
if (conn->remote_component == remote_component) {
int ret_val = strcmp(conn->remote_port, remote_port);
if (ret_val == 0) return conn;
else if (ret_val > 0) break;
} else if (conn->remote_component > remote_component) break;
}
return NULL;
}
void PORT::add_local_connection(PORT *other_endpoint)
{
port_connection *conn_ptr = add_connection(self, other_endpoint->port_name,
TRANSPORT_LOCAL);
conn_ptr->connection_state = CONN_CONNECTED;
conn_ptr->local.port_ptr = other_endpoint;
TTCN_Logger::log_port_misc(TitanLoggerApi::Port__Misc_reason::local__connection__established,
port_name, NULL_COMPREF, other_endpoint->port_name);
}
void PORT::remove_local_connection(port_connection *conn_ptr)
{
if (conn_ptr->transport_type != TRANSPORT_LOCAL)
TTCN_error("Internal error: The transport type used by the connection "
"between port %s and %d:%s is not LOCAL.", port_name,
conn_ptr->remote_component, conn_ptr->remote_port);
PORT *other_endpoint = conn_ptr->local.port_ptr;
remove_connection(conn_ptr);
TTCN_Logger::log_port_misc(TitanLoggerApi::Port__Misc_reason::local__connection__terminated,
port_name, NULL_COMPREF, other_endpoint->port_name);
}
unsigned int PORT::get_connection_hash(component local_component,
const char *local_port, component remote_component, const char *remote_port)
{
const size_t N = sizeof(unsigned int);
unsigned char hash_buffer[N];
// fill the buffer with an initial pattern
for (size_t i = 0; i < N; i++) hash_buffer[i] = i % 2 ? 0x55 : 0xAA;
// add the PID of the current process to the buffer
pid_t pid = getpid();
for (size_t i = 0; i < sizeof(pid); i++)
hash_buffer[i % N] ^= (pid >> (8 * i)) & 0xFF;
// add the local and remote component reference and port name to the buffer
for (size_t i = 0; i < sizeof(local_component); i++)
hash_buffer[(N - 1) - i % N] ^= (local_component >> (8 * i)) & 0xFF;
for (size_t i = 0; local_port[i] != '\0'; i++)
hash_buffer[(N - 1) - i % N] ^= local_port[i];
for (size_t i = 0; i < sizeof(remote_component); i++)
hash_buffer[i % N] ^= (remote_component >> (8 * i)) & 0xFF;
for (size_t i = 0; remote_port[i] != '\0'; i++)
hash_buffer[i % N] ^= remote_port[i];
// convert the buffer to an integer value
unsigned int ret_val = 0;
for (size_t i = 0; i < N; i++)
ret_val = (ret_val << 8) | hash_buffer[i];
return ret_val;
}
void PORT::unlink_unix_pathname(int socket_fd)
{
struct sockaddr_un local_addr;
// querying the local pathname used by socket_fd
socklen_type addr_len = sizeof(local_addr);
if (getsockname(socket_fd, (struct sockaddr*)&local_addr, &addr_len)) {
TTCN_warning_begin("System call getsockname() failed on UNIX socket "
"file descriptor %d.", socket_fd);
TTCN_Logger::OS_error();
TTCN_Logger::log_event_str(" The associated socket file will not be "
"removed from the file system.");
TTCN_warning_end();
} else if (local_addr.sun_family != AF_UNIX) {
TTCN_warning("System call getsockname() returned invalid address "
"family for UNIX socket file descriptor %d. The associated socket "
"file will not be removed from the file system.", socket_fd);
} else if (unlink(local_addr.sun_path)) {
if (errno != ENOENT) {
TTCN_warning_begin("System call unlink() failed when trying to "
"remove UNIX socket file %s.", local_addr.sun_path);
TTCN_Logger::OS_error();
TTCN_Logger::log_event_str(" The file will remain in the file "
"system.");
TTCN_warning_end();
} else errno = 0;
}
}
void PORT::connect_listen_inet_stream(component remote_component,
const char *remote_port)
{
// creating the TCP server socket
int server_fd = NetworkHandler::socket(TTCN_Communication::get_network_family());
if (server_fd < 0) {
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Creation of the TCP server socket failed. (%s)",
strerror(errno));
errno = 0;
return;
}
// binding the socket to an ephemeral TCP port
// using the same local IP address as the control connection to MC
IPAddress *local_addr = IPAddress::create_addr(TTCN_Communication::get_network_family());
*local_addr = *TTCN_Communication::get_local_address();
local_addr->set_port(0);
if (bind(server_fd, local_addr->get_addr(), local_addr->get_addr_len())) {
close(server_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Binding of server socket to an ephemeral TCP port "
"failed. (%s)", strerror(errno));
errno = 0;
delete local_addr;
return;
}
// zero backlog is enough since we are waiting for only one client
if (listen(server_fd, 0)) {
close(server_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Listening on an ephemeral TCP port failed. (%s)",
strerror(errno));
errno = 0;
delete local_addr;
return;
}
// querying the IP address and port number used by the TCP server
if (local_addr->getsockname(server_fd)) {
close(server_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "System call getsockname() failed on the TCP server "
"socket. (%s)", strerror(errno));
errno = 0;
delete local_addr;
return;
}
if (!TTCN_Communication::set_close_on_exec(server_fd)) {
close(server_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Setting the close-on-exec flag failed on the TCP "
"server socket.");
delete local_addr;
return;
}
port_connection *new_connection = add_connection(remote_component,
remote_port, TRANSPORT_INET_STREAM);
new_connection->connection_state = CONN_LISTENING;
new_connection->stream.comm_fd = server_fd;
Fd_And_Timeout_User::add_fd(server_fd, new_connection, FD_EVENT_RD);
TTCN_Communication::send_connect_listen_ack_inet_stream(port_name,
remote_component, remote_port, local_addr);
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::port__is__waiting__for__connection__tcp,
port_name, remote_component, remote_port);
delete local_addr;
}
void PORT::connect_listen_unix_stream(component remote_component,
const char *remote_port)
{
// creating the UNIX server socket
int server_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (server_fd < 0) {
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Creation of the UNIX server socket failed. (%s)",
strerror(errno));
errno = 0;
return;
}
// binding the socket to a temporary file
struct sockaddr_un local_addr;
// the file name is constructed using a hash function
unsigned int hash_value =
get_connection_hash(self, port_name, remote_component, remote_port);
for (unsigned int i = 1; ; i++) {
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sun_family = AF_UNIX;
snprintf(local_addr.sun_path, sizeof(local_addr.sun_path),
"/tmp/ttcn3-portconn-%x", hash_value);
if (bind(server_fd, (struct sockaddr*)&local_addr, sizeof(local_addr))
== 0) {
// the operation was successful, jump out of the loop
break;
} else if (errno == EADDRINUSE) {
// the temporary file name is already used by someone else
errno = 0;
if (i < UNIX_BIND_MAX_ITER) {
// try another hash value
hash_value++;
} else {
close(server_fd);
TTCN_Communication::send_connect_error(port_name,
remote_component, remote_port, "Could not find a free "
"pathname to bind the UNIX server socket to after %u "
"iterations.", i);
errno = 0;
return;
}
} else {
close(server_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Binding of UNIX server socket to pathname %s "
"failed. (%s)", local_addr.sun_path, strerror(errno));
errno = 0;
return;
}
}
// zero backlog is enough since we are waiting for only one client
if (listen(server_fd, 0)) {
close(server_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Listening on UNIX pathname %s failed. (%s)",
local_addr.sun_path, strerror(errno));
errno = 0;
return;
}
if (!TTCN_Communication::set_close_on_exec(server_fd)) {
close(server_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Setting the close-on-exec flag failed on the UNIX "
"server socket.");
return;
}
port_connection *new_connection = add_connection(remote_component,
remote_port, TRANSPORT_UNIX_STREAM);
new_connection->connection_state = CONN_LISTENING;
new_connection->stream.comm_fd = server_fd;
Fd_And_Timeout_User::add_fd(server_fd, new_connection, FD_EVENT_RD);
TTCN_Communication::send_connect_listen_ack_unix_stream(port_name,
remote_component, remote_port, &local_addr);
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::port__is__waiting__for__connection__unix,
port_name, remote_component, remote_port, local_addr.sun_path);
}
void PORT::connect_local(component remote_component, const char *remote_port)
{
if (self != remote_component) {
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Message CONNECT with transport type LOCAL refers "
"to a port of another component (%d).", remote_component);
return;
}
PORT *remote_ptr = lookup_by_name(remote_port);
if (remote_ptr == NULL) {
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Port %s does not exist.", remote_port);
return;
} else if (!remote_ptr->is_active) TTCN_error("Internal error: Port %s is "
"inactive when trying to connect it to local port %s.", remote_port,
port_name);
add_local_connection(remote_ptr);
if (this != remote_ptr) remote_ptr->add_local_connection(this);
TTCN_Communication::send_connected(port_name, remote_component,
remote_port);
}
void PORT::connect_stream(component remote_component, const char *remote_port,
transport_type_enum transport_type, Text_Buf& text_buf)
{
#ifdef WIN32
again:
#endif
const char *transport_str;
int client_fd;
switch (transport_type) {
case TRANSPORT_INET_STREAM:
transport_str = "TCP";
// creating the TCP client socket
client_fd = NetworkHandler::socket(TTCN_Communication::get_network_family());
break;
case TRANSPORT_UNIX_STREAM:
transport_str = "UNIX";
// creating the UNIX client socket
client_fd = socket(PF_UNIX, SOCK_STREAM, 0);
break;
default:
TTCN_error("Internal error: PORT::connect_stream(): invalid transport "
"type (%d).", transport_type);
}
if (client_fd < 0) {
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Creation of the %s client socket failed. (%s)",
transport_str, strerror(errno));
errno = 0;
return;
}
switch (transport_type) {
case TRANSPORT_INET_STREAM: {
// connect to the IP address and port number given in message CONNECT
IPAddress *remote_addr = IPAddress::create_addr(TTCN_Communication::get_network_family());
remote_addr->pull_raw(text_buf);
#ifdef SOLARIS
if (connect(client_fd, (struct sockaddr*)remote_addr->get_addr(), remote_addr->get_addr_len())) {
#else
if (connect(client_fd, remote_addr->get_addr(), remote_addr->get_addr_len())) {
#endif
#ifdef WIN32
if (errno == EADDRINUSE) {
close(client_fd);
errno = 0;
TTCN_warning("connect() returned error code EADDRINUSE. "
"Perhaps this is a Cygwin bug. Trying to connect again.");
goto again;
}
#endif
close(client_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "TCP connection establishment failed to %s:%d. (%s)",
remote_addr->get_addr_str(), remote_addr->get_port(), strerror(errno));
errno = 0;
delete remote_addr;
return;
}
delete remote_addr;
break; }
case TRANSPORT_UNIX_STREAM: {
// connect to the UNIX pathname given in the message CONNECT
struct sockaddr_un remote_addr;
memset(&remote_addr, 0, sizeof(remote_addr));
remote_addr.sun_family = AF_UNIX;
size_t pathname_len = text_buf.pull_int().get_val();
if (pathname_len >= sizeof(remote_addr.sun_path)) {
close(client_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "The UNIX pathname used by the server socket is "
"too long. It consists of %lu bytes although it should be "
"shorter than %lu bytes to fit in the appropriate structure.",
(unsigned long) pathname_len,
(unsigned long) sizeof(remote_addr.sun_path));
return;
}
text_buf.pull_raw(pathname_len, remote_addr.sun_path);
if (connect(client_fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr))) {
close(client_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "UNIX socket connection establishment failed to "
"pathname %s. (%s)", remote_addr.sun_path, strerror(errno));
errno = 0;
return;
}
break; }
default:
TTCN_error("Internal error: PORT::connect_stream(): invalid transport "
"type (%d).", transport_type);
}
if (!TTCN_Communication::set_close_on_exec(client_fd)) {
close(client_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Setting the close-on-exec flag failed on the %s "
"client socket.", transport_str);
return;
}
if (!TTCN_Communication::set_non_blocking_mode(client_fd, TRUE)) {
close(client_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Setting the non-blocking mode failed on the %s "
"client socket.", transport_str);
return;
}
if (transport_type == TRANSPORT_INET_STREAM &&
!TTCN_Communication::set_tcp_nodelay(client_fd)) {
close(client_fd);
TTCN_Communication::send_connect_error(port_name, remote_component,
remote_port, "Setting the TCP_NODELAY flag failed on the TCP "
"client socket.");
return;
}
port_connection *new_connection = add_connection(remote_component,
remote_port, transport_type);
new_connection->connection_state = CONN_CONNECTED;
new_connection->stream.comm_fd = client_fd;
Fd_And_Timeout_User::add_fd(client_fd, new_connection, FD_EVENT_RD);
TTCN_Logger::log_port_misc(TitanLoggerApi::Port__Misc_reason::connection__established,
port_name, remote_component, remote_port, transport_str);
}
void PORT::disconnect_local(port_connection *conn_ptr)
{
PORT *remote_ptr = conn_ptr->local.port_ptr;
remove_local_connection(conn_ptr);
if (this != remote_ptr) {
port_connection *conn2_ptr =
remote_ptr->lookup_connection(self, port_name);
if (conn2_ptr == NULL) TTCN_error("Internal error: Port %s is "
"connected with local port %s, but port %s does not have a "
"connection to %s.", port_name, remote_ptr->port_name,
remote_ptr->port_name, port_name);
else remote_ptr->remove_local_connection(conn2_ptr);
}
TTCN_Communication::send_disconnected(port_name, self,
remote_ptr->port_name);
}
void PORT::disconnect_stream(port_connection *conn_ptr)
{
switch (conn_ptr->connection_state) {
case CONN_LISTENING:
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::destroying__unestablished__connection,
port_name, conn_ptr->remote_component, conn_ptr->remote_port);
remove_connection(conn_ptr);
// do not send back any acknowledgment
break;
case CONN_CONNECTED: {
TTCN_Logger::log_port_misc(TitanLoggerApi::Port__Misc_reason::terminating__connection,
port_name, conn_ptr->remote_component, conn_ptr->remote_port);
Text_Buf outgoing_buf;
outgoing_buf.push_int(CONN_DATA_LAST);
if (send_data_stream(conn_ptr, outgoing_buf, TRUE)) {
// sending the last message was successful
// wait for the acknowledgment from the peer
conn_ptr->connection_state = CONN_LAST_MSG_SENT;
} else {
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::sending__termination__request__failed,
port_name, conn_ptr->remote_component, conn_ptr->remote_port);
// send an acknowledgment to MC immediately to avoid deadlock
// in case of communication failure
// (i.e. when the peer does not send DISCONNECTED)
TTCN_Communication::send_disconnected(port_name,
conn_ptr->remote_component, conn_ptr->remote_port);
TTCN_warning("The last outgoing messages on port %s may be lost.",
port_name);
// destroy the connection immediately
remove_connection(conn_ptr);
}
break; }
default:
TTCN_error("The connection of port %s to %d:%s is in unexpected "
"state when trying to terminate it.", port_name,
conn_ptr->remote_component, conn_ptr->remote_port);
}
}
void PORT::send_data_local(port_connection *conn_ptr, Text_Buf& outgoing_data)
{
outgoing_data.rewind();
PORT *dest_ptr = conn_ptr->local.port_ptr;
if (this != dest_ptr) {
port_connection *conn2_ptr =
dest_ptr->lookup_connection(self, port_name);
if (conn2_ptr == NULL) TTCN_error("Internal error: Port %s is "
"connected with local port %s, but port %s does not have a "
"connection to %s.", port_name, dest_ptr->port_name,
dest_ptr->port_name, port_name);
dest_ptr->process_data(conn2_ptr, outgoing_data);
} else process_data(conn_ptr, outgoing_data);
}
boolean PORT::send_data_stream(port_connection *conn_ptr,
Text_Buf& outgoing_data, boolean ignore_peer_disconnect)
{
boolean would_block_warning=FALSE;
outgoing_data.calculate_length();
const char *msg_ptr = outgoing_data.get_data();
size_t msg_len = outgoing_data.get_len(), sent_len = 0;
while (sent_len < msg_len) {
int ret_val = send(conn_ptr->stream.comm_fd, msg_ptr + sent_len,
msg_len - sent_len, 0);
if (ret_val > 0) sent_len += ret_val;
else {
switch (errno) {
case EINTR:
// a signal occurred: do nothing, just try again
errno = 0;
break;
case EAGAIN: {
// the output buffer is full: try to increase it if possible
errno = 0;
int old_bufsize, new_bufsize;
if (TTCN_Communication::increase_send_buffer(
conn_ptr->stream.comm_fd, old_bufsize, new_bufsize)) {
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::sending__would__block,
port_name, conn_ptr->remote_component, conn_ptr->remote_port,
NULL, old_bufsize, new_bufsize);
} else {
if(!would_block_warning){
TTCN_warning_begin("Sending data on the connection of "
"port %s to ", port_name);
COMPONENT::log_component_reference(
conn_ptr->remote_component);
TTCN_Logger::log_event(":%s would block execution and it "
"is not possible to further increase the size of the "
"outgoing buffer. Trying to process incoming data to "
"avoid deadlock.", conn_ptr->remote_port);
TTCN_warning_end();
would_block_warning=TRUE;
}
TTCN_Snapshot::block_for_sending(conn_ptr->stream.comm_fd);
}
break; }
case ECONNRESET:
case EPIPE:
if (ignore_peer_disconnect) return FALSE;
// no break
default:
TTCN_error("Sending data on the connection of port %s to "
"%d:%s failed.", port_name, conn_ptr->remote_component,
conn_ptr->remote_port);
}
}
}
if(would_block_warning){
TTCN_warning_begin("The message finally was sent on "
"port %s to ", port_name);
COMPONENT::log_component_reference(
conn_ptr->remote_component);
TTCN_Logger::log_event(":%s.", conn_ptr->remote_port);
TTCN_warning_end();
}
return TRUE;
}
void PORT::handle_incoming_connection(port_connection *conn_ptr)
{
const char *transport_str =
conn_ptr->transport_type == TRANSPORT_INET_STREAM ? "TCP" : "UNIX";
int comm_fd = accept(conn_ptr->stream.comm_fd, NULL, NULL);
if (comm_fd < 0) {
TTCN_Communication::send_connect_error(port_name,
conn_ptr->remote_component, conn_ptr->remote_port,
"Accepting of incoming %s connection failed. (%s)", transport_str,
strerror(errno));
errno = 0;
remove_connection(conn_ptr);
return;
}
if (!TTCN_Communication::set_close_on_exec(comm_fd)) {
close(comm_fd);
TTCN_Communication::send_connect_error(port_name,
conn_ptr->remote_component, conn_ptr->remote_port,
"Setting the close-on-exec flag failed on the server-side %s "
"socket.", transport_str);
remove_connection(conn_ptr);
return;
}
if (!TTCN_Communication::set_non_blocking_mode(comm_fd, TRUE)) {
close(comm_fd);
TTCN_Communication::send_connect_error(port_name,
conn_ptr->remote_component, conn_ptr->remote_port,
"Setting the non-blocking mode failed on the server-side %s "
"socket.", transport_str);
remove_connection(conn_ptr);
return;
}
if (conn_ptr->transport_type == TRANSPORT_INET_STREAM &&
!TTCN_Communication::set_tcp_nodelay(comm_fd)) {
close(comm_fd);
TTCN_Communication::send_connect_error(port_name,
conn_ptr->remote_component, conn_ptr->remote_port,
"Setting the TCP_NODELAY flag failed on the server-side TCP "
"socket.");
remove_connection(conn_ptr);
return;
}
// shutting down the server socket and replacing it with the
// communication socket of the new connection
Fd_And_Timeout_User::remove_fd(conn_ptr->stream.comm_fd, conn_ptr,
FD_EVENT_RD);
if (conn_ptr->transport_type == TRANSPORT_UNIX_STREAM)
unlink_unix_pathname(conn_ptr->stream.comm_fd);
close(conn_ptr->stream.comm_fd);
conn_ptr->connection_state = CONN_CONNECTED;
conn_ptr->stream.comm_fd = comm_fd;
Fd_And_Timeout_User::add_fd(comm_fd, conn_ptr, FD_EVENT_RD);
TTCN_Communication::send_connected(port_name, conn_ptr->remote_component,
conn_ptr->remote_port);
TTCN_Logger::log_port_misc(TitanLoggerApi::Port__Misc_reason::connection__accepted,
port_name, conn_ptr->remote_component, conn_ptr->remote_port);
}
void PORT::handle_incoming_data(port_connection *conn_ptr)
{
if (conn_ptr->stream.incoming_buf == NULL)
conn_ptr->stream.incoming_buf = new Text_Buf;
Text_Buf& incoming_buf = *conn_ptr->stream.incoming_buf;
char *buf_ptr;
int buf_len;
incoming_buf.get_end(buf_ptr, buf_len);
int recv_len = recv(conn_ptr->stream.comm_fd, buf_ptr, buf_len, 0);
if (recv_len < 0) {
// an error occurred
if (errno == ECONNRESET) {
// TCP connection was reset by peer
errno = 0;
TTCN_Communication::send_disconnected(port_name,
conn_ptr->remote_component, conn_ptr->remote_port);
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::connection__reset__by__peer,
port_name, conn_ptr->remote_component, conn_ptr->remote_port);
TTCN_warning("The last outgoing messages on port %s may be lost.",
port_name);
conn_ptr->connection_state = CONN_IDLE;
} else {
TTCN_error("Receiving data on the connection of port %s from "
"%d:%s failed.", port_name, conn_ptr->remote_component,
conn_ptr->remote_port);
}
} else if (recv_len > 0) {
// data was received
incoming_buf.increase_length(recv_len);
// processing all messages in the buffer after each other
while (incoming_buf.is_message()) {
incoming_buf.pull_int(); // message_length
process_data(conn_ptr, incoming_buf);
incoming_buf.cut_message();
}
} else {
// the connection was closed by the peer
TTCN_Communication::send_disconnected(port_name,
conn_ptr->remote_component, conn_ptr->remote_port);
if (conn_ptr->connection_state != CONN_LAST_MSG_RCVD) {
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::connection__closed__by__peer,
port_name, conn_ptr->remote_component, conn_ptr->remote_port);
}
// the connection can be removed
conn_ptr->connection_state = CONN_IDLE;
}
if (conn_ptr->connection_state == CONN_IDLE) {
// terminating and removing connection
int msg_len = incoming_buf.get_len();
if (msg_len > 0) {
TTCN_warning_begin("Message fragment remained in the buffer of "
"port connection between %s and ", port_name);
COMPONENT::log_component_reference(conn_ptr->remote_component);
TTCN_Logger::log_event(":%s: ", conn_ptr->remote_port);
const unsigned char *msg_ptr =
(const unsigned char*)incoming_buf.get_data();
for (int i = 0; i < msg_len; i++)
TTCN_Logger::log_octet(msg_ptr[i]);
TTCN_warning_end();
}
TTCN_Logger::log_port_misc(TitanLoggerApi::Port__Misc_reason::port__disconnected,
port_name, conn_ptr->remote_component, conn_ptr->remote_port);
remove_connection(conn_ptr);
}
}
void PORT::process_last_message(port_connection *conn_ptr)
{
switch (conn_ptr->transport_type) {
case TRANSPORT_INET_STREAM:
case TRANSPORT_UNIX_STREAM:
break;
default:
TTCN_error("Internal error: Connection termination request was "
"received on the connection of port %s with %d:%s, which has an "
"invalid transport type (%d).", port_name,
conn_ptr->remote_component, conn_ptr->remote_port,
conn_ptr->transport_type);
}
switch (conn_ptr->connection_state) {
case CONN_CONNECTED: {
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::termination__request__received,
port_name, conn_ptr->remote_component, conn_ptr->remote_port);
Text_Buf outgoing_buf;
outgoing_buf.push_int(CONN_DATA_LAST);
if (send_data_stream(conn_ptr, outgoing_buf, TRUE)) {
// sending the last message was successful
// wait until the peer closes the transport connection
conn_ptr->connection_state = CONN_LAST_MSG_RCVD;
} else {
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::acknowledging__termination__request__failed,
port_name, conn_ptr->remote_component, conn_ptr->remote_port);
// send an acknowledgment to MC immediately to avoid deadlock
// in case of communication failure
// (i.e. when the peer does not send DISCONNECTED)
TTCN_Communication::send_disconnected(port_name,
conn_ptr->remote_component, conn_ptr->remote_port);
// the connection can be removed immediately
TTCN_warning("The last outgoing messages on port %s may be lost.",
port_name);
conn_ptr->connection_state = CONN_IDLE;
}
break; }
case CONN_LAST_MSG_SENT:
// the connection can be removed
conn_ptr->connection_state = CONN_IDLE;
break;
case CONN_LAST_MSG_RCVD:
case CONN_IDLE:
TTCN_warning("Unexpected data arrived after the indication of "
"connection termination on port %s from %d:%s.", port_name,
conn_ptr->remote_component, conn_ptr->remote_port);
break;
default:
TTCN_error("Internal error: Connection of port %s with %d:%s has "
"invalid state (%d).", port_name, conn_ptr->remote_component,
conn_ptr->remote_port, conn_ptr->connection_state);
}
}
void PORT::map(const char *system_port, Map_Params& params, boolean translation)
{
if (!is_active) TTCN_error("Inactive port %s cannot be mapped.", port_name);
int new_posn;
for (new_posn = 0; new_posn < n_system_mappings; new_posn++) {
int str_diff = strcmp(system_port, system_mappings[new_posn]);
if (str_diff < 0) break;
else if (str_diff == 0) {
if (translation == FALSE) {
TTCN_warning("Port %s is already mapped to system:%s."
" Map operation was ignored.", port_name, system_port);
} else {
TTCN_warning("System:%s is already mapped to port %s."
" Map operation was ignored.", system_port, port_name);
}
return;
}
}
if (translation == FALSE) {
set_system_parameters(system_port);
} else {
set_system_parameters(port_name);
}
if (params.get_nof_params() == 0) {
// call the legacy function if there are no parameters (for backward compatibility)
user_map(system_port);
}
else {
user_map(system_port, params);
}
if (translation == FALSE) {
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::port__was__mapped__to__system,
port_name, SYSTEM_COMPREF, system_port);
} else {
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::port__was__mapped__to__system,
system_port, SYSTEM_COMPREF, port_name);
}
// the mapping shall be registered in the table only if user_map() was
// successful
system_mappings = (char**)Realloc(system_mappings,
(n_system_mappings + 1) * sizeof(*system_mappings));
memmove(system_mappings + new_posn + 1, system_mappings + new_posn,
(n_system_mappings - new_posn) * sizeof(*system_mappings));
system_mappings[new_posn] = mcopystr(system_port);
n_system_mappings++;
if (n_system_mappings > 1) TTCN_warning("Port %s has now more than one "
"mappings. Message cannot be sent on it to system even with explicit "
"addressing.", port_name);
}
void PORT::unmap(const char *system_port, Map_Params& params, boolean translation)
{
int del_posn;
for (del_posn = 0; del_posn < n_system_mappings; del_posn++) {
int str_diff = strcmp(system_port, system_mappings[del_posn]);
if (str_diff == 0) break;
else if (str_diff < 0) {
del_posn = n_system_mappings;
break;
}
}
if (del_posn >= n_system_mappings) {
if (translation == FALSE) {
TTCN_warning("Port %s is not mapped to system:%s. "
"Unmap operation was ignored.", port_name, system_port);
} else {
TTCN_warning("System:%s is not mapped to port %s. "
"Unmap operation was ignored.", system_port, port_name);
}
return;
}
char *unmapped_port = system_mappings[del_posn];
// first remove the mapping from the table
n_system_mappings--;
memmove(system_mappings + del_posn, system_mappings + del_posn + 1,
(n_system_mappings - del_posn) * sizeof(*system_mappings));
system_mappings = (char**)Realloc(system_mappings,
n_system_mappings * sizeof(*system_mappings));
try {
if (params.get_nof_params() == 0) {
// call the legacy function if there are no parameters (for backward compatibility)
user_unmap(system_port);
}
else {
user_unmap(system_port, params);
}
} catch (...) {
// prevent from memory leak
Free(unmapped_port);
throw;
}
// Only valid for provider ports and standard like translation (user) ports.
// Currently the requirement is that the port needs to map only when mapped
// to one port. If it would be mapped to more ports then this call would
// remove all translation capability.
// This does not resets the 'port' variables in the port type definition, but
// the internal c++ port variables of the port type.
if (n_system_mappings == 0) {
reset_port_variables();
}
TTCN_Logger::log_port_misc(
TitanLoggerApi::Port__Misc_reason::port__was__unmapped__from__system,
port_name, SYSTEM_COMPREF, system_port);
Free(unmapped_port);
}
void PORT::reset_port_variables() {
}
void PORT::init_port_variables() {
}
void PORT::change_port_state(translation_port_state /*state*/) {
}
void PORT::process_connect_listen(const char *local_port,
component remote_component, const char *remote_port,
transport_type_enum transport_type)
{
PORT *port_ptr = lookup_by_name(local_port);
if (port_ptr == NULL) {
TTCN_Communication::send_connect_error(local_port, remote_component,
remote_port, "Port %s does not exist.", local_port);
return;
} else if (!port_ptr->is_active) {
TTCN_error("Internal error: Port %s is inactive when trying to "
"connect it to %d:%s.", local_port, remote_component, remote_port);
} else if (port_ptr->lookup_connection(remote_component, remote_port)
!= NULL) {
TTCN_Communication::send_connect_error(local_port, remote_component,
remote_port, "Port %s already has a connection towards %d:%s.",
local_port, remote_component, remote_port);
return;
} else if (port_ptr->lookup_connection_to_compref(remote_component, NULL)
!= NULL) {
TTCN_warning_begin("Port %s will have more than one connections with "
"ports of test component ", local_port);
COMPONENT::log_component_reference(remote_component);
TTCN_Logger::log_event_str(". These connections cannot be used for "
"sending even with explicit addressing.");
TTCN_warning_end();
}
switch (transport_type) {
case TRANSPORT_LOCAL:
TTCN_Communication::send_connect_error(local_port, remote_component,
remote_port, "Message CONNECT_LISTEN cannot refer to transport "
"type LOCAL.");
break;
case TRANSPORT_INET_STREAM:
port_ptr->connect_listen_inet_stream(remote_component, remote_port);
break;
case TRANSPORT_UNIX_STREAM:
port_ptr->connect_listen_unix_stream(remote_component, remote_port);
break;
default:
TTCN_Communication::send_connect_error(local_port, remote_component,
remote_port, "Message CONNECT_LISTEN refers to invalid transport "
"type (%d).", transport_type);
break;
}
}
void PORT::process_connect(const char *local_port,
component remote_component, const char *remote_port,
transport_type_enum transport_type, Text_Buf& text_buf)
{
PORT *port_ptr = lookup_by_name(local_port);
if (port_ptr == NULL) {
TTCN_Communication::send_connect_error(local_port, remote_component,
remote_port, "Port %s does not exist.", local_port);
return;
} else if (!port_ptr->is_active) {
TTCN_error("Internal error: Port %s is inactive when trying to "
"connect it to %d:%s.", local_port, remote_component, remote_port);
} else if (port_ptr->lookup_connection(remote_component, remote_port)
!= NULL) {
TTCN_Communication::send_connect_error(local_port, remote_component,
remote_port, "Port %s already has a connection towards %d:%s.",
local_port, remote_component, remote_port);
return;
} else if (port_ptr->lookup_connection_to_compref(remote_component, NULL)
!= NULL) {
TTCN_warning_begin("Port %s will have more than one connections with "
"ports of test component ", local_port);
COMPONENT::log_component_reference(remote_component);
TTCN_Logger::log_event_str(". These connections cannot be used for "
"sending even with explicit addressing.");
TTCN_warning_end();
}
switch (transport_type) {
case TRANSPORT_LOCAL:
port_ptr->connect_local(remote_component, remote_port);
break;
case TRANSPORT_INET_STREAM:
case TRANSPORT_UNIX_STREAM:
port_ptr->connect_stream(remote_component, remote_port, transport_type,
text_buf);
break;
default:
TTCN_Communication::send_connect_error(local_port, remote_component,
remote_port, "Message CONNECT refers to invalid transport type "
"(%d).", transport_type);
break;
}
}
void PORT::process_disconnect(const char *local_port,
component remote_component, const char *remote_port)
{
PORT *port_ptr = lookup_by_name(local_port);
if (port_ptr == NULL) {
TTCN_Communication::send_error("Message DISCONNECT refers to "
"non-existent local port %s.", local_port);
return;
} else if (!port_ptr->is_active) {
TTCN_error("Internal error: Port %s is inactive when trying to "
"disconnect it from %d:%s.", local_port, remote_component,
remote_port);
}
port_connection *conn_ptr = port_ptr->lookup_connection(remote_component,
remote_port);
if (conn_ptr == NULL) {
// the connection does not exist
if (self == remote_component && lookup_by_name(remote_port) == NULL) {
// the remote endpoint is in the same component,
// but it does not exist
TTCN_Communication::send_error("Message DISCONNECT refers to "
"non-existent port %s.", remote_port);
} else {
TTCN_Communication::send_disconnected(local_port, remote_component,
remote_port);
}
return;
}
switch (conn_ptr->transport_type) {
case TRANSPORT_LOCAL:
port_ptr->disconnect_local(conn_ptr);
break;
case TRANSPORT_INET_STREAM:
case TRANSPORT_UNIX_STREAM:
port_ptr->disconnect_stream(conn_ptr);
break;
default:
TTCN_error("Internal error: The connection of port %s to %d:%s has "
"invalid transport type (%d) when trying to terminate the "
"connection.", local_port, remote_component, remote_port,
conn_ptr->transport_type);
}
}
void PORT::make_local_connection(const char *src_port, const char *dest_port)
{
PORT *src_ptr = lookup_by_name(src_port);
if (src_ptr == NULL) TTCN_error("Connect operation refers to "
"non-existent port %s.", src_port);
else if (!src_ptr->is_active) TTCN_error("Internal error: Port %s is "
"inactive when trying to connect it with local port %s.", src_port,
dest_port);
else if (src_ptr->lookup_connection(MTC_COMPREF, dest_port) != NULL) {
TTCN_warning("Port %s is already connected with local port %s. "
"Connect operation had no effect.", src_port, dest_port);
return;
} else if (src_ptr->lookup_connection_to_compref(MTC_COMPREF, NULL)
!= NULL) {
TTCN_warning("Port %s will have more than one connections with local "
"ports. These connections cannot be used for communication even "
"with explicit addressing.", src_port);
}
PORT *dest_ptr = lookup_by_name(dest_port);
if (dest_ptr == NULL) TTCN_error("Connect operation refers to "
"non-existent port %s.", dest_port);
else if (!dest_ptr->is_active) TTCN_error("Internal error: Port %s is "
"inactive when trying to connect it with local port %s.", dest_port,
src_port);
src_ptr->add_local_connection(dest_ptr);
if (src_ptr != dest_ptr) dest_ptr->add_local_connection(src_ptr);
}
void PORT::terminate_local_connection(const char *src_port,
const char *dest_port)
{
PORT *src_ptr = lookup_by_name(src_port);
if (src_ptr == NULL) TTCN_error("Disconnect operation refers to "
"non-existent port %s.", src_port);
else if (!src_ptr->is_active) TTCN_error("Internal error: Port %s is "
"inactive when trying to disconnect it from local port %s.", src_port,
dest_port);
port_connection *conn_ptr = src_ptr->lookup_connection(MTC_COMPREF,
dest_port);
if (conn_ptr != NULL) {
PORT *dest_ptr = conn_ptr->local.port_ptr;
src_ptr->remove_local_connection(conn_ptr);
if (src_ptr != dest_ptr) {
if (!dest_ptr->is_active) TTCN_error("Internal error: Port %s is "
"inactive when trying to disconnect it from local port %s.",
dest_port, src_port);
port_connection *conn2_ptr =
dest_ptr->lookup_connection(MTC_COMPREF, src_port);
if (conn2_ptr == NULL) TTCN_error("Internal error: Port %s is "
"connected with local port %s, but port %s does not have a "
"connection to %s.", src_port, dest_port, dest_port, src_port);
else dest_ptr->remove_local_connection(conn2_ptr);
}
} else {
PORT *dest_ptr = lookup_by_name(dest_port);
if (dest_ptr == NULL) TTCN_error("Disconnect operation refers to "
"non-existent port %s.", dest_port);
else if (src_ptr != dest_ptr) {
if (!dest_ptr->is_active) TTCN_error("Internal error: Port %s is "
"inactive when trying to disconnect it from local port %s.",
dest_port, src_port);
else if (dest_ptr->lookup_connection(MTC_COMPREF, src_port) != NULL)
TTCN_error("Internal error: Port %s is connected with local "
"port %s, but port %s does not have a connection to %s.",
dest_port, src_port, src_port, dest_port);
}
TTCN_warning("Port %s does not have connection with local port %s. "
"Disconnect operation had no effect.", src_port, dest_port);
}
}
void PORT::map_port(const char *component_port, const char *system_port,
Map_Params& params, boolean translation)
{
if (translation == TRUE) {
TTCN_Runtime::initialize_system_port(system_port);
}
const char *port_name = translation == FALSE ? component_port : system_port;
PORT *port_ptr = lookup_by_name(port_name, translation);
if (port_ptr == NULL) TTCN_error("Map operation refers to "
"non-existent port %s.", port_name);
if (port_ptr->connection_list_head != NULL) {
TTCN_error("Map operation is not allowed on a connected port (%s).", port_name);
}
if (translation == FALSE) {
port_ptr->map(system_port, params, translation);
} else {
port_ptr->map(component_port, params, translation);
}
if (translation == TRUE) {
PORT* other_port_ptr = lookup_by_name(component_port, FALSE);
if (other_port_ptr == NULL) {
TTCN_error("Map operation refers to non-existent port %s.", port_name);
}
other_port_ptr->add_port(port_ptr);
port_ptr->add_port(other_port_ptr);
}
}
void PORT::unmap_port(const char *component_port, const char *system_port,
Map_Params& params, boolean translation)
{
if (translation == TRUE) {
TTCN_Runtime::initialize_system_port(system_port);
}
const char *port_name = translation == FALSE ? component_port : system_port;
PORT *port_ptr = lookup_by_name(port_name, translation);
if (port_ptr == NULL) TTCN_error("Unmap operation refers to "
"non-existent port %s.", port_name);
if (translation == FALSE) {
port_ptr->unmap(system_port, params, translation);
} else {
port_ptr->unmap(component_port, params, translation);
}
if (translation == TRUE) {
PORT* other_port_ptr = lookup_by_name(component_port, FALSE);
if (other_port_ptr == NULL) {
TTCN_error("Unmap operation refers to non-existent port %s.", port_name);
}
other_port_ptr->remove_port(port_ptr);
port_ptr->remove_port(other_port_ptr);
}
}
boolean PORT::check_port_state(const CHARSTRING& type) const
{
if (type == "Started") {
return is_started;
} else if (type == "Halted") {
return is_halted;
} else if (type == "Stopped") {
return (!is_started && !is_halted);
} else if (type == "Connected") {
return connection_list_head != NULL;
} else if (type == "Mapped") {
return n_system_mappings > 0;
} else if (type == "Linked") {
return (connection_list_head != NULL || n_system_mappings > 0);
}
TTCN_error("%s is not an allowed parameter of checkstate().", (const char*)type);
}
boolean PORT::any_check_port_state(const CHARSTRING& type)
{
boolean result = FALSE;
for (PORT *port = list_head; port != NULL; port = port->list_next) {
result = port->check_port_state(type);
if (result) {
return TRUE;
}
}
for (PORT *port = system_list_head; port != NULL; port = port->list_next) {
result = port->check_port_state(type);
if (result) {
return TRUE;
}
}
return FALSE;
}
boolean PORT::all_check_port_state(const CHARSTRING& type)
{
boolean result = TRUE;
for (PORT *port = list_head; port != NULL && result; port = port->list_next) {
result = port->check_port_state(type);
}
for (PORT *port = system_list_head; port != NULL && result; port = port->list_next) {
result = port->check_port_state(type);
}
return result;
}
|