imsapi_impl.cpp 77.1 KB
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
/*
 * Copyright 2002-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      CC/LICENSE
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <sstream>
#include "imsapi_impl.h"
#include "ims_core.h"
#include "ims_core_request.h"
#include "ims_conf_loader.h"
#include "ims_core_session_mgr.h"
#include "threadpool.h"
#include "fs_mgr.h"
#include "ims_tool.h"
#include "ims_log.h"

namespace ims {

CcResultT imsapi_impl_t::Register(const ServiceTypeT& type,
                                  ReqIdT& reqid,
                                  const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("Register start. type:%s ", type.get_desc().c_str());

    std::string peerIP, peerPort, peerProxyInfo;
    std::map<std::string, std::string>::const_iterator cit;
    cit = ctx.find("PeerIP");

    if (cit != ctx.end()) {
        peerIP = cit->second;
    }

    cit = ctx.find("PeerPort");

    if (cit != ctx.end()) {
        peerPort = cit->second;
    }

    cit = ctx.find("ProxyName");

    if (cit != ctx.end()) {
        peerProxyInfo = cit->second;
    }

    is_empty("Register", "PeerIP", peerIP);
    is_empty("Register", "ProxyName", peerProxyInfo);
    TRACE_LOG("Peer ip=%s, Peer port=%s", peerIP.c_str(), peerPort.c_str());

    uint32_t naddr = ims_tool_t::inet_pton(peerIP.c_str());

    if (0 == naddr) {
        WARNING_LOG("Register type:%s peerProxyInfo:%s failed, InvalidParam",
                    type.get_desc().c_str(), peerProxyInfo.c_str());
        return CcResultT::ResInvalidParam;
    }

    if (!ims_tool_t::is_accessible_ip(peerIP, type)) {
        WARNING_LOG("Register type:%s peerProxyInfo:%s failed, AccessDenied",
                    type.get_desc().c_str(), peerProxyInfo.c_str());
        return CcResultT::ResAccessDenied;
    }

    uint64_t ireqid;
    ireqid = ims_tool_t::get_reqid(type, naddr, ims_tool_t::str_to_uint32(peerPort.c_str()));

    if (ims_session_manager_t::instance()->rt_add_req(ireqid, peerProxyInfo.c_str())) {
        TRACE_LOG("Register OK, type:%s peerProxyInfo:%s reqid:%ld",
                  type.get_desc().c_str(), peerProxyInfo.c_str(), ireqid);

        reqid = (ReqIdT)ireqid;
        return CcResultT::ResSuccess;
    } else {
        WARNING_LOG("Register failed, type:%s peerProxyInfo:%s failed, add request failed.",
                    type.get_desc().c_str(), peerProxyInfo.c_str());
        return CcResultT::ResFailed;
    }
}

CcResultT imsapi_impl_t::UnRegister(ReqIdT reqid, const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld] UnRegister start.", reqid);

    judge_req("UnRegister", reqid);

    if (!session_mgr->rt_del_req(reqid)) {
        WARNING_LOG("[%ld] UnRegister failed.", reqid);
        return CcResultT::ResFailed;
    }

    TRACE_LOG("[%ld] UnRegister ok.", reqid);
    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::Assign(ReqIdT reqid,
                                const std::string& devicenumber,
                                const DnTypeT& type,
                                const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld] Assign, dn:%s, type:%s",
               reqid, devicenumber.c_str(), type.get_desc().c_str());

    is_empty("Assign", "devicenumber", devicenumber);
    judge_req("Assign", reqid);

    std::string assign_num;

    if (type == DnTypeT::IvrANI) {
        assign_num = devicenumber;
    } else {
        assign_num = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    }

    if (!session_mgr->rt_add_dn(assign_num.c_str(), type, reqid)) {
        WARNING_LOG("[%ld] Assign failed, dn:%s, type:%s", reqid, assign_num.c_str(),
                    type.get_desc().c_str());
        return CcResultT::ResFailed;
    }

    NOTICE_LOG("[%ld] Assign ok, dn:%s, type:%s", reqid, assign_num.c_str(), type.get_desc().c_str());
    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::DeAssign(ReqIdT reqid,
                                  const std::string& devicenumber,
                                  const DnTypeT& type,
                                  const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld] DeAssign, dn:%s, type:%s",
               reqid, devicenumber.c_str(), type.get_desc().c_str());

    is_empty("DeAssign", "devicenumber", devicenumber);
    judge_req("DeAssign", reqid);

    std::string assign_num;

    if (type == DnTypeT::IvrANI) {
        assign_num = devicenumber;
    } else {
        assign_num = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    }

    if (!ims_session_manager_t::instance()->rt_del_dn(assign_num.c_str(), type)) {
        WARNING_LOG("[%lu] DeAssign failed, dn:%s, type:%s", reqid, assign_num.c_str(),
                    type.get_desc().c_str());
        return CcResultT::ResFailed;
    }

    NOTICE_LOG("[%ld] DeAssign ok, dn:%s, type:%s", reqid, assign_num.c_str(), type.get_desc().c_str());
    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::OutboundCall(ReqIdT reqid,
                                      const std::string& srcUri,
                                      const std::string& destUri,
                                      const std::string& setSrcUri,
                                      const std::string& setDestUri,
                                      int32_t timeout,
                                      const CallModeT& callmode,
                                      SessionIdT& sessionid,
                                      const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld] OutboundCall start. src:%s, dest:%s, setsrc:%s, setdest:%s, timeout:%d, mode:%s, sid=%ld",
               reqid, srcUri.c_str(),
               destUri.c_str(), setSrcUri.c_str(), setDestUri.c_str(),
               timeout, callmode.get_desc().c_str(), sessionid);

    is_empty("OutboundCall", "srcUri", srcUri);
    is_empty("OutboundCall", "destUri", destUri);
    judge_req("OutboundCall", reqid);

    if (timeout < 10) {
        timeout = 10;
    } else if (timeout > 60) {
        timeout = 60;
    }

    std::string caller = ims_tool_t::get_assigned_caller(srcUri, srcUri);
    std::string called = ims_tool_t::get_assigned_called(destUri, srcUri);
    judge_agent("OutboundCall", caller.c_str(), reqid);

    if (caller == called) {
        WARNING_LOG("[%ld] OutboundCall failed, agent==dest(%s,%s)"
                    , reqid, caller.c_str(), called.c_str());
        return CcResultT::ResInvalidParam;
    }

    if (!is_idle("OutboundCall", ims_tool_t::chlname2no(caller), caller)) {
        WARNING_LOG("[%ld] OutboundCall failed, agent %s is busy"
                    , reqid, caller.c_str());
        return CcResultT::ResInvalidState;
    }

    if (!is_idle("OutboundCall", ims_tool_t::chlname2no(called), called)) {
        WARNING_LOG("[%ld] OutboundCall failed, agent %s is busy"
                    , reqid, called.c_str());
        return CcResultT::ResInvalidState;
    }

    uint32_t fsno = (~0);

    // ivr makecall
    if (session_mgr->is_IVRoutbound_session(sessionid)) {
        TRACE_LOG("[%ld]OutboundCall4IVR start. sessionid:%ld", reqid, sessionid);
        ims_route_info_t info;

        if (!session_mgr->rt_query_session_route(sessionid, info)) {
            WARNING_LOG("[%ld]OutboundCall4IVR failed(sid=%ld), sessiond not exist",
                        reqid, sessionid);
            return CcResultT::ResInvalidParam;
        }

        if (0 == info.targetUri.size()) {
            WARNING_LOG("[%ld]OutboundCall4IVR failed(sid=%ld), targetUri is null",
                        reqid, sessionid);
            return CcResultT::ResInvalidState;
        }

        // if route request success, ivr should makecall within $validtime second
        if (ims_tool_t::get_timestamp_s() - info.timestamp / 1000000 > info.validtime) {
            WARNING_LOG("[%ld]OutboundCall4IVR failed(sid=%ld), ivr should makecall \
						within $validtime seconds after routerequest.", reqid, sessionid);
            return CcResultT::ResInvalidState;
        }

        fsno = ims_tool_t::get_fsno(sessionid);
    }
    // acd makecall, ivr is not allowed to make normal call
    else if ((reqid >> 56) == ServiceTypeT::ServiceACD) {
        sessionid = 0;

        if (fs_mgr_t::instance()->decision_fs(fsno) != IMS_SUCCESS) {
            WARNING_LOG("[%ld] OutboundCall failed, decision_fs error.", reqid);
            return CcResultT::ResNoEnoughResource;
        }

        session_thread_ptr thrd = session_thrd_mgr::instance()->get_prefer_thread();

        if (!thrd.is_valid()) {
            WARNING_LOG("[%ld] OutboundCall failed, get_prefer_thread error.", reqid);
            return CcResultT::ResFailed;
        }

        if (!session_mgr->create_session(fsno,
                                         reqid,
                                         SCRT_OUTBOUND_NORMAL,
                                         thrd,
                                         ims_tool_t::chlname2no(caller).c_str(),
                                         ims_tool_t::chlname2no(called).c_str(),
                                         sessionid)) {
            WARNING_LOG("[%ld] OutboundCall failed, create_session error.", reqid);
            return CcResultT::ResFailed;
        }
    } else {
        WARNING_LOG("[%ld] OutboundCall failed, invalid param.", reqid);
        return CcResultT::ResInvalidParam;
    }

    session_mgr->update_session_operation(sessionid, SOPR_MAKECALL, caller.c_str());

    fs_tool_t opr(fsno);

    if (!opr.valid()) {
        WARNING_LOG("[%ld][%ld] OutboundCall failed, fetch_opr error.", reqid, sessionid);
        return CcResultT::ResNoEnoughResource;
    }

    if (opr.opr().originate(caller.c_str(), called.c_str(), setSrcUri.c_str(),
                            setDestUri.c_str(), sessionid, timeout) != IMS_SUCCESS) {
        WARNING_LOG("[%ld] OutboundCall failed, originate error.", reqid);

        // add when callcloud 3.0 for ivr outboundcall
        if (session_mgr->is_IVRoutbound_session(sessionid)) {
            ims_tool_t::push_cancel_route_event(sessionid);
        }

        session_mgr->remove_session(sessionid);
        return CcResultT::ResFailed;
    }

    NOTICE_LOG("[%ld] OutboundCall OK. src:%s, dest:%s, setsrc:%s, setdest:%s, timeout:%d,mode:%s, sessionid:%ld",
               reqid, srcUri.c_str(), destUri.c_str(), setSrcUri.c_str(), setDestUri.c_str(), timeout,
               callmode.get_desc().c_str(), sessionid);
    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::Answer(ReqIdT reqid,
                                SessionIdT sessionid,
                                const std::string& devicenumber,
                                const std::map<std::string, std::string>& ctx) {
    //IVR answer
    TRACE_LOG("[%ld][%ld] Answer dn:%s", reqid, sessionid, devicenumber.c_str());
    is_empty("Answer", "devicenumber", devicenumber);
    judge_req("Answer", reqid);

    std::string called = ims_tool_t::get_assigned_called(devicenumber, devicenumber);
    char channelid[LEN_64 + 1] = {0};

    if (!session_mgr->locate_channel(sessionid, called.c_str(), channelid, LEN_64)) {
        TRACE_LOG("[%ld][%ld] Answer failed, cannot locate channel by devicenumber. ", reqid, sessionid);
        return CcResultT::ResFailed;
    }

    uint32_t fsno = ims_tool_t::get_fsno(sessionid);
    fs_tool_t opr(fsno);

    if (!opr.valid()) {
        WARNING_LOG("[%ld][%ld] Answer fetch_opr error.", reqid, sessionid);
        return CcResultT::ResNoEnoughResource;
    }

    if (opr.opr().answer(channelid) != IMS_SUCCESS) {
        WARNING_LOG("[%ld][%ld] Answer error.", reqid, sessionid);
        return CcResultT::ResFailed;
    }

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::Release(ReqIdT reqid,
                                 SessionIdT sessionid,
                                 const std::string& devicenumber,
                                 const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld][%ld] Release dn:%s", reqid, sessionid, devicenumber.c_str());

    is_empty("Release", "devicenumber", devicenumber);
    judge_req("Release", reqid);
    std::string assign_num = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    judge_agent("Release", assign_num.c_str(), reqid);

    char channelid[LEN_64 + 1] = {0};

    if (!session_mgr->locate_channel(sessionid, assign_num.c_str(), channelid, LEN_64)) {
        TRACE_LOG("[%ld][%ld] Release failed, cannot locate channel by dn. ", reqid, sessionid);
        return CcResultT::ResFailed;
    }

    fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

    if (!opr.valid()) {
        WARNING_LOG("[%ld][%ld] Release fetch_opr error.", reqid, sessionid);
        return CcResultT::ResNoEnoughResource;
    }

    if (opr.opr().hangup(channelid) != IMS_SUCCESS) {
        WARNING_LOG("[%ld][%ld] Release hangup error.", reqid, sessionid);
    }

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::Hold(ReqIdT reqid,
                              SessionIdT sessionid,
                              const std::string& devicenumber,
                              const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld][%ld] Hold agent dn:%s", reqid, sessionid, devicenumber.c_str());

    is_empty("Hold", "devicenumber", devicenumber);
    judge_req("Hold", reqid);
    std::string assign_num = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    judge_agent("Hold", assign_num.c_str(), reqid);

    std::string chlid;
    callid_t callid = 0;
    std::list<const char*> otherids;

    if (!is_state("hold",
                  sessionid, ims_tool_t::chlname2no(assign_num), assign_num, SOPR_MAKECALL, callid, chlid, otherids)
            && !is_inbound("hold", sessionid, ims_tool_t::chlname2no(assign_num), assign_num, callid, chlid,
                           otherids)) {

        return CcResultT::ResInvalidState;
    }

    if (session_mgr->is_internal_call(sessionid, callid)) {
        WARNING_LOG("[%ld][%ld] Hold failed, internal call", reqid, sessionid);
        return CcResultT::ResInvalidState;
    }

    session_mgr->update_session_operation(sessionid, SOPR_HOLD, assign_num.c_str());

    fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

    if (!opr.valid()) {
        session_mgr->update_session_operation(sessionid, SOPR_MAKECALL, assign_num.c_str());
        WARNING_LOG("[%ld][%ld] Hold fetch_opr error.", reqid, sessionid);
        return CcResultT::ResFailed;
    }

    return
        (opr.opr().unbridge(chlid.c_str()) == IMS_SUCCESS
         && opr.opr().play(otherids.front(), DEFAULT_HOLDON_MUSIC) == IMS_SUCCESS
        ) ? CcResultT::ResSuccess : CcResultT::ResFailed;
}

CcResultT imsapi_impl_t::Retrieve(ReqIdT reqid,
                                  SessionIdT sessionid,
                                  const std::string& devicenumber,
                                  const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld][%ld] Retrieve agent dn:%s", reqid, sessionid, devicenumber.c_str());

    judge_req("Retrieve", reqid);
    std::string assign_num = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    judge_agent("Retrieve", assign_num.c_str(), reqid);

    std::string chlid;
    callid_t callid = 0;
    std::list<const char*> otherids;

    if (!is_state("retrieve",
                  sessionid, ims_tool_t::chlname2no(assign_num), assign_num, SOPR_HOLD, callid, chlid, otherids)) {

        return CcResultT::ResInvalidState;
    }

    session_mgr->update_session_operation(sessionid, SOPR_RETRIEVE);

    fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

    if (!opr.valid()) {
        WARNING_LOG("[%ld][%ld] Retrieve fetch_opr error.", reqid, sessionid);
        return CcResultT::ResFailed;
    }

    return
        (opr.opr().stop_media(otherids.front()) == IMS_SUCCESS
         && opr.opr().uuid_bridge(chlid.c_str(), otherids.front()) == IMS_SUCCESS
        ) ? CcResultT::ResSuccess : CcResultT::ResFailed;

}/*}}}*/

CcResultT imsapi_impl_t::Consult(ReqIdT reqid,
                                 SessionIdT sessionid,
                                 const std::string& devicenumber,
                                 const std::string& destUri,
                                 const std::string& setDestUri,
                                 const std::map<std::string, std::string>& ctx) {
    /*{{{*/
    NOTICE_LOG("[%ld][%ld] Consult agentdn:%s,destno:%s,setDestUri:%s",
               reqid, sessionid, devicenumber.c_str(), destUri.c_str(), setDestUri.c_str());

    is_empty("Consult", "devicenumber", devicenumber);
    is_empty("Consult", "destUri", destUri);
    judge_req("Consult", reqid);
    std::string assign_num = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    std::string assign_dest_num = ims_tool_t::get_assigned_called(destUri, devicenumber);
    judge_agent("Consult", assign_num.c_str(), reqid);

    std::string chlid;
    callid_t callid = 0;
    std::list<const char*> otherids;

    if (assign_num == assign_dest_num) {
        WARNING_LOG("[%ld] Consult return failed, agent==dest(%s,%s)"
                    , reqid, assign_num.c_str(), assign_dest_num.c_str());
        return CcResultT::ResInvalidParam;
    }

    if (!is_state("consult",
                  sessionid, ims_tool_t::chlname2no(assign_num), assign_num, SOPR_MAKECALL, callid, chlid, otherids)
            && !is_inbound("consult", sessionid, ims_tool_t::chlname2no(assign_num), assign_num, callid, chlid,
                           otherids)) {
        return CcResultT::ResInvalidState;
    }

    if (session_mgr->is_internal_call(sessionid, callid)) {
        WARNING_LOG("[%ld][%ld] Consult failed, internal call", reqid, sessionid);
        return CcResultT::ResInvalidState;
    }

    if (!is_idle("consult", ims_tool_t::chlname2no(assign_dest_num), assign_dest_num)) {
        WARNING_LOG("[%ld] Consult failed, agent %s is busy"
                    , reqid, assign_dest_num.c_str());
        return CcResultT::ResInvalidState;
    }

    session_mgr->update_session_operation(sessionid, SOPR_CONSULT, assign_num.c_str());

    fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

    if (!opr.valid()) {
        WARNING_LOG("[%ld][%ld] Consult fetch_opr error.", reqid, sessionid);
        return CcResultT::ResNoEnoughResource;
    }

    std::list<const char*>::iterator chit = otherids.begin();

    for (; chit != otherids.end(); ++chit) {
        if (opr.opr().unbridge(*chit) != IMS_SUCCESS) {
            WARNING_LOG("[%ld][%ld] Consult unbridge error. [%s]", reqid, sessionid, *chit);
            session_mgr->update_session_operation(sessionid, SOPR_MAKECALL, assign_num.c_str());
            return CcResultT::ResFailed;
        }

        if (opr.opr().play(*chit, DEFAULT_HOLDON_MUSIC) != IMS_SUCCESS) {
            WARNING_LOG("[%ld][%ld] Consult play hold music error.", reqid, sessionid);
        } else {
            TRACE_LOG("[%ld][%ld] Consult playing default hold music on chl[%s].", reqid, sessionid, *chit);
        }
    }

    if (opr.opr().bridge(chlid.c_str(), setDestUri.c_str(), assign_dest_num.c_str()) != IMS_SUCCESS) {
        WARNING_LOG("[%ld][%ld] Consult invoke fs brideg failed.", reqid, sessionid);
        return CcResultT::ResFailed;
    }

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::Reconnect(ReqIdT reqid,
                                   SessionIdT sessionid,
                                   const std::string& devicenumber,
                                   const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld][%ld] Reconnect agent dn:%s", reqid, sessionid, devicenumber.c_str());

    is_empty("Reconnect", "devicenumber", devicenumber);
    judge_req("Reconnect", reqid);
    std::string assign_num = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    judge_agent("Reconnect", assign_num.c_str(), reqid);

    std::string chlid;
    callid_t callid = 0;
    std::list<const char*> otherids;

    if (!is_state("reconnect",
                  sessionid, ims_tool_t::chlname2no(assign_num), assign_num, SOPR_CONSULT, callid, chlid, otherids)) {
        return CcResultT::ResInvalidState;
    }

    fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

    if (!opr.valid()) {
        WARNING_LOG("[%ld][%ld] Reconnect fetch_opr error.", reqid, sessionid);
        return CcResultT::ResNoEnoughResource;
    }

    session_mgr->update_session_operation(sessionid, SOPR_RECONNECT);

    std::list<const char*>::iterator chit = otherids.begin();

    for (; chit != otherids.end(); ++chit) {
        if (opr.opr().unbridge(chlid.c_str()) != IMS_SUCCESS
                || opr.opr().hangup(*chit) != IMS_SUCCESS) {
            WARNING_LOG("[%ld][%ld] Reconnect unbridge error. [%s]", reqid, sessionid, *chit);
        }

        break;
    }

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::Transfer(ReqIdT reqid,
                                  SessionIdT sessionid,
                                  const std::string& devicenumber,
                                  const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld][%ld] Transfer agent dn:%s", reqid, sessionid, devicenumber.c_str());

    is_empty("Transfer", "devicenumber", devicenumber);
    judge_req("Transfer", reqid);
    std::string assign_num = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    judge_agent("Transfer", assign_num.c_str(), reqid);


    std::string chlid;
    callid_t callid = 0;
    std::list<const char*> otherids;

    if (!is_state("transfer",
                  sessionid, ims_tool_t::chlname2no(assign_num), assign_num, SOPR_CONSULT, callid, chlid, otherids)) {
        return CcResultT::ResInvalidState;
    }

    session_mgr->update_session_operation(sessionid, SOPR_TRANSFER);

    fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

    if (!opr.valid()) {
        WARNING_LOG("[%ld][%ld]Transfer fetch_opr error.", reqid, sessionid);
        return CcResultT::ResNoEnoughResource;
    }

    if (opr.opr().unbridge(chlid.c_str()) != IMS_SUCCESS) {
        WARNING_LOG("[%ld][%ld] Transfer unbridge error. [%s]", reqid, sessionid, chlid.c_str());
    }

    if (opr.opr().hangup(chlid.c_str()) != IMS_SUCCESS) {
        WARNING_LOG("[%ld][%ld] Transfer hangup error. [%s]", reqid, sessionid, chlid.c_str());
    }

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::Conference(ReqIdT reqid,
                                    SessionIdT sessionid,
                                    const std::string& devicenumber,
                                    const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld][%ld] Conference agent dn:%s", reqid, sessionid, devicenumber.c_str());

    is_empty("Conference", "devicenumber", devicenumber);
    judge_req("Conference", reqid);
    std::string assign_num = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    judge_agent("Conference", assign_num.c_str(), reqid);

    std::string chlid;
    callid_t callid = 0;
    std::list<const char*> otherids;

    if (!is_state("conference",
                  sessionid, ims_tool_t::chlname2no(assign_num), assign_num, SOPR_CONSULT, callid, chlid, otherids)) {
        return CcResultT::ResInvalidState;
    }

    std::list<const char*> chls_held;
    callid_t callid_held;

    if (!session_mgr->locate_othercall(sessionid, callid, callid_held)
            || !session_mgr->get_channelsbycall(sessionid, callid_held, chls_held)
            || chls_held.size() < 1) {

        TRACE_LOG("[%ld][%ld] Conference failed, cannot locate held channels by call(%lu) "
                  , reqid, sessionid, callid_held);
        return CcResultT::ResFailed;
    }

    fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

    if (!opr.valid()) {
        WARNING_LOG("[%ld][%ld]Conference fetch_opr error.", reqid, sessionid);
        return CcResultT::ResNoEnoughResource;
    }

    session_mgr->update_session_operation(sessionid, SOPR_CONFERENCE);

    session_mgr->remove_channelfromcall(sessionid, callid_held, chls_held.front());
    session_mgr->update_channel_call(chls_held.front(), callid);

    std::string conf_name = ims_tool_t::generate_uuid();
    //session_mgr->add_rundata(sessionid,"conf_name",conf_name.c_str());

    opr.opr().unbridge(chlid.c_str());
    opr.opr().stop_media(chls_held.front());
    ims_tool_t::safe_sleepms(50);
    opr.opr().conference(chlid.c_str(), conf_name.c_str());
    ims_tool_t::safe_sleepms(50);
    opr.opr().conference(otherids.front(), conf_name.c_str());
    ims_tool_t::safe_sleepms(50);
    opr.opr().conference(chls_held.front(), conf_name.c_str());

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::SingleStepTransfer(ReqIdT reqid,
        SessionIdT sessionid,
        const std::string& devicenumber,
        const std::string& destUri,
        const std::string& dispForCalled,
        const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld][%ld] SingleStepTransfer agent dn:%s,destdn:%s,disp:%s",
               reqid, sessionid, devicenumber.c_str(), destUri.c_str(), dispForCalled.c_str());

    is_empty("SingleStepTransfer", "devicenumber", devicenumber);
    judge_req("SingleStepTransfer", reqid);
    std::string chlid;
    callid_t callid;
    std::string name = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    std::string dest_channel = ims_tool_t::get_assigned_called(destUri, devicenumber);

    if (name == dest_channel) {
        WARNING_LOG("[%ld] SingleStepTransfer return failed, agent==dest(%s,%s)"
                    , reqid, name.c_str(), dest_channel.c_str());
        return CcResultT::ResInvalidParam;
    }

    std::list<const char*> otherids;
    judge_agent("SingleStepTransfer", name.c_str(), reqid);

    if (!is_state("singlesteptransfer",
                  sessionid, ims_tool_t::chlname2no(name), name, SOPR_MAKECALL, callid, chlid, otherids)
            && !is_inbound("singlesteptransfer", sessionid, ims_tool_t::chlname2no(name), name, callid, chlid,
                           otherids)) {
        return CcResultT::ResInvalidState;
    }

    if (session_mgr->is_internal_call(sessionid, callid)) {
        WARNING_LOG("singlesteptransfer(sid=%lu,reqid=%lu,dn=%s,dest=%s)failed internal call",
                    sessionid, reqid, devicenumber.c_str(), destUri.c_str());
        return CcResultT::ResInvalidState;
    }

    if (!is_idle("singlesteptransfer", ims_tool_t::chlname2no(dest_channel), dest_channel)) {
        WARNING_LOG("[%ld] singlesteptransfer failed, agent %s is busy"
                    , reqid, dest_channel.c_str());
        return CcResultT::ResInvalidState;
    }

    fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

    if (!opr.valid()) {
        WARNING_LOG("singlesteptransfer(sid=%ld,reqid=%ld)failed fetch_opr error."
                    , sessionid, reqid);
        return CcResultT::ResNoEnoughResource;
    }

    session_mgr->update_session_operation(sessionid, SOPR_SINGLESTEPTRANSFER, name.c_str());

    opr.opr().unbridge(chlid.c_str());
    opr.opr().hangup(chlid.c_str());
    opr.opr().bridge(otherids.front(), dispForCalled.c_str(), dest_channel.c_str());

    return CcResultT::ResSuccess;
}


CcResultT imsapi_impl_t::SingleStepConference(ReqIdT reqid,
        SessionIdT sessionid,
        const std::string& devicenumber,
        const std::string& destUri,
        const std::string& dispForCalled,
        const SsConferenceTypeT& type,
        const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld][%ld] SingleStepConference agent dn:%s,destdn:%s,mode:%s",
               reqid, sessionid, devicenumber.c_str(), destUri.c_str(), type.get_desc().c_str());

    is_empty("SingleStepConference", "devicenumber", devicenumber);
    is_empty("SingleStepConference", "destUri", destUri);
    judge_req("SingleStepConference", reqid);
    std::string chlid;
    callid_t callid;
    std::string name = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    std::string dest_channel = ims_tool_t::get_assigned_called(destUri, devicenumber);

    if (name == dest_channel) {
        WARNING_LOG("[%ld] SingleStepConference return failed, agent==dest(%s,%s)"
                    , reqid, name.c_str(), dest_channel.c_str());
        return CcResultT::ResInvalidParam;
    }

    std::list<const char*> otherids;
    judge_agent("SingleStepConference", name.c_str(), reqid);

    if (!is_state("singlestepconference",
                  sessionid, destUri, dest_channel, SOPR_MAKECALL, callid, chlid, otherids)
            && !is_inbound("singlestepconference", sessionid, destUri, dest_channel, callid, chlid, otherids)) {

        return CcResultT::ResInvalidState;
    }

    if (session_mgr->is_internal_call(sessionid, callid)) {
        WARNING_LOG("singlestepconference(sid=%lu,reqid=%lu,dn=%s,dest=%s)failed internal call",
                    sessionid, reqid, devicenumber.c_str(), destUri.c_str());

        return CcResultT::ResInvalidState;
    }

    if (!is_idle("singlestepconference", ims_tool_t::chlname2no(name), name)) {
        WARNING_LOG("[%ld] singlestepconference return failed, agent %s is busy"
                    , reqid, dest_channel.c_str());

        return CcResultT::ResInvalidState;
    }

    fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

    if (!opr.valid()) {
        WARNING_LOG("singlestepconference(sid=%ld,reqid=%ld)failed fetch_opr error."
                    , sessionid, reqid);
        return CcResultT::ResNoEnoughResource;
    }

    fs_opr_t::CONF_MODE conf_mod = fs_opr_t::CONF_INSERT;
    session_mgr->update_session_operation(sessionid, SOPR_INSERT, name.c_str());

    if (type == ims::SsConferenceTypeT::Monitor) {
        conf_mod = fs_opr_t::CONF_LISTEN;
        session_mgr->update_session_operation(sessionid, SOPR_LISTEN, name.c_str());
    }

    std::string conf_name = ims_tool_t::generate_uuid();

    if (!session_mgr->add_rundata(sessionid, "conf_name", conf_name.c_str())) {
        WARNING_LOG("singlestepconference(sid=%lu,reqid=%lu,dn=%s,dest=%s)fail add_runndata(%s))"
                    , sessionid, reqid, devicenumber.c_str(), destUri.c_str(), conf_name.c_str());
        return CcResultT::ResFailed;
    }

    if (opr.opr().join_conference(conf_name.c_str(), dispForCalled.c_str(),
                                  name.c_str(), conf_mod, sessionid) != IMS_SUCCESS) {
        WARNING_LOG("singlestepconference(sid=%lu,reqid=%lu,dn=%s,dest=%s)fail join conference(%s))"
                    , sessionid, reqid, devicenumber.c_str(), destUri.c_str(), conf_name.c_str());
        session_mgr->update_session_operation(sessionid, SOPR_MAKECALL, dest_channel.c_str());
        return CcResultT::ResNoEnoughResource;
    }

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::SwitchListenAndIntrude(ReqIdT reqid,
        SessionIdT sessionid,
        const std::string& devicenumber,
        const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld][%ld] SwitchListenAndIntrude agent dn:%s",
               reqid, sessionid, devicenumber.c_str());

    judge_req("SwitchListenAndIntrude", reqid);
    std::string chlid;
    callid_t callid;
    std::string name = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    std::list<const char*> otherids;
    judge_agent("SwitchListenAndIntrude", name.c_str(), reqid);

    char conf_name[LEN_64 + 1] = {0};
    char conf_memid[LEN_64 + 1] = {0};

    fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

    if (!opr.valid()) {
        WARNING_LOG("switchlistentointrude(sid=%ld,reqid=%ld)failed fetch_opr error."
                    , sessionid, reqid);
        return CcResultT::ResNoEnoughResource;
    }

    if (is_state("switchlistentointrude",
                 sessionid, devicenumber, name, SOPR_LISTEN, callid, chlid, otherids)) {
        //change to insert

        if (!session_mgr->get_rundata(sessionid, "conf_name", conf_name, LEN_64)
                || !session_mgr->get_rundata(sessionid, chlid.c_str(), conf_memid, LEN_64)) {
            WARNING_LOG("switchlistentointrude(sid=%ld,reqid=%ld)failed get rundata"
                        , sessionid, reqid);
            return CcResultT::ResFailed;
        }

        if (opr.opr().conference_unmute(conf_name, conf_memid) != IMS_SUCCESS) {
            return CcResultT::ResFailed;
        }
    } else if (is_state("switchlistentointrude",
                        sessionid, devicenumber, name, SOPR_INSERT, callid, chlid, otherids)) {
        //change to listen

        if (!session_mgr->get_rundata(sessionid, "conf_name", conf_name, LEN_64)
                || !session_mgr->get_rundata(sessionid, chlid.c_str(), conf_memid, LEN_64)) {
            WARNING_LOG("switchlistentointrude(sid=%ld,reqid=%ld)failed get rundata"
                        , sessionid, reqid);
            return CcResultT::ResFailed;
        }

        if (opr.opr().conference_mute(conf_name, conf_memid) != IMS_SUCCESS) {
            return CcResultT::ResFailed;
        }
    } else {
        return CcResultT::ResInvalidState;
    }

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::SetAssociateData(ReqIdT reqid,
        SessionIdT sessionid,
        const std::string& key,
        const std::string& val,
        const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("SetAssociateData reqid=%lu,sid=%lu,key=%s,val=%s",
               reqid, sessionid, key.c_str(), val.c_str());
    judge_req("SetAssociateData", reqid);

    if (session_mgr->save_session_data(sessionid, key.c_str(), val.c_str())) {
        return CcResultT::ResSuccess;
    } else {
        return CcResultT::ResFailed;
    }
}

CcResultT imsapi_impl_t::GetAssociateData(ReqIdT reqid,
        SessionIdT sessionid,
        const std::string& key,
        std::string& val,
        const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("GetAssociateData reqid=%lu,sid=%lu,key=%s",
               reqid, sessionid, key.c_str());
    judge_req("GetAssociateData", reqid);

    char tmp[LEN_256 + 1] = {0};

    if (session_mgr->get_session_data(sessionid, key.c_str(), tmp, LEN_256)) {
        val = tmp;
        return CcResultT::ResSuccess;
    } else {
        return CcResultT::ResFailed;
    }
}

CcResultT imsapi_impl_t::RouteRequest(ReqIdT reqid,
                                      SessionIdT sessionid,
                                      const ServiceTypeT& destService,
                                      int32_t timeout,
                                      int32_t valid_time,
                                      const RouteRequestTypeT& reqType,
                                      const std::string& reqArgs,
                                      int32_t level,
                                      const std::string& caller_uri,
                                      const std::string& callee_uri,
                                      const std::string& buffer,
                                      RouteRequestIDT& requestId,
                                      const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("RouteRequest start.  reqid=%ld,sid=%ld,destService=%s,timeout=%d,"
               "valid_time=%d,reqtype=%s,reqargs=%s,level=%d,caller=%s,callee=%s,buffer=%s",
               reqid, sessionid, destService.get_desc().c_str(), timeout, valid_time,
               reqType.get_desc().c_str(), reqArgs.c_str(), level,
               caller_uri.c_str(), callee_uri.c_str(), buffer.c_str());

    judge_req("RouteRequest", reqid);

    if (timeout < 0 || valid_time < 0) {
        WARNING_LOG("RouteRequest failed(sid=%ld timeout and valid_time < 0)", sessionid);
        return CcResultT::ResInvalidParam;
    }

    if (!session_mgr->is_valid_request(sessionid)) {
        WARNING_LOG("RouteRequest failed(sid=%ld already has a route request, waiting for reponse)"
                    , sessionid);
        return CcResultT::ResRouteRequestRepeated;
    }

    ReqIdT reqid_dest = 0;

    if (!session_mgr->rt_query_register_service(destService, reqid_dest)) {
        WARNING_LOG("RouteRequest failed(sid=%ld,destservice=%s no dest service)"
                    , sessionid, destService.get_desc().c_str());
        return CcResultT::ResInvalidParam;
    }

    //找到callid
    std::list<callid_t> calls;

    if (session_mgr->is_IVRinbound_session(sessionid)) {
        ims_session_operation_t operation = SOPR_UNKNOWN;

        if (!session_mgr->get_session_operation(sessionid, operation)
                || (SOPR_IVRTRANSFER != operation)) {
            WARNING_LOG("RouteRequest failed(sid=%ld session operation type error.)", sessionid);
            return CcResultT::ResInvalidParam;
        }

        std::list<const char*> chls;

        if (!session_mgr->get_channelsbysession(sessionid, chls) || chls.size() == 0) {
            WARNING_LOG("RouteRequest failed(sid=%ld not contain channels)", sessionid);
            return CcResultT::ResInvalidParam;
        }

        if (!session_mgr->locate_call(sessionid, calls) || calls.size() == 0) {
            WARNING_LOG("RouteRequest failed(sid=%ld) locate call by session error.", sessionid);
            return CcResultT::ResFailed;
        }
    } else if (session_mgr->is_IVRoutbound_session(sessionid)) {
        ims_session_operation_t operation = SOPR_UNKNOWN;

        if (!session_mgr->get_session_operation(sessionid, operation)
                || (SOPR_MAKECALL != operation)) {
            WARNING_LOG("RouteRequest failed(sid=%ld session operation type error.)", sessionid);
            return CcResultT::ResInvalidParam;
        }

        // avoid ivr recall RouteRequest after agent ring
        std::list<const char*> chls;

        if (!session_mgr->get_channelsbysession(sessionid, chls) || chls.size() != 0) {
            WARNING_LOG("RouteRequest failed(sid=%ld session should be empty now.)", sessionid);
            return CcResultT::ResInvalidState;
        }
    } else {
        WARNING_LOG("RouteRequest failed(sid=%ld create reason error)", sessionid);
        return CcResultT::ResInvalidParam;
    }

    requestId = ims_tool_t::get_requestid(sessionid, reqType);

    ims_route_info_t info;
    info.timestamp   = ims_tool_t::get_timestamp_ms();
    info.reqid       = reqid;
    info.dest_reqid  = reqid_dest;
    info.sessionid   = sessionid;
    info.requestId   = requestId;
    info.callid      = calls.size() > 0 ? ims_tool_t::num2str(calls.front()) : "";
    info.destService = destService;
    info.requestType = reqType;
    info.requestArgs = reqArgs;
    info.level       = level;
    info.callerDn    = caller_uri;
    info.calleeDn    = callee_uri;
    info.validtime   = valid_time;
    info.timeout     = timeout;
    info.buffer      = buffer;
    //组织request事件,e籺Id = tmstamp << 16 | sessionid_random;笸扑偷讲檎业降膔equest
    RouteEventT ims_event;
    ims_tool_t::prepare_route_event(ims_event, sessionid, info);
    ims_event.eventType = RouteEventTypeT::RT_RouteRequest;
    ims_event.reason = RouteEventReasonT::RouteReasonSuccess;

    if (!session_mgr->rt_add_imsevent_route(reqid_dest, ims_event)) {
        WARNING_LOG("RouteRequest failed(sid=%ld,requestId=%ld,add ims route event error, ommiting)",
                    sessionid, requestId);
        return CcResultT::ResFailed;
    }

    if (!session_mgr->rt_add_route(requestId, info)) {
        WARNING_LOG("RouteRequest failed(sid=%ld,requestId=%ld,add route_info error)",
                    sessionid, requestId);
        return CcResultT::ResFailed;
    }

    NOTICE_LOG("RouteRequest OK.  reqid=%ld,sid=%ld,destService=%s,timeout=%d,"
               "valid_time=%d,reqtype=%s,reqargs=%s,level=%d,caller=%s,callee=%s,buffer=%s",
               reqid, sessionid, destService.get_desc().c_str(), timeout, valid_time,
               reqType.get_desc().c_str(), reqArgs.c_str(), level,
               caller_uri.c_str(), callee_uri.c_str(), buffer.c_str());
    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::CancelRouteRequest(ims::ReqIdT reqid,
        SessionIdT sessionid,
        RouteRequestIDT requestId,
        const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("CancelRouteRequest start. reqid=%ld,sid=%ld,requestId=%ld", reqid, sessionid,
               requestId);
    judge_req("CancelRouteRequest", reqid);
    ims_route_info_t info;

    if (!session_mgr->rt_query_route(requestId, info)) {
        WARNING_LOG("CancelRouteRequest failed(sid=%ld,requestId=%ld,requestId not exist)",
                    sessionid, requestId);
        return CcResultT::ResInvalidParam;
    }

    if (sessionid != info.sessionid) {
        WARNING_LOG("CancelRouteRequest failed(sid=%ld,requestId=%ld),sessionid!=info.sessionid.",
                    sessionid, requestId);
        return CcResultT::ResInvalidParam;
    }

    RouteEventT ims_event;
    ims_tool_t::prepare_route_event(ims_event, sessionid, info);
    ims_event.eventType = RouteEventTypeT::RT_RouteRequestCancel;
    ims_event.reason = RouteEventReasonT::RouteReasonIVRCancel;

    if (session_mgr->rt_add_imsevent_route(info.dest_reqid , ims_event)) {
        session_mgr->rt_del_route(requestId);

        // add when callcloud 3.0 ivr outboundcall
        if (session_mgr->is_IVRoutbound_session(sessionid)) {
            session_mgr->remove_session(sessionid);
        }

        NOTICE_LOG("CancelRouteRequest OK. reqid=%ld,sid=%ld,requestId=%ld", reqid, sessionid, requestId);
        return CcResultT::ResSuccess;
    } else {
        WARNING_LOG("CancelRouteRequest failed(routeid=%ld,add route error)", requestId);
        return CcResultT::ResFailed;
    }
}

CcResultT imsapi_impl_t::RouteRequestRespond(ReqIdT reqid,
        SessionIdT sessionid,
        RouteRequestIDT requestId,
        const RouteEventReasonT& result,
        const std::string& targetUri,
        const std::string& targetAgentId,
        const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("RouteRequestRespond start, reqid=%ld,sid=%ld,requestId=%ld,result=%s,target=%s",
               reqid, sessionid, requestId, result.get_desc().c_str(), targetUri.c_str());
    judge_req("RouteRequestRespond", reqid);

    ims_route_info_t info;

    if (!session_mgr->rt_query_route(requestId, info)) {
        WARNING_LOG("RouteRequestRespond failed(sid=%ld,requestId=%ld,requestId not exist)",
                    sessionid, requestId);
        return CcResultT::ResInvalidParam;
    }

    if (sessionid != info.sessionid) {
        WARNING_LOG("RouteRequestRespond failed(sid=%ld,requestId=%ld),sessionid!=info.sessionid.",
                    sessionid, requestId);
        return CcResultT::ResInvalidParam;
    }

    if (info.targetUri.size() != 0) {
        WARNING_LOG("RouteRequestRespond failed(sid=%ld,requestId=%ld),repeat response.",
                    sessionid, requestId);
        return CcResultT::ResInvalidParam;
    }

    RouteEventT ims_event;
    ims_tool_t::prepare_route_event(ims_event, sessionid, info);
    ims_event.reason = result;
    ims_event.eventType = ims::RouteEventTypeT::RT_RouteRespond;
    ims_event.targetDevice = targetUri;
	ims_event.targetAgentId = targetAgentId;

    /*        //先删除记录再推送事件,因为事件推送是不可逆的。
            //但是这样更容易因为线程之间的竞争导致不一致的结果。
            if(result != RouteEventReasonT::RouteReasonSuccess){
                if(!session_mgr->rt_del_route(requestId)){
                    WARNING_LOG("RouteRequestRespond failed(sid=%ld,requestId=%ld) del route error.",
                        sessionid,requestId);
                    return CcResultT::ResFailed;
                }
            }
            else {
                ims_event.eventType=ims::RouteEventTypeT::RT_RouteRespond;
                ims_event.targetDevice=targetUri;
            }

            if(!session_mgr->rt_add_imsevent_route(info.reqid, ims_event)){
                WARNING_LOG("RouteRequestRespond failed(sid=%ld,requestId=%ld) add event error.",
                        sessionid,requestId);
                session_mgr->rt_add_route(requestId, info);//如果没add成功,怎么办?
                return CcResultT::ResFailed;
            }
    */

    if (result == RouteEventReasonT::RouteReasonSuccess) {
        std::string assign_num = ims_tool_t::get_assigned_caller(targetUri, targetUri);
        judge_agent("RouteRequestRespond", assign_num.c_str(), reqid);
    }

    //先推事件
    if (!session_mgr->rt_add_imsevent_route(info.reqid, ims_event)) {
        WARNING_LOG("RouteRequestRespond failed(sid=%ld,requestId=%ld) add event error.",
                    sessionid, requestId);
        return CcResultT::ResFailed;
    }

    if (result == RouteEventReasonT::RouteReasonSuccess) {
        is_empty("RouteRequestRespond", "targetUri", targetUri);
        info.targetUri = targetUri;

        //把targetUri放到route_info中去
        if (!session_mgr->rt_update_route(requestId, info)) {
            WARNING_LOG("RouteRequestRespond failed, update route error.");
            return CcResultT::ResFailed;
        }
    } else {
        if (!session_mgr->rt_del_route(requestId)) {
            WARNING_LOG("RouteRequestRespond failed(sid=%ld,requestId=%ld) del route error.",
                        sessionid, requestId);
            return CcResultT::ResFailed;
        }

        // add when callcloud 3.0 ivr outboundcall
        if (session_mgr->is_IVRoutbound_session(sessionid)) {
            session_mgr->remove_session(sessionid);
        }
    }

    NOTICE_LOG("RouteRequestRespond OK. reqid=%ld,sid=%ld,requestId=%ld,result=%s,target=%s,"
               "agentID = %s",
               reqid, sessionid, requestId, result.get_desc().c_str(), targetUri.c_str(), 
			   targetAgentId.c_str());
    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::CreateSession(ReqIdT reqid,
                                       const std::string& callsource,
                                       const std::string& channel_id,
                                       const std::string& oricalled,
                                       SessionIdT& sid,
                                       const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld] CreateSession start. callsource:%s, channel_id:%s, oricalled=%s",
               reqid, callsource.c_str(), channel_id.c_str(), oricalled.c_str());

    judge_req("CreateSession", reqid);
    is_empty("CreateSession", "callsource", callsource);

    ReqIdT reqid_dest = 0;

    if (!session_mgr->rt_query_register_service(ServiceTypeT::ServiceACD, reqid_dest)) {
        WARNING_LOG("[%lu]CreateSession failed, no master acd connected.", reqid);
        return CcResultT::ResNoAgentAssigned;
    }

    TRACE_LOG("callsource = %s", callsource.c_str());
    uint32_t naddr = ims_tool_t::inet_pton(callsource.c_str());

    if (0 == naddr) {
        WARNING_LOG("[%ld]CreateSession failed, InvalidParam callsource.", reqid);
        return CcResultT::ResInvalidParam;
    }

    uint32_t fs_no = 0;

    if (IMS_SUCCESS != fs_mgr_t::instance()->fetch_fs_no(naddr, fs_no)) {
        WARNING_LOG("[%ld]CreateSession failed, callsource is not connected to ims.", reqid);
        return CcResultT::ResCallSourceNotFound;
    }

    if (SPECIAL_CHANNELID == channel_id) {
        return create_ivr_outbound_session(reqid, sid, fs_no);
    }

    is_empty("CreateSession", "channel_id", channel_id);
    is_empty("CreateSession", "oricalled", oricalled);

    //取得fs_opr,用来检查uuid_exists
    fs_tool_t opr(fs_no);

    if (!opr.valid()) {
        WARNING_LOG("[%ld]CreateSession failed, fetch opr error.", reqid);
        return CcResultT::ResFailed;
    }

    if (IMS_SUCCESS != opr.opr().uuid_exists(channel_id.c_str())) {
        WARNING_LOG("[%ld]CreateSession failed(uuid=%s), uuid doesn't exist.", reqid, channel_id.c_str());
        return CcResultT::ResInvalidParam;
    }

    if (session_mgr->locate_session(channel_id.c_str(), sid)) {
        WARNING_LOG("[%ld]CreateSession failed(uuid=%s), channel id already exist.", reqid,
                    channel_id.c_str());
        return CcResultT::ResInvalidParam;
    }

    //get channel name from channel id
    char szcmd[LEN_128 + 1] = {0};
    snprintf(szcmd, LEN_128, "uuid:%s ${channel-name}", channel_id.c_str());
    char chlname[LEN_256 + 1] = {0};

    if (IMS_SUCCESS != opr.opr().eval(szcmd, chlname, LEN_256)) {
        WARNING_LOG("[%ld]CreateSession failed(uuid=%s),get channel name error", reqid, channel_id.c_str());
        return CcResultT::ResFailed;
    }

    TRACE_LOG("channel name get by uuid = %s.", chlname);

    {
        session_thread_ptr thrd = session_thrd_mgr::instance()->get_prefer_thread();

        if (!thrd.is_valid()) {
            WARNING_LOG("[%ld]CreateSession failed, get prefer thread error", reqid);
            return CcResultT::ResFailed;
        }

        if (!session_mgr->create_session(fs_no,
                                         reqid,
                                         SCRT_INBOUND_IVR,
                                         thrd,
                                         ims_tool_t::chlname2no(chlname).c_str(),
                                         oricalled.c_str(),
                                         sid)) {
            WARNING_LOG("[%ld]CreateSession failed, create session error", reqid);
            return CcResultT::ResFailed;
        }
    }

    session_mgr->update_session_operation(sid, SOPR_IVRTRANSFER, chlname);

    if (!session_mgr->create_channel(sid, channel_id.c_str(), chlname)) {
        WARNING_LOG("[%ld]CreateSession failed, create channel error, ommiting session", reqid);
        session_mgr->remove_session(sid);
        return CcResultT::ResFailed;
    }

    callid_t callid = 0;

    if (!session_mgr->create_call(sid, channel_id.c_str(), NULL, callid)) {
        WARNING_LOG("[%ld]CreateSession failed, create call error, ommiting...", reqid);
        session_mgr->destroy_channel(channel_id.c_str());
        return CcResultT::ResFailed;
    }

    session_mgr->update_call_ani(sid, callid, ims_tool_t::chlname2no(chlname).c_str());

    //mark channel with sessionid
    if (IMS_SUCCESS != opr.opr().mark_sessionid(channel_id.c_str(), sid)) {
        WARNING_LOG("[%ld]CreateSession failed, mark sessionid error, ommiting...", reqid);
        session_mgr->destroy_channel(channel_id.c_str());
        return CcResultT::ResFailed;
    }

    NOTICE_LOG("[%ld] CreateSession OK. callsource:%s, channel_id:%s, sessionid:%ld",
               reqid, callsource.c_str(), channel_id.c_str(), sid);

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::TransferAgent(ReqIdT reqid,
                                       SessionIdT sessionid,
                                       RouteRequestIDT requestId,
                                       const TransferTypeT& transType,
                                       const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("TransferAgent start. reqid=%ld,sid=%ld,requestId=%ld,transType=%s",
               reqid, sessionid, requestId, transType.get_desc().c_str());
    judge_req("TransferAgent", reqid);

    ims_route_info_t info;

    if (!session_mgr->rt_query_route(requestId, info)) {
        WARNING_LOG("TransferAgent failed(sid=%ld,requestId=%ld,requestId not exist)",
                    sessionid, requestId);
        return CcResultT::ResInvalidParam;
    }

    if (sessionid != info.sessionid) {
        WARNING_LOG("TransferAgent failed(sid=%ld,requestId=%ld),sessionid!=info.sessionid.",
                    sessionid, requestId);
        return CcResultT::ResInvalidParam;
    }

    if (0 == info.targetUri.size()) {
        WARNING_LOG("TransferAgent failed(sid=%ld,requestId=%ld), info.targetUri is null.",
                    sessionid, requestId);
        return CcResultT::ResInvalidState;
    }

    std::list<const char*> chls;

    if (!session_mgr->get_channelsbysession(sessionid, chls) || 0 == chls.size()) {
        WARNING_LOG("TransferAgent failed(sid=%ld,requestId=%ld), session no channel.",
                    sessionid, requestId);
        return CcResultT::ResInvalidParam;
    }

    std::string client_chlname;

    if (!session_mgr->get_channelnamebyid(sessionid, chls.front(), client_chlname)
            || client_chlname.size() == 0) {
        WARNING_LOG("TransferAgent failed(sid=%ld,requestId=%ld), get channel name error.",
                    sessionid, requestId);
        return CcResultT::ResInvalidParam;
    }

    callid_t callid;
    session_mgr->locate_call(chls.front(), callid);

    //把transType 放到 selfrun data,在ivrtransfer event handler中判断来确定要不要挂断客户
    if (!session_mgr->add_rundata(sessionid, "TransferTypeT", transType.get_desc().c_str())) {
        WARNING_LOG("[%ld][%ld]TransferAgent faied, add rundata failed.", reqid, sessionid);
        //return;
    }

    std::string assign_num = ims_tool_t::get_assigned_caller(info.targetUri, info.targetUri);

    switch (transType.get_value()) {
    case ims::TransferTypeT::TT_INSTANT: {
        char result[LEN_128 + 1] = {0};
        int ret = IMS_FAIL_GENERAL;
        {
            fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

            if (!opr.valid()) {
                WARNING_LOG("TransferAgent failed(sid=%ld,reqid=%ld, opr error)", reqid, sessionid);
                return CcResultT::ResNoEnoughResource;
            }

            ret = opr.opr().bridge_ex(chls.front(), info.callerDn.c_str(), assign_num.c_str(), true, true);
        }

        if (IMS_SUCCESS != ret) {
            WARNING_LOG("[%ld][%ld]TransferAgent failed, esl_execute_async bridge failed.", reqid, sessionid);
            OtherEventT otherevent;
            otherevent.sessionid = sessionid;
            otherevent.device = info.targetUri;
            otherevent.callid = ims_tool_t::num2str(callid);
            otherevent.eventType = OtherEventTypeT::OG_OperationFailed;
            otherevent.data.insert(std::pair<std::string, std::string>("REASON", std::string(result)));
            otherevent.timestamp = ims_tool_t::get_timestamp_ms();
            session_mgr->add_imsevent_other(sessionid, otherevent);

            goto fail;
        }

        break;
    }

    case ims::TransferTypeT::TT_RING: {
        bgcc::Semaphore ring_sema;
        info.psema = &ring_sema;

        if (!session_mgr->rt_update_route(requestId, info)) {
            WARNING_LOG("[%ld][%ld]TransferAgent failed, update route error.", reqid, sessionid);
            goto fail;
        }

        char result[LEN_128 + 1] = {0};
        int ret = IMS_FAIL_GENERAL;
        {
            fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

            if (!opr.valid()) {
                WARNING_LOG("TransferAgent failed(sid=%ld,reqid=%ld, opr error)", reqid, sessionid);
                return CcResultT::ResNoEnoughResource;
            }

            ret = opr.opr().bridge_ex(chls.front(), info.callerDn.c_str(), assign_num.c_str(), false, true);
        }

        if (IMS_SUCCESS != ret) {
            WARNING_LOG("[%ld][%ld]TransferAgent failed, esl_execute bridge failed.", reqid, sessionid);
            OtherEventT otherevent;
            otherevent.sessionid = sessionid;
            otherevent.device = info.targetUri;
            otherevent.callid = ims_tool_t::num2str(callid);
            otherevent.eventType = OtherEventTypeT::OG_OperationFailed;
            otherevent.data.insert(std::pair<std::string, std::string>("REASON", std::string(result)));
            otherevent.timestamp = ims_tool_t::get_timestamp_ms();
            session_mgr->add_imsevent_other(sessionid, otherevent);

            goto fail;
        }

        TRACE_LOG("[%ld][%ld]before wait until agent ring : event CHANNEL_PROGRESS", reqid, sessionid);
        //any valid call is surposed to be ringed with 10 seconds
        int32_t retw = ring_sema.wait(IMS_WAIT_RING_TIMEOUT);
        TRACE_LOG("[%ld][%ld]after wait until agent ring : event CHANNEL_PROGRESS", reqid, sessionid);

        if (0 != retw) {
            WARNING_LOG("[%ld][%ld]TransferAgent failed, wait for ring failed.ret=(%d)", reqid, sessionid,
                        retw);
            goto fail;
        } else {
            ims_route_info_t info_temp;

            if (!session_mgr->rt_query_route(requestId, info_temp)) {
                WARNING_LOG("[%ld][%ld]TransferAgent failed, query route failed. requestId=(%ld)",
                            reqid, sessionid, requestId);
                goto fail;
            }

            DEBUG_LOG("[%ld][%ld] state_signal (%d)", reqid, sessionid, info_temp.state_signal);

            if (info_temp.state_signal != STATE_RING) {
                WARNING_LOG("[%ld][%ld]TransferAgent failed, state_signal is not STATE_RING. state=(%d)",
                            reqid, sessionid, info_temp.state_signal);
                goto fail;
            }
        }

        break;
    }

    case ims::TransferTypeT::TT_ANSWER: {
        bgcc::Semaphore answer_sema;
        info.psema = &answer_sema;

        if (!session_mgr->rt_update_route(requestId, info)) {
            WARNING_LOG("[%ld][%ld]TransferAgent failed, update route error.", reqid, sessionid);
            goto fail;
        }

        char result[LEN_128 + 1] = {0};
        int retl = IMS_FAIL_GENERAL;
        {
            fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

            if (!opr.valid()) {
                WARNING_LOG("TransferAgent failed(sid=%ld,reqid=%ld, opr error)", reqid, sessionid);
                return CcResultT::ResNoEnoughResource;
            }

            retl = opr.opr().single_originate(assign_num.c_str(),
                                              info.callerDn.c_str(),
                                              sessionid,
                                              false, false,
                                              NULL, 0,
                                              result, LEN_128);
        }

        if (IMS_SUCCESS != retl) {
            WARNING_LOG("[%ld][%ld]TransferAgent failed, invoke fs single_originate failed.", reqid, sessionid);
            OtherEventT otherevent;
            otherevent.sessionid = sessionid;
            otherevent.device = info.targetUri;
            otherevent.callid = ims_tool_t::num2str(callid);
            otherevent.eventType = OtherEventTypeT::OG_OperationFailed;
            otherevent.data.insert(std::pair<std::string, std::string>("REASON", std::string(result)));
            otherevent.timestamp = ims_tool_t::get_timestamp_ms();
            session_mgr->add_imsevent_other(sessionid, otherevent);

            goto fail;
        }

        TRACE_LOG("[%ld][%ld]before wait until agent answer : event CHANNEL_ANSWER", reqid, sessionid);
        //any valid call is surposed to be answered with 60 seconds
        int32_t retw = answer_sema.wait(IMS_WAIT_ANSWER_TIMEOUT);
        TRACE_LOG("[%ld][%ld]after wait until agent answer : event CHANNEL_ANSWER", reqid, sessionid);

        if (0 != retw) {
            WARNING_LOG("[%ld][%ld]TransferAgent failed, wait for answer failed.ret=(%d)", reqid, sessionid,
                        retw);
            goto fail;
        } else {
            ims_route_info_t info_temp;

            if (!session_mgr->rt_query_route(requestId, info_temp)) {
                WARNING_LOG("[%ld][%ld]TransferAgent failed, query route failed. requestId=(%ld)",
                            reqid, sessionid, requestId);
                goto fail;
            }

            DEBUG_LOG("[%ld][%ld] state_signal (%d)", reqid, sessionid, info_temp.state_signal);

            if (info_temp.state_signal != STATE_ANSWER) {
                WARNING_LOG("[%ld][%ld]TransferAgent failed, state_signal is not STATE_ANSWER. state=(%d)",
                            reqid, sessionid, info_temp.state_signal);
                goto fail;
            }
        }

        break;
    }

    default:
        WARNING_LOG("[%ld][%ld]TransferAgent failed, Unkonwn transfertype:%s", reqid, sessionid,
                    transType.get_desc().c_str());
        return CcResultT::ResInvalidParam;
    }

    NOTICE_LOG("TransferAgent OK. reqid=%ld,sid=%ld,requestId=%ld,transType=%s",
               reqid, sessionid, requestId, transType.get_desc().c_str());
    return CcResultT::ResSuccess;

fail:

    if (!session_mgr->rt_del_session_route(sessionid)) {
        WARNING_LOG("[%ld][%ld]TransferAgent failed, delete session route info failed.", reqid, sessionid);
    }

    return CcResultT::ResFailed;
}
CcResultT imsapi_impl_t::Record(ReqIdT reqid,
                                SessionIdT sessionid,
                                const std::string& devicenumber,
                                const std::string& filename,
                                int32_t maxTime,
                                const std::string& interruptKeys,
                                const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("Record start.reqid=%lu,sid=%lu,devicenumber=%s,filename=%s",
               reqid, sessionid, devicenumber.c_str(), filename.c_str());
    judge_req("Record", reqid);
    char tmp[LEN_64 + 1] = {0};
    std::string chlname = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    judge_agent("Record", chlname.c_str(), reqid);

    if (session_mgr->locate_channel(sessionid, chlname.c_str(), tmp, LEN_64)) {
        fs_tool_t opr(ims_tool_t::get_fsno(sessionid));

        if (opr.valid()
                && opr.opr().record(tmp, filename.c_str(), maxTime) == IMS_SUCCESS) {

            NOTICE_LOG("Record OK. reqid=%lu,sid=%lu,devicenumber=%s,filename=%s.",
                       reqid, sessionid, devicenumber.c_str(), filename.c_str());
            return CcResultT::ResSuccess;
        } else {
            WARNING_LOG("Record failed(sid=%lu,reqid=%ld,deviceno=%s fs error)",
                        reqid, sessionid, devicenumber.c_str());
            return CcResultT::ResNoEnoughResource;
        }
    } else {
        WARNING_LOG("Record failed(sid=%lu,reqid=%ld,deviceno=%s locate channel err)",
                    reqid, sessionid, devicenumber.c_str());

        return CcResultT::ResFailed;
    }
}

CcResultT imsapi_impl_t::SendDTMF(ReqIdT reqid,
                                  SessionIdT sessionid,
                                  const std::string& devicenumber,
                                  const std::string& dtmf,
                                  const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("SendDTMF reqid=%lu,sid=%lu,devicenumber=%s,dtmf=%s",
               reqid, sessionid, devicenumber.c_str(), dtmf.c_str());
    judge_req("SendDTMF", reqid);
    char tmp[LEN_64 + 1] = {0};
    std::string chlname = ims_tool_t::get_assigned_caller(devicenumber, devicenumber);
    judge_agent("SendDTMF", chlname.c_str(), reqid);

    if (dtmf.size() == 0 || dtmf.size() > 20) {
        return CcResultT::ResInvalidParam;
    }

    if (session_mgr->locate_channel(sessionid, chlname.c_str(), tmp, LEN_64)) {
        uint32_t fs_no = ims_tool_t::get_fsno(sessionid);
        fs_tool_t opr(fs_no);

        if (!opr.valid()) {
            WARNING_LOG("SendDTMF failed(sid=%lu,reqid=%ld,deviceno=%s fs error)",
                        reqid, sessionid, devicenumber.c_str());
            return CcResultT::ResNoEnoughResource;
        }

        dtmf_type_t dtmf_type;
        session_mgr->config().get_gwdtmftype(fs_no, dtmf_type);
        std::list<const char*> chls;
        char sz_digit[LEN_32 + 1] = {0};

        if (!session_mgr->get_channelsbysession(sessionid, chls)
                || chls.size() < 1) {
            WARNING_LOG("SendDTMF failed(sid=%lu,reqid=%ld,deviceno=%s locate other err)",
                        reqid, sessionid, devicenumber.c_str());
            return CcResultT::ResFailed;
        }

        int ret = -1;

        if (DTMF_INBAND == dtmf_type) {
            std::string::const_iterator it = dtmf.begin();

            while (it != dtmf.end()) {
                if (isdigit(*it) || '*' == (*it) || '#' == (*it)) {
                    for (std::list<const char*>::iterator chl_it = chls.begin();
                            chl_it != chls.end(); ++chl_it) {
                        snprintf(sz_digit, LEN_32, "${sound_prefix}/dtmf/%c.wav", *it);
                        ret = opr.opr().play(*chl_it, sz_digit);

                        if (ret == IMS_SUCCESS) {
                            TRACE_LOG("send_dtmf %c success", *it);
                        } else {
                            TRACE_LOG("send_dtmf %c failed", *it);
                        }
                    }

                    if ((++it) != dtmf.end()) {
                        ims_tool_t::safe_sleepms(200);
                    }

                } else {
                    ++it;
                }
            }
        } else {
            snprintf(sz_digit, LEN_32, "%s", dtmf.c_str());

            for (std::list<const char*>::iterator chl_it = chls.begin();
                    chl_it != chls.end(); ++chl_it) {
                ret = opr.opr().send_dtmf(*chl_it, sz_digit);

                if (ret == IMS_SUCCESS) {
                    NOTICE_LOG("send_dtmf %s success", dtmf.c_str());
                } else {
                    TRACE_LOG("send_dtmf %s failed", dtmf.c_str());
                }
            }
        }

        return CcResultT::ResSuccess;
    } else {
        WARNING_LOG("SendDTMF failed(sid=%lu,reqid=%ld,deviceno=%s locate channel err)",
                    reqid, sessionid, devicenumber.c_str());

        return CcResultT::ResFailed;
    }
}

CcResultT imsapi_impl_t::DestroySession(ReqIdT reqid,
                                        SessionIdT sessionid,
                                        const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("[%ld][%ld] DestroySession start.", reqid, sessionid);
    judge_req("DestroySession", reqid);

    if (!session_mgr->destroy_session(sessionid)) {
        WARNING_LOG("[%ld][%ld]DestroySession failed.", reqid, sessionid);
        return CcResultT::ResFailed;
    }

    TRACE_LOG("in DestroySession clear dead session before return");
    session_mgr->clear_dead_session();

    NOTICE_LOG("[%ld][%ld] DestroySession OK.", reqid, sessionid);
    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::QuerySession(ReqIdT reqid,
                                      SessionIdT sessionid,
                                      std::string& originalAni,
                                      std::string& originalDnis,
                                      CallIdListT& callidList,
                                      std::vector<std::string>& deviceList,
                                      const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("enter querysession(%lu,%lu)", reqid, sessionid);
    judge_req("QuerySession", reqid);
    callidList.clear();
    deviceList.clear();

    std::list<const char*> chls;
    std::list<callid_t> calls;

    if (!session_mgr->get_channelsbysession(sessionid, chls) || chls.size() == 0
            || !session_mgr->locate_call(sessionid, calls)
            || calls.size() == 0
            || !session_mgr->get_callercalled(sessionid, originalAni, originalDnis)) {
        return CcResultT::ResInvalidParam;
    }

    std::string chlname;

    for (std::list<const char*>::iterator it = chls.begin(); it != chls.end(); ++it) {
        if (session_mgr->get_channelnamebyid(sessionid, *it, chlname)) {
            std::string chlno = ims_tool_t::chlname2no(chlname);
            deviceList.push_back(chlno);
        }
    }

    for (std::list<callid_t>::iterator it = calls.begin(); it != calls.end(); ++it) {
        callidList.push_back(ims_tool_t::num2str(*it));
    }

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::QueryCall(ReqIdT reqid,
                                   SessionIdT sessionid,
                                   const CallIdT& callId,
                                   std::string& deviceAni,
                                   std::string& deviceDnis,
                                   std::string& originalAni,
                                   std::string& originalDnis,
                                   std::vector<std::string>& devicelist,
                                   int32_t& fsid,
                                   const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("enter querycall(%lu,%lu,%s)", reqid, sessionid, callId.c_str());
    judge_req("QueryCall", reqid);
    devicelist.clear();
    deviceAni = "";
    deviceDnis = "";

    std::list<const char*> chls;

    if (!session_mgr->get_channelsbycall(sessionid, atoll(callId.c_str()), chls)
            || chls.size() == 0
            || !session_mgr->get_callercalled(sessionid, originalAni, originalDnis)) {
        return CcResultT::ResInvalidParam;
    }

    session_mgr->get_call_info(sessionid, atoll(callId.c_str()), deviceAni, deviceDnis);

    std::string chlname, chlno;

    for (std::list<const char*>::iterator it = chls.begin(); it != chls.end(); ++it) {
        if (session_mgr->get_channelnamebyid(sessionid, *it, chlname)) {
            chlno = ims_tool_t::chlname2no(chlname);
            devicelist.push_back(chlno);
        }
    }

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::QueryDevice(ReqIdT reqid,
                                     const std::string& device,
                                     SessionIdT& sessionid,
                                     CallIdT& callId,
                                     CallStateT& callstate,
                                     MediaStateT& mediastate,
                                     const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("enter querydevice(%lu,%s)", reqid, device.c_str());
    judge_req("QueryDevice", reqid);
    std::string caller = ims_tool_t::get_assigned_caller(device, device);
    judge_agent("QueryDevice", caller.c_str(), reqid);

    callId = "";
    callstate = ims::CallStateT::SG_UnknownState;
    mediastate = ims::MediaStateT::MG_UnknownState;

    char chlno[LEN_64 + 1] = {0};
    SessionIdT sid = 0;

    if (!session_mgr->locate_sessionbyname(caller.c_str(), sid)
            || !session_mgr->locate_channel(sid, caller.c_str(), chlno, LEN_64)) {
        callstate = ims::CallStateT::SG_IdleState;
        mediastate = ims::MediaStateT::MG_IdleState;
        return CcResultT::ResSuccess;
    }

    sessionid = sid;
    callid_t callid;

    if (session_mgr->locate_call(chlno, callid)) {
        callId = ims_tool_t::num2str(callid);
    }

    session_mgr->get_channel_callstate(chlno, callstate);
    session_mgr->get_channel_mediastate(chlno, mediastate);

    NOTICE_LOG("leave querydevice(%lu,%s)", reqid, device.c_str());

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::QuerySysInfo(ReqIdT reqid,
                                      SysInfoT& sysinfo,
                                      const std::map<std::string, std::string>& ctx) {

    NOTICE_LOG("enter querysysinfo(%lu)", reqid);
    judge_req("QuerySysInfo", reqid);
    sysinfo.sessionCount = session_mgr->get_sessioncount();
    sysinfo.sessionSinceStartup = session_mgr->get_sessionaccu();

    uint64_t duration = (ims_tool_t::get_timestamp_ms() - session_mgr->get_starttime()) / 1000 / 1000;
    TRACE_LOG("now:%ld,start:%ld,duration:%ld", ims_tool_t::get_timestamp_ms(),
              session_mgr->get_starttime(), duration);

    if (duration) {
        sysinfo.sessionPerSec = session_mgr->get_sessionaccu() * 1.0 / duration;
    } else {
        sysinfo.sessionPerSec = 0.0;
    }

    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::ReloadConfig(const ims::ConfigTypeT& type,
                                      const std::map<std::string, std::string>& ctx) {
    NOTICE_LOG("enter reloadconfig");
    ims_session_manager_t* pmgr = ims_session_manager_t::instance();
    ims_conf_t& conf = pmgr->config();
    return (ims_conf_loader_t::instance()->load_conf(pmgr->pool(), conf, true) ?
            CcResultT::ResSuccess : CcResultT::ResFailed);
}

bool imsapi_impl_t::is_inbound(
    const std::string& func,
    const SessionIdT sessionid,
    const std::string& device,
    const std::string& name,
    callid_t& callid,
    std::string& chlid_self,
    std::list<const char*>& otherids) {

    ims_session_manager_t* session_mgr = ims_session_manager_t::instance();
    char chlid[LEN_64 + 1] = {0};

    if (!session_mgr->is_inbound_session(sessionid)) {
        TRACE_LOG("%s(sid=%ld) failed, not inbound sessionid"
                  , func.c_str(), sessionid);
        return false;
    }

    if (!session_mgr->locate_callbyname(sessionid, name.c_str(), callid, chlid, LEN_64)) {
        TRACE_LOG("%s(sid=%ld) failed, cannot locate channel by dn(%s)"
                  , func.c_str(), sessionid, name.c_str());
        return false;
    }

    if (!session_mgr->locate_otherchannel(sessionid, callid, chlid, otherids)
            || otherids.size() == 0) {
        WARNING_LOG("%s(sid=%lu,callid=%lu,self=%s)locate_otherchannel failed"
                    , func.c_str(), sessionid, callid, chlid);
        return false;
    }

    if (otherids.size() > 1) {
        WARNING_LOG("%s,inbound call && callparty>3", func.c_str());
        return false;
    }

    chlid_self = chlid;

    return true;
}

bool imsapi_impl_t::is_state(
    const std::string& func,
    const SessionIdT sessionid,
    const std::string& device,
    const std::string& name,
    const ims_session_operation_t param_operation,
    callid_t& callid,
    std::string& chlid_self,
    std::list<const char*>& otherids) {

    ims_session_manager_t* session_mgr = ims_session_manager_t::instance();
    char chlid[LEN_64 + 1] = {0};

    ims_session_operation_t operation;
    {
        char opr[LEN_64 + 1] = {0};

        if (!session_mgr->get_session_operation(sessionid, operation, opr, LEN_64)
                || param_operation != operation
                || strcasecmp(opr, name.c_str())) {
            TRACE_LOG("%s(sid=%ld) failed, %s(active operation:%u require %u, active opr is %s)"
                      , func.c_str(), sessionid,
                      name.c_str(), operation, param_operation, opr);

            return false;
        }
    }

    if (!session_mgr->locate_callbyname(sessionid, name.c_str(), callid, chlid, LEN_64)) {
        TRACE_LOG("%s(sid=%ld) failed, cannot locate channel by dn(%s)"
                  , func.c_str(), sessionid, name.c_str());
        return false;
    }

    if (!session_mgr->locate_otherchannel(sessionid, callid, chlid, otherids)
            || otherids.size() == 0) {
        WARNING_LOG("%s(sid=%lu,callid=%lu,self=%s)locate_otherchannel failed"
                    , func.c_str(), sessionid, callid, chlid);
        return false;
    }

    chlid_self = chlid;

    return true;
}

bool imsapi_impl_t::is_idle(
    const std::string& func,
    const std::string& device,
    const std::string& name) {
    ims_session_manager_t* session_mgr = ims_session_manager_t::instance();

    if (!session_mgr->rt_query_dn(name.c_str(), DnTypeT::AgentDn)) {
        return true;
    }

    SessionIdT sid = 0;

    if (!session_mgr->locate_sessionbyname(name.c_str(), sid)) {
        return true;
    } else {
        TRACE_LOG("%s channelname(%s) exist in session(%lu), check if it's dead session. "
                  , func.c_str(), name.c_str(), sid);
        // check if the channelid exist in freeswitch.
        // if func"channel exist in ims session must exist in freeswitch"
        fs_tool_t opr(ims_tool_t::get_fsno(sid));
        std::list<const char*> chls;

        if (opr.valid() && session_mgr->get_channelsbysession(sid, chls)) {
            bool is_dead_session = true;

            for (std::list<const char*>::iterator it = chls.begin();
                    it != chls.end(); ++it) {
                if (IMS_SUCCESS == opr.opr().uuid_exists(*it)) {
                    is_dead_session = false;
                }
            }

            if (is_dead_session && session_mgr->destroy_session(sid)) {
                TRACE_LOG("%s all channels in session(%lu) don't exist in freeswitch."
                          , func.c_str(), sid);
                return true;
            }
        }

        TRACE_LOG("%s %s is busy", func.c_str(), name.c_str());
        return false;
    }
}

CcResultT imsapi_impl_t::create_ivr_outbound_session(
    ReqIdT reqid,
    SessionIdT& sid,
    uint32_t fsno) {
    judge_req("create_ivr_outbound_session", reqid);

    session_thread_ptr thrd = session_thrd_mgr::instance()->get_prefer_thread();

    if (!thrd.is_valid()) {
        WARNING_LOG("[%ld] CreateSessionOut failed, get_prefer_thread error.", reqid);
        return CcResultT::ResFailed;
    }

    if (!session_mgr->create_session(fsno,
                                     reqid,
                                     SCRT_OUTBOUND_IVR,
                                     thrd,
                                     NULL,
                                     NULL,
                                     sid)) {
        WARNING_LOG("[%ld] CreateSessionOut failed, create_session error.", reqid);
        return CcResultT::ResFailed;
    }

    session_mgr->update_session_operation(sid, SOPR_MAKECALL, NULL);

    NOTICE_LOG("[%ld] CreateSessionOut OK. channelid:%s, sessionid:%ld", reqid, SPECIAL_CHANNELID, sid);
    return CcResultT::ResSuccess;
}

CcResultT imsapi_impl_t::HeartBeat(ims::ReqIdT reqid,
                                   const std::string& info,
                                   const std::map<std::string, std::string>& ctx) {
    //NOTICE_LOG("HeartBeat.(%lu,%s)",reqid,info.c_str());
    judge_req("HeartBeat", reqid);
    ims::ReqIdT id = reqid;

    if ((id >> 56) == ims::ServiceTypeT::ServiceACD) {
        //TRACE_LOG("update acd as master or slave.");
        is_empty("HeartBear", "info", info);
        ims_session_manager_t* session_mgr = ims_session_manager_t::instance();

        if (!session_mgr->rt_update_reqstate(reqid, info)) {
            WARNING_LOG("update acd as master or slave failed!");
        } else {
            //TRACE_LOG("update acd as master or slave done!");
        }
    } else {
        //do nothing if it's IVR's reqid
    }

    return CcResultT::ResSuccess;
}
}




/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */