Implemented a real fake server. Corrected a couple bindings.

This commit is contained in:
Maximilien Rigaut 2021-11-04 01:15:59 +01:00
parent 3ae407afc9
commit edf93e60f6
2 changed files with 104 additions and 77 deletions

View File

@ -571,13 +571,13 @@ class analogController(object):
self.genericSEND("QUICKFA","{action:b}CTqfl".format(action=action)) self.genericSEND("QUICKFA","{action:b}CTqfl".format(action=action))
self.st_quickframeall=action self.st_quickframeall=action
def freezeScreen(self,screen,action=False): def freezeScreen(self,screen,action=None):
"""Freeze screen <screen> """Freeze screen <screen>
<screen>,1GCfsc: Freeze screen <screen>,1GCfsc: Freeze screen
<screen>,0GCfsc: Un Freeze screen ? <screen>,0GCfsc: Un Freeze screen ?
""" """
if action==None: if action==None:
action=not self.st_freeze[screen] action=self.st_freeze[screen]
self.genericSEND("FREEZE","{screen},{action:b}GCfsc".format(screen=screen,action=action)) self.genericSEND("FREEZE","{screen},{action:b}GCfsc".format(screen=screen,action=action))
self.st_freeze[screen]=not action self.st_freeze[screen]=not action
@ -594,8 +594,9 @@ class analogController(object):
"""Freeze layer <layer> on <screen> """Freeze layer <layer> on <screen>
<layer>,<screen>,<action>GCfrl: Freeze screen <layer>,<screen>,<action>GCfrl: Freeze screen
""" """
if action==None: if not action==None:
action=self.st_freeze_layer[layer] action=not self.st_freeze_layer[layer]
print(action)
self.genericSEND("FREEZELAYER","{layer},{screen},{action:b}GCfrl".format(layer=layer,screen=screen,action=action)) self.genericSEND("FREEZELAYER","{layer},{screen},{action:b}GCfrl".format(layer=layer,screen=screen,action=action))
self.st_freeze_layer[layer]=not self.st_freeze_layer[layer] self.st_freeze_layer[layer]=not self.st_freeze_layer[layer]

View File

@ -1,96 +1,122 @@
import socket,time import multiprocessing
from _thread import start_new_thread import socketserver
_DELAY=0.5
import re import re
_MESSAGE_REGEX=re.compile("(?P<preargs>(\d)*(,\d)*)(?P<msg>\D*)(?P<postargs>(\d)*(,\d)*)") import sys
import threading
import time
_DELAY = 0.0
_MESSAGE_REGEX = re.compile(r"(?P<preargs>(\d)*(,\d)*)(?P<msg>\D*)(?P<postargs>(\d)*(,\d)*)")
list_connections = []
main_server = None
class Answer(object): class Answer(object):
"Output an answer to a message" """Output an answer to a message"""
def __init__(self,function):
if type(function)==str: def __init__(self, function):
self.function=lambda x:function if type(function) == str:
else: self.function = lambda x: function
self.function=function else:
self.function = function
def __add__(self, other):
if type(other) == str:
other = Answer(other)
def __add__(self,other):
if type(other)==str:
other=Answer(other)
def new_funct(match): def new_funct(match):
return self.function(match)+other.function(match) return self.function(match) + other.function(match)
return Answer(new_funct) return Answer(new_funct)
def __radd__(self,other): def __radd__(self, other):
if type(other)==str: if type(other) == str:
def new_funct(match): def new_funct(match):
return other+self.function(match) return other + self.function(match)
return Answer(new_funct) return Answer(new_funct)
else: else:
raise TypeError raise TypeError
def __call__(self,match): def __call__(self, match):
return self.function(match) return self.function(match)
__=Answer(lambda x:"")
_ARG=Answer(lambda x:x.group("preargs"))
_0=Answer(lambda x:"0")
_1=Answer(lambda x:"1")
_MSG=Answer(lambda x:x.group("msg"))
_RN=Answer(lambda x:"\r\n")
_goodanswers={ __ = Answer(lambda x: "")
"*\r\n":_MSG+_1+_RN, _ARG = Answer(lambda x: x.group("preargs"))
"?\r\n":Answer("DEV259")+_RN, _0 = Answer(lambda x: "0")
"VEvar":_MSG+_RN, _1 = Answer(lambda x: "1")
"#":_MSG+_0, _MSG = Answer(lambda x: x.group("msg"))
"PRinp":_MSG+_ARG, _RN = Answer(lambda x: "\r\n")
"PUscu":_MSG+_ARG,
",GCtav":"GCtav"+_ARG+Answer(",1"), _goodanswers = {
"_":_MSG+_ARG, "*\r\n": _MSG + _1 + _RN,
"?\r\n": Answer("DEV259") + _RN,
"VEvar": _MSG + _RN,
"#": _MSG + _0+_RN,
"PRinp": _MSG + _ARG + _RN + "GCfsc1,1"+_RN,
"PUscu": _MSG + _ARG+_RN,
",GCtav": "GCtav" + _ARG + Answer(",1")+_RN,
"*": _MSG + _ARG+_RN,
} }
def goodServer(s,addr,delay=_DELAY):
"Give good-ish answers to queries"
try:
while True:
recv=s.recv(3000)
match=_MESSAGE_REGEX.match(recv.decode())
try:
msg=_goodanswers[match.group("msg")](match)
except KeyError:
msg=_goodanswers["_"](match)
print("[{}] Received : {} | Sending {}".format(addr[1],recv,msg.strip("\r\n")))
s.sendall((msg+"\r\n").encode())
except ConnectionResetError as e:
print("[{}] Has stopped the connection".format(addr[1]))
print(e)
def Main(delay=_DELAY):
host = "127.0.0.1"
# reverse a port on your computer class Goodserver(socketserver.BaseRequestHandler):
# in our case it is 12345 but it """
# can be anything The request handler class for our server.
port = 3000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("Fake analogController (pulse2) on ({}@{})".format(host,port))
# put the socket into listening mode It is instantiated once per connection to the server, and must
s.listen(5) override the handle() method to implement communication to the
print("Server is listening to incomming connections") client.
"""
def handle(self):
"""Give good-ish answers to queries"""
global list_connections
try:
while True:
data = self.request.recv(1024)
print("Received data", data)
if data:
_match = _MESSAGE_REGEX.match(data.decode("utf-8"))
try:
answer = _goodanswers[_match.group("msg")](_match)
except KeyError:
answer = _goodanswers["*"](_match)
print("[{}] Received : {} | Sent {}".format(self.request.getpeername(),
data.decode("utf-8").strip(), answer.strip()))
time.sleep(_DELAY)
self.request.sendall(answer.encode("utf-8"))
else:
break
except ConnectionResetError as e:
print("[{}] Has stopped the connection".format(self.request.getpeername()))
print(e)
# list_connections.remove(self.sck)
# self.sck.close()
# a forever loop until client wants to exit
while True: def main_listener(host, port):
c, addr = s.accept() """Listening loop"""
# print(c.recv(4000)) global main_server
# c.sendall(b"*\r\n") main_server = socketserver.TCPServer((host, port), Goodserver)
start_new_thread(goodServer, (c,addr,delay,)) print("Listening on {}:{}".format(host, port))
cont=input("Keep accepting connections ?") main_server.serve_forever()
if cont.lower()=="n":
break
s.close() def Main():
host, port = "127.0.0.1", 3000
print("Fake analogController (pulse2) on ({}@{})".format(host, port))
threading.Thread(target=main_listener, args=(host, port)).start()
time.sleep(0.5)
input("Press any key to shutdown...")
print("Shutting down...")
main_server.shutdown()
sys.exit()
if __name__ == '__main__': if __name__ == '__main__':
Main(0.05) Main()