Ejemplo n.º 1
0
    def message_received(self, message):
        self.log.debug('Message data: ' + message['data'])

        if message['TER'] == True:
            self.log.debug('Terminating Communications...')
            sys.exit(0)

        if message['actor'] == 'Chan':
            self.chan_termination -= 1
            if self.chan_termination <= 0:
                message['RST'] = True
        elif message['FIN'] == True or message[
                'data'] == 'Okay Goodbye' or 'FIN' in message['data']:
            self.log.debug('Ann connection terminated')
            sys.exit(0)

        #Send new packet in response
        send = Packet(src_port=self.routes['Ann'],
                      dest_port=self.routes[message['actor']])

        #Connection with Chan to be terminated
        if message['RST'] == True and self.chan_termination <= 0:
            self.log.debug('Terminating connection with Chan...')
            send.data = 'TER'
            send.actor = 'Ann'
            send.TER = True
            send.RST = True
            deliverable = send.serialize().encode()
            self.log.debug('Sending packet to Router A')
            self.sockets['A'].sendall(deliverable)
        else:
            #Acknowledge packet, check for urgent phrases, accept message input
            send.acknowledgement(message)
            self.log.debug('Returning acknowledgment')

            if '32° 43’ 22.77” N,97° 9’ 7.53” W' in message['data']:
                send.URG = True
                send.auth_code = 'PEPPER THE PEPPER'

            if 'CONGRATULATIONS WE FRIED DRY GREEN LEAVES' in message['data']:
                send.URG = True
                send.data = 'Congrats Meet me at this location: 32.76” N, -97.07” W'
            else:
                send.data = input('What is your message: ')

            send.actor = 'Ann'
            self.log.debug('Sending packet to Router A')
            send.pretty_print()
            deliverable = send.serialize().encode()
            self.sockets['A'].sendall(deliverable)