def test_average_bitrate_single_packet(self):
     for i in range(10):
         packet_size_bytes = random.uniform(1.0, 8000.0)
         delay_ms = random.uniform(10.0, 300.0)
         packet = Packet(1, 0.0, packet_size_bytes)
         packet.arrival_time_ms = delay_ms
         self.assertEqual(average_bitrate_kbps([packet]), 8.0 * packet_size_bytes / delay_ms)
Ejemplo n.º 2
0
 def run(self):
     while 1:
         msg = self.connection.recv(1024)
         data = bytearray(msg)
         pck = Packet()
         pck.data = data
         self.onpacketdata(pck.getPacketID(), data)
Ejemplo n.º 3
0
 def on_tunnel_received(_, __, data):
     packet = Packet(data)
     addr_list, id_, _ = try_parse_dns_result(packet)
     if addr_list is not None:
         update_blocked_address(addr_list)
         try_restore_dns(packet, id_)
     tundev.send(packet.get_packet())
Ejemplo n.º 4
0
 def run(self):
     while True:
         data, addr = self.server.recv()
         pkg = Packet.unpack(data)
         # print "got packet: %s, %s, %s" % (pkg.tick, pkg.players, pkg.input)
         with self.lock:
             net_tick = self.parent.net_tick
             if addr not in self.clients:
                 if pkg.tick == 0 and len(pkg.players) == 0 and len(pkg.input) == 0:
                     self.clients[addr] = Client(addr, self.next_id, Player(50 + self.next_id * 30, 50))
                     self.next_id += 1
                     pkg_response = Packet(net_tick)
                     pkg_response.add_player(self.clients[addr].id, self.clients[addr].player)
                     self.server.send(pkg_response, addr)
                     print "authenticating new client: %s -> %s" % (addr, self.clients[addr].id)
                 else:
                     print "invalid authentication attempt: %s, %s, %s" % (pkg.tick, pkg.players, pkg.input)
             else:
                 client = self.clients[addr]
                 client.ack()
                 if pkg.tick >= net_tick:
                     if len(pkg.input) > 0:
                         client.events[pkg.tick] = pkg.input
                     # if net_tick not in self.pkg_queue:
                     # self.pkg_queue[net_tick] = []
                     # self.pkg_queue[net_tick].insert(0, pkg)
                 else:
                     print "discard packet (too old: %d < %d)" % (pkg.tick, net_tick)
Ejemplo n.º 5
0
	def transfer_file(self, share_ID, file_name, return_address, start_offset=0, end_offset=999999999999, block_skip=1):
		"""Sends a piece of a file to a peer in pieces.
		Initiated by JSONRPC: get_file()
		start_offset: start sending file from this offset
		end_offset: if this bytes is reached, send no more.
		block_skip: send a block, then skip this many blocks, then send the next etc.
		"""
		#continue to send all file blocks
		full_file_path = os.path.join(self.storage_directory, share_ID, file_name)
		f = open(full_file_path, 'rb')
		f.seek(start_offset)
		block_size = 5
		file_offset = start_offset
		
		logging.debug('file_name: %s start_offset: %d, end_offset: %d' %(file_name, start_offset, end_offset))
		
		while (file_offset < end_offset):  #@TODO: stop sending if given signal from return_address
			logging.debug('file_offset: %d' %(file_offset))
			block_bytes = f.read(64*block_size)
			if block_bytes == "":
				logging.debug('no bytes read from file')
				break
			p_out_block = Packet()
			p_out_block.json_RPC_object = dict(jsonrpc="2.0", method="save_file_block", params=[1.0, None, [self.my_machine_ID, share_ID, file_name, file_offset]], )  #id=rpc_id
			p_out_block.binary_blob = block_bytes
			p_out_block.to_address = return_address
			
			self.send_block_choke(p_out_block, return_address, 3)
			
			time.sleep(0.002)
			file_offset+=block_size
		
		logging.debug('finished file transfer')
		f.close()
Ejemplo n.º 6
0
    def _send_command(self, command, data):
        p = Packet(seq=self.tx_seq, cmd=command, data=data, seq_sync=False)
        msg = p.toMessage(key = self.key)

        self.logger.debug('%s Msg to door: %s'%(self.name, list(p.toMessage())))
        self.interface.writeMessage(self.priority, self.address, msg, self.tx_msg_queue)
        self.tx_seq += 1
Ejemplo n.º 7
0
    def run(self):
        """
        Run the ETL Chain.
        :return:
        """
        log.info('Running Chain: %s' % self.chain_str)

        # One time init for entire Chain
        self.first_comp.do_init()

        # Do ETL as long as input available in Packet
        packet = Packet()
        rounds = 0
        try:
            while not packet.is_end_of_stream():
                # try:
                # Invoke the first component to start the chain
                packet.init()
                packet = self.first_comp.process(packet)
                rounds += 1
                #            except (Exception), e:
                #                log.error("Fatal Error in ETL: %s"% str(e))
                #                break
        finally:
            # Always one time exit for entire Chain
            self.first_comp.do_exit()

        log.info('DONE - %d rounds - chain=%s ' % (rounds, self.chain_str))
Ejemplo n.º 8
0
    def _send_command(self, command, data):
        p = Packet(seq=self.txseq, cmd=command, data=data)
        self.logger.debug("Msg to mastercontroller: %s" % list(p.toMessage()))

        msg = self.aes.encrypt([ord(x) for x in p.toMessage()], self.key, AES.keySize["SIZE_128"])
        msg = "".join([chr(x) for x in msg])
        self.interface.writeMessage(self.address, msg)
Ejemplo n.º 9
0
Archivo: tests.py Proyecto: grapo/ETO
 def test_drop_old_packet(self):
     w1 = Node(name="w1")
     p = Packet(content="X", receiver="w2", sender="w3")
     p._hopcounter = p.MAX_HOP
     w1.drop_packet = FunctionCalledChecker(w1.drop_packet)
     w1.accept(p)
     self.assertTrue(w1.drop_packet.was_called)
Ejemplo n.º 10
0
Archivo: tests.py Proyecto: grapo/ETO
    def test_valid_packet_counter(self):
        p = Packet(content="content", receiver="receiver")
        self.assertFalse(p.is_looped())
        for i in range(Packet.MAX_HOP - 1):
            p.hop()

        self.assertFalse(p.is_looped())
Ejemplo n.º 11
0
 def get(self):
     import sys
     pkt = Packet()
     args = map(lambda v: String(v), sys.argv)
     pkt.item = Array(args)
     pkt.item.append(String("aaa"))
     pkt.item.append(Url("http://www.yahoo.co.jp/"))
     return chain([pkt,])
Ejemplo n.º 12
0
 def __init__(self,source_address=1,source_port=0,
              destination_address=1,destination_port=0,
              ident=0,protocol="TCP",body="",length=0,
              syn=False,ack=False,fin=False,sequence=0,ack_number=0):
     Packet.__init__(self,source_address,source_port,destination_address,
                     destination_port,ident,protocol,body,length)
     self.sequence = sequence
     self.ack_number = ack_number
Ejemplo n.º 13
0
    def packet_read(self, conn, raw_packet):
        """
        Read in a packet and dispatch -- asyncore callback.
        """
        packet = Packet.decode(raw_packet)
        print "packet in with protocol"

        if Packet.is_named(packet.protocol):
            # protocol is named, resolve id first
            name = packet.protocol
            id = self.server_map.resolve_id(name)

            if id:
                named = MappedName()
                named.name = name
                named.id = id
                msg = Message(0, named)
                self.q_out.fifo_put((conn, msg))
            else:
                print "unmapped name: " + str(name)
                rejected = MessageRejected()
                rejected.id = 0
                rejected.reason = rejected.UNMAPPED_NAME
                rejected.message = "Unmapped name: '%s'" % name
                msg = Message(0, rejected)
                self.q_out.fifo_put((conn, msg))
                # stop processing
                return

        else:
            # just take id
            id = packet.protocol

        # this seems slightly silly in the case where the message was
        # a named message (it shouldn't hurt though)
        name = self.server_map.resolve_name(id)

        if not name:
            # if the reverse name lookup fails
            print "unmapped id: " + str(id)
            rejected = MessageRejected()
            rejected.id = 0
            rejected.reason = rejected.UNMAPPED_ID
            rejected.message = "Unmapped ID: %d" % id
            msg = Message(0, rejected)
            self.q_out.fifo_put((conn, msg))
            # stop processing
            return

        protobuf_class = self.server_map.resolve_protobuffer(name)
        assert protobuf_class

        protobuf = protobuf_class()
        protobuf.ParseFromString(packet.payload)
        message = Message(packet.track_id, protobuf, name=name)
        print "have protobuf: {" + str(protobuf) + "}"

        self.q_in.fifo_put((conn, message))
Ejemplo n.º 14
0
    def _send_command(self, command, data):
        p = Packet(seq=self.txseq, cmd=command, data=data)
        msg = self.aes.encrypt([ord(x) for x in p.toMessage()], self.key,
                    AES.keySize["SIZE_128"])
        msg = ''.join([chr(x) for x in msg])

        self.logger.debug('Msg to door %s: %s'%(self.name, list(p.toMessage())))
        self.interface.writeMessage(self.address, msg)
        '''
Ejemplo n.º 15
0
 def encode(self, protocol):
     """
     Encodes the message as a packet (serializing it first if
     needed) using the given protocol.
     """
     if not self.serialized:
         self.serialized = self.pbuffer.SerializeToString()
     packet = Packet(self.track_id, protocol, self.serialized)
     return packet.encode()
Ejemplo n.º 16
0
 def get(self):
     for pkt in self.source.get():
         items = pkt.item
         if isinstance(items, Array):
             for item in items:
                 ret = Packet()
                 ret.item = item
                 yield ret
     raise StopIteration
Ejemplo n.º 17
0
	def enqueuePacket(self, data):
		packet = Packet(data)
		if self.init:
			packet.seg = self.seg
			self.seg += 1
		else:
			self.requesting = True
			packet.con = self.nextSeg # con packet: seg = 0; ack = 0; con = nextSeg

		self.buff.put(packet)
 def test_receiving_rate_kbps_single_packet(self):
     payload_sizes_bytes = [0.0, 500.0, 1200.0] # Empty or non-empty packet.
     for payload_size_bytes in payload_sizes_bytes:
         # id and send_time don't matter here.
         packet = Packet(None, None, payload_size_bytes)
         for i in range(10):
             packet.arrival_time_ms = i
             time_window_ms = random.uniform(100.0, 1000.0)
             self.assertEqual(receiving_rate_kbps([packet], time_window_ms),
                             8.0 * packet.payload_size_bytes / time_window_ms)
 def test_average_bitrate_two_packets(self):
     for i in range(10):
         packet_size_bytes = random.uniform(1.0, 8000.0)
         delay_ms = random.uniform(10.0, 300.0)
         gap_ms = random.uniform(10.0, 50.0)
         packet1 = Packet(1, 0.0, packet_size_bytes)
         packet2 = Packet(2, gap_ms, packet_size_bytes)
         packet1.arrival_time_ms = delay_ms
         packet2.arrival_time_ms = gap_ms + delay_ms
         self.assertNear(average_bitrate_kbps([packet1, packet2]), 8.0 * packet_size_bytes / gap_ms, 0.001)
Ejemplo n.º 20
0
 def get_state(self, *args):
     """
     Respond to an incoming get_state request
     :param args: not applicable to this command
     :return:
     """
     log.msg('get_state: %r %d' % (self.connections, self.num_connections))
     if len(self.connections) == self.num_connections:
         return Packet.create('CONNECTED', PacketType.PA_STATUS)
     return Packet.create('DISCONNECTED', PacketType.PA_STATUS)
Ejemplo n.º 21
0
 def get(self):
     for pkt in self.source.get():
         url = pkt.item
         if isinstance(url, Url):
             rss = Rss(url)
             for entry in rss.entries():
                 pkt = Packet()
                 pkt.item = entry
                 yield pkt
     raise StopIteration
Ejemplo n.º 22
0
 def __init__(self,source_address=1,source_port=0,
              destination_address=1,destination_port=0,
              ident=0,ttl=100,protocol="TCP",body="",length=0,
              syn=False,ack=False,fin=False,sequence=0,ack_number=0,drop_packet=False):
     Packet.__init__(self,source_address=source_address,
                     source_port=source_port,
                     destination_address=destination_address,
                     destination_port=destination_port,
                     ttl=ttl,ident=ident,protocol=protocol,
                     body=body,length=length,drop_packet=drop_packet)
     self.sequence = sequence
     self.ack_number = ack_number
 def receiveFromClient(self,s,address):
     while True:
         pkt = Packet()
         try:
             pkt.construct(s)
         except socket.error as error:
             if error.errno == 10054 or error.errno == 104:
                 del self.toClient[address[0]]
                 print address[0] + " closed client!"
                 break
             else:
                 raise
         pkt.returnIP = address[0]
         self.enqueueToVirus(pkt)
Ejemplo n.º 24
0
    def getBasicPacket(self, timeout = DEFAULT_TIMEOUT) :
        try:
            self.s.setTimeout(timeout)

            packet = Packet([])
            character = self.s.read()
            while character :
                packet.addBytes(character)
                if packet.isComplete :
                    break
                character = self.s.read()
            return packet
        except Exception, e:
            print 'Exception occured in getBasicPacket(): ' + str(e)
            raise CommException('Error occured in getBasicPacket()')
Ejemplo n.º 25
0
    def fileRetrieved(self, write_result):

        file_obj, file_attributes, file_size = write_result

        # Remove '.part' from the end of the file name now that the download is complete
        new_file_path = file_obj.name[:-5]

        os.rename(file_obj.name, new_file_path)

        new_filename = os.path.basename(new_file_path)

        log.msg('File downloaded: ', new_filename)

        orig_filename = new_filename.split('_')[1]

        self.retrieved_file_queue.append(orig_filename)

        file_obj.close()

        # Send a message to the driver indicating that a new image has been retrieved
        # The driver will then associate metadata with the image file name
        packets = Packet.create('New Image:' + str(new_filename), PacketType.FROM_INSTRUMENT)
        self.router.got_data(packets)

        reactor.callLater(0, self.fetch_file)
Ejemplo n.º 26
0
    def __init__(self, update_flag, update_data, reconfig_flag):
        """
        Extend threading class.

        Parameters
        ----------
        callback : obj
            Callback for controller, relays data from rf subsystem to
            controller

        """
        threading.Thread.__init__(self)

        self.update_flag = update_flag
        self.update_data = update_data
        self.reconfig_flag = reconfig_flag

        self.stop_event = threading.Event()
        
        self.data = NodeAData()
        self.packet = Packet('A')
        self.radio = RadioAPI()

        self.last_state = 'stop'
        self.current_state = 'stop'
        self.current_location = 1
        
        self.tx_packet_number = 1

        self.cw_rssi = []
Ejemplo n.º 27
0
    def authenticate(self):
        """ call this before opening the game
        gets the current tick from the server
        and synchronizes the game state """

        def threaded_recv(retlist):
            response, addr = self.client.recv()
            retlist.append(response)

        pkg_request = Packet(0)
        self.client.send(pkg_request)
        retlist = []
        t = Thread(target=lambda: threaded_recv(retlist))
        t.daemon = True
        t.start()
        wait_start = time.get_ticks()
        wait_end = wait_start + 1000
        while len(retlist) <= 0 and time.get_ticks() < wait_end:
            time.wait(1)

        if len(retlist) > 0:
            response = retlist[0]
            pkg_response = Packet.unpack(response)
            self.start_tick = pkg_response.tick
            if len(pkg_response.players) <= 0:
                raise RuntimeError("Invalid response: %s" % pkg_response)
            self.id = pkg_response.players[0][0]
            self.set_state(pkg_response, backtrack=False)
        else:
            raise RuntimeError("Server not responding")
Ejemplo n.º 28
0
    def _read(self):
        """
        Read one packet, publish if appropriate, then return.
        We must not read all packets in a loop here, or we will not actually publish them until the end...
        """
        if self._filehandle is None and not self.files:
            log.msg('Completed reading specified port agent logs, exiting...')
            reactor.stop()
            return

        if self._filehandle is None:
            name = self.files.pop(0)
            log.msg('Begin reading:', name)
            self._filehandle = open(name, 'r')

        packet = Packet.packet_from_fh(self._filehandle)
        if packet is not None:
            if packet.header.packet_type in self.target_types:
                self.router.got_data([packet])

        else:
            self._filehandle.close()
            self._filehandle = None

        # allow the reactor loop to process other events
        reactor.callLater(0.01, self._read)
Ejemplo n.º 29
0
 def _set_select(self, command, *args):
     if len(args) == 0:
         num_sources = self.orb.select('')
     else:
         num_sources = self.orb.select(args[0])
     msg = 'Orb select(%s) yielded num_sources: %d' % (args[:1], num_sources)
     return Packet.create(msg + NEWLINE, PacketType.PA_STATUS)
Ejemplo n.º 30
0
    def add(self, dst, data):
        ptype, pid, unknown, _ = Packet.get_type(data)

        if not ptype:
            self.invalid += 1
            return

        self.records += 1
        self.size_accum += len(data)

        if ptype == PacketType.Control:
            self.control += 1
            self.control_types[pid] += 1

            self.control_dst[pid][dst.value] += 1
        elif ptype == PacketType.Game:
            self.game += 1
            self.game_types[pid] += 1

            self.game_dst[pid][dst.value] += 1
        else:
            raise RuntimeError("Unsupported packet type: " + str(ptype))

        if dst == PacketDest.Server:
            self.to_server += 1
        elif dst == PacketDest.Client:
            self.to_client += 1
        else:
            raise RuntimeError("Unsupported packet destination: " + str(dst))

        if unknown:
            self.unknown += 1