Client/model/Protocol.py

89 lines
3.3 KiB
Python
Raw Normal View History

2022-02-02 01:43:38 +01:00
import re
class Protocol:
2022-02-02 01:43:38 +01:00
RX_DIGIT = r"[0-9]"
RX_SIZE = r"(" + RX_DIGIT + "{1,10})"
RX_LINE = r"(\\x0d\\x0a){0,1}"
RX_PASSCHAR = r"[\x22-\xff]"
RX_BL = r" "
RX_FILENAME = r"(" + RX_PASSCHAR + "{1,20})"
RX_SIGNOK = r"SIGN_OK" + RX_LINE
RX_SIGNERROR = r"SIGN_ERROR" + RX_LINE
RX_FILES = r"FILES((" + RX_BL + RX_FILENAME + "!" + RX_SIZE + "){0,50})" + RX_LINE
RX_SAVEFILEOK = r"SAVEFILE_OK" + RX_LINE
RX_SAVEFILEERROR = r"SAVEFILE_ERROR" + RX_LINE
RX_GETFILEOK = r"GETFILE_OK" + RX_BL + RX_FILENAME + RX_BL + RX_SIZE + RX_LINE
RX_GETFILEERROR = r"GETFILE_ERROR" + RX_LINE
RX_REMOVEFILEOK = r"REMOVEFILE_OK" + RX_LINE
RX_REMOVEFILEERROR = r"REMOVEFILE_ERROR" + RX_LINE
ALL_MESSAGES = [RX_SIGNOK, RX_SIGNERROR, RX_FILES, RX_SAVEFILEOK, RX_SAVEFILEERROR, RX_GETFILEOK, RX_GETFILEERROR, RX_REMOVEFILEOK, RX_REMOVEFILEERROR]
PARSE_UNKNOWN = -1
PARSE_SIGNOK = 0
PARSE_SIGNERROR = 1
PARSE_FILES = 2
PARSE_SAVEFILEOK = 3
PARSE_SAVEFILEERROR = 4
PARSE_GETFILEOK = 5
PARSE_GETFILEERROR = 6
PARSE_REMOVEFILEOK = 7
PARSE_REMOVEFILEERROR = 8
MSG_CLIENT_SIGNIN = "SIGNIN <login> <password>\r\n"
MSG_CLIENT_SIGNUP = "SIGNUP <login> <password>\r\n"
MSG_CLIENT_FILELIST = "FILELIST\r\n"
MSG_CLIENT_SAVEFILE = "SAVEFILE <filename> <size>\r\n"
MSG_CLIENT_GETFILE = "GETFILE <filename>\r\n"
MSG_CLIENT_REMOVEFILE = "REMOVEFILE <filename>\r\n"
MSG_CLIENT_SIGNOUT = "SIGNOUT\r\n"
def build_SIGNIN(self, login, password):
return Protocol.MSG_CLIENT_SIGNIN.replace("<login>", login).replace("<password>", password)
def build_SIGNUP(self, login, password):
return Protocol.MSG_CLIENT_SIGNUP.replace("<login>", login).replace("<password>", password)
def build_FILELIST(self):
return Protocol.MSG_CLIENT_FILELIST
def build_SAVEFILE(self, filename, size):
return Protocol.MSG_CLIENT_SAVEFILE.replace("<filename>", filename).replace("<size>", str(size))
def build_GETFILE(self, filename):
return Protocol.MSG_CLIENT_GETFILE.replace("<filename>", filename)
def build_REMOVEFILE(self, filename):
return Protocol.MSG_CLIENT_REMOVEFILE.replace("<filename>", filename)
def build_SIGNOUT(self):
return Protocol.MSG_CLIENT_SIGNOUT
def parse(self, line, debug_enabled):
for i in range(len(Protocol.ALL_MESSAGES)):
if(re.match(Protocol.ALL_MESSAGES[i], line) != None):
if(debug_enabled):
print("OK REGEXP:" + Protocol.ALL_MESSAGES[i])
return i
else:
if(debug_enabled):
print("KO REGEXP:" + Protocol.ALL_MESSAGES[i])
return Protocol.PARSE_UNKNOWN
def parse_FILES(self, line):
if(self.parse(line, False) == Protocol.PARSE_FILES):
tokens = re.match(Protocol.RX_FILES, line)
return [tokens[1]]
return None
def parse_GETFILEOK(self, line):
if(self.parse(line, False) == Protocol.PARSE_GETFILEOK):
tokens = re.match(Protocol.RX_GETFILEOK, line)
return [tokens[1], tokens[2]]
return None
if __name__ == '__main__':
protocol = Protocol()
line = input("> ")
while(line != ""):
val = protocol.parse(line, True)
print("Val =" + str(val))
line = input("> ")