/usr/share/pyshared/autobahn/wamp.py is in python-autobahn 0.5.14-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ###############################################################################
##
## Copyright 2011,2012 Tavendo GmbH
##
## 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
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## 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.
##
###############################################################################
import json
import random
import inspect, types
import traceback
import hashlib, hmac, binascii
from twisted.python import log
from twisted.internet import reactor
from twisted.internet.defer import Deferred, \
maybeDeferred, \
returnValue, \
inlineCallbacks
from _version import __version__
from websocket import WebSocketProtocol, HttpException, Timings
from websocket import WebSocketClientProtocol, WebSocketClientFactory
from websocket import WebSocketServerFactory, WebSocketServerProtocol
from httpstatus import HTTP_STATUS_CODE_BAD_REQUEST
from pbkdf2 import PBKDF2
from prefixmap import PrefixMap
from util import utcstr, utcnow, parseutc, newid
def exportRpc(arg = None):
"""
Decorator for RPC'ed callables.
"""
## decorator without argument
if type(arg) is types.FunctionType:
arg._autobahn_rpc_id = arg.__name__
return arg
## decorator with argument
else:
def inner(f):
f._autobahn_rpc_id = arg
return f
return inner
def exportSub(arg, prefixMatch = False):
"""
Decorator for subscription handlers.
"""
def inner(f):
f._autobahn_sub_id = arg
f._autobahn_sub_prefix_match = prefixMatch
return f
return inner
def exportPub(arg, prefixMatch = False):
"""
Decorator for publication handlers.
"""
def inner(f):
f._autobahn_pub_id = arg
f._autobahn_pub_prefix_match = prefixMatch
return f
return inner
class Call:
"""
Thin-wrapper for incoming RPCs provided to call handlers registered via
- registerHandlerMethodForRpc
- registerHandlerProcedureForRpc
"""
def __init__(self,
proto,
callid,
uri,
args,
extra = None):
self.proto = proto
self.callid = callid
self.uri = uri
self.args = args
self.extra = extra
self.timings = None
class WampProtocol:
"""
WAMP protocol base class. Mixin for WampServerProtocol and WampClientProtocol.
"""
URI_WAMP_BASE = "http://api.wamp.ws/"
"""
WAMP base URI for WAMP predefined things.
"""
URI_WAMP_ERROR = URI_WAMP_BASE + "error#"
"""
Prefix for WAMP errors.
"""
URI_WAMP_PROCEDURE = URI_WAMP_BASE + "procedure#"
"""
Prefix for WAMP predefined RPC endpoints.
"""
URI_WAMP_TOPIC = URI_WAMP_BASE + "topic#"
"""
Prefix for WAMP predefined PubSub topics.
"""
URI_WAMP_ERROR_GENERIC = URI_WAMP_ERROR + "generic"
"""
WAMP error URI for generic errors.
"""
DESC_WAMP_ERROR_GENERIC = "generic error"
"""
Description for WAMP generic errors.
"""
URI_WAMP_ERROR_INTERNAL = URI_WAMP_ERROR + "internal"
"""
WAMP error URI for internal errors.
"""
DESC_WAMP_ERROR_INTERNAL = "internal error"
"""
Description for WAMP internal errors."
"""
WAMP_PROTOCOL_VERSION = 1
"""
WAMP version this server speaks. Versions are numbered consecutively
(integers, no gaps).
"""
MESSAGE_TYPEID_WELCOME = 0
"""
Server-to-client welcome message containing session ID.
"""
MESSAGE_TYPEID_PREFIX = 1
"""
Client-to-server message establishing a URI prefix to be used in CURIEs.
"""
MESSAGE_TYPEID_CALL = 2
"""
Client-to-server message initiating an RPC.
"""
MESSAGE_TYPEID_CALL_RESULT = 3
"""
Server-to-client message returning the result of a successful RPC.
"""
MESSAGE_TYPEID_CALL_ERROR = 4
"""
Server-to-client message returning the error of a failed RPC.
"""
MESSAGE_TYPEID_SUBSCRIBE = 5
"""
Client-to-server message subscribing to a topic.
"""
MESSAGE_TYPEID_UNSUBSCRIBE = 6
"""
Client-to-server message unsubscribing from a topic.
"""
MESSAGE_TYPEID_PUBLISH = 7
"""
Client-to-server message publishing an event to a topic.
"""
MESSAGE_TYPEID_EVENT = 8
"""
Server-to-client message providing the event of a (subscribed) topic.
"""
def connectionMade(self):
self.debugWamp = self.factory.debugWamp
self.debugApp = self.factory.debugApp
self.prefixes = PrefixMap()
def connectionLost(self, reason):
pass
def _protocolError(self, reason):
if self.debugWamp:
log.msg("Closing Wamp session on protocol violation : %s" % reason)
## FIXME: subprotocols are probably not supposed to close with CLOSE_STATUS_CODE_PROTOCOL_ERROR
##
self.protocolViolation("Wamp RPC/PubSub protocol violation ('%s')" % reason)
def shrink(self, uri, passthrough = False):
"""
Shrink given URI to CURIE according to current prefix mapping.
If no appropriate prefix mapping is available, return original URI.
:param uri: URI to shrink.
:type uri: str
:returns str -- CURIE or original URI.
"""
return self.prefixes.shrink(uri)
def resolve(self, curieOrUri, passthrough = False):
"""
Resolve given CURIE/URI according to current prefix mapping or return
None if cannot be resolved.
:param curieOrUri: CURIE or URI.
:type curieOrUri: str
:returns: str -- Full URI for CURIE or None.
"""
return self.prefixes.resolve(curieOrUri)
def resolveOrPass(self, curieOrUri):
"""
Resolve given CURIE/URI according to current prefix mapping or return
string verbatim if cannot be resolved.
:param curieOrUri: CURIE or URI.
:type curieOrUri: str
:returns: str -- Full URI for CURIE or original string.
"""
return self.prefixes.resolveOrPass(curieOrUri)
class WampFactory:
"""
WAMP factory base class. Mixin for WampServerFactory and WampClientFactory.
"""
def _serialize(self, obj):
"""
Default object serializer.
"""
return json.dumps(obj)
def _unserialize(self, bytes):
"""
Default object deserializer.
"""
return json.loads(bytes)
class WampServerProtocol(WebSocketServerProtocol, WampProtocol):
"""
Server factory for Wamp RPC/PubSub.
"""
SUBSCRIBE = 1
PUBLISH = 2
def onSessionOpen(self):
"""
Callback fired when WAMP session was fully established.
"""
pass
def onOpen(self):
"""
Default implementation for WAMP connection opened sends
Welcome message containing session ID.
"""
self.session_id = newid()
## include traceback as error detail for RPC errors with
## no error URI - that is errors returned with URI_WAMP_ERROR_GENERIC
self.includeTraceback = True
msg = [WampProtocol.MESSAGE_TYPEID_WELCOME,
self.session_id,
WampProtocol.WAMP_PROTOCOL_VERSION,
"Autobahn/%s" % __version__]
o = self.factory._serialize(msg)
self.sendMessage(o)
self.factory._addSession(self, self.session_id)
self.onSessionOpen()
def onConnect(self, connectionRequest):
"""
Default implementation for WAMP connection acceptance:
check if client announced WAMP subprotocol, and only accept connection
if client did so.
"""
for p in connectionRequest.protocols:
if p in self.factory.protocols:
return p
raise HttpException(HTTP_STATUS_CODE_BAD_REQUEST[0], "this server only speaks WAMP")
def connectionMade(self):
WebSocketServerProtocol.connectionMade(self)
WampProtocol.connectionMade(self)
## RPCs registered in this session (a URI map of (object, procedure)
## pairs for object methods or (None, procedure) for free standing procedures)
self.procs = {}
## Publication handlers registered in this session (a URI map of (object, pubHandler) pairs
## pairs for object methods (handlers) or (None, None) for topic without handler)
self.pubHandlers = {}
## Subscription handlers registered in this session (a URI map of (object, subHandler) pairs
## pairs for object methods (handlers) or (None, None) for topic without handler)
self.subHandlers = {}
def connectionLost(self, reason):
self.factory._unsubscribeClient(self)
self.factory._removeSession(self)
WampProtocol.connectionLost(self, reason)
WebSocketServerProtocol.connectionLost(self, reason)
def sendMessage(self, payload):
if self.debugWamp:
log.msg("TX WAMP: %s" % str(payload))
WebSocketServerProtocol.sendMessage(self, payload)
def _getPubHandler(self, topicUri):
## Longest matching prefix based resolution of (full) topic URI to
## publication handler.
## Returns a 5-tuple (consumedUriPart, unconsumedUriPart, handlerObj, handlerProc, prefixMatch)
##
for i in xrange(len(topicUri), -1, -1):
tt = topicUri[:i]
if self.pubHandlers.has_key(tt):
h = self.pubHandlers[tt]
return (tt, topicUri[i:], h[0], h[1], h[2])
return None
def _getSubHandler(self, topicUri):
## Longest matching prefix based resolution of (full) topic URI to
## subscription handler.
## Returns a 5-tuple (consumedUriPart, unconsumedUriPart, handlerObj, handlerProc, prefixMatch)
##
for i in xrange(len(topicUri), -1, -1):
tt = topicUri[:i]
if self.subHandlers.has_key(tt):
h = self.subHandlers[tt]
return (tt, topicUri[i:], h[0], h[1], h[2])
return None
def registerForPubSub(self, topicUri, prefixMatch = False, pubsub = PUBLISH | SUBSCRIBE):
"""
Register a topic URI as publish/subscribe channel in this session.
:param topicUri: Topic URI to be established as publish/subscribe channel.
:type topicUri: str
:param prefixMatch: Allow to match this topic URI by prefix.
:type prefixMatch: bool
:param pubsub: Allow publication and/or subscription.
:type pubsub: WampServerProtocol.PUB, WampServerProtocol.SUB, WampServerProtocol.PUB | WampServerProtocol.SUB
"""
if pubsub & WampServerProtocol.PUBLISH:
self.pubHandlers[topicUri] = (None, None, prefixMatch)
if self.debugWamp:
log.msg("registered topic %s for publication (match by prefix = %s)" % (topicUri, prefixMatch))
if pubsub & WampServerProtocol.SUBSCRIBE:
self.subHandlers[topicUri] = (None, None, prefixMatch)
if self.debugWamp:
log.msg("registered topic %s for subscription (match by prefix = %s)" % (topicUri, prefixMatch))
def registerHandlerForPubSub(self, obj, baseUri = ""):
"""
Register a handler object for PubSub. A handler object has methods
which are decorated using @exportPub and @exportSub.
:param obj: The object to be registered (in this WebSockets session) for PubSub.
:type obj: Object with methods decorated using @exportPub and @exportSub.
:param baseUri: Optional base URI which is prepended to topic names for export.
:type baseUri: String.
"""
for k in inspect.getmembers(obj.__class__, inspect.ismethod):
if k[1].__dict__.has_key("_autobahn_pub_id"):
uri = baseUri + k[1].__dict__["_autobahn_pub_id"]
prefixMatch = k[1].__dict__["_autobahn_pub_prefix_match"]
proc = k[1]
self.registerHandlerForPub(uri, obj, proc, prefixMatch)
elif k[1].__dict__.has_key("_autobahn_sub_id"):
uri = baseUri + k[1].__dict__["_autobahn_sub_id"]
prefixMatch = k[1].__dict__["_autobahn_sub_prefix_match"]
proc = k[1]
self.registerHandlerForSub(uri, obj, proc, prefixMatch)
def registerHandlerForSub(self, uri, obj, proc, prefixMatch = False):
"""
Register a method of an object as subscription handler.
:param uri: Topic URI to register subscription handler for.
:type uri: str
:param obj: The object on which to register a method as subscription handler.
:type obj: object
:param proc: Unbound object method to register as subscription handler.
:type proc: unbound method
:param prefixMatch: Allow to match this topic URI by prefix.
:type prefixMatch: bool
"""
self.subHandlers[uri] = (obj, proc, prefixMatch)
if not self.pubHandlers.has_key(uri):
self.pubHandlers[uri] = (None, None, False)
if self.debugWamp:
log.msg("registered subscription handler for topic %s" % uri)
def registerHandlerForPub(self, uri, obj, proc, prefixMatch = False):
"""
Register a method of an object as publication handler.
:param uri: Topic URI to register publication handler for.
:type uri: str
:param obj: The object on which to register a method as publication handler.
:type obj: object
:param proc: Unbound object method to register as publication handler.
:type proc: unbound method
:param prefixMatch: Allow to match this topic URI by prefix.
:type prefixMatch: bool
"""
self.pubHandlers[uri] = (obj, proc, prefixMatch)
if not self.subHandlers.has_key(uri):
self.subHandlers[uri] = (None, None, False)
if self.debugWamp:
log.msg("registered publication handler for topic %s" % uri)
def registerForRpc(self, obj, baseUri = "", methods = None):
"""
Register an service object for RPC. A service object has methods
which are decorated using @exportRpc.
:param obj: The object to be registered (in this WebSockets session) for RPC.
:type obj: Object with methods decorated using @exportRpc.
:param baseUri: Optional base URI which is prepended to method names for export.
:type baseUri: String.
:param methods: If not None, a list of unbound class methods corresponding to obj
which should be registered. This can be used to register only a subset
of the methods decorated with @exportRpc.
:type methods: List of unbound class methods.
"""
for k in inspect.getmembers(obj.__class__, inspect.ismethod):
if k[1].__dict__.has_key("_autobahn_rpc_id"):
if methods is None or k[1] in methods:
uri = baseUri + k[1].__dict__["_autobahn_rpc_id"]
proc = k[1]
self.registerMethodForRpc(uri, obj, proc)
def registerMethodForRpc(self, uri, obj, proc):
"""
Register a method of an object for RPC.
:param uri: URI to register RPC method under.
:type uri: str
:param obj: The object on which to register a method for RPC.
:type obj: object
:param proc: Unbound object method to register RPC for.
:type proc: unbound method
"""
self.procs[uri] = (obj, proc, False)
if self.debugWamp:
log.msg("registered remote method on %s" % uri)
def registerProcedureForRpc(self, uri, proc):
"""
Register a (free standing) function/procedure for RPC.
:param uri: URI to register RPC function/procedure under.
:type uri: str
:param proc: Free-standing function/procedure.
:type proc: callable
"""
self.procs[uri] = (None, proc, False)
if self.debugWamp:
log.msg("registered remote procedure on %s" % uri)
def registerHandlerMethodForRpc(self, uri, obj, handler, extra = None):
"""
Register a handler on an object for RPC.
:param uri: URI to register RPC method under.
:type uri: str
:param obj: The object on which to register the RPC handler
:type obj: object
:param proc: Unbound object method to register RPC for.
:type proc: unbound method
:param extra: Optional extra data that will be given to the handler at call time.
:type extra: object
"""
self.procs[uri] = (obj, handler, True, extra)
if self.debugWamp:
log.msg("registered remote handler method on %s" % uri)
def registerHandlerProcedureForRpc(self, uri, handler, extra = None):
"""
Register a (free standing) handler for RPC.
:param uri: URI to register RPC handler under.
:type uri: str
:param proc: Free-standing handler
:type proc: callable
:param extra: Optional extra data that will be given to the handler at call time.
:type extra: object
"""
self.procs[uri] = (None, handler, True, extra)
if self.debugWamp:
log.msg("registered remote handler procedure on %s" % uri)
def dispatch(self, topicUri, event, exclude = [], eligible = None):
"""
Dispatch an event for a topic to all clients subscribed to
and authorized for that topic.
Optionally, exclude list of clients and/or only consider clients
from explicit eligibles. In other words, the event is delivered
to the set
(subscribers - excluded) & eligible
:param topicUri: URI of topic to publish event to.
:type topicUri: str
:param event: Event to dispatch.
:type event: obj
:param exclude: Optional list of clients (WampServerProtocol instances) to exclude.
:type exclude: list of obj
:param eligible: Optional list of clients (WampServerProtocol instances) eligible at all (or None for all).
:type eligible: list of obj
"""
self.factory.dispatch(topicUri, event, exclude, eligible)
def _onBeforeCall(self, callid, uri, args):
## fire user callback
uri, args = self.onBeforeCall(callid, uri, args, self.procs.has_key(uri))
## create call object to move around call data
call = Call(self, callid, uri, args)
## track timings
if self.trackTimings:
self.trackedTimings.track("onBeforeCall")
call.timings = self.trackedTimings
self.trackedTimings = Timings()
return call
def _onAfterCallSuccess(self, result, call):
## track timings
if self.trackTimings:
self.trackedTimings = call.timings
self.trackedTimings.track("onAfterCallSuccess")
## fire user callback
call.result = self.onAfterCallSuccess(result, call)
## send out WAMP message
self._sendCallResult(call)
def _onAfterCallError(self, error, call):
## track timings
if self.trackTimings:
self.trackedTimings = call.timings
self.trackedTimings.track("onAfterCallError")
## fire user callback
call.error = self.onAfterCallError(error, call)
## send out WAMP message
self._sendCallError(call)
def _callProcedure(self, call):
"""
INTERNAL METHOD! Actually performs the call of a procedure invoked via RPC.
"""
if self.procs.has_key(call.uri):
m = self.procs[call.uri]
if not m[2]:
## RPC method/procedure
##
if call.args:
## method/function called with args
cargs = tuple(call.args)
if m[0]:
## call object method
return m[1](m[0], *cargs)
else:
## call free-standing function/procedure
return m[1](*cargs)
else:
## method/function called without args
if m[0]:
## call object method
return m[1](m[0])
else:
## call free-standing function/procedure
return m[1]()
else:
## RPC handler
##
call.extra = m[3]
if m[0]:
## call RPC handler on object
return m[1](m[0], call)
else:
## call free-standing RPC handler
return m[1](call)
else:
raise Exception("no procedure %s" % call.uri)
def onBeforeCall(self, callid, uri, args, isRegistered):
"""
Callback fired before executing incoming RPC. This can be used for
logging, statistics tracking or redirecting RPCs or argument mangling i.e.
The default implementation just returns the incoming URI/args.
:param uri: RPC endpoint URI (fully-qualified).
:type uri: str
:param args: RPC arguments array.
:type args: list
:param isRegistered: True, iff RPC endpoint URI is registered in this session.
:type isRegistered: bool
:returns pair -- Must return URI/Args pair.
"""
return uri, args
def onAfterCallSuccess(self, result, call):
"""
Callback fired after executing incoming RPC with success, but before
sending the RPC success message.
The default implementation will just return `result` to the client.
:param result: Result returned for executing the incoming RPC.
:type result: Anything returned by the user code for the endpoint.
:param call: WAMP call object for incoming RPC.
:type call: instance of Call
:returns obj -- Result send back to client.
"""
return result
def onAfterCallError(self, error, call):
"""
Callback fired after executing incoming RPC with failure, but before
sending the RPC error message.
The default implementation will just return `error` to the client.
:param error: Error that occurred during incomnig RPC call execution.
:type error: Instance of twisted.python.failure.Failure
:param call: WAMP call object for incoming RPC.
:type call: instance of Call
:returns twisted.python.failure.Failure -- Error sent back to client.
"""
return error
def onAfterSendCallSuccess(self, msg, call):
"""
Callback fired after sending RPC success message.
:param msg: Serialized WAMP message.
:type msg: str
:param call: WAMP call object for incoming RPC.
:type call: instance of Call
"""
pass
def onAfterSendCallError(self, msg, call):
"""
Callback fired after sending RPC error message.
:param msg: Serialized WAMP message.
:type msg: str
:param call: WAMP call object for incoming RPC.
:type call: instance of Call
"""
pass
def _sendCallResult(self, call):
"""
INTERNAL METHOD! Marshal and send a RPC success result.
"""
msg = [WampProtocol.MESSAGE_TYPEID_CALL_RESULT, call.callid, call.result]
try:
rmsg = self.factory._serialize(msg)
except:
raise Exception("call result not JSON serializable")
else:
self.sendMessage(rmsg)
if self.trackTimings:
self.trackedTimings.track("onAfterSendCallSuccess")
self.onAfterSendCallSuccess(rmsg, call)
def _sendCallError(self, call):
"""
INTERNAL METHOD! Marshal and send a RPC error result.
"""
killsession = False
try:
## get error args and len
##
eargs = call.error.value.args
leargs = len(eargs)
## error provides no args at all
##
if leargs == 0:
erroruri = WampProtocol.URI_WAMP_ERROR_GENERIC
errordesc = WampProtocol.DESC_WAMP_ERROR_GENERIC
if self.includeTraceback:
errordetails = call.error.getTraceback().splitlines()
else:
errordetails = None
## error only provides description
##
elif leargs == 1:
if type(eargs[0]) not in [str, unicode]:
raise Exception("invalid type %s for errorDesc" % type(eargs[0]))
erroruri = WampProtocol.URI_WAMP_ERROR_GENERIC
errordesc = eargs[0]
if self.includeTraceback:
errordetails = call.error.getTraceback().splitlines()
else:
errordetails = None
## error provides URI, description, optional details and optional kill-session flag
##
elif leargs in [2, 3, 4]:
if type(eargs[0]) not in [str, unicode]:
raise Exception("invalid type %s for errorUri" % type(eargs[0]))
erroruri = eargs[0]
if type(eargs[1]) not in [str, unicode]:
raise Exception("invalid type %s for errorDesc" % type(eargs[1]))
errordesc = eargs[1]
if leargs > 2:
errordetails = eargs[2] # this must be JSON serializable .. if not, we get exception later in sendMessage
else:
errordetails = None
if leargs > 3:
if type(eargs[3]) not in [bool, types.NoneType]:
raise Exception("invalid type %s for killSession" % type(eargs[3]))
else:
killsession = eargs[3]
else:
killsession = False
## error provides illegal number of args
##
else:
raise Exception("invalid args length %d for exception" % leargs)
## assemble WAMP RPC error message
##
if errordetails is not None:
msg = [WampProtocol.MESSAGE_TYPEID_CALL_ERROR,
call.callid,
self.prefixes.shrink(erroruri),
errordesc,
errordetails]
else:
msg = [WampProtocol.MESSAGE_TYPEID_CALL_ERROR,
call.callid,
self.prefixes.shrink(erroruri),
errordesc]
## serialize message. this can fail if errorDetails is not serializable
##
try:
rmsg = self.factory._serialize(msg)
except Exception, e:
raise Exception("invalid object for errorDetails - not serializable (%s)" % str(e))
except Exception, e:
## something bad happened during error processing itself
msg = [WampProtocol.MESSAGE_TYPEID_CALL_ERROR,
call.callid,
self.prefixes.shrink(WampProtocol.URI_WAMP_ERROR_INTERNAL),
str(e)]
if self.includeTraceback:
msg.append(call.error.getTraceback().splitlines())
rmsg = self.factory._serialize(msg)
finally:
self.sendMessage(rmsg)
if self.trackTimings:
self.trackedTimings.track("onAfterSendCallError")
self.onAfterSendCallError(rmsg, call)
if killsession:
self.sendClose(3000, "killing WAMP session upon request by application exception")
def onMessage(self, msg, binary):
"""
INTERNAL METHOD! Handle WAMP messages received from WAMP client.
"""
if self.debugWamp:
log.msg("RX WAMP: %s" % str(msg))
if not binary:
try:
obj = self.factory._unserialize(msg)
if type(obj) == list:
## Call Message
##
if obj[0] == WampProtocol.MESSAGE_TYPEID_CALL:
## parse message and create call object
callid = obj[1]
uri = self.prefixes.resolveOrPass(obj[2])
args = obj[3:]
call = self._onBeforeCall(callid, uri, args)
## execute incoming RPC
d = maybeDeferred(self._callProcedure, call)
## process and send result/error
d.addCallbacks(self._onAfterCallSuccess,
self._onAfterCallError,
callbackArgs = (call,),
errbackArgs = (call,))
## Subscribe Message
##
elif obj[0] == WampProtocol.MESSAGE_TYPEID_SUBSCRIBE:
topicUri = self.prefixes.resolveOrPass(obj[1])
h = self._getSubHandler(topicUri)
if h:
## either exact match or prefix match allowed
if h[1] == "" or h[4]:
## direct topic
if h[2] is None and h[3] is None:
self.factory._subscribeClient(self, topicUri)
## topic handled by subscription handler
else:
try:
## handler is object method
if h[2]:
a = h[3](h[2], str(h[0]), str(h[1]))
## handler is free standing procedure
else:
a = h[3](str(h[0]), str(h[1]))
## only subscribe client if handler did return True
if a:
self.factory._subscribeClient(self, topicUri)
except:
if self.debugWamp:
log.msg("execption during topic subscription handler")
else:
if self.debugWamp:
log.msg("topic %s matches only by prefix and prefix match disallowed" % topicUri)
else:
if self.debugWamp:
log.msg("no topic / subscription handler registered for %s" % topicUri)
## Unsubscribe Message
##
elif obj[0] == WampProtocol.MESSAGE_TYPEID_UNSUBSCRIBE:
topicUri = self.prefixes.resolveOrPass(obj[1])
self.factory._unsubscribeClient(self, topicUri)
## Publish Message
##
elif obj[0] == WampProtocol.MESSAGE_TYPEID_PUBLISH:
topicUri = self.prefixes.resolveOrPass(obj[1])
h = self._getPubHandler(topicUri)
if h:
## either exact match or prefix match allowed
if h[1] == "" or h[4]:
## Event
##
event = obj[2]
## Exclude Sessions List
##
exclude = [self] # exclude publisher by default
if len(obj) >= 4:
if type(obj[3]) == bool:
if not obj[3]:
exclude = []
elif type(obj[3]) == list:
## map session IDs to protos
exclude = self.factory.sessionIdsToProtos(obj[3])
else:
## FIXME: invalid type
pass
## Eligible Sessions List
##
eligible = None # all sessions are eligible by default
if len(obj) >= 5:
if type(obj[4]) == list:
## map session IDs to protos
eligible = self.factory.sessionIdsToProtos(obj[4])
else:
## FIXME: invalid type
pass
## direct topic
if h[2] is None and h[3] is None:
self.factory.dispatch(topicUri, event, exclude, eligible)
## topic handled by publication handler
else:
try:
## handler is object method
if h[2]:
e = h[3](h[2], str(h[0]), str(h[1]), event)
## handler is free standing procedure
else:
e = h[3](str(h[0]), str(h[1]), event)
## only dispatch event if handler did return event
if e:
self.factory.dispatch(topicUri, e, exclude, eligible)
except:
if self.debugWamp:
log.msg("execption during topic publication handler")
else:
if self.debugWamp:
log.msg("topic %s matches only by prefix and prefix match disallowed" % topicUri)
else:
if self.debugWamp:
log.msg("no topic / publication handler registered for %s" % topicUri)
## Define prefix to be used in CURIEs
##
elif obj[0] == WampProtocol.MESSAGE_TYPEID_PREFIX:
prefix = obj[1]
uri = obj[2]
self.prefixes.set(prefix, uri)
else:
log.msg("unknown message type")
else:
log.msg("msg not a list")
except Exception, e:
traceback.print_exc()
else:
log.msg("binary message")
class WampServerFactory(WebSocketServerFactory, WampFactory):
"""
Server factory for Wamp RPC/PubSub.
"""
protocol = WampServerProtocol
"""
Twisted protocol used by default for WAMP servers.
"""
def __init__(self, url, debug = False, debugCodePaths = False, debugWamp = False, debugApp = False, externalPort = None):
WebSocketServerFactory.__init__(self, url, protocols = ["wamp"], debug = debug, debugCodePaths = debugCodePaths, externalPort = externalPort)
self.debugWamp = debugWamp
self.debugApp = debugApp
def onClientSubscribed(self, proto, topicUri):
"""
Callback fired when peer was (successfully) subscribed on some topic.
:param proto: Peer protocol instance subscribed.
:type proto: Instance of WampServerProtocol.
:param topicUri: Fully qualified, resolved URI of topic subscribed.
:type topicUri: str
"""
pass
def _subscribeClient(self, proto, topicUri):
"""
INTERNAL METHOD! Called from proto to subscribe client for topic.
"""
if not self.subscriptions.has_key(topicUri):
self.subscriptions[topicUri] = set()
if self.debugWamp:
log.msg("subscriptions map created for topic %s" % topicUri)
if not proto in self.subscriptions[topicUri]:
self.subscriptions[topicUri].add(proto)
if self.debugWamp:
log.msg("subscribed peer %s on topic %s" % (proto.peerstr, topicUri))
self.onClientSubscribed(proto, topicUri)
else:
if self.debugWamp:
log.msg("peer %s already subscribed on topic %s" % (proto.peerstr, topicUri))
def onClientUnsubscribed(self, proto, topicUri):
"""
Callback fired when peer was (successfully) unsubscribed from some topic.
:param proto: Peer protocol instance unsubscribed.
:type proto: Instance of WampServerProtocol.
:param topicUri: Fully qualified, resolved URI of topic unsubscribed.
:type topicUri: str
"""
pass
def _unsubscribeClient(self, proto, topicUri = None):
"""
INTERNAL METHOD! Called from proto to unsubscribe client from topic.
"""
if topicUri:
if self.subscriptions.has_key(topicUri) and proto in self.subscriptions[topicUri]:
self.subscriptions[topicUri].discard(proto)
if self.debugWamp:
log.msg("unsubscribed peer %s from topic %s" % (proto.peerstr, topicUri))
if len(self.subscriptions[topicUri]) == 0:
del self.subscriptions[topicUri]
if self.debugWamp:
log.msg("topic %s removed from subscriptions map - no one subscribed anymore" % topicUri)
self.onClientUnsubscribed(proto, topicUri)
else:
if self.debugWamp:
log.msg("peer %s not subscribed on topic %s" % (proto.peerstr, topicUri))
else:
for topicUri, subscribers in self.subscriptions.items():
if proto in subscribers:
subscribers.discard(proto)
if self.debugWamp:
log.msg("unsubscribed peer %s from topic %s" % (proto.peerstr, topicUri))
if len(subscribers) == 0:
del self.subscriptions[topicUri]
if self.debugWamp:
log.msg("topic %s removed from subscriptions map - no one subscribed anymore" % topicUri)
self.onClientUnsubscribed(proto, topicUri)
if self.debugWamp:
log.msg("unsubscribed peer %s from all topics" % (proto.peerstr))
def dispatch(self, topicUri, event, exclude = [], eligible = None):
"""
Dispatch an event to all peers subscribed to the event topic.
:param topicUri: Topic to publish event to.
:type topicUri: str
:param event: Event to publish (must be JSON serializable).
:type event: obj
:param exclude: List of WampServerProtocol instances to exclude from receivers.
:type exclude: List of obj
:param eligible: List of WampServerProtocol instances eligible as receivers (or None for all).
:type eligible: List of obj
:returns twisted.internet.defer.Deferred -- Will be fired when event was
dispatched to all subscribers. The return value provided to the deferred
is a pair (delivered, requested), where delivered = number of actual
receivers, and requested = number of (subscribers - excluded) & eligible.
"""
if self.debugWamp:
log.msg("publish event %s for topicUri %s" % (str(event), topicUri))
d = Deferred()
if self.subscriptions.has_key(topicUri) and len(self.subscriptions[topicUri]) > 0:
## FIXME: this might break ordering of event delivery from a
## receiver perspective. We might need to have send queues
## per receiver OR do recvs = deque(sorted(..))
## However, see http://twistedmatrix.com/trac/ticket/1396
if eligible is not None:
subscrbs = set(eligible) & self.subscriptions[topicUri]
else:
subscrbs = self.subscriptions[topicUri]
if len(exclude) > 0:
recvs = subscrbs - set(exclude)
else:
recvs = subscrbs
l = len(recvs)
if l > 0:
## ok, at least 1 subscriber not excluded and eligible
## => prepare message for mass sending
##
o = [WampProtocol.MESSAGE_TYPEID_EVENT, topicUri, event]
try:
msg = self._serialize(o)
if self.debugWamp:
log.msg("serialized event msg: " + str(msg))
except Exception, e:
raise Exception("invalid type for event - serialization failed [%s]" % e)
preparedMsg = self.prepareMessage(msg)
## chunked sending of prepared message
##
self._sendEvents(preparedMsg, recvs.copy(), 0, l, d)
else:
## receivers list empty after considering exlude and eligible sessions
##
d.callback((0, 0))
else:
## no one subscribed on topic
##
d.callback((0, 0))
return d
def _sendEvents(self, preparedMsg, recvs, delivered, requested, d):
"""
INTERNAL METHOD! Delivers events to receivers in chunks and
reenters the reactor in-between, so that other stuff can run.
"""
## deliver a batch of events
done = False
for i in xrange(0, 256):
try:
proto = recvs.pop()
if proto.state == WebSocketProtocol.STATE_OPEN:
try:
proto.sendPreparedMessage(preparedMsg)
except:
pass
else:
if self.debugWamp:
log.msg("delivered event to peer %s" % proto.peerstr)
delivered += 1
except KeyError:
# all receivers done
done = True
break
if not done:
## if there are receivers left, redo
reactor.callLater(0, self._sendEvents, preparedMsg, recvs, delivered, requested, d)
else:
## else fire final result
d.callback((delivered, requested))
def _addSession(self, proto, session_id):
"""
INTERNAL METHOD! Add proto for session ID.
"""
if not self.protoToSessions.has_key(proto):
self.protoToSessions[proto] = session_id
else:
raise Exception("logic error - dublicate _addSession for protoToSessions")
if not self.sessionsToProto.has_key(session_id):
self.sessionsToProto[session_id] = proto
else:
raise Exception("logic error - dublicate _addSession for sessionsToProto")
def _removeSession(self, proto):
"""
INTERNAL METHOD! Remove session by proto.
"""
if self.protoToSessions.has_key(proto):
session_id = self.protoToSessions[proto]
del self.protoToSessions[proto]
if self.sessionsToProto.has_key(session_id):
del self.sessionsToProto[session_id]
def sessionIdToProto(self, sessionId):
"""
Map WAMP session ID to connected protocol instance (object of type WampServerProtocol).
:param sessionId: WAMP session ID to be mapped.
:type sessionId: str
:returns obj -- WampServerProtocol instance or None.
"""
return self.sessionsToProto.get(sessionId, None)
def sessionIdsToProtos(self, sessionIds):
"""
Map WAMP session IDs to connected protocol instances (objects of type WampServerProtocol).
:param sessionIds: List of session IDs to be mapped.
:type sessionIds: list of str
:returns list -- List of WampServerProtocol instances corresponding to the WAMP session IDs.
"""
protos = []
for s in sessionIds:
if self.sessionsToProto.has_key(s):
protos.append(self.sessionsToProto[s])
return protos
def protoToSessionId(self, proto):
"""
Map connected protocol instance (object of type WampServerProtocol) to WAMP session ID.
:param proto: Instance of WampServerProtocol to be mapped.
:type proto: obj of WampServerProtocol
:returns str -- WAMP session ID or None.
"""
return self.protoToSessions.get(proto, None)
def protosToSessionIds(self, protos):
"""
Map connected protocol instances (objects of type WampServerProtocol) to WAMP session IDs.
:param protos: List of instances of WampServerProtocol to be mapped.
:type protos: list of WampServerProtocol
:returns list -- List of WAMP session IDs corresponding to the protos.
"""
sessionIds = []
for p in protos:
if self.protoToSessions.has_key(p):
sessionIds.append(self.protoToSessions[p])
return sessionIds
def startFactory(self):
"""
Called by Twisted when the factory starts up. When overriding, make
sure to call the base method.
"""
if self.debugWamp:
log.msg("WampServerFactory starting")
self.subscriptions = {}
self.protoToSessions = {}
self.sessionsToProto = {}
def stopFactory(self):
"""
Called by Twisted when the factory shuts down. When overriding, make
sure to call the base method.
"""
if self.debugWamp:
log.msg("WampServerFactory stopped")
class WampClientProtocol(WebSocketClientProtocol, WampProtocol):
"""
Twisted client protocol for WAMP.
"""
def onSessionOpen(self):
"""
Callback fired when WAMP session was fully established. Override
in derived class.
"""
pass
def onOpen(self):
## do nothing here .. onSessionOpen is only fired when welcome
## message was received (and thus session ID set)
pass
def onConnect(self, connectionResponse):
if connectionResponse.protocol not in self.factory.protocols:
raise Exception("server does not speak WAMP")
def connectionMade(self):
WebSocketClientProtocol.connectionMade(self)
WampProtocol.connectionMade(self)
self.calls = {}
self.subscriptions = {}
def connectionLost(self, reason):
WampProtocol.connectionLost(self, reason)
WebSocketClientProtocol.connectionLost(self, reason)
def sendMessage(self, payload):
if self.debugWamp:
log.msg("TX WAMP: %s" % str(payload))
WebSocketClientProtocol.sendMessage(self, payload)
def onMessage(self, msg, binary):
"""Internal method to handle WAMP messages received from WAMP server."""
## WAMP is text message only
##
if binary:
self._protocolError("binary WebSocket message received")
return
if self.debugWamp:
log.msg("RX WAMP: %s" % str(msg))
## WAMP is proper JSON payload
##
try:
obj = self.factory._unserialize(msg)
except Exception, e:
self._protocolError("WAMP message payload could not be unserialized [%s]" % e)
return
## Every WAMP message is a list
##
if type(obj) != list:
self._protocolError("WAMP message payload not a list")
return
## Every WAMP message starts with an integer for message type
##
if len(obj) < 1:
self._protocolError("WAMP message without message type")
return
if type(obj[0]) != int:
self._protocolError("WAMP message type not an integer")
return
## WAMP message type
##
msgtype = obj[0]
## Valid WAMP message types received by WAMP clients
##
if msgtype not in [WampProtocol.MESSAGE_TYPEID_WELCOME,
WampProtocol.MESSAGE_TYPEID_CALL_RESULT,
WampProtocol.MESSAGE_TYPEID_CALL_ERROR,
WampProtocol.MESSAGE_TYPEID_EVENT]:
self._protocolError("invalid WAMP message type %d" % msgtype)
return
## WAMP CALL_RESULT / CALL_ERROR
##
if msgtype in [WampProtocol.MESSAGE_TYPEID_CALL_RESULT, WampProtocol.MESSAGE_TYPEID_CALL_ERROR]:
## Call ID
##
if len(obj) < 2:
self._protocolError("WAMP CALL_RESULT/CALL_ERROR message without <callid>")
return
if type(obj[1]) not in [unicode, str]:
self._protocolError("WAMP CALL_RESULT/CALL_ERROR message with invalid type %s for <callid>" % type(obj[1]))
return
callid = str(obj[1])
## Pop and process Call Deferred
##
d = self.calls.pop(callid, None)
if d:
## WAMP CALL_RESULT
##
if msgtype == WampProtocol.MESSAGE_TYPEID_CALL_RESULT:
## Call Result
##
if len(obj) != 3:
self._protocolError("WAMP CALL_RESULT message with invalid length %d" % len(obj))
return
result = obj[2]
## Fire Call Success Deferred
##
d.callback(result)
## WAMP CALL_ERROR
##
elif msgtype == WampProtocol.MESSAGE_TYPEID_CALL_ERROR:
if len(obj) not in [4, 5]:
self._protocolError("call error message invalid length %d" % len(obj))
return
## Error URI
##
if type(obj[2]) not in [unicode, str]:
self._protocolError("invalid type %s for errorUri in call error message" % str(type(obj[2])))
return
erroruri = str(obj[2])
## Error Description
##
if type(obj[3]) not in [unicode, str]:
self._protocolError("invalid type %s for errorDesc in call error message" % str(type(obj[3])))
return
errordesc = str(obj[3])
## Error Details
##
if len(obj) > 4:
errordetails = obj[4]
else:
errordetails = None
## Fire Call Error Deferred
##
e = Exception()
e.args = (erroruri, errordesc, errordetails)
d.errback(e)
else:
raise Exception("logic error")
else:
if self.debugWamp:
log.msg("callid not found for received call result/error message")
## WAMP EVENT
##
elif msgtype == WampProtocol.MESSAGE_TYPEID_EVENT:
## Topic
##
if len(obj) != 3:
self._protocolError("WAMP EVENT message invalid length %d" % len(obj))
return
if type(obj[1]) not in [unicode, str]:
self._protocolError("invalid type for <topic> in WAMP EVENT message")
return
unresolvedTopicUri = str(obj[1])
topicUri = self.prefixes.resolveOrPass(unresolvedTopicUri)
## Fire PubSub Handler
##
if self.subscriptions.has_key(topicUri):
event = obj[2]
self.subscriptions[topicUri](topicUri, event)
else:
## event received for non-subscribed topic (could be because we
## just unsubscribed, and server already sent out event for
## previous subscription)
pass
## WAMP WELCOME
##
elif msgtype == WampProtocol.MESSAGE_TYPEID_WELCOME:
## Session ID
##
if len(obj) < 2:
self._protocolError("WAMP WELCOME message invalid length %d" % len(obj))
return
if type(obj[1]) not in [unicode, str]:
self._protocolError("invalid type for <sessionid> in WAMP WELCOME message")
return
self.session_id = str(obj[1])
## WAMP Protocol Version
##
if len(obj) > 2:
if type(obj[2]) not in [int]:
self._protocolError("invalid type for <version> in WAMP WELCOME message")
return
else:
self.session_protocol_version = obj[2]
else:
self.session_protocol_version = None
## Server Ident
##
if len(obj) > 3:
if type(obj[3]) not in [unicode, str]:
self._protocolError("invalid type for <server> in WAMP WELCOME message")
return
else:
self.session_server = obj[3]
else:
self.session_server = None
self.onSessionOpen()
else:
raise Exception("logic error")
def call(self, *args):
"""
Perform a remote-procedure call (RPC). The first argument is the procedure
URI (mandatory). Subsequent positional arguments can be provided (must be
JSON serializable). The return value is a Twisted Deferred.
"""
if len(args) < 1:
raise Exception("missing procedure URI")
if type(args[0]) not in [unicode, str]:
raise Exception("invalid type for procedure URI")
procuri = args[0]
while True:
callid = newid()
if not self.calls.has_key(callid):
break
d = Deferred()
self.calls[callid] = d
msg = [WampProtocol.MESSAGE_TYPEID_CALL, callid, procuri]
msg.extend(args[1:])
try:
o = self.factory._serialize(msg)
except:
raise Exception("call argument(s) not JSON serializable")
self.sendMessage(o)
return d
def prefix(self, prefix, uri):
"""
Establishes a prefix to be used in CURIEs instead of URIs having that
prefix for both client-to-server and server-to-client messages.
:param prefix: Prefix to be used in CURIEs.
:type prefix: str
:param uri: URI that this prefix will resolve to.
:type uri: str
"""
if type(prefix) != str:
raise Exception("invalid type for prefix")
if type(uri) not in [unicode, str]:
raise Exception("invalid type for URI")
if self.prefixes.get(prefix):
raise Exception("prefix already defined")
self.prefixes.set(prefix, uri)
msg = [WampProtocol.MESSAGE_TYPEID_PREFIX, prefix, uri]
self.sendMessage(self.factory._serialize(msg))
def publish(self, topicUri, event, excludeMe = None, exclude = None, eligible = None):
"""
Publish an event under a topic URI. The latter may be abbreviated using a
CURIE which has been previously defined using prefix(). The event must
be JSON serializable.
:param topicUri: The topic URI or CURIE.
:type topicUri: str
:param event: Event to be published (must be JSON serializable) or None.
:type event: value
:param excludeMe: When True, don't deliver the published event to myself (when I'm subscribed).
:type excludeMe: bool
:param exclude: Optional list of session IDs to exclude from receivers.
:type exclude: list of str
:param eligible: Optional list of session IDs to that are eligible as receivers.
:type eligible: list of str
"""
if type(topicUri) not in [unicode, str]:
raise Exception("invalid type for parameter 'topicUri' - must be string (was %s)" % type(topicUri))
if excludeMe is not None:
if type(excludeMe) != bool:
raise Exception("invalid type for parameter 'excludeMe' - must be bool (was %s)" % type(excludeMe))
if exclude is not None:
if type(exclude) != list:
raise Exception("invalid type for parameter 'exclude' - must be list (was %s)" % type(exclude))
if eligible is not None:
if type(eligible) != list:
raise Exception("invalid type for parameter 'eligible' - must be list (was %s)" % type(eligible))
if exclude is not None or eligible is not None:
if exclude is None:
if excludeMe is not None:
if excludeMe:
exclude = [self.session_id]
else:
exclude = []
else:
exclude = [self.session_id]
if eligible is not None:
msg = [WampProtocol.MESSAGE_TYPEID_PUBLISH, topicUri, event, exclude, eligible]
else:
msg = [WampProtocol.MESSAGE_TYPEID_PUBLISH, topicUri, event, exclude]
else:
if excludeMe:
msg = [WampProtocol.MESSAGE_TYPEID_PUBLISH, topicUri, event]
else:
msg = [WampProtocol.MESSAGE_TYPEID_PUBLISH, topicUri, event, excludeMe]
try:
o = self.factory._serialize(msg)
except:
raise Exception("invalid type for parameter 'event' - not JSON serializable")
self.sendMessage(o)
def subscribe(self, topicUri, handler):
"""
Subscribe to topic. When already subscribed, will overwrite the handler.
:param topicUri: URI or CURIE of topic to subscribe to.
:type topicUri: str
:param handler: Event handler to be invoked upon receiving events for topic.
:type handler: Python callable, will be called as in <callable>(eventUri, event).
"""
if type(topicUri) not in [unicode, str]:
raise Exception("invalid type for parameter 'topicUri' - must be string (was %s)" % type(topicUri))
if type(handler) not in [types.FunctionType, types.MethodType, types.BuiltinFunctionType, types.BuiltinMethodType]:
raise Exception("invalid type for parameter 'handler' - must be a callable (was %s)" % type(handler))
turi = self.prefixes.resolveOrPass(topicUri)
if not self.subscriptions.has_key(turi):
msg = [WampProtocol.MESSAGE_TYPEID_SUBSCRIBE, topicUri]
o = self.factory._serialize(msg)
self.sendMessage(o)
self.subscriptions[turi] = handler
def unsubscribe(self, topicUri):
"""
Unsubscribe from topic. Will do nothing when currently not subscribed to the topic.
:param topicUri: URI or CURIE of topic to unsubscribe from.
:type topicUri: str
"""
if type(topicUri) not in [unicode, str]:
raise Exception("invalid type for parameter 'topicUri' - must be string (was %s)" % type(topicUri))
turi = self.prefixes.resolveOrPass(topicUri)
if self.subscriptions.has_key(turi):
msg = [WampProtocol.MESSAGE_TYPEID_UNSUBSCRIBE, topicUri]
o = self.factory._serialize(msg)
self.sendMessage(o)
del self.subscriptions[turi]
class WampClientFactory(WebSocketClientFactory, WampFactory):
"""
Twisted client factory for WAMP.
"""
protocol = WampClientProtocol
def __init__(self, url, debug = False, debugCodePaths = False, debugWamp = False, debugApp = False):
WebSocketClientFactory.__init__(self, url, protocols = ["wamp"], debug = debug, debugCodePaths = debugCodePaths)
self.debugWamp = debugWamp
self.debugApp = debugApp
def startFactory(self):
"""
Called by Twisted when the factory starts up. When overriding, make
sure to call the base method.
"""
if self.debugWamp:
log.msg("WebSocketClientFactory starting")
def stopFactory(self):
"""
Called by Twisted when the factory shuts down. When overriding, make
sure to call the base method.
"""
if self.debugWamp:
log.msg("WebSocketClientFactory stopped")
class WampCraProtocol(WampProtocol):
"""
Base class for WAMP Challenge-Response Authentication protocols (client and server).
WAMP-CRA is a cryptographically strong challenge response authentication
protocol based on HMAC-SHA256.
The protocol performs in-band authentication of WAMP clients to WAMP servers.
WAMP-CRA does not introduce any new WAMP protocol level message types, but
implements the authentication handshake via standard WAMP RPCs with well-known
procedure URIs and signatures.
"""
def authSignature(self, authChallenge, authSecret = None, authExtra=None):
"""
Compute the authentication signature from an authentication challenge and a secret.
:param authChallenge: The authentication challenge.
:type authChallenge: str
:param authSecret: The authentication secret.
:type authSecret: str
:authExtra: Extra authentication information for salting the secret. (salt, keylen,
iterations)
:type authExtra: dict
:returns str -- The authentication signature.
"""
if authSecret is None:
authSecret = ""
if authExtra is not None:
authSalt = authExtra.get('salt')
keylen = authExtra.get('keylen', 32)
iterations = authExtra.get('iterations', 10000)
kdf = PBKDF2(authSecret, authSalt, iterations, hashlib.sha256, hmac)
b = kdf.hexread(keylen)
authSecret = binascii.b2a_base64(b).strip()
h = hmac.new(authSecret, authChallenge, hashlib.sha256)
sig = binascii.b2a_base64(h.digest()).strip()
return sig
class WampCraClientProtocol(WampClientProtocol, WampCraProtocol):
"""
Simple, authenticated WAMP client protocol.
The client can perform WAMP-Challenge-Response-Authentication ("WAMP-CRA") to authenticate
itself to a WAMP server. The server needs to implement WAMP-CRA also of course.
"""
def authenticate(self, authKey = None, authExtra = None, authSecret = None):
"""
Authenticate the WAMP session to server.
:param authKey: The key of the authentication credentials, something like a user or application name.
:type authKey: str
:param authExtra: Any extra authentication information.
:type authExtra: dict
:param authSecret: The secret of the authentication credentials, something like the user password or application secret key.
:type authsecret: str
:returns Deferred -- Deferred that fires upon authentication success (with permissions) or failure.
"""
def _onAuthChallenge(challenge):
if authKey is not None:
challengeObj = self.factory._unserialize(challenge)
if 'authextra' in challengeObj:
authExtra = challengeObj['authextra']
sig = self.authSignature(challenge, authSecret, authExtra)
else:
sig = self.authSignature(challenge, authSecret)
else:
sig = None
d = self.call(WampProtocol.URI_WAMP_PROCEDURE + "auth", sig)
return d
d = self.call(WampProtocol.URI_WAMP_PROCEDURE + "authreq", authKey, authExtra)
d.addCallback(_onAuthChallenge)
return d
class WampCraServerProtocol(WampServerProtocol, WampCraProtocol):
"""
Simple, authenticating WAMP server protocol.
The server lets clients perform WAMP-Challenge-Response-Authentication ("WAMP-CRA")
to authenticate. The clients need to implement WAMP-CRA also of course.
To implement an authenticating server, override:
* getAuthSecret
* getAuthPermissions
* onAuthenticated
in your class deriving from this class.
"""
clientAuthTimeout = 0
"""
Client authentication timeout in seconds or 0 for infinite. A client
must perform authentication after the initial WebSocket handshake within
this timeout or the connection is failed.
"""
clientAuthAllowAnonymous = True
"""
Allow anonymous client authentication. When this is set to True, a client
may "authenticate" as anonymous.
"""
def getAuthPermissions(self, authKey, authExtra):
"""
Get the permissions the session is granted when the authentication succeeds
for the given key / extra information.
Override in derived class to implement your authentication.
A permissions object is structured like this::
{'permissions': {'rpc': [
{'uri': / RPC Endpoint URI - String /,
'call': / Allow to call? - Boolean /}
],
'pubsub': [
{'uri': / PubSub Topic URI / URI prefix - String /,
'prefix': / URI matched by prefix? - Boolean /,
'pub': / Allow to publish? - Boolean /,
'sub': / Allow to subscribe? - Boolean /}
]
}
}
You can add custom information to this object. The object will be provided again
when the client authentication succeeded in :meth:`onAuthenticated`.
:param authKey: The authentication key.
:type authKey: str
:param authExtra: Authentication extra information.
:type authExtra: dict
:returns obj or Deferred -- Return a permissions object or None when no permissions granted.
"""
return None
def getAuthSecret(self, authKey):
"""
Get the authentication secret for an authentication key, i.e. the
user password for the user name. Return None when the authentication
key does not exist.
Override in derived class to implement your authentication.
:param authKey: The authentication key.
:type authKey: str
:returns str or Deferred -- The authentication secret for the key or None when the key does not exist.
"""
return None
def onAuthTimeout(self):
"""
Fired when the client does not authenticate itself in time. The default implementation
will simply fail the connection.
May be overridden in derived class.
"""
if not self._clientAuthenticated:
log.msg("failing connection upon client authentication timeout [%s secs]" % self.clientAuthTimeout)
self.failConnection()
def onAuthenticated(self, permissions):
"""
Fired when client authentication was successful.
Override in derived class and register PubSub topics and/or RPC endpoints.
:param permissions: The permissions object returned from :meth:`getAuthPermissions`.
:type permissions: obj
"""
pass
def registerForPubSubFromPermissions(self, permissions):
"""
Register topics for PubSub from auth permissions.
:param permissions: The permissions granted to the now authenticated client.
:type permissions: list
"""
for p in permissions['pubsub']:
## register topics for the clients
##
pubsub = (WampServerProtocol.PUBLISH if p['pub'] else 0) | \
(WampServerProtocol.SUBSCRIBE if p['sub'] else 0)
topic = p['uri']
if self.pubHandlers.has_key(topic) or self.subHandlers.has_key(topic):
## FIXME: handle dups!
log.msg("DUPLICATE TOPIC PERMISSION !!! " + topic)
self.registerForPubSub(topic, p['prefix'], pubsub)
def onSessionOpen(self):
"""
Called when WAMP session has been established, but not yet authenticated. The default
implementation will prepare the session allowing the client to authenticate itself.
"""
## register RPC endpoints for WAMP-CRA authentication
##
self.registerForRpc(self, WampProtocol.URI_WAMP_PROCEDURE, [WampCraServerProtocol.authRequest,
WampCraServerProtocol.auth])
## reset authentication state
##
self._clientAuthenticated = False
self._clientPendingAuth = None
self._clientAuthTimeoutCall = None
## client authentication timeout
##
if self.clientAuthTimeout > 0:
self._clientAuthTimeoutCall = reactor.callLater(self.clientAuthTimeout, self.onAuthTimeout)
@exportRpc("authreq")
def authRequest(self, authKey = None, extra = None):
"""
RPC endpoint for clients to initiate the authentication handshake.
:param authKey: Authentication key, such as user name or application name.
:type authKey: str
:param extra: Authentication extra information.
:type extra: dict
:returns str -- Authentication challenge. The client will need to create an authentication signature from this.
"""
## check authentication state
##
if self._clientAuthenticated:
raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "already-authenticated"), "already authenticated")
if self._clientPendingAuth is not None:
raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "authentication-already-requested"), "authentication request already issues - authentication pending")
## check extra
##
if extra:
if type(extra) != dict:
raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "invalid-argument"), "extra not a dictionary (was %s)." % str(type(extra)))
else:
extra = {}
#for k in extra:
# if type(extra[k]) not in [str, unicode, int, long, float, bool, types.NoneType]:
# raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "invalid-argument"), "attribute '%s' in extra not a primitive type (was %s)" % (k, str(type(extra[k]))))
## check authKey
##
if authKey is None and not self.clientAuthAllowAnonymous:
raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "anonymous-auth-forbidden"), "authentication as anonymous forbidden")
if type(authKey) not in [str, unicode, types.NoneType]:
raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "invalid-argument"), "authentication key must be a string (was %s)" % str(type(authKey)))
d = maybeDeferred(self.getAuthSecret, authKey)
def onGetAuthSecretOk(authSecret, authKey, extra):
if authKey is not None and authSecret is None:
raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "no-such-authkey"), "authentication key '%s' does not exist." % authKey)
## each authentication request gets a unique authid, which can only be used (later) once!
##
authid = newid()
## create authentication challenge
##
info = {}
info['authid'] = authid
info['authkey'] = authKey
info['timestamp'] = utcnow()
info['sessionid'] = self.session_id
info['extra'] = extra
pp = maybeDeferred(self.getAuthPermissions, authKey, extra)
def onAuthPermissionsOk(res):
if res is None:
res = {'permissions': {}}
res['permissions'] = {'pubsub': [], 'rpc': []}
info['permissions'] = res['permissions']
if 'authextra' in res:
info['authextra'] = res['authextra']
if authKey:
## authenticated session
##
infoser = self.factory._serialize(info)
sig = self.authSignature(infoser, authSecret)
self._clientPendingAuth = (info, sig, res)
return infoser
else:
## anonymous session
##
self._clientPendingAuth = (info, None, res)
return None
def onAuthPermissionsError(e):
raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "auth-permissions-error"), str(e))
pp.addCallbacks(onAuthPermissionsOk, onAuthPermissionsError)
return pp
d.addCallback(onGetAuthSecretOk, authKey, extra)
return d
@exportRpc("auth")
def auth(self, signature = None):
"""
RPC endpoint for clients to actually authenticate after requesting authentication and computing
a signature from the authentication challenge.
:param signature: Authentication signature computed by the client.
:type signature: str
:returns list -- A list of permissions the client is granted when authentication was successful.
"""
## check authentication state
##
if self._clientAuthenticated:
raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "already-authenticated"), "already authenticated")
if self._clientPendingAuth is None:
raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "no-authentication-requested"), "no authentication previously requested")
## check signature
##
if type(signature) not in [str, unicode, types.NoneType]:
raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "invalid-argument"), "signature must be a string or None (was %s)" % str(type(signature)))
if self._clientPendingAuth[1] != signature:
## delete pending authentication, so that no retries are possible. authid is only valid for 1 try!!
## FIXME: drop the connection?
self._clientPendingAuth = None
raise Exception(self.shrink(WampProtocol.URI_WAMP_ERROR + "invalid-signature"), "signature for authentication request is invalid")
## at this point, the client has successfully authenticated!
## get the permissions we determined earlier
##
perms = self._clientPendingAuth[2]
## delete auth request and mark client as authenticated
##
authKey = self._clientPendingAuth[0]['authkey']
self._clientAuthenticated = True
self._clientPendingAuth = None
if self._clientAuthTimeoutCall is not None:
self._clientAuthTimeoutCall.cancel()
self._clientAuthTimeoutCall = None
## fire authentication callback
##
self.onAuthenticated(authKey, perms)
## return permissions to client
##
return perms['permissions']
|