2024-07-08 16:21:14 +03:00
|
|
|
import asyncio
|
|
|
|
|
import proto
|
|
|
|
|
from transport import Transport
|
|
|
|
|
|
|
|
|
|
class Executor:
|
|
|
|
|
def __init__(self, addr, port, col_num):
|
|
|
|
|
self.addr = addr
|
|
|
|
|
self.port = port
|
|
|
|
|
self.col_num = col_num
|
|
|
|
|
self.connections = []
|
|
|
|
|
self.interval = 10
|
|
|
|
|
|
|
|
|
|
async def execute(self):
|
2024-07-11 14:03:13 +03:00
|
|
|
blob_id = b"test"
|
|
|
|
|
data = b"TEST DATA"
|
2024-07-08 16:21:14 +03:00
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
for transport in self.connections:
|
2024-07-11 14:03:13 +03:00
|
|
|
message = proto.new_dispersal_req_msg(blob_id, data)
|
2024-07-08 16:21:14 +03:00
|
|
|
await transport.write(message)
|
|
|
|
|
await asyncio.sleep(self.interval)
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
break
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Error during message sending: {e}")
|
|
|
|
|
|
|
|
|
|
async def connect(self):
|
|
|
|
|
try:
|
|
|
|
|
reader, writer = await asyncio.open_connection(self.addr, self.port)
|
|
|
|
|
conn_id = len(self.connections)
|
|
|
|
|
transport = Transport(conn_id, reader, writer, self._handle)
|
|
|
|
|
self.connections.append(transport)
|
|
|
|
|
print(f"Connected to {self.addr}:{self.port}, ID: {conn_id}")
|
|
|
|
|
asyncio.create_task(transport.read_and_process())
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Failed to connect or lost connection: {e}")
|
|
|
|
|
|
|
|
|
|
async def _handle(self, conn_id, writer, message):
|
2024-07-11 14:03:13 +03:00
|
|
|
if message.HasField('dispersal_res'):
|
|
|
|
|
print(f"Received DispersalRes: blob_id={message.dispersal_res.blob_id}")
|
|
|
|
|
elif message.HasField('sample_res'):
|
|
|
|
|
print(f"Received SampleRes: blob_id={message.sample_res.blob_id}")
|
|
|
|
|
else:
|
|
|
|
|
print("Received unknown message type")
|
2024-07-08 16:21:14 +03:00
|
|
|
|
|
|
|
|
async def run(self):
|
|
|
|
|
await asyncio.gather(*(self.connect() for _ in range(self.col_num)))
|
|
|
|
|
await self.execute()
|
|
|
|
|
|