From edf93e60f62066a1e60e55f8df1314863cb3a170 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Thu, 4 Nov 2021 01:15:59 +0100 Subject: [PATCH] Implemented a real fake server. Corrected a couple bindings. --- bindings.py | 9 +-- falseserver.py | 172 ++++++++++++++++++++++++++++--------------------- 2 files changed, 104 insertions(+), 77 deletions(-) diff --git a/bindings.py b/bindings.py index ac354c5..5d48032 100755 --- a/bindings.py +++ b/bindings.py @@ -571,13 +571,13 @@ class analogController(object): self.genericSEND("QUICKFA","{action:b}CTqfl".format(action=action)) self.st_quickframeall=action - def freezeScreen(self,screen,action=False): + def freezeScreen(self,screen,action=None): """Freeze screen ,1GCfsc: Freeze screen ,0GCfsc: Un Freeze screen ? """ 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.st_freeze[screen]=not action @@ -594,8 +594,9 @@ class analogController(object): """Freeze layer on ,,GCfrl: Freeze screen """ - if action==None: - action=self.st_freeze_layer[layer] + if not action==None: + 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.st_freeze_layer[layer]=not self.st_freeze_layer[layer] diff --git a/falseserver.py b/falseserver.py index 29e9e4e..6ebaa27 100644 --- a/falseserver.py +++ b/falseserver.py @@ -1,96 +1,122 @@ -import socket,time -from _thread import start_new_thread - -_DELAY=0.5 - +import multiprocessing +import socketserver import re -_MESSAGE_REGEX=re.compile("(?P(\d)*(,\d)*)(?P\D*)(?P(\d)*(,\d)*)") +import sys +import threading +import time + +_DELAY = 0.0 +_MESSAGE_REGEX = re.compile(r"(?P(\d)*(,\d)*)(?P\D*)(?P(\d)*(,\d)*)") +list_connections = [] +main_server = None + class Answer(object): - "Output an answer to a message" - def __init__(self,function): - if type(function)==str: - self.function=lambda x:function - else: - self.function=function + """Output an answer to a message""" + + def __init__(self, function): + if type(function) == str: + self.function = lambda x: 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): - return self.function(match)+other.function(match) + return self.function(match) + other.function(match) + return Answer(new_funct) - def __radd__(self,other): - if type(other)==str: + def __radd__(self, other): + if type(other) == str: def new_funct(match): - return other+self.function(match) + return other + self.function(match) + return Answer(new_funct) else: raise TypeError - def __call__(self,match): + def __call__(self, 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={ - "*\r\n":_MSG+_1+_RN, - "?\r\n":Answer("DEV259")+_RN, - "VEvar":_MSG+_RN, - "#":_MSG+_0, - "PRinp":_MSG+_ARG, - "PUscu":_MSG+_ARG, - ",GCtav":"GCtav"+_ARG+Answer(",1"), - "_":_MSG+_ARG, +__ = 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 = { + "*\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 - # in our case it is 12345 but it - # can be anything - port = 3000 - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.bind((host, port)) - print("Fake analogController (pulse2) on ({}@{})".format(host,port)) +class Goodserver(socketserver.BaseRequestHandler): + """ + The request handler class for our server. - # put the socket into listening mode - s.listen(5) - print("Server is listening to incomming connections") + It is instantiated once per connection to the server, and must + override the handle() method to implement communication to the + 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: - c, addr = s.accept() - # print(c.recv(4000)) - # c.sendall(b"*\r\n") - start_new_thread(goodServer, (c,addr,delay,)) - cont=input("Keep accepting connections ?") - if cont.lower()=="n": - break - s.close() + +def main_listener(host, port): + """Listening loop""" + global main_server + main_server = socketserver.TCPServer((host, port), Goodserver) + print("Listening on {}:{}".format(host, port)) + main_server.serve_forever() + + +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__': - Main(0.05) + Main()