Python Library Socket Protocol
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

__init__.py 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. socket_protocol (Socket Protocol)
  6. =================================
  7. **Author:**
  8. * Dirk Alders <sudo-dirk@mount-mockery.de>
  9. **Description:**
  10. This Module supports point to point communication for client-server issues.
  11. **Submodules:**
  12. * :class:`socket_protocol.data_storage`
  13. * :class:`socket_protocol.pure_json_protocol`
  14. * :class:`socket_protocol.struct_json_protocol`
  15. **Unittest:**
  16. See also the :download:`unittest <socket_protocol/_testresults_/unittest.pdf>` documentation.
  17. **Module Documentation:**
  18. """
  19. __DEPENDENCIES__ = ['stringtools']
  20. import stringtools
  21. import binascii
  22. import hashlib
  23. import json
  24. import logging
  25. import os
  26. import struct
  27. import sys
  28. import time
  29. try:
  30. from config import APP_NAME as ROOT_LOGGER_NAME
  31. except ImportError:
  32. ROOT_LOGGER_NAME = 'root'
  33. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  34. __DESCRIPTION__ = """The Module {\\tt %s} is designed for point to point communication for client-server issues.
  35. For more Information read the sphinx documentation.""" % __name__.replace('_', '\_')
  36. """The Module Description"""
  37. __INTERPRETER__ = (2, 3)
  38. """The Tested Interpreter-Versions"""
  39. SID_AUTH_REQUEST = 0
  40. """SID for authentification request"""
  41. SID_AUTH_RESPONSE = 1
  42. """SID for authentification response"""
  43. DID_AUTH_SEED = 0
  44. """DID for authentification (seed)"""
  45. DID_AUTH_KEY = 1
  46. """DID for authentification (key)"""
  47. SID_CHANNEL_NAME_REQUEST = 8
  48. """SID for channel name exchange request """
  49. SID_CHANNEL_NAME_RESPONSE = 9
  50. """SID for channel name exchange response"""
  51. DID_CHANNEL_NAME = 0
  52. """DID for channel name """
  53. SID_READ_REQUEST = 10
  54. """SID for a read data request"""
  55. SID_READ_RESPONSE = 11
  56. """SID for read data response"""
  57. SID_WRITE_REQUEST = 20
  58. """SID for a write data request"""
  59. SID_WRITE_RESPONSE = 21
  60. """SID for a write data response"""
  61. SID_EXECUTE_REQUEST = 30
  62. """SID for a execute request"""
  63. SID_EXECUTE_RESPONSE = 31
  64. """SID for a execute response"""
  65. STATUS_OKAY = 0
  66. """Status for 'okay'"""
  67. STATUS_BUFFERING_UNHANDLED_REQUEST = 1
  68. """Status for 'unhandled request'"""
  69. STATUS_CALLBACK_ERROR = 2
  70. """Status for 'callback errors'"""
  71. STATUS_AUTH_REQUIRED = 3
  72. """Status for 'authentification is required'"""
  73. STATUS_SERVICE_OR_DATA_UNKNOWN = 4
  74. """Status for 'service or data unknown'"""
  75. STATUS_CHECKSUM_ERROR = 5
  76. """Status for 'checksum error'"""
  77. STATUS_OPERATION_NOT_PERMITTED = 6
  78. """Status for 'operation not permitted'"""
  79. STATUS_LOG_LVL = {
  80. STATUS_OKAY: logging.INFO,
  81. STATUS_BUFFERING_UNHANDLED_REQUEST: logging.WARNING,
  82. STATUS_CALLBACK_ERROR: logging.ERROR,
  83. STATUS_AUTH_REQUIRED: logging.WARNING,
  84. STATUS_SERVICE_OR_DATA_UNKNOWN: logging.ERROR,
  85. STATUS_CHECKSUM_ERROR: logging.ERROR,
  86. STATUS_OPERATION_NOT_PERMITTED: logging.WARNING,
  87. }
  88. """Status depending log level for messages"""
  89. AUTH_STATE_UNTRUSTED_CONNECTION = 0
  90. """Authentification Status for an 'Untrusted Connection'"""
  91. AUTH_STATE_SEED_REQUESTED = 1
  92. """Authentification Status for 'Seed was requested'"""
  93. AUTH_STATE_SEED_TRANSFERRED = 2
  94. """Authentification Status for 'Seed has been sent'"""
  95. AUTH_STATE_KEY_TRANSFERRED = 3
  96. """Authentification Status for 'Key has been sent'"""
  97. AUTH_STATE_TRUSTED_CONNECTION = 4
  98. """Authentification Status for a 'Trusted Connection'"""
  99. AUTH_STATE__NAMES = {AUTH_STATE_UNTRUSTED_CONNECTION: 'Untrusted Connection',
  100. AUTH_STATE_SEED_REQUESTED: 'Seed was requested',
  101. AUTH_STATE_SEED_TRANSFERRED: 'Seed has been sent',
  102. AUTH_STATE_KEY_TRANSFERRED: 'Key has been sent',
  103. AUTH_STATE_TRUSTED_CONNECTION: 'Trusted Connection'}
  104. """Authentification Status names for previous defined authentification states"""
  105. class RequestSidExistsError(Exception):
  106. pass
  107. class ResponseSidExistsError(Exception):
  108. pass
  109. class _callback_storage(dict):
  110. DEFAULT_CHANNEL_NAME = 'all_others'
  111. def __init__(self, channel_name, log_prefix):
  112. self.init_channel_name(channel_name)
  113. self.__log_prefix__ = log_prefix
  114. dict.__init__(self)
  115. def init_channel_name(self, channel_name):
  116. if channel_name is None:
  117. self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__ + '.' + self.DEFAULT_CHANNEL_NAME)
  118. else:
  119. self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__ + '.' + channel_name)
  120. def get(self, service_id, data_id):
  121. if dict.get(self, service_id, {}).get(data_id, None) is not None:
  122. return self[service_id][data_id]
  123. elif dict.get(self, service_id, {}).get(None, None) is not None:
  124. return self[service_id][None]
  125. elif dict.get(self, None, {}).get(data_id, None) is not None:
  126. return self[None][data_id]
  127. elif dict.get(self, None, {}).get(None, None) is not None:
  128. return self[None][None]
  129. else:
  130. return (None, None, None)
  131. def add(self, service_id, data_id, callback, *args, **kwargs):
  132. cb_data = self.get(service_id, data_id)
  133. if dict.get(self, service_id, {}).get(data_id, None) is not None:
  134. if callback is None:
  135. self.logger.warning("%s Deleting existing callback %s for service_id (%s) and data_id (%s)!", self.__log_prefix__(), repr(cb_data[0].__name__), repr(service_id), repr(data_id))
  136. del(self[service_id][data_id])
  137. return
  138. else:
  139. self.logger.warning("%s Overwriting existing callback %s for service_id (%s) and data_id (%s) to %s!", self.__log_prefix__(), repr(cb_data[0].__name__), repr(service_id), repr(data_id), repr(callback.__name__))
  140. else:
  141. self.logger.debug("%s Adding callback %s for SID=%s and DID=%s", self.__log_prefix__(), repr(callback.__name__), repr(service_id), repr(data_id))
  142. if service_id not in self:
  143. self[service_id] = {}
  144. self[service_id][data_id] = (callback, args, kwargs)
  145. class data_storage(dict):
  146. """
  147. This is a storage object for socket_protocol messages.
  148. :param status: The message status.
  149. :type status: int
  150. :param service_id: The Service-ID.
  151. :type service_id: int
  152. :param data_id: The Data-ID.
  153. :type data_id: int
  154. :param data: The transfered data.
  155. :type data: any
  156. """
  157. KEY_STATUS = 'status'
  158. KEY_SERVICE_ID = 'service_id'
  159. KEY_DATA_ID = 'data_id'
  160. KEY_DATA = 'data'
  161. ALL_KEYS = [KEY_DATA, KEY_DATA_ID, KEY_SERVICE_ID, KEY_STATUS]
  162. def __init__(self, *args, **kwargs):
  163. dict.__init__(self, *args, **kwargs)
  164. for key in self.ALL_KEYS:
  165. if key not in self:
  166. self[key] = None
  167. def get_status(self, default=None):
  168. """
  169. This Method returns the message status.
  170. :param default: The default value, if no data is available.
  171. """
  172. return self.get(self.KEY_STATUS, default)
  173. def get_service_id(self, default=None):
  174. """
  175. This Method returns the message Service-ID.
  176. :param default: The default value, if no data is available.
  177. """
  178. return self.get(self.KEY_SERVICE_ID, default)
  179. def get_data_id(self, default=None):
  180. """
  181. This Method returns the message Data-ID.
  182. :param default: The default value, if no data is available.
  183. """
  184. return self.get(self.KEY_DATA_ID, default)
  185. def get_data(self, default=None):
  186. """
  187. This Method returns the message data.
  188. :param default: The default value, if no data is available.
  189. """
  190. return self.get(self.KEY_DATA, default)
  191. class pure_json_protocol(object):
  192. """
  193. This `class` supports to transfer a message and it's data.
  194. :param comm_instance: A communication instance.
  195. :type comm_instance: instance
  196. :param secret: An optinal secret (e.g. created by ``binascii.hexlify(os.urandom(24))``).
  197. :type secret: str
  198. :param auto_auth: An optional parameter to enable (True) automatic authentification, otherwise you need to do it manually, if needed.
  199. :type auto_auth: bool
  200. :param channel_name: An optional parameter to set a channel name for logging of the communication.
  201. :type channel_name: str
  202. .. hint::
  203. * The Service-ID is designed to identify the type of the communication (e.g. :const:`READ_REQUEST`, :const:`WRITE_REQUEST`, :const:`READ_RESPONSE`, :const:`WRITE_RESPONSE`, ...)
  204. * The Data-ID is designed to identify the requests / responses using the same Service_ID.
  205. .. note:: The :class:`comm_instance` needs to have at least the following interface:
  206. * A Method :func:`comm_instance.init_channel_name` to set the channel name.
  207. * A Constant :const:`comm_instance.IS_CLIENT` to identify that the :class:`comm_instance` is a client (True) or a server (False).
  208. * A Method :func:`comm_instance.is_connected` to identify if the instance is connected (True) or not (False).
  209. * A Method :func:`comm_instance.reconnect` to initiate a reconnect.
  210. * A Method :func:`comm_instance.register_callback` to register a data available callback.
  211. * A Method :func:`comm_instance.register_connect_callback` to register a connect callback.
  212. * A Method :func:`comm_instance.register_disconnect_callback` to register a disconnect callback.
  213. * A Method :func:`comm_instance.send` to send data via the :class:`comm_instance`.
  214. .. note:: The parameter :const:`auto_auth` is only relevant, if a secret is given and the :class:`comm_instance` is a client. The authentification is initiated directly after the connection is established.
  215. .. note:: The :const:`channel_name`-exchange will be initiated by the client directly after the the connection is established.
  216. * If a channel_name is given at both communication sides and they are different, the client name is taken over and the server will log a warning message.
  217. """
  218. DEFAULT_CHANNEL_NAME = 'all_others'
  219. def __init__(self, comm_instance, secret=None, auto_auth=False, channel_name=None):
  220. self.__comm_inst__ = comm_instance
  221. self.__secret__ = secret
  222. self.__auto_auth__ = auto_auth
  223. #
  224. self.__auth_whitelist__ = {}
  225. self.__sid_response_dict__ = {}
  226. self.__sid_name_dict__ = {}
  227. self.__did_name_dict__ = {}
  228. #
  229. self.__status_name_dict = {}
  230. self.add_status(STATUS_OKAY, 'okay')
  231. self.add_status(STATUS_BUFFERING_UNHANDLED_REQUEST, 'no callback for service, data buffered')
  232. self.add_status(STATUS_CALLBACK_ERROR, 'callback error')
  233. self.add_status(STATUS_AUTH_REQUIRED, 'authentification required')
  234. self.add_status(STATUS_SERVICE_OR_DATA_UNKNOWN, 'service or data unknown')
  235. self.add_status(STATUS_CHECKSUM_ERROR, 'checksum error')
  236. self.add_status(STATUS_OPERATION_NOT_PERMITTED, 'operation not permitted')
  237. #
  238. self.__callbacks__ = _callback_storage(channel_name, self.__log_prefix__)
  239. self.__init_channel_name__(channel_name)
  240. #
  241. self.__clean_receive_buffer__()
  242. self.add_service(SID_AUTH_REQUEST, SID_AUTH_RESPONSE, 'authentification request', 'authentification response')
  243. self.add_data((SID_AUTH_REQUEST, SID_AUTH_RESPONSE), DID_AUTH_SEED, 'seed')
  244. self.add_data(SID_AUTH_REQUEST, DID_AUTH_KEY, 'key')
  245. self.add_data(SID_AUTH_RESPONSE, DID_AUTH_KEY, 'key')
  246. self.add_msg_to_auth_whitelist_(SID_AUTH_REQUEST, DID_AUTH_SEED)
  247. self.add_msg_to_auth_whitelist_(SID_AUTH_RESPONSE, DID_AUTH_SEED)
  248. self.add_msg_to_auth_whitelist_(SID_AUTH_REQUEST, DID_AUTH_KEY)
  249. self.add_msg_to_auth_whitelist_(SID_AUTH_RESPONSE, DID_AUTH_KEY)
  250. self.__callbacks__.add(SID_AUTH_REQUEST, DID_AUTH_SEED, self.__authentificate_create_seed__)
  251. self.__callbacks__.add(SID_AUTH_RESPONSE, DID_AUTH_SEED, self.__authentificate_create_key__)
  252. self.__callbacks__.add(SID_AUTH_REQUEST, DID_AUTH_KEY, self.__authentificate_check_key__)
  253. self.__callbacks__.add(SID_AUTH_RESPONSE, DID_AUTH_KEY, self.__authentificate_process_feedback__)
  254. self.__authentification_state_reset__()
  255. self.add_service(SID_CHANNEL_NAME_REQUEST, SID_CHANNEL_NAME_RESPONSE, 'channel name request', 'channel name response')
  256. self.add_data((SID_CHANNEL_NAME_REQUEST, SID_CHANNEL_NAME_RESPONSE), DID_CHANNEL_NAME, 'name')
  257. self.add_msg_to_auth_whitelist_(SID_CHANNEL_NAME_REQUEST, DID_CHANNEL_NAME)
  258. self.add_msg_to_auth_whitelist_(SID_CHANNEL_NAME_RESPONSE, DID_CHANNEL_NAME)
  259. self.__callbacks__.add(SID_CHANNEL_NAME_REQUEST, DID_CHANNEL_NAME, self.__channel_name_request__)
  260. self.__callbacks__.add(SID_CHANNEL_NAME_RESPONSE, DID_CHANNEL_NAME, self.__channel_name_response__)
  261. self.add_service(SID_READ_REQUEST, SID_READ_RESPONSE, 'read data request', 'read data response')
  262. self.add_service(SID_WRITE_REQUEST, SID_WRITE_RESPONSE, 'write data request', 'write data response')
  263. self.add_service(SID_EXECUTE_REQUEST, SID_EXECUTE_RESPONSE, 'execute request', 'execute response')
  264. self.__seed__ = None
  265. self.__comm_inst__.register_callback(self.__data_available_callback__)
  266. self.__comm_inst__.register_connect_callback(self.__connection_established__)
  267. self.__comm_inst__.register_disconnect_callback(self.__authentification_state_reset__)
  268. logger.info('%s Initialisation finished.', self.__log_prefix__())
  269. def __analyse_frame__(self, frame):
  270. if sys.version_info >= (3, 0):
  271. return data_storage(json.loads(frame[:-4].decode('utf-8')))
  272. else:
  273. return data_storage(json.loads(frame[:-4]))
  274. def __authentificate_check_key__(self, msg):
  275. key = msg.get_data()
  276. if key == self.__authentificate_salt_and_hash__(self.__seed__):
  277. self.__authentification_state__ = AUTH_STATE_TRUSTED_CONNECTION
  278. return STATUS_OKAY, True
  279. else:
  280. self.__authentification_state__ = AUTH_STATE_UNTRUSTED_CONNECTION
  281. return STATUS_OKAY, False
  282. def __authentificate_create_key__(self, msg):
  283. self.__authentification_state__ = AUTH_STATE_KEY_TRANSFERRED
  284. seed = msg.get_data()
  285. key = self.__authentificate_salt_and_hash__(seed)
  286. self.send(SID_AUTH_REQUEST, DID_AUTH_KEY, key)
  287. def __authentificate_create_seed__(self, msg):
  288. self.__authentification_state__ = AUTH_STATE_SEED_TRANSFERRED
  289. if sys.version_info >= (3, 0):
  290. self.__seed__ = binascii.hexlify(os.urandom(32)).decode('utf-8')
  291. else:
  292. self.__seed__ = binascii.hexlify(os.urandom(32))
  293. return STATUS_OKAY, self.__seed__
  294. def __authentificate_process_feedback__(self, msg):
  295. feedback = msg.get_data()
  296. if feedback:
  297. self.__authentification_state__ = AUTH_STATE_TRUSTED_CONNECTION
  298. self.logger.info("%s Got positive authentification feedback", self.__log_prefix__())
  299. else:
  300. self.__authentification_state__ = AUTH_STATE_UNTRUSTED_CONNECTION
  301. self.logger.warning("%s Got negative authentification feedback", self.__log_prefix__())
  302. return STATUS_OKAY, None
  303. def __authentificate_salt_and_hash__(self, seed):
  304. if sys.version_info >= (3, 0):
  305. return hashlib.sha512(bytes(seed, 'utf-8') + self.__secret__).hexdigest()
  306. else:
  307. return hashlib.sha512(seed.encode('utf-8') + self.__secret__.encode('utf-8')).hexdigest()
  308. def __authentification_state_reset__(self):
  309. self.logger.info("%s Resetting authentification state to AUTH_STATE_UNTRUSTED_CONNECTION", self.__log_prefix__())
  310. self.__authentification_state__ = AUTH_STATE_UNTRUSTED_CONNECTION
  311. def __authentification_required__(self, service_id, data_id):
  312. return data_id not in self.__auth_whitelist__.get(service_id, [])
  313. def __buffer_received_data__(self, msg):
  314. if not msg.get_service_id() in self.__msg_buffer__:
  315. self.__msg_buffer__[msg.get_service_id()] = {}
  316. if not msg.get_data_id() in self.__msg_buffer__[msg.get_service_id()]:
  317. self.__msg_buffer__[msg.get_service_id()][msg.get_data_id()] = []
  318. self.__msg_buffer__[msg.get_service_id()][msg.get_data_id()].append(msg)
  319. self.logger.debug("%s Message data is stored in buffer and is now ready to be retrieved by receive method", self.__log_prefix__())
  320. def __build_frame__(self, msg):
  321. data_frame = json.dumps(self.__mk_msg__(msg.get_status(), msg.get_service_id(), msg.get_data_id(), msg.get_data()))
  322. if sys.version_info >= (3, 0):
  323. data_frame = bytes(data_frame, 'utf-8')
  324. checksum = self.__calc_chksum__(data_frame)
  325. return data_frame + checksum
  326. def __calc_chksum__(self, raw_data):
  327. return struct.pack('>I', binascii.crc32(raw_data) & 0xffffffff)
  328. @property
  329. def __channel_name__(self):
  330. cn = self.logger.name.split('.')[-1]
  331. if cn != self.DEFAULT_CHANNEL_NAME:
  332. return cn
  333. def __channel_name_response__(self, msg):
  334. data = msg.get_data()
  335. if self.__channel_name__ is None and data is not None:
  336. self.__init_channel_name__(data)
  337. self.logger.info('%s channel name is now %s', self.__log_prefix__(), repr(self.__channel_name__))
  338. return STATUS_OKAY, None
  339. def __channel_name_request__(self, msg):
  340. data = msg.get_data()
  341. if data is None:
  342. return STATUS_OKAY, self.__channel_name__
  343. else:
  344. prev_channel_name = self.__channel_name__
  345. self.__init_channel_name__(data)
  346. if prev_channel_name is not None and prev_channel_name != data:
  347. self.logger.warning('%s overwriting user defined channel name from %s to %s', self.__log_prefix__(), repr(prev_channel_name), repr(data))
  348. elif prev_channel_name is None:
  349. self.logger.info('%s channel name is now %s', self.__log_prefix__(), repr(self.__channel_name__))
  350. return STATUS_OKAY, None
  351. def __check_frame_checksum__(self, frame):
  352. return self.__calc_chksum__(frame[:-4]) == frame[-4:]
  353. def __clean_receive_buffer__(self):
  354. self.logger.debug("%s Cleaning up receive-buffer", self.__log_prefix__())
  355. self.__msg_buffer__ = {}
  356. def __connection_established__(self):
  357. self.__clean_receive_buffer__()
  358. if self.__comm_inst__.IS_CLIENT:
  359. self.send(SID_CHANNEL_NAME_REQUEST, 0, self.__channel_name__)
  360. if self.__auto_auth__ and self.__comm_inst__.IS_CLIENT and self.__secret__ is not None:
  361. self.authentificate()
  362. def __log_msg__(self, msg, rx_tx_prefix):
  363. self.logger.log(
  364. self.__status_log_lvl__(msg.get_status()),
  365. '%s %s %s, %s, data: "%s"',
  366. self.__log_prefix__(),
  367. rx_tx_prefix,
  368. self.__get_message_name__(msg.get_service_id(), msg.get_data_id()),
  369. self.__get_status_name__(msg.get_status()),
  370. repr(msg.get_data())
  371. )
  372. def __data_available_callback__(self, comm_inst):
  373. frame = comm_inst.receive()
  374. msg = self.__analyse_frame__(frame)
  375. if not self.__check_frame_checksum__(frame):
  376. # Wrong Checksum
  377. self.logger.log(self.__status_log_lvl__(STATUS_CHECKSUM_ERROR), "%s Received message has an invalid checksum. Message will be ignored.", self.__log_prefix__())
  378. return # No response needed
  379. elif not self.check_authentification_state() and self.__authentification_required__(msg.get_service_id(), msg.get_data_id()):
  380. # Authentification required
  381. self.__log_msg__(msg, 'RX <-')
  382. if msg.get_service_id() in self.__sid_response_dict__.keys():
  383. self.logger.log(self.__status_log_lvl__(STATUS_AUTH_REQUIRED), "%s Authentification is required. Just sending negative response.", self.__log_prefix__())
  384. status = STATUS_AUTH_REQUIRED
  385. data = None
  386. else:
  387. self.logger.log(self.__status_log_lvl__(STATUS_AUTH_REQUIRED), "%s Authentification is required. Incomming message will be ignored.", self.__log_prefix__())
  388. return # No response needed
  389. else:
  390. # Valid message
  391. self.__log_msg__(msg, 'RX <-')
  392. callback, args, kwargs = self.__callbacks__.get(msg.get_service_id(), msg.get_data_id())
  393. if msg.get_service_id() in self.__sid_response_dict__.keys():
  394. #
  395. # REQUEST RECEIVED
  396. #
  397. if callback is None:
  398. self.logger.warning("%s Incomming message with no registered callback. Sending negative response.", self.__log_prefix__())
  399. status = STATUS_BUFFERING_UNHANDLED_REQUEST
  400. data = None
  401. else:
  402. try:
  403. self.logger.debug("%s Executing callback %s to process received data", self.__log_prefix__(), callback.__name__)
  404. status, data = callback(msg, *args, **kwargs)
  405. except Exception:
  406. logger.error('{lp} Exception raised. Check callback {callback_name} and it\'s return values for service_id {service_id} and data_id {data_id}'.format(lp=self.__log_prefix__(), callback_name=callback.__name__, service_id=repr(msg.get_service_id()), data_id=repr(msg.get_data_id())))
  407. status = STATUS_CALLBACK_ERROR
  408. data = None
  409. else:
  410. #
  411. # RESPONSE RECEIVED
  412. #
  413. if callback is None:
  414. self.__buffer_received_data__(msg)
  415. else:
  416. self.logger.debug("%s Executing callback %s to process received data", self.__log_prefix__(), callback.__name__)
  417. callback(msg, *args, **kwargs)
  418. return # No response needed
  419. self.send(self.__sid_response_dict__[msg.get_service_id()], msg.get_data_id(), data, status=status)
  420. def __get_message_name__(self, service_id, data_id):
  421. return 'service: %s, data_id: %s' % (
  422. self.__sid_name_dict__.get(service_id, repr(service_id)),
  423. self.__did_name_dict__.get(service_id, {}).get(data_id, repr(data_id)),
  424. )
  425. def __get_status_name__(self, status):
  426. return 'status: %s' % (self.__status_name_dict.get(status, 'unknown status: %s' % repr(status)))
  427. def __init_channel_name__(self, channel_name):
  428. self.__comm_inst__.init_channel_name(channel_name)
  429. self.__callbacks__.init_channel_name(channel_name)
  430. if channel_name is None:
  431. self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__ + '.' + self.DEFAULT_CHANNEL_NAME)
  432. else:
  433. self.logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__ + '.' + channel_name)
  434. def __log_prefix__(self):
  435. return 'prot-client:' if self.__comm_inst__.IS_CLIENT else 'prot-server:'
  436. def __mk_msg__(self, status, service_id, data_id, data):
  437. return data_storage({data_storage.KEY_DATA_ID: data_id, data_storage.KEY_SERVICE_ID: service_id, data_storage.KEY_STATUS: status, data_storage.KEY_DATA: data})
  438. def __status_log_lvl__(self, status):
  439. return STATUS_LOG_LVL.get(status, logging.CRITICAL)
  440. def add_data(self, service_id, data_id, name):
  441. """
  442. Method to add a name for a specific message.
  443. :param service_id: The Service-ID of the message. See class definitions starting with ``SID_``.
  444. :type service_id: int or list of ints
  445. :param data_id: The Data-ID of the message.
  446. :type data_id: int
  447. :param name: The Name for the transfered message.
  448. :type name: str
  449. """
  450. try:
  451. iter(service_id)
  452. except Exception:
  453. service_id = (service_id, )
  454. for sid in service_id:
  455. if sid not in self.__did_name_dict__:
  456. self.__did_name_dict__[sid] = {}
  457. self.__did_name_dict__[sid][data_id] = name
  458. def add_msg_to_auth_whitelist_(self, service_id, data_id):
  459. """
  460. Method to add a specific message to the list, where no authentification is required.
  461. :param service_id: The Service-ID of the message. See class definitions starting with ``SID_``.
  462. :type service_id: int
  463. :param data_id: The Data-ID of the message.
  464. :type data_id: int
  465. """
  466. if service_id not in self.__auth_whitelist__:
  467. self.__auth_whitelist__[service_id] = []
  468. self.__auth_whitelist__[service_id].append(data_id)
  469. logger.debug('%s Adding Message (%s) to the authentification whitelist', self.__log_prefix__(), self.__get_message_name__(service_id, data_id))
  470. def add_service(self, req_sid, resp_sid, req_name=None, resp_name=None):
  471. """
  472. Method to add a Service defined by Request- and Response Serivce-ID.
  473. :param req_sid: The Request Service-ID.
  474. :type req_sid: int
  475. :param resp_sid: The Response Service-ID.
  476. :type resp_sid: int
  477. """
  478. if req_sid in self.__sid_response_dict__:
  479. logger.error('%s Service with Request-SID=%d and Response-SID=%d not added, because request SID is already registered', self.__log_prefix__(), req_sid, resp_sid)
  480. raise RequestSidExistsError("Request for this Service is already registered")
  481. elif resp_sid in self.__sid_response_dict__.values():
  482. logger.error('%s Service with Request-SID=%d and Response-SID=%d not added, because response SID is already registered', self.__log_prefix__(), req_sid, resp_sid)
  483. raise ResponseSidExistsError("Response for this Service is already registered")
  484. else:
  485. self.__sid_response_dict__[req_sid] = resp_sid
  486. if req_name is not None:
  487. self.__sid_name_dict__[req_sid] = req_name
  488. if resp_name is not None:
  489. self.__sid_name_dict__[resp_sid] = resp_name
  490. logger.debug('%s Adding Service with Request=%s and Response=%s', self.__log_prefix__(), req_name or repr(req_sid), resp_name or repr(resp_sid))
  491. def add_status(self, status, name):
  492. """
  493. Method to add a name for a status.
  494. :param status: The Status. See class definitions starting with ``STATUS_``.
  495. :type status: int
  496. :param name: The Name for the Status.
  497. :type name: str
  498. """
  499. self.__status_name_dict[status] = name
  500. def authentificate(self, timeout=2):
  501. """
  502. This method authetificates the client at the server.
  503. :param timeout: The timeout for the authentification (requesting seed, sending key and getting authentification_feedback).
  504. :type timeout: float
  505. :returns: True, if authentification was successfull; False, if not.
  506. :rtype: bool
  507. .. note:: An authentification will only processed, if a secret had been given on initialisation.
  508. .. note:: Client and Server needs to use the same secret.
  509. """
  510. if self.__secret__ is not None:
  511. self.__authentification_state__ = AUTH_STATE_SEED_REQUESTED
  512. self.send(SID_AUTH_REQUEST, DID_AUTH_SEED, None)
  513. cnt = 0
  514. while cnt < timeout * 10:
  515. time.sleep(0.1)
  516. if self.__authentification_state__ == AUTH_STATE_TRUSTED_CONNECTION:
  517. return True
  518. elif self.__authentification_state__ == AUTH_STATE_UNTRUSTED_CONNECTION:
  519. break
  520. cnt += 1
  521. return False
  522. def check_authentification_state(self):
  523. """
  524. This Method return the Authitification State as boolean value.
  525. :return: True, if authentification state is okay, otherwise False
  526. :rtype: bool
  527. """
  528. return self.__secret__ is None or self.__authentification_state__ == AUTH_STATE_TRUSTED_CONNECTION
  529. def connection_established(self):
  530. """
  531. This Method returns the Connection state including authentification as a boolean value.
  532. :return: True, if the connection is established (incl. authentification, if a secret has been given)
  533. :rtype: bool
  534. """
  535. return self.is_connected() and (self.__secret__ is None or self.check_authentification_state())
  536. def is_connected(self):
  537. """
  538. This Methods returns Connection state of the Communication Instance :func:`comm_instance.is_connected`.
  539. :return: True if the :class:`comm_instance` is connected, otherwise False..
  540. :rtype: bool
  541. """
  542. return self.__comm_inst__.is_connected()
  543. def receive(self, service_id, data_id, timeout=1):
  544. """
  545. This Method returns a message object for a defined message or None, if this message is not available after the given timout.
  546. :param service_id: The Service-ID for the message. See class definitions starting with ``SID_``.
  547. :type service_id: int
  548. :param data_id: The Data-ID for the message.
  549. :type data_id: int
  550. :param timeout: The timeout for receiving.
  551. :type timeout: float
  552. :returns: The received data storage object or None, if no data was received.
  553. :rtype: data_storage
  554. """
  555. data = None
  556. cnt = 0
  557. while data is None and cnt < timeout * 10:
  558. try:
  559. data = self.__msg_buffer__.get(service_id, {}).get(data_id, []).pop(0)
  560. except IndexError:
  561. data = None
  562. cnt += 1
  563. time.sleep(0.1)
  564. if data is None and cnt >= timeout * 10:
  565. self.logger.warning('%s TIMEOUT (%ss): Requested data (service_id: %s; data_id: %s) not in buffer.', self.__log_prefix__(), repr(timeout), repr(service_id), repr(data_id))
  566. return data
  567. def reconnect(self):
  568. """
  569. This methods initiates a reconnect by calling :func:`comm_instance.reconnect`.
  570. """
  571. return self.__comm_inst__.reconnect()
  572. def register_callback(self, service_id, data_id, callback, *args, **kwargs):
  573. """
  574. This method registers a callback for the given parameters. Giving ``None`` means, that all Service-IDs or all Data-IDs are used.
  575. If a message hitting these parameters has been received, the callback will be executed.
  576. :param service_id: The Service-ID for the message. See class definitions starting with ``SID_``.
  577. :type service_id: int
  578. :param data_id: The Data-ID for the message.
  579. :type data_id: int
  580. .. note:: The :func:`callback` is priorised in the following order:
  581. * Callbacks with defined Service-ID and Data-ID.
  582. * Callbacks with a defined Service-ID and all Data-IDs.
  583. * Callbacks with a defined Data-ID and all Service-IDs.
  584. * Unspecific Callbacks.
  585. .. note:: The :func:`callback` is executed with these arguments:
  586. **Parameters given at the callback call:**
  587. * The first Arguments is the received message as :class:`data_storage` object.
  588. * Further arguments given at registration.
  589. * Further keyword arguments given at registration.
  590. **Return value of the callback:**
  591. If the Callback is a Request Callback for a registered Service, the return value has to be a tuple or list with
  592. * :const:`response_status`: The response status (see class definitions starting with :const:`STA_*`.
  593. * :const:`response_data`: A JSON iterable object to be used as data for the response.
  594. .. note:: Only registered services will respond via the callbacks return values with the same data_id.
  595. """
  596. self.__callbacks__.add(service_id, data_id, callback, *args, **kwargs)
  597. def send(self, service_id, data_id, data, status=STATUS_OKAY, timeout=2):
  598. """
  599. This methods sends out a message with the given content.
  600. :param service_id: The Service-ID for the message. See class definitions starting with ``SERVICE_``.
  601. :type service_id: int
  602. :param data_id: The Data-ID for the message.
  603. :type data_id: int
  604. :param data: The data to be transfered. The data needs to be json compatible.
  605. :type data: str
  606. :param status: The Status for the message. All requests should have ``STATUS_OKAY``.
  607. :type status: int
  608. :param timeout: The timeout for sending data (e.g. time to establish new connection).
  609. :type timeout: float
  610. :return: True if data had been sent, otherwise False.
  611. :rtype: bool
  612. """
  613. if (self.check_authentification_state() or not self.__authentification_required__(service_id, data_id)) or (service_id in self.__sid_response_dict__.values() and status == STATUS_AUTH_REQUIRED and data is None):
  614. msg = data_storage(service_id=service_id, data_id=data_id, data=data, status=status)
  615. self.__log_msg__(msg, 'TX ->')
  616. return self.__comm_inst__.send(self.__build_frame__(msg), timeout=timeout)
  617. else:
  618. # Authentification required
  619. self.logger.warning("%s Authentification is required. TX-Message %s, %s, data: %s will be ignored.", self.__log_prefix__(), self.__get_message_name__(service_id, data_id), self.__get_status_name__(status), repr(data))
  620. return False
  621. class struct_json_protocol(pure_json_protocol):
  622. """
  623. This Class has the same functionality like :class:`pure_json_protocol`. The message length is less than for :class:`pure_json_protocol`, but the functionality and compatibility is reduced.
  624. See also parent :py:class:`pure_json_protocol`.
  625. .. note::
  626. This class is depreceated and here for compatibility reasons (to support old clients or servers). Usage of :class:`pure_json_protocol` is recommended.
  627. """
  628. def __init__(self, *args, **kwargs):
  629. pure_json_protocol.__init__(self, *args, **kwargs)
  630. def __analyse_frame__(self, frame):
  631. status, service_id, data_id = struct.unpack('>III', frame[0:12])
  632. if sys.version_info >= (3, 0):
  633. data = json.loads(frame[12:-1].decode('utf-8'))
  634. else:
  635. data = json.loads(frame[12:-1])
  636. return self.__mk_msg__(status, service_id, data_id, data)
  637. def __build_frame__(self, msg):
  638. frame = struct.pack('>III', msg.get_status(), msg.get_service_id(), msg.get_data_id())
  639. if sys.version_info >= (3, 0):
  640. frame += bytes(json.dumps(msg.get_data()), 'utf-8')
  641. frame += self.__calc_chksum__(frame)
  642. else:
  643. frame += json.dumps(msg.get_data())
  644. frame += self.__calc_chksum__(frame)
  645. return frame
  646. def __calc_chksum__(self, raw_data):
  647. chksum = 0
  648. for b in raw_data:
  649. if sys.version_info >= (3, 0):
  650. chksum ^= b
  651. else:
  652. chksum ^= ord(b)
  653. if sys.version_info >= (3, 0):
  654. return bytes([chksum])
  655. else:
  656. return chr(chksum)
  657. def __check_frame_checksum__(self, frame):
  658. return self.__calc_chksum__(frame[:-1]) == frame[-1:]