-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer.py
64 lines (48 loc) · 1.69 KB
/
producer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
from time import sleep
from argparse import ArgumentParser
from socket import socket, AF_INET, SOCK_DGRAM, SOL_SOCKET, SO_BROADCAST, SO_REUSEPORT
from frame import RP_Dat, ID_Dat
def msleep(sec):
sleep(sec / 1000)
RETURN_TIME = 50
class Producer(object):
def __init__(self, id: int, data: bytes):
self._id = id
self._data = data
def run_server(self, port=5432):
self._sock = socket(AF_INET, SOCK_DGRAM)
self._sock.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
self._sock.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
self._sock.bind(('', port))
self._port = port
def send_rp_dat(self, msg: RP_Dat):
self._sock.sendto(msg.get_repr(), ('<broadcast>', self._port))
def recv_id_dat(self):
data, addr = self._sock.recvfrom(ID_Dat.size())
try:
return ID_Dat.from_repr(data)
except:
return None
def do_loop(self):
while True:
# 1. Get the ID_Dat
id_dat = self.recv_id_dat()
# 2. Ignore messages for which we are not the producer
if not id_dat or id_dat.id != self._id:
continue
# 3. Send back the object to the bus
msleep(RETURN_TIME)
rp_dat = RP_Dat(self._data)
print(f'sending {rp_dat}')
self.send_rp_dat(rp_dat)
def parse_args():
parser = ArgumentParser()
parser.add_argument('id', type=int)
parser.add_argument('msg', default='tititata', nargs='?')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
prod = Producer(args.id, bytes(args.msg, 'utf-8'))
prod.run_server()
prod.do_loop()