From ca1adcb090596879c2e062d219b78533fc70b58f Mon Sep 17 00:00:00 2001 From: "pierre.johner" <pierre.johner@etu.hesge.ch> Date: Sun, 22 Jan 2017 14:37:54 +0100 Subject: [PATCH] Upload new file --- server.py | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 server.py diff --git a/server.py b/server.py new file mode 100644 index 0000000..adf3581 --- /dev/null +++ b/server.py @@ -0,0 +1,181 @@ +#Author Rochat Johner Ares + +import sys +from socket import * +import file_class +import simu_perte_class +import struct +import md5 + +#Check if the arg are valide +def controle(argv): + flag = True + + if not argv[1].isdigit(): + print("Numero Port Invalide") + flag = False + + if not argv[2].isdigit(): + print("Taux de Perte Invalide") + flag = False + + return flag + +#State Machine +class Statemachine: + def __init__(self, canal): + self.currentstate = 0 + self.timeOut = 0 + self.counter = 0 + self.canal = canal + self.counter = 1 + + print("Start Server") + state = { + 0 : self.state0, + 1 : self.state1, + 2 : self.state2, + 3 : self.state3, + 4 : self.state4, + } + + #Choose the state + while True: + state[self.currentstate]() + + #Generate the first packet (DATA OR ACK) + def state0(self): + print("Start state 0") + self.counter = 1 + self.file = file_class.File("") + self.file.RRQ() + + s = socket(AF_INET, SOCK_DGRAM) + #Bind to all address + s.bind(("0.0.0.0", 69)) + self.canal.setSocket(s) + + d = self.file.getPacket().receive(self.canal) + + s = socket(AF_INET, SOCK_DGRAM) + #Bind to all address + s.bind(("0.0.0.0", 50001)) + s.settimeout(5.0) + self.canal.setSocket(s) + + d = struct.unpack(">h"+str(len(d)-9)+"s?5s?", d) + self.file = file_class.File(d[1]) + + if d[0] == 1: + self.file.WRQ() + self.file.data.seek(0) + x = self.file.data.read(512) + self.file.Data(1, x) + packet = self.file.getPacket() + packet.send(self.canal) + if len(x) < 512: + self.currentstate = 4 + else: + self.currentstate = 2 + else: + self.file.ACK(0) + packet = self.file.getPacket() + packet.send(self.canal) + self.currentstate = 1 + + print("End state 0") + + #State WRQ + def state1(self): + print("Start state 1") + try : + d = self.file.getPacket().receive(self.canal); + self.timeOut = 0 + d = struct.unpack(">hh"+str(len(d)-4)+"s", d); + if d[0] == 5: + self.currentstate = 3 + else: + self.file.ACK(d[1]) + packet = self.file.getPacket() + packet.send(self.canal) + if d[1] == self.counter: + self.file.receivePacket(d[2]) + self.counter += 1 + if len(d[2]) < 512: + self.currentstate = 0 + m = md5.new() + m.update(self.file.data) + result = m.hexdigest() + print(result) + + except timeout: + packet = self.file.getPacket() + packet.send(self.canal) + self.timeOut += 1 + print("timeOut") + if self.timeOut > 4 : + self.timeOut = 0 + self.currentstate = 3 + print("End state 1") + + #State RRD + def state2(self): + print("Start state 2") + try : + d = self.file.getPacket().receive(self.canal); + self.timeOut = 0 + d = struct.unpack(">hh", d); + self.file.data.seek((d[1])*512) + x = self.file.data.read(512) + self.file.Data(d[1]+1, x) + packet = self.file.getPacket() + packet.send(self.canal) + if len(x) < 512: + self.currentstate = 4 + except timeout: + packet = self.file.getPacket() + packet.send(self.canal) + self.timeOut += 1 + print("timeOut") + if self.timeOut > 4 : + self.timeOut = 0 + self.currentstate = 3 + print("End state 2") + + #Error + def state3(self): + print("Start state 3") + print("Error TimeOut or FileNotFound") + self.currentstate = 0 + print("End state 3") + + #Last data for get request + def state4(self): + print("Start state 4") + try : + d = self.file.getPacket().receive(self.canal); + self.timeOut = 0 + self.currentstate = 0 + except timeout: + packet = self.file.getPacket() + packet.send(self.canal) + self.timeOut += 1 + print("timeOut") + if self.timeOut > 4 : + self.timeOut = 0 + self.currentstate = 3 + print("End state 4") + +# Entry of Progm +if len(sys.argv) != 3: + print("Mauvais Nombre Argument : mytftps <numero_de_port> <taux_de_perte>") +else: + if controle(sys.argv): + + canal = simu_perte_class.Canal(sys.argv[2], None, "",0) + + print "Wait on {}".format(sys.argv[1]) + + stateMachine = Statemachine(canal) + + s.close() \ No newline at end of file -- GitLab