Пример #1
0
import logging
import os
import select
import socket
import sys
import time
import unittest
sys.path.append(os.path.abspath('src'))
import tplogger
from tpmessage import TPMessage
logger = tplogger.getTPLogger('tpmessage_test.log', logging.DEBUG)
class TPMessageTest(unittest.TestCase):
    def template_SerializeAndDeserialize(self, msg):
        b = msg.Serialize()
        t = TPMessage()
        t.Deserialize(b[4:])
        self.assertTrue(msg == t)

    def test_SerializeAndDeserialize_1(self):
        msg = TPMessage()
        self.template_SerializeAndDeserialize(msg)

    def test_SerializeAndDeserialize_2(self):
        msg = TPMessage()
        msg.timestamp = 10
        self.template_SerializeAndDeserialize(msg)
import logging
import multiprocessing
import os
import sys
import unittest
sys.path.append(os.path.abspath('src'))
from endgameevent import EndGameEvent
from gameconfig import GameConfig
from gameevent import GameEvent
from gamestate import GameState
import tplogger
from tpmessage import TPMessage
from udpeventsocket import UDPEventSocket
from udpsocket import UDPSocket
logger = tplogger.getTPLogger('udpeventsocket_test.log', logging.DEBUG)
def UDPEventSocketJar_RecvSync(e, timeout, q):
    try:
        status = e.RecvSync(timeout)
    except Exception as e:
        logger.exception(e)
        status = -2
    q.put(status)

class UDPEventSocketTest(unittest.TestCase):
    def template_ReadAndWriteEvent(self, evt):
        s, t = UDPSocket.Pair()
        e = UDPEventSocket(s)
        f = UDPEventSocket(t)
        status = 0
        try:
            for i in range(0, 20):
Пример #3
0
import logging
import os
import select
import socket
import struct
import sys
sys.path.append(os.path.abspath('src'))
import tplogger
from udpdatagram import UDPDatagram
logger = tplogger.getTPLogger('udpsocket.log', logging.DEBUG)
class UDPSocket:
    '''
    Constants:
    GUID_1    -- The bytes sent when initiating a handshake.
    GUID_3    -- The bytes sent when completing a handshake.
    Attributes:
    sock      -- A socket object.
    ttl       -- The time-to-live for the connection.
    seq       -- The number of datagrams sent (16 bits).
    ack       -- The number of acknowledged datagrams (16 bits).
    ackbits   -- Acknowledgement of the previous 32 datagrams. (32 bits).
    should_ignore_old -- If True, ignore datagrams earlier than the latest ack.
    '''
    MAX_TIME_TO_LIVE = 60
    GUID_1 = b'0e27b7418ee54d648b20dd82dc53905b'
    GUID_3 = b'4200150f5d5a46a283483cc501f395e4'
    def __init__(self):
        self.sock = None
        self.ttl = 0
        self.seq = 0
        self.ack = 0
Пример #4
0
import multiprocessing
import os
import sys
import unittest
sys.path.append(os.path.abspath('src'))
from gameconfig import GameConfig
import tplogger
from udpclient import UDPClient
from udpeventsocket import UDPEventSocket
from udpserver import UDPServer
from udpsocket import UDPSocket
sys.path.append(os.path.abspath('tests'))
from mockkeyboard import MockKeyboard
from nullkeyboard import NullKeyboard
from nullrenderer import NullRenderer
logger = tplogger.getTPLogger('udpserver_test.log', logging.DEBUG)

def UDPServerTestPickleJar_AcceptN(timeout, svr, svrsock, n, q):
    socks = []
    try:
        svr.AcceptN(svrsock, socks, n, timeout)
    except Exception as e:
        logger.exception(e)
    svrsock.Close()
    q.put(len(socks))

def UDPServerTestPickleJar_Handshake(resend, timeout, client, svrsock, q):
    try:
        res = client.Handshake(svrsock, resend, timeout)
    except Exception as e:
        logger.exception(e)
Пример #5
0
import select
import sys
import time
sys.path.append(os.path.abspath('src'))
from bitrecord import BitRecord
from eventtype import EventType
from endgameevent import EndGameEvent
from gameconfig import GameConfig
from gameobject import GameObject
from gamestate import GameState
from gameevent import GameEvent
from gamerecord import GameRecord
from nullrenderer import NullRenderer
from nullkeyboard import NullKeyboard
import tplogger
logger = tplogger.getTPLogger('udpgameengine.log', logging.DEBUG)

class UDPGameEngine(object):
    '''This class contains the logic for running the game.

    To play the game, define an object that implements PlayFrames and 
    PrintStats and call PlayAs() from the engine.

    Attributes:
    state              -- The GameState.
    renderer           -- The renderer to use. 
    keyboard           -- The keyboard to get input from. 
    rec                -- The GameRecord.
    bitrec             -- The BitRecord.
    key_cool_down_time -- The minimum frame between game events for each player.
    player_id          -- The player ID of the client.
Пример #6
0
import copy
import logging
import os
import socket
import sys
import time
import unittest
sys.path.append(os.path.abspath('src'))
from endgameevent import EndGameEvent
from udpgameengine import UDPGameEngine
from gamerecord import GameRecord
from gamestate import GameState
from gameevent import GameEvent
import tplogger
logger = tplogger.getTPLogger('engine_test.log', logging.DEBUG)
sys.path.append(os.path.abspath('tests'))
from mockkeyboard import MockKeyboard
from mockeventsocket import MockEventSocket
class UDPGameEngineTest(unittest.TestCase):

    def template_GetCurrentFrame(self, start, frame_rate,
            now, expected_frame):
        e = UDPGameEngine()
        self.assertTrue(e.GetCurrentFrame(start, frame_rate, 
            now) == expected_frame)

    def template_RotateBits(self, bits, shift, size, expected_bits):
        e = UDPGameEngine()
        self.assertTrue(e.RotateBits(bits, shift, size) == expected_bits)

    def template_UpdateHistory(self, frame, keybits, update_frame, 
Пример #7
0
import sys
import time
sys.path.append(os.path.abspath('src'))
from bitrecord import BitRecord
from udpgameengine import UDPGameEngine
from eventtype import EventType
from gameevent import GameEvent
from gamestate import GameState
import tplogger
from gameconfig import GameConfig
from nullkeyboard import NullKeyboard
from nullrenderer import NullRenderer
from tpmessage import TPMessage
from udpeventsocket import UDPEventSocket
from udpsocket import UDPSocket
logger = tplogger.getTPLogger('udpclient.log', logging.DEBUG)
class UDPClient:
    '''This class implements the Handshake and PlayFrames of the
    Triple Pong client.
    Attributes:
    keyboard            -- The keyboard to use for input.
    renderer            -- The renderer to use.
    unacked_1           -- The first unacked frame.
    unacked_2           -- The second unacked frame.
    key_is_released     -- True if the key is not held down.
    loss_count          -- The number of event losses.
    state_update_count  -- The number of state updates with overwrite.
    old_count           -- The number of old events received.
    rewind_count        -- The number of normal state updates.
    behind_count        -- The number of times the client was behind schedule.
    past_count          -- The number of times an update was in the past.