Example #1
0
    def _push_if_expected_sequence(self, packet):

        if packet.get_sequence_number() == self._next_sequence_number:

            data = packet._payload.split('|')
            payload = data[1]

            self.log.debug('Before Updated 2 Message: %s', self._message)
            if payload in self._message:
                self.log.debug('This content is already in here.')
            else:
                self._message += payload
            self.log.debug('After Updated Message: %s', self._message)

            # [1] Never send packets directly!
            self._packet_sender.send(
                Packet.createAcknowledgementPacket(
                    packet.get_sequence_number(),
                    self._packet_sender._transport.guid,
                    self._packet_sender._transport.pubkey))
            self._next_sequence_number += 1

            self._packets.seek()
            if self._packets.hasNext():
                self._push_if_expected_sequence(self._packets.nextValue())
Example #2
0
    def _push_if_expected_sequence(self, packet):

        if packet.get_sequence_number() == self._next_sequence_number:

            data = packet._payload.split('|')
            payload = data[1]

            self.log.debug('Before Updated 2 Message: %s', self._message)
            if payload in self._message:
                self.log.debug('This content is already in here.')
            else:
                self._message += payload
            self.log.debug('After Updated Message: %s', self._message)

            # [1] Never send packets directly!
            self._packet_sender.send(Packet.createAcknowledgementPacket(packet.get_sequence_number(),
                                                                        self._packet_sender._transport.guid,
                                                                        self._packet_sender._transport.pubkey))
            self._next_sequence_number += 1

            self._packets.seek()
            if self._packets.hasNext():
                self._push_if_expected_sequence(self._packets.nextValue())
Example #3
0
 def get_packet(i, pdata):
     packet = Packet(
         float(i) + self._base_sequence_number, pdata, not i,
         i == (len(window) - 1))
     return PendingPacket(packet, self._packet_sender)
Example #4
0
        def on_message(msg):

            data, addr = msg[0], msg[1]
            self.log.debug('Got Packet: %s from %s', data, addr)
            relayed_message = False

            # Punch message
            if data[:5] == 'punch':
                data = data.split(' ')
                guid = data[1]
                peer = self.dht.routing_table.get_contact(guid)
                if peer:
                    peer.reachable = True
                    peer.relaying = False
                    peer._rudp_connection._sender._packet_sender.reachable = True
                    peer._rudp_connection._sender._packet_sender.relaying = False
                else:
                    self.log.debug('Do not know about this peer yet.')
                return

            try:

                # Relayed message
                if data[:6] == 'relay ':
                    msg_parts = data.split(' ', 1)
                    data = msg_parts[1]
                    data_body = json.loads(data)
                    hostname = data_body.get('hostname')
                    port = data_body.get('port')
                    relayed_message = True
                else:
                    data_body = json.loads(data)
                    port = addr[1]
                    hostname = addr[0]

                # Peer metadata
                guid = data_body.get('guid')
                pubkey = data_body.get('pubkey')
                nickname = data_body.get('nick')
                nat_type = data_body.get('nat_type')

                inbound_peer = self.dht.add_peer(hostname, port, pubkey, guid,
                                                 nickname, nat_type)

                if inbound_peer:
                    inbound_peer.reachable = True

                    if relayed_message:
                        inbound_peer._rudp_connection._sender._packet_sender.relaying = True

                    packet = Packet(data, packet_buffer=True)

                    if packet._finish:

                        inbound_peer.reset()
                        return
                        # del self._connections[address_key]
                    else:

                        inbound_peer._rudp_connection.receive(packet)

                else:
                    self.log.debug('Did not find a peer')
            except Exception as e:
                self.log.error('Could not deserialize message: %s', e)
Example #5
0
    def receive(self, packet):

        self.log.debug('Receive Packet #%s', packet.get_sequence_number())

        try:
            packet_data = packet._payload.split('|')

            message_id = packet_data[0]
            message_size = packet_data[1]
            payload = packet_data[2]

            if message_id not in self.incoming_messages:
                message = IncomingMessage(message_id, message_size)
                self.incoming_messages[message_id] = message

                # pylint: disable=unused-variable
                @message.ee.on('complete')
                def on_complete(body):
                    self.log.debug('IncomingMessage Complete')
                    self.ee.emit('data', {
                        'payload': message.body,
                        'size': message.size
                    })
            else:
                message = self.incoming_messages[message_id]

            # Process Packet Contents
            if packet._synchronize:
                if not message.synced:
                    self.log.debug('Receive Sync Packet')
                    if packet._sequenceNumber == message._sync_sequence_number:
                        return

                    self.log.debug('Inserting Packet #%s',
                                   packet._sequenceNumber)
                    message._packets.insertSorted(packet)

                    if not message.waiting:
                        message.body = payload
                    else:
                        self.log.debug('Appending to Waiting Message: %s',
                                       self._message)
                        message.add_to_body(payload)

                    message._next_sequence_number = packet._sequenceNumber + 1
                    message.synced = True
                    message._sync_sequence_number = packet._sequenceNumber

                    if packet._reset:
                        message.reset()

                    self._packet_sender.send(
                        Packet.createAcknowledgementPacket(
                            packet._sequenceNumber,
                            self._packet_sender._transport.guid,
                            self._packet_sender._transport.pubkey))

                    return

            elif packet._reset:
                self.log.debug('Receive Reset Packet')

                if message._next_sequence_number == packet.get_sequence_number(
                ):
                    if payload in message.body:
                        self.log.debug('This content is already in here.')
                        return
                    else:
                        message.body += payload
                    self.log.debug('Message Updated: %s', message.body)
                    message.reset()
                self._packet_sender.send(
                    Packet.createAcknowledgementPacket(
                        packet._sequenceNumber,
                        self._packet_sender._transport.guid,
                        self._packet_sender._transport.pubkey))
                return
            else:
                self.log.debug('Receive Inside Packet')

                if message._packets.count(packet) == 0:
                    message._packets.insertSorted(packet)
                    if packet.get_sequence_number(
                    ) == message._next_sequence_number:
                        if payload in message.body:
                            self.log.debug('This content is already in here.')
                            return
                        else:
                            message.body += payload
                        message._next_sequence_number += 1
                        # message._packets.seek()
                        # if message._packets.hasNext():
                        #     self._push_if_expected_sequence(self._packets.nextValue())
                    self._packet_sender.send(
                        Packet.createAcknowledgementPacket(
                            packet._sequenceNumber,
                            self._packet_sender._transport.guid,
                            self._packet_sender._transport.pubkey))
                else:
                    self.log.debug('Already have this packet')

            # Ignores packets that have a sequence number less than the next sequence
            # number
            # if not packet._synchronize and packet._sequenceNumber < self._sync_sequence_number:
            #     self.log.debug('Just ignoring this packet')
            #     return

            # if packet._synchronize and not self._synced:
            #
            #     # This is the beginning of the stream.
            #     self.log.debug('Beginning of stream %s %s', packet._sequenceNumber, self._sync_sequence_number)
            #
            #     data = packet._payload.split('|', 2)
            #
            #     if len(data) > 1:
            #         self._message_id = data[0]
            #         self._message_size = data[1]
            #         packet._payload = data[2]
            #         self.log.debug('Message #%s (%s bytes): %s', self._message_id,
            #                        self._message_size, packet._payload)
            #
            #     self._packet_sender.send(Packet.createAcknowledgementPacket(
            #         packet._sequenceNumber,
            #         self._packet_sender._transport.guid,
            #         self._packet_sender._transport.pubkey
            #     ))
            #
            #     if packet._sequenceNumber == self._sync_sequence_number:
            #         return
            #
            #     # Send the packet upstream, send acknowledgement packet to end host, and
            #     # increment the next expected packet.
            #     self._packets.clear()
            #
            #     self.log.debug('Inserting Packet #%s', packet._sequenceNumber)
            #     # self.log.debug('Before Packets: %s', self._packets)
            #     self._packets.insertSorted(packet)
            #
            #     if not self._waiting:
            #         self._message = packet._payload
            #     else:
            #         self.log.debug('Appending to Waiting Message: %s', self._message)
            #         self._message += packet._payload
            #
            #     self.log.debug('Updated Message: %s', self._message)
            #
            #     self._next_sequence_number = packet._sequenceNumber + 1
            #     self._synced = True
            #     self._sync_sequence_number = packet._sequenceNumber
            #
            #     if packet._reset:
            #         self.reset()
            #
            #     return
            #
            # elif not self._synced:
            #     # If we are not synchronized with sender, then this means that we should
            #     # wait for the end host to send a synchronization packet.
            #
            #     # We are done.
            #     self.log.debug('Got an out of order packet.')
            #     return
            #
            # elif packet._sequenceNumber < self._sync_sequence_number:
            #     # This is a troll packet. Ignore it.
            #     self.log.debug('Ignoring packet out of the current window.')
            #     return
            #
            # elif packet._sequenceNumber >= (self._packets.currentValue()._sequenceNumber
            #                                 + rudp.constants.WINDOW_SIZE):
            #     # This means that the next packet received is not within the window size.
            #     self.ee.emit('_window_size_exceeded')
            #     self.log.debug('Ignoring packet out of the current window.')
            #
            #     return
            #
            # elif packet._reset:
            #
            #     data = packet._payload.split('|')
            #     payload = data[1]
            #
            #     self.log.debug(data)
            #     if self._message != '' and data[0] == self._message_id:
            #         self.log.debug('Message Before Appending: %s', self._message)
            #         self._message += payload
            #         self.log.debug('After Updated Message: %s', self._message)
            #         self._packet_sender.send(Packet.createAcknowledgementPacket(
            #             packet._sequenceNumber,
            #             self._packet_sender._transport.guid,
            #             self._packet_sender._transport.pubkey
            #         ))
            #         self.reset()
            #         return

        except Exception as e:
            self.log.error(e)
Example #6
0
    def receive(self, packet):

        self.log.debug('Receive Packet #%s', packet.get_sequence_number())

        try:
            packet_data = packet._payload.split('|')

            message_id = packet_data[0]
            message_size = packet_data[1]
            payload = packet_data[2]

            if message_id not in self.incoming_messages:
                message = IncomingMessage(message_id, message_size)
                self.incoming_messages[message_id] = message

                # pylint: disable=unused-variable
                @message.ee.on('complete')
                def on_complete(body):
                    self.log.debug('IncomingMessage Complete')
                    self.ee.emit('data', {'payload': message.body, 'size': message.size})
            else:
                message = self.incoming_messages[message_id]

            # Process Packet Contents
            if packet._synchronize:
                if not message.synced:
                    self.log.debug('Receive Sync Packet')
                    if packet._sequenceNumber == message._sync_sequence_number:
                        return

                    self.log.debug('Inserting Packet #%s', packet._sequenceNumber)
                    message._packets.insertSorted(packet)

                    if not message.waiting:
                        message.body = payload
                    else:
                        self.log.debug('Appending to Waiting Message: %s', self._message)
                        message.add_to_body(payload)

                    message._next_sequence_number = packet._sequenceNumber + 1
                    message.synced = True
                    message._sync_sequence_number = packet._sequenceNumber

                    if packet._reset:
                        message.reset()

                    self._packet_sender.send(Packet.createAcknowledgementPacket(
                        packet._sequenceNumber,
                        self._packet_sender._transport.guid,
                        self._packet_sender._transport.pubkey
                    ))

                    return

            elif packet._reset:
                self.log.debug('Receive Reset Packet')

                if message._next_sequence_number == packet.get_sequence_number():
                    if payload in message.body:
                        self.log.debug('This content is already in here.')
                        return
                    else:
                        message.body += payload
                    self.log.debug('Message Updated: %s', message.body)
                    message.reset()
                self._packet_sender.send(Packet.createAcknowledgementPacket(
                    packet._sequenceNumber,
                    self._packet_sender._transport.guid,
                    self._packet_sender._transport.pubkey
                ))
                return
            else:
                self.log.debug('Receive Inside Packet')

                if message._packets.count(packet) == 0:
                    message._packets.insertSorted(packet)
                    if packet.get_sequence_number() == message._next_sequence_number:
                        if payload in message.body:
                            self.log.debug('This content is already in here.')
                            return
                        else:
                            message.body += payload
                        message._next_sequence_number += 1
                        # message._packets.seek()
                        # if message._packets.hasNext():
                        #     self._push_if_expected_sequence(self._packets.nextValue())
                    self._packet_sender.send(Packet.createAcknowledgementPacket(
                        packet._sequenceNumber,
                        self._packet_sender._transport.guid,
                        self._packet_sender._transport.pubkey
                    ))
                else:
                    self.log.debug('Already have this packet')

            # Ignores packets that have a sequence number less than the next sequence
            # number
            # if not packet._synchronize and packet._sequenceNumber < self._sync_sequence_number:
            #     self.log.debug('Just ignoring this packet')
            #     return

            # if packet._synchronize and not self._synced:
            #
            #     # This is the beginning of the stream.
            #     self.log.debug('Beginning of stream %s %s', packet._sequenceNumber, self._sync_sequence_number)
            #
            #     data = packet._payload.split('|', 2)
            #
            #     if len(data) > 1:
            #         self._message_id = data[0]
            #         self._message_size = data[1]
            #         packet._payload = data[2]
            #         self.log.debug('Message #%s (%s bytes): %s', self._message_id,
            #                        self._message_size, packet._payload)
            #
            #     self._packet_sender.send(Packet.createAcknowledgementPacket(
            #         packet._sequenceNumber,
            #         self._packet_sender._transport.guid,
            #         self._packet_sender._transport.pubkey
            #     ))
            #
            #     if packet._sequenceNumber == self._sync_sequence_number:
            #         return
            #
            #     # Send the packet upstream, send acknowledgement packet to end host, and
            #     # increment the next expected packet.
            #     self._packets.clear()
            #
            #     self.log.debug('Inserting Packet #%s', packet._sequenceNumber)
            #     # self.log.debug('Before Packets: %s', self._packets)
            #     self._packets.insertSorted(packet)
            #
            #     if not self._waiting:
            #         self._message = packet._payload
            #     else:
            #         self.log.debug('Appending to Waiting Message: %s', self._message)
            #         self._message += packet._payload
            #
            #     self.log.debug('Updated Message: %s', self._message)
            #
            #     self._next_sequence_number = packet._sequenceNumber + 1
            #     self._synced = True
            #     self._sync_sequence_number = packet._sequenceNumber
            #
            #     if packet._reset:
            #         self.reset()
            #
            #     return
            #
            # elif not self._synced:
            #     # If we are not synchronized with sender, then this means that we should
            #     # wait for the end host to send a synchronization packet.
            #
            #     # We are done.
            #     self.log.debug('Got an out of order packet.')
            #     return
            #
            # elif packet._sequenceNumber < self._sync_sequence_number:
            #     # This is a troll packet. Ignore it.
            #     self.log.debug('Ignoring packet out of the current window.')
            #     return
            #
            # elif packet._sequenceNumber >= (self._packets.currentValue()._sequenceNumber
            #                                 + rudp.constants.WINDOW_SIZE):
            #     # This means that the next packet received is not within the window size.
            #     self.ee.emit('_window_size_exceeded')
            #     self.log.debug('Ignoring packet out of the current window.')
            #
            #     return
            #
            # elif packet._reset:
            #
            #     data = packet._payload.split('|')
            #     payload = data[1]
            #
            #     self.log.debug(data)
            #     if self._message != '' and data[0] == self._message_id:
            #         self.log.debug('Message Before Appending: %s', self._message)
            #         self._message += payload
            #         self.log.debug('After Updated Message: %s', self._message)
            #         self._packet_sender.send(Packet.createAcknowledgementPacket(
            #             packet._sequenceNumber,
            #             self._packet_sender._transport.guid,
            #             self._packet_sender._transport.pubkey
            #         ))
            #         self.reset()
            #         return

        except Exception as e:
            self.log.error(e)