Beispiel #1
0
class SourcePeerService(PeerService):

    def __init__(self, ip, port, tracker_url):
        super(SourcePeerService, self).__init__(ip, port, tracker_url)

        self.splitter = None
        self.stream_client  = None

        self._create_splitter()

    def _peer_logic(self):
        self._send_requested_pieces()

    def _packet_created(self, splitter):
        packet = splitter.pop_packet()
        self.receive_packet(packet, None)
        logging.debug('Packet created. Seq: {0}'.format(packet.sequence))

    def _input_stream_end(self, splitter):
        self.joiner.end_join()

    def _create_splitter(self):
        self.splitter = Splitter(SPLIT_PACKET_SIZE)
        self.splitter.on_new_packet.add_handler(self._packet_created)
        self.splitter.on_stream_end.add_handler(self._input_stream_end)

    def _stream_received(self, data):
        self.splitter.push_stream(data)

    def _stream_end(self):
        self.splitter.end_stream()

    def attach_stream_client(self, client):
        self.stream_client = client
        client.on_stream_received.add_handler(self._stream_received)
        client.on_stream_end.add_handler(self._stream_end)
    def test_splitter_joiner_(self):
        SIZE = 4 * (10**3)
        CHUNK_SIZE = SIZE / 100

        self.initial_data = ''.join(random.choice(string.letters)
                                    for _ in range(SIZE))

        splitter = Splitter(CHUNK_SIZE)
        joiner = Joiner()

        packets = []
        self.buffer = bytes()

        def on_data_joined(sender):
            self.buffer += joiner.pop_stream()

        def on_end_join(sender):
            self.assertEqual(self.buffer, self.initial_data)

        def on_new_packet(sender):
            packets.append(sender.pop_packet())

        def on_stream_end(sender):
            random.shuffle(packets)
            for packet in packets:
                joiner.push_packet(packet)
            joiner.end_join()

        splitter.on_new_packet.add_handler(on_new_packet)
        splitter.on_stream_end.add_handler(on_stream_end)
        joiner.on_data_joined.add_handler(on_data_joined)
        joiner.on_end_join.add_handler(on_end_join)

        for char in self.initial_data:
            splitter.push_stream(char)
        splitter.end_stream()
Beispiel #3
0
 def _create_splitter(self):
     self.splitter = Splitter(SPLIT_PACKET_SIZE)
     self.splitter.on_new_packet.add_handler(self._packet_created)
     self.splitter.on_stream_end.add_handler(self._input_stream_end)