Get client role as first line

This commit is contained in:
Davte 2020-04-09 23:05:03 +02:00
parent 1853d85a79
commit ca3aa5857b
2 changed files with 14 additions and 3 deletions

View File

@ -48,7 +48,11 @@ class Client:
async def run_sending_client(self, file_path='~/output.txt'):
self._file_path = file_path
_, writer = await asyncio.open_connection(host=self.host, port=self.port)
reader, writer = await asyncio.open_connection(host=self.host,
port=self.port)
writer.write("sender\n".encode('utf-8'))
await writer.drain()
await reader.readline() # Wait for server start signal
await self.send(writer=writer)
async def send(self, writer: asyncio.StreamWriter):
@ -73,7 +77,11 @@ class Client:
async def run_receiving_client(self, file_path='~/input.txt'):
self._file_path = file_path
reader, _ = await asyncio.open_connection(host=self.host, port=self.port)
reader, writer = await asyncio.open_connection(host=self.host,
port=self.port)
writer.write("receiver\n".encode('utf-8'))
await writer.drain()
await reader.readline() # Wait for server start signal
await self.receive(reader=reader)
async def receive(self, reader: asyncio.StreamReader):

View File

@ -87,7 +87,10 @@ class Server:
Decide whether client is sender or receiver and start transmission.
"""
peer_is_sender = not self.working # TODO: ask peer role
client_hello = await reader.readline()
peer_is_sender = client_hello.decode('utf-8') == 'sender\n'
writer.write("Start!\n".encode('utf-8')) # Send start signal to client
await writer.drain()
self._working = True
if peer_is_sender:
logging.info("Sender is connecting...")