lbry-sdk/lbry/lbrynet/blob_exchange/server.py

135 lines
5.9 KiB
Python
Raw Normal View History

2019-01-22 18:52:32 +01:00
import asyncio
import binascii
import logging
import typing
from json.decoder import JSONDecodeError
from lbrynet.blob_exchange.serialization import BlobResponse, BlobRequest, blob_response_types
from lbrynet.blob_exchange.serialization import BlobAvailabilityResponse, BlobPriceResponse, BlobDownloadResponse, \
BlobPaymentAddressResponse
if typing.TYPE_CHECKING:
2019-03-28 19:51:55 +01:00
from lbrynet.blob.blob_manager import BlobManager
2019-01-22 18:52:32 +01:00
log = logging.getLogger(__name__)
2019-01-25 19:10:40 +01:00
class BlobServerProtocol(asyncio.Protocol):
2019-03-28 19:51:55 +01:00
def __init__(self, loop: asyncio.BaseEventLoop, blob_manager: 'BlobManager', lbrycrd_address: str):
2019-01-22 18:52:32 +01:00
self.loop = loop
self.blob_manager = blob_manager
self.server_task: asyncio.Task = None
self.started_listening = asyncio.Event(loop=self.loop)
self.buf = b''
self.transport = None
self.lbrycrd_address = lbrycrd_address
2019-06-03 05:50:17 +02:00
self.peer_address_and_port: typing.Optional[str] = None
2019-01-22 18:52:32 +01:00
def connection_made(self, transport):
self.transport = transport
2019-06-03 05:50:17 +02:00
self.peer_address_and_port = "%s:%i" % self.transport.get_extra_info('peername')
self.blob_manager.connection_manager.connection_received(self.peer_address_and_port)
def connection_lost(self, exc: typing.Optional[Exception]) -> None:
self.blob_manager.connection_manager.incoming_connection_lost(self.peer_address_and_port)
2019-01-22 18:52:32 +01:00
def send_response(self, responses: typing.List[blob_response_types]):
to_send = []
while responses:
to_send.append(responses.pop())
2019-06-03 05:50:17 +02:00
serialized = BlobResponse(to_send).serialize()
self.transport.write(serialized)
self.blob_manager.connection_manager.sent_data(self.peer_address_and_port, len(serialized))
2019-01-22 18:52:32 +01:00
async def handle_request(self, request: BlobRequest):
addr = self.transport.get_extra_info('peername')
peer_address, peer_port = addr
responses = []
address_request = request.get_address_request()
if address_request:
responses.append(BlobPaymentAddressResponse(lbrycrd_address=self.lbrycrd_address))
availability_request = request.get_availability_request()
if availability_request:
responses.append(BlobAvailabilityResponse(available_blobs=list(set((
filter(lambda blob_hash: blob_hash in self.blob_manager.completed_blob_hashes,
availability_request.requested_blobs)
)))))
price_request = request.get_price_request()
if price_request:
responses.append(BlobPriceResponse(blob_data_payment_rate='RATE_ACCEPTED'))
download_request = request.get_blob_request()
if download_request:
blob = self.blob_manager.get_blob(download_request.requested_blob)
if blob.get_is_verified():
incoming_blob = {'blob_hash': blob.blob_hash, 'length': blob.length}
responses.append(BlobDownloadResponse(incoming_blob=incoming_blob))
self.send_response(responses)
2019-02-02 00:13:45 +01:00
log.debug("send %s to %s:%i", blob.blob_hash[:8], peer_address, peer_port)
try:
sent = await blob.sendfile(self)
2019-01-31 22:03:28 +01:00
except (ConnectionResetError, BrokenPipeError, RuntimeError, OSError):
if self.transport:
self.transport.close()
return
2019-01-22 18:52:32 +01:00
log.info("sent %s (%i bytes) to %s:%i", blob.blob_hash[:8], sent, peer_address, peer_port)
if responses:
self.send_response(responses)
# self.transport.close()
def data_received(self, data):
request = None
if data:
2019-06-03 05:50:17 +02:00
self.blob_manager.connection_manager.received_data(self.peer_address_and_port, len(data))
2019-01-22 18:52:32 +01:00
message, separator, remainder = data.rpartition(b'}')
if not separator:
self.buf += data
return
try:
2019-01-30 16:11:05 +01:00
request = BlobRequest.deserialize(self.buf + data)
2019-01-22 18:52:32 +01:00
self.buf = remainder
except JSONDecodeError:
addr = self.transport.get_extra_info('peername')
peer_address, peer_port = addr
log.error("failed to decode blob request from %s:%i (%i bytes): %s", peer_address, peer_port,
len(data), '' if not data else binascii.hexlify(data).decode())
if not request:
addr = self.transport.get_extra_info('peername')
peer_address, peer_port = addr
log.warning("failed to decode blob request from %s:%i", peer_address, peer_port)
self.transport.close()
return
self.loop.create_task(self.handle_request(request))
2019-01-25 19:10:40 +01:00
class BlobServer:
2019-03-28 19:51:55 +01:00
def __init__(self, loop: asyncio.BaseEventLoop, blob_manager: 'BlobManager', lbrycrd_address: str):
2019-01-25 19:10:40 +01:00
self.loop = loop
self.blob_manager = blob_manager
2019-06-03 05:50:17 +02:00
self.server_task: typing.Optional[asyncio.Task] = None
2019-01-25 19:10:40 +01:00
self.started_listening = asyncio.Event(loop=self.loop)
self.lbrycrd_address = lbrycrd_address
2019-02-12 07:08:34 +01:00
self.server_protocol_class = BlobServerProtocol
2019-01-25 19:10:40 +01:00
2019-01-22 18:52:32 +01:00
def start_server(self, port: int, interface: typing.Optional[str] = '0.0.0.0'):
if self.server_task is not None:
raise Exception("already running")
async def _start_server():
2019-01-25 19:10:40 +01:00
server = await self.loop.create_server(
2019-02-12 07:08:34 +01:00
lambda: self.server_protocol_class(self.loop, self.blob_manager, self.lbrycrd_address),
2019-01-25 19:10:40 +01:00
interface, port
)
2019-01-22 18:52:32 +01:00
self.started_listening.set()
log.info("Blob server listening on TCP %s:%i", interface, port)
async with server:
await server.serve_forever()
self.server_task = self.loop.create_task(_start_server())
def stop_server(self):
if self.server_task:
self.server_task.cancel()
self.server_task = None
log.info("Stopped blob server")