def count_received (expected_volume, match_data, receive_IP, receive_port):
	receiver = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	receiver.bind((receive_IP, receive_port))
	packet_count = 0
	while (packet_count < expected_volume) :
		try : 
			data, addr = receiver.recvfrom(512)
			seq_num, moves = helpers.unserializer(data)
			#if data == match_data :
			packet_count += 1
			print 'count: ', packet_count, 'seq_num: ', seq_num
		except KeyboardInterrupt :
			print "packets received :", packet_count
			print "percent received :", packet_count/float(expected_volume)
			receiver.close()
			sys.exit()
	receiver.close()
	print "packets recceived :", packet_count
	print "percent received :", packet_count/float(expected_volume)
def count_received(expected_volume, match_data, receive_IP, receive_port):
    receiver = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    receiver.bind((receive_IP, receive_port))
    packet_count = 0
    while (packet_count < expected_volume):
        try:
            data, addr = receiver.recvfrom(512)
            seq_num, moves = helpers.unserializer(data)
            #if data == match_data :
            packet_count += 1
            print 'count: ', packet_count, 'seq_num: ', seq_num
        except KeyboardInterrupt:
            print "packets received :", packet_count
            print "percent received :", packet_count / float(expected_volume)
            receiver.close()
            sys.exit()
    receiver.close()
    print "packets recceived :", packet_count
    print "percent received :", packet_count / float(expected_volume)
Пример #3
0
def client(qi, ServerIP):
    sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # timer = helpers.Stopwatch()

    rate = 0.11
    ## we start and customize the pygame gui
    pygame.init()
    s = pygame.display.set_mode((600, 600))
    pygame.display.set_caption("Snake")

    # initial snake block positions
    xs = [290, 290, 290, 290, 290]
    ys = [290, 270, 250, 230, 210]

    # initial snake direction, score & position of the apple
    dirs = 0
    score = 0

    # we create our apple and our snake blocks
    block_size = (20, 20)
    appleimage = pygame.Surface((block_size[0], block_size[1]))
    # hacky bullsheeeet below so double check
    applepos = (330, 270)
    appleimage.fill((0, 255, 0))
    img = pygame.Surface((block_size[0], block_size[1]))
    img.fill((255, 0, 0))

    # other stuff
    f = pygame.font.SysFont("Arial", 20)

    loops = 0
    curr_gui_number = 0
    # 	round_trip = 0
    dirs_list = [-1, -1, -1]
    pre1 = -1
    pre2 = -1
    sttime = time.time()
    while True:
        # we detect keystrokes and put them in our queue qo
        for e in pygame.event.get():
            # 			start_time = time.time()
            if e.type == QUIT:
                sys.exit(0)
            elif e.type == KEYDOWN:
                if e.key == K_UP and dirs != 0:
                    dirs = 2
                elif e.key == K_DOWN and dirs != 2:
                    dirs = 0
                elif e.key == K_LEFT and dirs != 1:
                    dirs = 3
                elif e.key == K_RIGHT and dirs != 3:
                    dirs = 1

                # 	timer.start()
                # below looks ugly, but slightly faster than pythonic list modification
        pre1 = dirs_list[0]
        pre2 = dirs_list[1]

        dirs_list[0] = dirs
        dirs_list[1] = pre1
        dirs_list[2] = pre2

        # send packet multiple times for redundancy, sleeps reduce packet loss
        packet = helpers.serializer(loops, dirs_list)
        sender.sendto(packet, (ServerIP, Client_send_server_receive))
        sender.sendto(packet, (ServerIP, Client_send_server_receive))
        sender.sendto(packet, (ServerIP, Client_send_server_receive))

        # we wait and listen for incomming gui info in qi
        while time.time() - sttime - loops * rate < (rate - 0.1):
            if qi.qsize() > 0:

                # timer.stop()
                # print(timer.time_elapsed)

                # need to unserialize packet
                seq_number, data = helpers.unserializer(qi.get())

                # need to handle configuration sequence orderings here
                # should it just be < ??
                if curr_gui_number <= seq_number:
                    curr_gui_number = seq_number
                    guidict = data
                    with qi.mutex:
                        qi.queue.clear()
                    if guidict["GameOver"] == True:
                        sys.exit()
                    xs = guidict["xs"]
                    ys = guidict["ys"]
                    applepos = guidict["applepos"]
                    score = guidict["score"]
                    break

                    ##rendering when gui info received
        s.fill((255, 255, 255))
        s.blit(appleimage, applepos)
        for i in range(0, len(xs)):
            s.blit(img, (xs[i], ys[i]))

        t = f.render("score:" + str(score), True, (0, 0, 0))
        s.blit(t, (10, 10))
        pygame.display.update()

        while time.time() - sttime - loops * rate < (rate - 0.001):
            pass
        loops += 1
Пример #4
0
def client(qi, ServerIP):
    sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #timer = helpers.Stopwatch()

    rate = 0.11
    ## we start and customize the pygame gui
    pygame.init()
    s = pygame.display.set_mode((600, 600))
    pygame.display.set_caption('Snake')

    # initial snake block positions
    xs = [290, 290, 290, 290, 290]
    ys = [290, 270, 250, 230, 210]

    # initial snake direction, score & position of the apple
    dirs = 0
    score = 0

    # we create our apple and our snake blocks
    block_size = (20, 20)
    appleimage = pygame.Surface((block_size[0], block_size[1]))
    # hacky bullsheeeet below so double check
    applepos = (330, 270)
    appleimage.fill((0, 255, 0))
    img = pygame.Surface((block_size[0], block_size[1]))
    img.fill((255, 0, 0))

    # other stuff
    f = pygame.font.SysFont('Arial', 20)

    loops = 0
    curr_gui_number = 0
    #	round_trip = 0
    dirs_list = [-1, -1, -1]
    pre1 = -1
    pre2 = -1
    sttime = time.time()
    while True:
        # we detect keystrokes and put them in our queue qo
        for e in pygame.event.get():
            #			start_time = time.time()
            if e.type == QUIT:
                sys.exit(0)
            elif e.type == KEYDOWN:
                if e.key == K_UP and dirs != 0:
                    dirs = 2
                elif e.key == K_DOWN and dirs != 2:
                    dirs = 0
                elif e.key == K_LEFT and dirs != 1:
                    dirs = 3
                elif e.key == K_RIGHT and dirs != 3:
                    dirs = 1

    #	timer.start()
    # below looks ugly, but slightly faster than pythonic list modification
        pre1 = dirs_list[0]
        pre2 = dirs_list[1]

        dirs_list[0] = dirs
        dirs_list[1] = pre1
        dirs_list[2] = pre2

        # send packet multiple times for redundancy, sleeps reduce packet loss
        packet = helpers.serializer(loops, dirs_list)
        sender.sendto(packet, (ServerIP, Client_send_server_receive))
        sender.sendto(packet, (ServerIP, Client_send_server_receive))
        sender.sendto(packet, (ServerIP, Client_send_server_receive))

        # we wait and listen for incomming gui info in qi
        while time.time() - sttime - loops * rate < (rate - 0.1):
            if qi.qsize() > 0:

                #timer.stop()
                #print(timer.time_elapsed)

                # need to unserialize packet
                seq_number, data = helpers.unserializer(qi.get())

                # need to handle configuration sequence orderings here
                # should it just be < ??
                if (curr_gui_number <= seq_number):
                    curr_gui_number = seq_number
                    guidict = data
                    with qi.mutex:
                        qi.queue.clear()
                    if guidict['GameOver'] == True:
                        sys.exit()
                    xs = guidict['xs']
                    ys = guidict['ys']
                    applepos = guidict['applepos']
                    score = guidict['score']
                    break

        ##rendering when gui info received
        s.fill((255, 255, 255))
        s.blit(appleimage, applepos)
        for i in range(0, len(xs)):
            s.blit(img, (xs[i], ys[i]))

        t = f.render("score:" + str(score), True, (0, 0, 0))
        s.blit(t, (10, 10))
        pygame.display.update()

        while time.time() - sttime - loops * rate < (rate - 0.001):
            pass
        loops += 1