Skip to content
Snippets Groups Projects
Select Git revision
  • ee863d5e9b0a9b161301080b20bc4b1982218a89
  • master default protected
2 results

client.py

Blame
  • client.py 4.61 KiB
    #Author Rochat Johner Ares
    
    import file_class
    import packet_class
    import simu_perte_class
    import struct
    from socket import *
    import md5
    import sys
    
    #Check if the arg are valide  
    def controle(argv):
        flag = True
        if not (argv[1] == "put" or argv[1] == "get"):
            print("Mode Invalide")
            flag = False
            
        if not argv[3].isdigit():
            print("Numero Port Invalide")
            flag = False
    
        if not argv[5].isdigit():
            print("Taux de Perte Invalide")
            flag = False
            
        return flag
    
    #State Machine
    class Statemachine:
        def __init__(self, canal, filename, action):
            self.canal = canal
            self.action = action
            self.currentstate = 0
            self.timeOut = 0
            self.counter = 1
            self.file = file_class.File(filename)
            
            print("Start Client")
            state = {
        	    0 : self.state0,
        	    1 : self.state1,
        	    2 : self.state2,
        	    3 : self.state3,
        	    4 : self.state4,
        	}
        	
        	#Choose the state
            while True:
                if self.currentstate != 5:
                    state[self.currentstate]()
                else:
                    break
        #Generate the first packet (RRD OR WRQ)        
        def state0(self):
            print("Start state 0")
    
            next = 0
            if self.action == 'get':
                next = 1
                self.file.RRQ()
            else:
                next = 2
                self.file.WRQ()
       
            packet = self.file.getPacket()
            packet.send(self.canal)
            self.currentstate = next
            print("End state 0")
        
        #State RRD
        def state1(self):
            print("Start state 1")
            try :
                d = self.file.getPacket().receive(self.canal);
                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 = 5
                        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 WRQ    
        def state2(self):
            print("Start state 2")
            try :
                d = self.file.getPacket().receive(self.canal);
                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 = 5
            print("End state 3")
        #Last data for send request
        def state4(self):
            print("Start state 4")
            try :
                 d = self.file.getPacket().receive(self.canal);
                 self.currentstate = 5
            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")
        def state5(self):
            print("Start state 5")
            print("End state 5")
    
    # Entry of Progm
    if len(sys.argv) != 6:
        print("Mauvais Nombre Argument : mytftpc <put/get> <nom_DNS_du_serveur> <numero_de_port> <nom_fichier> <taux_de_perte>")
    else:
        if controle(sys.argv):
        
            socket = socket(AF_INET, SOCK_DGRAM)
            #Bind to all address
            socket.bind(("0.0.0.0", 50000))
            socket.settimeout(5.0)
    
            print "Connection on {}".format(sys.argv[3])
            
            canal = simu_perte_class.Canal(sys.argv[5],socket,sys.argv[2],sys.argv[3])
    
            stateMachine = Statemachine(canal,sys.argv[4],sys.argv[1])
    
            socket.close()