statemachine
This commit is contained in:
parent
5e60c3e540
commit
f91da7a47c
1 changed files with 54 additions and 24 deletions
|
@ -30,44 +30,74 @@ def convert_to_strings(obj):
|
||||||
return str(obj)[2:-1]
|
return str(obj)[2:-1]
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def get_msg(socket):
|
class NetworkThread(threading.Thread):
|
||||||
|
def __init__(self, name, queue):
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
self.name = name
|
||||||
|
self.q = queue
|
||||||
|
self.context = zmq.Context()
|
||||||
|
|
||||||
msg = socket.recv()
|
def run(self):
|
||||||
|
self.connect()
|
||||||
|
msg = self.send_msg_and_expect()
|
||||||
|
|
||||||
|
def disconnect(self):
|
||||||
|
self.socket.setsockopt(zmq.LINGER,0)
|
||||||
|
self.socket.close()
|
||||||
|
|
||||||
|
def reconnect(self):
|
||||||
|
self.disconnect()
|
||||||
|
self.connect()
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
self.socket = self.context.socket(zmq.PAIR)
|
||||||
|
self.socket.connect("tcp://127.0.0.1:12345")
|
||||||
|
|
||||||
|
def send_msg_and_expect(this, msg, expect, timeout=5, retries=3):
|
||||||
|
while retries:
|
||||||
|
a = convert_to_strings(msg)
|
||||||
|
self.socket.send(str.encode(json.dumps(a)))
|
||||||
|
if (client.poll(REQUEST_TIMEOUT) & zmq.POLLIN) != 0:
|
||||||
|
msg = self.socket.recv()
|
||||||
try:
|
try:
|
||||||
if msg:
|
if msg:
|
||||||
return json.loads(msg)
|
result = json.loads(msg)
|
||||||
|
if result["msg"] in expect:
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
print("got unexpected message {result}")
|
||||||
|
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
print(f"malformed message received {msg}")
|
print(f"malformed message received {msg}")
|
||||||
|
retries -= 1
|
||||||
|
self.reconnect()
|
||||||
return NO_MSG
|
return NO_MSG
|
||||||
|
|
||||||
|
"""
|
||||||
def send_msg(msg, socket):
|
|
||||||
a = convert_to_strings(msg)
|
|
||||||
socket.send(str.encode(json.dumps(a)))
|
|
||||||
|
|
||||||
def networking(q):
|
def networking(q):
|
||||||
print("starting thread")
|
print("starting thread")
|
||||||
|
|
||||||
context = zmq.Context()
|
state = NetworkState.DISCONNECTED
|
||||||
connected = False
|
|
||||||
a = None
|
a = None
|
||||||
while not connected:
|
while state == NetworkState.DISCONNECTED:
|
||||||
socket = context.socket(zmq.PAIR)
|
socket = context.socket(zmq.PAIR)
|
||||||
socket.connect("tcp://127.0.0.1:12345")
|
socket.connect("tcp://127.0.0.1:12345")
|
||||||
msg = get_msg(socket)
|
msg = get_msg(socket)
|
||||||
if msg["msg"] == "init":
|
if msg["msg"] == "init":
|
||||||
send_msg(ACK_MSG, socket)
|
send_msg(ACK_MSG, socket)
|
||||||
connected = True
|
state = NetworkState.CONNECTED
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
timer = time.monotonic()
|
timer = time.monotonic()
|
||||||
while connected:
|
while state != NetworkState.DISCONNECTED:
|
||||||
if timer - time.monotonic() >= 5:
|
if state == NetworkState.CONNECTED and timer - time.monotonic() >= 5:
|
||||||
timer = time.monotonic()
|
timer = time.monotonic()
|
||||||
send_msg(PING_MSG,socket)
|
send_msg(PING_MSG,socket)
|
||||||
msg = get_msg(socket)
|
msg = get_msg(socket)
|
||||||
if msg["msg"] != "pong":
|
if msg["msg"] != "pong":
|
||||||
connected = False
|
state = NetworkState.
|
||||||
|
|
||||||
msg = get_msg(socket)
|
msg = get_msg(socket)
|
||||||
if msg['msg'] == "ping":
|
if msg['msg'] == "ping":
|
||||||
|
@ -88,11 +118,11 @@ def networking(q):
|
||||||
self.q.task_done()
|
self.q.task_done()
|
||||||
else:
|
else:
|
||||||
connected = False
|
connected = False
|
||||||
|
"""
|
||||||
class Counter:
|
class LittleSnitchBridge:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.q = Queue()
|
self.q = Queue()
|
||||||
self.thread = threading.Thread(name="NetworkThread", target=networking, args=(self.q,))
|
self.thread = NetworkThread("network", self.q)
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
|
|
||||||
def request(self, flow):
|
def request(self, flow):
|
||||||
|
@ -104,5 +134,5 @@ class Counter:
|
||||||
self.q.join()
|
self.q.join()
|
||||||
|
|
||||||
addons = [
|
addons = [
|
||||||
Counter()
|
LittleSnitchBridge()
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue