2015-08-20 17:27:15 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# This library is free software, distributed under the terms of
|
|
|
|
# the GNU Lesser General Public License Version 3, or any later version.
|
|
|
|
# See the COPYING file included in this archive
|
|
|
|
#
|
|
|
|
# The docstrings in this module contain epytext markup; API documentation
|
|
|
|
# may be created by processing this file with epydoc: http://epydoc.sf.net
|
|
|
|
|
2017-04-10 16:51:49 +02:00
|
|
|
from lbrynet.core.utils import generate_id
|
2018-07-03 06:51:25 +02:00
|
|
|
from . import constants
|
2017-03-31 19:32:43 +02:00
|
|
|
|
2017-04-10 19:59:31 +02:00
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class Message:
|
2015-08-20 17:27:15 +02:00
|
|
|
""" Base class for messages - all "unknown" messages use this class """
|
2017-03-31 19:32:43 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def __init__(self, rpcID, nodeID):
|
2017-10-25 02:05:30 +02:00
|
|
|
if len(rpcID) != constants.rpc_id_length:
|
|
|
|
raise ValueError("invalid rpc id: %i bytes (expected 20)" % len(rpcID))
|
|
|
|
if len(nodeID) != constants.key_bits / 8:
|
|
|
|
raise ValueError("invalid node id: %i bytes (expected 48)" % len(nodeID))
|
2015-08-20 17:27:15 +02:00
|
|
|
self.id = rpcID
|
|
|
|
self.nodeID = nodeID
|
|
|
|
|
|
|
|
|
|
|
|
class RequestMessage(Message):
|
|
|
|
""" Message containing an RPC request """
|
2017-03-31 19:32:43 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def __init__(self, nodeID, method, methodArgs, rpcID=None):
|
2017-04-25 20:21:13 +02:00
|
|
|
if rpcID is None:
|
2017-10-25 02:05:30 +02:00
|
|
|
rpcID = generate_id()[:constants.rpc_id_length]
|
2018-07-22 00:34:59 +02:00
|
|
|
super().__init__(rpcID, nodeID)
|
2015-08-20 17:27:15 +02:00
|
|
|
self.request = method
|
|
|
|
self.args = methodArgs
|
|
|
|
|
|
|
|
|
|
|
|
class ResponseMessage(Message):
|
|
|
|
""" Message containing the result from a successful RPC request """
|
2017-03-31 19:32:43 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def __init__(self, rpcID, nodeID, response):
|
2018-07-22 00:34:59 +02:00
|
|
|
super().__init__(rpcID, nodeID)
|
2015-08-20 17:27:15 +02:00
|
|
|
self.response = response
|
|
|
|
|
|
|
|
|
|
|
|
class ErrorMessage(ResponseMessage):
|
|
|
|
""" Message containing the error from an unsuccessful RPC request """
|
2017-03-31 19:32:43 +02:00
|
|
|
|
2015-08-20 17:27:15 +02:00
|
|
|
def __init__(self, rpcID, nodeID, exceptionType, errorMessage):
|
2018-07-22 00:34:59 +02:00
|
|
|
super().__init__(rpcID, nodeID, errorMessage)
|
2015-08-20 17:27:15 +02:00
|
|
|
if isinstance(exceptionType, type):
|
2018-07-20 21:45:58 +02:00
|
|
|
exceptionType = ('%s.%s' % (exceptionType.__module__, exceptionType.__name__)).encode()
|
|
|
|
self.exceptionType = exceptionType
|