Exemplo n.º 1
0
class enlace(object):
    """ This class implements methods to the interface between Enlace and Application
    """
    def __init__(self, name):
        """ Initializes the enlace class
        """
        self.fisica = fisica(name)
        self.rx = RX(self.fisica)
        self.tx = TX(self.fisica)
        self.connected = False

    def enable(self):
        """ Enable reception and transmission
        """
        self.fisica.open()
        self.rx.threadStart()
        self.tx.threadStart()

    def disable(self):
        """ Disable reception and transmission
        """
        self.rx.threadKill()
        self.tx.threadKill()
        time.sleep(1)
        self.fisica.close()

    ################################
    # Application  interface       #
    ################################
    def sendData(self, data):
        """ Send data over the enlace interface
        """
        print("ENVIANDO DATA")
        data = Pacote(data, "data").empacota()
        self.tx.sendBuffer(data)
        print("***ENVIANDO DATA***")

    def getData(self):
        """ Get n data over the enlace interface
        Return the byte array and the size of the buffer
        """
        package = self.rx.getHeadPayload()
        print(binascii.hexlify(package), "getData", "linha 57")
        data = desempacota(package)
        print("***RECEBENDO DATA***")
        return (data[0], data[1], (len(data[0])), data[2])

    def sendACK(self):
        package = Pacote(None, "ACK").empacota()
        print("***ENVIANDO ACK***")
        self.tx.sendBuffer(package)

    def sendNACK(self):
        package = Pacote(None, "NACK").empacota()
        print("***ENVIANDO NACK***")
        self.tx.sendBuffer(package)

    def sendSync(self):
        package = Pacote(None, "sync").empacota()
        self.tx.sendBuffer(package)
        print("***ENVIANDO SYNC***")

    def waitConnection(self):
        print("Preparing to receive image")
        while self.connected == False:
            response = self.getData()
            print("Waiting sync...")
            if response[3] == "sync":
                print("Sync received")
                self.sendSync()
                print("***ENVIOU SYNC***")
                time.sleep(3)
                self.sendACK()
                time.sleep(3)
                print("ACK SENT")
                response = self.getData()
                time.sleep(3)
                print("Waiting ACK..")
                if response[3] == "ACK" or "sync":
                    print("***RECEBEU ACK OU SYNC***")
                    print("Ready to receive package")
                    return True
            else:
                print("***NÃO RECEBEU SYNC INICIAL***")
                return False

    def establishConnection(self):
        print("Preparing to send image")
        timeout = False
        print("Sending sync...")
        comeco = time.time()

        while self.connected == False:
            if timeout:
                timeout = False
                comeco = time.time()
                print("--Waiting sync...")
                if self.rx.getIsEmpty() == False:
                    self.sendSync()
                    time.sleep(0.3)
                    print("***ENVIOU SYNC***")
                    response = self.getData()
                    if response[3] == "sync":
                        print("Sync received")
                        response = self.getData()
                        print("Waiting ACK..")
                        if response[3] == "ACK":
                            print("ACK received")
                            time.sleep(0.3)
                            self.sendACK()
                            print("***ENVIOU ACK***")
                            print("Connection established")
                            return True
                    else:
                        print("***NÃO RECEBEU SYNC INICIAL***")
                        return False
            else:
                if ((time.time() - comeco) > 5):
                    print("Passou 3 s")
                    self.sendSync()
                    print("***ENVIOU SYNC timeout***")
                    timeout = True
Exemplo n.º 2
0
class enlace(object):
    """ This class implements methods to the interface between Enlace and Application
    """

    def __init__(self, name):
        """ Initializes the enlace class
        """
        self.fisica      = fisica(name)
        self.rx          = RX(self.fisica)
        self.tx          = TX(self.fisica)
        self.connected   = False

    def enable(self):
        """ Enable reception and transmission
        """
        self.fisica.open()
        self.rx.threadStart()
        self.tx.threadStart()

    def disable(self):
        """ Disable reception and transmission
        """
        self.rx.threadKill()
        self.tx.threadKill()
        time.sleep(1)
        self.fisica.close()

    ################################
    # Application  interface       #
    ################################
    def sendData(self, data):
        """ Send data over the enlace interface
        """
        package = Package(data,"data").buildPackage()
        self.tx.sendBuffer(package)


    def sendACK(self):
        package = Package(None,"ACK").buildPackage()
        self.tx.sendBuffer(package)
    
    def sendNACK(self):
        package = Package(None,"NACK").buildPackage()
        self.tx.sendBuffer(package)
    
    def sendSync(self):
        package = Package(None,"sync").buildPackage()
        self.tx.sendBuffer(package)

    def getData(self):
        """ Get n data over the enlace interface
        Return the byte array and the size of the buffer
        """
        
        package = self.rx.getHeadPayload()
        print(package)
        data = undoPackage(package)
        #print(data)
        return(data[0], data[1],(len(data[0])),data[2])
        

    def waitConnection(self):
        while self.connected ==  False:
            response = self.getData()
            print("Waiting sync...")
            if response[3] == "sync":
                print("Sync received")
                self.sendSync()
                time.sleep(0.5)
                self.sendACK()
                print("ACK SENT")
                response = self.getData()
                if response[3] == "ACK":
                    print("Ready to receive package")
                    return True
            else:
                return False

        
    def establishConnection(self):
        timeout = False
        print("Waiting sync...")
        comeco = time.time()
        while self.connected ==  False:
            if timeout:
                timeout=False
                comeco = time.time()
                print("--Waiting sync...")
                if self.rx.getIsEmpty() == False:
                    self.sendSync()
                    response = self.getData()
                    if response[3] == "ACK" or "sync":
                        print("Sync received")
                        response = self.getData()
                        if response[3] == "sync" or "ACK":
                            print("ACK received")
                            time.sleep(0.5)
                            self.sendACK()
                            return True
                    else:
                        return False      
            else:
                if ((time.time() - comeco) > 3):
                    print ("Passou 3 s")
                    self.sendSync()
                    timeout = True
Exemplo n.º 3
0
class enlace(object):
    """ This class implements methods to the interface between Enlace and Application
    """
    def __init__(self, name):
        """ Initializes the enlace class
        """
        self.fisica = fisica(name)
        self.rx = RX(self.fisica)
        self.tx = TX(self.fisica)
        self.connected = False

    def enable(self):
        """ Enable reception and transmission
        """
        self.fisica.open()
        self.rx.threadStart()
        self.tx.threadStart()

    def disable(self):
        """ Disable reception and transmission
        """
        self.rx.threadKill()
        self.tx.threadKill()
        time.sleep(1)
        self.fisica.close()

    ################################
    # Application  interface       #
    ################################

    def sendData(self, data):
        """ Send data over the enlace interface
        """
        package = Empacota(data, "data", 1, 1).buildPackage()
        self.tx.sendBuffer(package)

    def getData(self):
        """ Get n data over the enlace interface
        Return the byte array and the size of the buffer
        """

        package = self.rx.getHeadPayload()
        data = desempacota(package)

        return (data[0], data[1], (len(data[0])), data[2])

    def sendAck(self):
        #Envia os ACKs para autorizar inicio da conexão ou confirmar recebimentoss
        package = Empacota(None, "ACK", 1, 1).buildPackage()
        self.tx.sendBuffer(package)

    def sendNack(self):
        #Avisa que pacote chegou corrompido
        package = Empacota(None, "NACK", 1, 1).buildPackage()
        self.tx.sendBuffer(package)

    def sendSync(self):
        #Para estabelecer conexão
        package = Empacota(None, "sync", 1, 1).buildPackage()
        self.tx.sendBuffer(package)

    def waitConnection(self):  #Papel do Server
        #print("connected", connected)
        #Fica conferindo recebimento do sync e se recebe, confirma enviando ack. Depois envia o sync e confirma se recebeu ack de confirmação.
        while self.connected == False:
            print("Loop waitconnection")
            response = self.getData()
            print("Waiting sync...")
            #print("response", response)
            if response[3] == "sync":
                print("Sync received")
                self.sendSync()
                time.sleep(0.5)
                self.sendAck()
                print("ACK SENT")
                response = self.getData()
                if response[3] == "ACK":
                    print("Ready to receive package")
                    return True
                else:
                    print("falhou")
                    time.sleep(1)
                    self.sendNack()
                    return False
            else:
                print("falhou")
                time.sleep(1)
                self.sendNack()
                return False

    def establishConnection(self):  #Papel do Client
        #Envia o Sync para iniciar e pega a resposta. Se tiver Ack (confirmação de recebimento sync), procura pelo recebimento de sync e envia ack.
        while self.connected == False:
            self.sendSync()
            response = self.getData()
            print("Waiting sync...")
            if response[3] == "ACK" or "sync":
                print("Sync received")
                response = self.getData()
                if response[3] == "sync" or "ACK":
                    print("ACK received")
                    time.sleep(0.5)
                    self.sendAck()
                    return True
                elif response[3] == "NACK":
                    return False
            else:
                return False

    def verifyPackage(self, data):
        package = Package(data).buildPackage()
        sizePack = len(package)
        expected = len(self.getData())
        if sizePack == expected:
            self.sendAck()
            return True
        else:
            self.sendNack()
            return False