port reflector

This commit is contained in:
Victor Shyba 2018-08-10 00:49:17 -03:00 committed by Jack Robison
parent a204f0d3e6
commit 8c3552dd34
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
3 changed files with 14 additions and 12 deletions

View file

@ -16,7 +16,7 @@ class BlobReflectorClient(Protocol):
def connectionMade(self): def connectionMade(self):
self.blob_manager = self.factory.blob_manager self.blob_manager = self.factory.blob_manager
self.response_buff = '' self.response_buff = b''
self.outgoing_buff = '' self.outgoing_buff = ''
self.blob_hashes_to_send = self.factory.blobs self.blob_hashes_to_send = self.factory.blobs
self.next_blob_to_send = None self.next_blob_to_send = None
@ -39,7 +39,7 @@ class BlobReflectorClient(Protocol):
except IncompleteResponse: except IncompleteResponse:
pass pass
else: else:
self.response_buff = '' self.response_buff = b''
d = self.handle_response(msg) d = self.handle_response(msg)
d.addCallback(lambda _: self.send_next_request()) d.addCallback(lambda _: self.send_next_request())
d.addErrback(self.response_failure_handler) d.addErrback(self.response_failure_handler)
@ -73,7 +73,7 @@ class BlobReflectorClient(Protocol):
def send_handshake(self): def send_handshake(self):
log.debug('Sending handshake') log.debug('Sending handshake')
self.write(json.dumps({'version': self.protocol_version})) self.write(json.dumps({'version': self.protocol_version}).encode())
return defer.succeed(None) return defer.succeed(None)
def parse_response(self, buff): def parse_response(self, buff):
@ -150,7 +150,7 @@ class BlobReflectorClient(Protocol):
self.write(json.dumps({ self.write(json.dumps({
'blob_hash': self.next_blob_to_send.blob_hash, 'blob_hash': self.next_blob_to_send.blob_hash,
'blob_size': self.next_blob_to_send.length 'blob_size': self.next_blob_to_send.length
})) }).encode())
def disconnect(self, err): def disconnect(self, err):
self.transport.loseConnection() self.transport.loseConnection()

View file

@ -40,7 +40,7 @@ class ReflectorServer(Protocol):
self.receiving_blob = False self.receiving_blob = False
self.incoming_blob = None self.incoming_blob = None
self.blob_finished_d = None self.blob_finished_d = None
self.request_buff = "" self.request_buff = b""
self.blob_writer = None self.blob_writer = None
@ -52,7 +52,7 @@ class ReflectorServer(Protocol):
self.transport.loseConnection() self.transport.loseConnection()
def send_response(self, response_dict): def send_response(self, response_dict):
self.transport.write(json.dumps(response_dict)) self.transport.write(json.dumps(response_dict).encode())
############################ ############################
# Incoming blob file stuff # # Incoming blob file stuff #
@ -122,7 +122,7 @@ class ReflectorServer(Protocol):
self.request_buff += data self.request_buff += data
msg, extra_data = self._get_valid_response(self.request_buff) msg, extra_data = self._get_valid_response(self.request_buff)
if msg is not None: if msg is not None:
self.request_buff = '' self.request_buff = b''
d = self.handle_request(msg) d = self.handle_request(msg)
d.addErrback(self.handle_error) d.addErrback(self.handle_error)
if self.receiving_blob and extra_data: if self.receiving_blob and extra_data:
@ -134,7 +134,7 @@ class ReflectorServer(Protocol):
response = None response = None
curr_pos = 0 curr_pos = 0
while not self.receiving_blob: while not self.receiving_blob:
next_close_paren = response_msg.find('}', curr_pos) next_close_paren = response_msg.find(b'}', curr_pos)
if next_close_paren != -1: if next_close_paren != -1:
curr_pos = next_close_paren + 1 curr_pos = next_close_paren + 1
try: try:

View file

@ -1,4 +1,6 @@
import os import os
from binascii import hexlify
from twisted.internet import defer, error from twisted.internet import defer, error
from twisted.trial import unittest from twisted.trial import unittest
from lbrynet.core.StreamDescriptor import get_sd_info from lbrynet.core.StreamDescriptor import get_sd_info
@ -81,12 +83,12 @@ class TestReflector(unittest.TestCase):
return d return d
def create_stream(): def create_stream():
test_file = mocks.GenFile(5209343, b''.join([chr(i + 3) for i in range(0, 64, 6)])) test_file = mocks.GenFile(5209343, bytes([(i + 3) for i in range(0, 64, 6)]))
d = EncryptedFileCreator.create_lbry_file( d = EncryptedFileCreator.create_lbry_file(
self.client_blob_manager, self.client_storage, prm, self.client_lbry_file_manager, self.client_blob_manager, self.client_storage, prm, self.client_lbry_file_manager,
"test_file", "test_file",
test_file, test_file,
key="0123456701234567", key=b"0123456701234567",
iv_generator=iv_generator() iv_generator=iv_generator()
) )
d.addCallback(lambda lbry_file: lbry_file.stream_hash) d.addCallback(lambda lbry_file: lbry_file.stream_hash)
@ -165,7 +167,7 @@ class TestReflector(unittest.TestCase):
self.assertEqual(1, len(streams)) self.assertEqual(1, len(streams))
stream_info = yield self.server_storage.get_stream_info(self.stream_hash) stream_info = yield self.server_storage.get_stream_info(self.stream_hash)
self.assertEqual(self.sd_hash, stream_info[3]) self.assertEqual(self.sd_hash, stream_info[3])
self.assertEqual('test_file'.encode('hex'), stream_info[0]) self.assertEqual(hexlify(b'test_file').decode(), stream_info[0])
# check should_announce blobs on blob_manager # check should_announce blobs on blob_manager
blob_hashes = yield self.server_storage.get_all_should_announce_blobs() blob_hashes = yield self.server_storage.get_all_should_announce_blobs()
@ -334,4 +336,4 @@ def iv_generator():
iv = 0 iv = 0
while True: while True:
iv += 1 iv += 1
yield "%016d" % iv yield b"%016d" % iv