From ad985bebe836e2f038cc26bde6b52b592318d9b4 Mon Sep 17 00:00:00 2001 From: Wojciech Scigala Date: Fri, 20 Sep 2024 23:50:49 +0200 Subject: [PATCH] Some fixes and updates, hopefully non-breaking. --- mopp.py | 71 +++++++++++++++++++++++++++++++++------------- udp_chat_server.py | 5 ++-- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/mopp.py b/mopp.py index 744ba15..5a3fbe1 100644 --- a/mopp.py +++ b/mopp.py @@ -5,6 +5,20 @@ class Mopp: serial = 1 + morse = { + "0" : "-----", "1" : ".----", "2" : "..---", "3" : "...--", "4" : "....-", "5" : ".....", + "6" : "-....", "7" : "--...", "8" : "---..", "9" : "----.", + "a" : ".-", "b" : "-...", "c" : "-.-.", "d" : "-..", "e" : ".", "f" : "..-.", "g" : "--.", + "h" : "....", "i" : "..", "j" : ".---", "k" : "-.-", "l" : ".-..", "m" : "--", "n" : "-.", + "o" : "---", "p" : ".--.", "q" : "--.-", "r" : ".-.", "s" : "...", "t" : "-", "u" : "..-", + "v" : "...-", "w" : ".--", "x" : "-..-", "y" : "-.--", "z" : "--..", "=" : "-...-", + "/" : "-..-.", "+" : ".-.-.", "-" : "-....-", "." : ".-.-.-", "," : "--..--", "?" : "..--..", + ":" : "---...", "!" : "-.-.--", "'" : ".----." + } + + morse_inv = {v: k for k, v in morse.items()} + + def __init__(self, speed = 20): self.speed = speed return @@ -17,29 +31,23 @@ def _str2bin(self, bytes): bincode = "".join("{:08b}".format(c) for c in bytes) return bincode - def mopp(self, speed, msg): - logging.debug("Encoding message with "+str(speed)+" wpm :"+str(msg)) - - morse = { - "0" : "-----", "1" : ".----", "2" : "..---", "3" : "...--", "4" : "....-", "5" : ".....", - "6" : "-....", "7" : "--...", "8" : "---..", "9" : "----.", - "a" : ".-", "b" : "-...", "c" : "-.-.", "d" : "-..", "e" : ".", "f" : "..-.", "g" : "--.", - "h" : "....", "i" : "..", "j" : ".---", "k" : "-.-", "l" : ".-..", "m" : "--", "n" : "-.", - "o" : "---", "p" : ".--.", "q" : "--.-", "r" : ".-.", "s" : "...", "t" : "-", "u" : "..-", - "v" : "...-", "w" : ".--", "x" : "-..-", "y" : "-.--", "z" : "--..", "=" : "-...-", - "/" : "-..-.", "+" : ".-.-.", "-" : "-....-", "." : ".-.-.-", "," : "--..--", "?" : "..--..", - ":" : "---...", "!" : "-.-.--", "'" : ".----." - } + def mopp(self, speed, msg, for_transmission = True): m = '01' # protocol version m += bin(self.serial)[2:].zfill(6) m += bin(speed)[2:].zfill(6) + _txt = '' + for c in msg: if c == " ": continue # spaces not supported by morserino! - for b in morse[c.lower()]: + if c.lower() not in self.morse: + continue # unknown character + + _txt += c.lower() + for b in self.morse[c.lower()]: if b == '.': m += '01' else: @@ -47,6 +55,10 @@ def mopp(self, speed, msg): m += '00' # EOC + if len(m) <= 14: # empty message + return bytes('','latin_1') + + m = m[0:-2] + '11' # final EOW m = m.ljust(int(8*ceil(len(m)/8.0)),'0') @@ -57,7 +69,12 @@ def mopp(self, speed, msg): #print (m[i:i+8], bytes(chr(int(m[i:i+8],2)),"latin_1"), i, " ENCODER") res += chr(int(m[i:i+8],2)) - self.serial += 1 + if for_transmission: + logging.debug("Encoding message with "+str(speed)+" wpm: "+str(_txt)) + self.serial += 1 + if self.serial >= 64: + self.serial = 0 + return bytes(res,'latin_1') # WATCH OUT: UNICODE MAKES MULTI-BYTES def _stripheader(self, msg): @@ -65,7 +82,7 @@ def _stripheader(self, msg): return res def msg_strcmp (self, data_bytes, speed, msg): - if self._stripheader(data_bytes) == self._stripheader(self.mopp(speed, msg)): + if self._stripheader(data_bytes) == self._stripheader(self.mopp(speed, msg, False)): return True else: return False @@ -104,7 +121,9 @@ def decode_message (self, data_bytes): s = n[i:i+2] msg += self._mopp2morse(s) - return {"Protocol": protocol, "Serial": serial, "Speed": speed, "Message": msg} + txt = self._morse2txt(msg) + + return {"Protocol": protocol, "Serial": serial, "Speed": speed, "Message": msg, "Text": txt} def _mopp2morse(self, sym): @@ -121,5 +140,19 @@ def _mopp2morse(self, sym): logging.debug ("This should not happen: symbol ", s) return s - def _morse2txt(self, morse): - return \ No newline at end of file + def _morse2txt(self, ditcode): + s = "" + for word in ditcode.split("EOW"): + if word == '': + continue + for letter in word.split("EOC"): + if letter == '': + continue + if letter in self.morse_inv: + s += self.morse_inv[letter] + else: + s += "<" + letter + ">" + + s += " " + + return s.strip() diff --git a/udp_chat_server.py b/udp_chat_server.py index 3004ebf..176b3ed 100644 --- a/udp_chat_server.py +++ b/udp_chat_server.py @@ -82,12 +82,11 @@ def reject(client, speed): pass # clean clients list - for c in receivers.items(): - # FIXME: RuntimeError: dictionary changed size during iteration + for c in list(receivers.items()): if c[1] + config.CLIENT_TIMEOUT < time.time(): ip,port = c[0].split(':') bye_msg = ':bye' transmit(mopp.mopp(config.CHAT_WPM, bye_msg), ip, int(port)) del receivers[c[0]] logging.debug ("Removing expired client %s" % c[0]) - \ No newline at end of file +