-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
128 lines (109 loc) · 5.37 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from thrift.protocol import TBinaryProtocol
from thrift.transport import TSocket, TTransport
from soccer import Game
from soccer.ttypes import DoChangeMode, DoMovePlayer, State, Empty, PlayerActions, CoachActions, TrainerActions, PlayerAction, GameModeType
from soccer.ttypes import ServerParam, PlayerParam, PlayerType, InitMessage, RegisterRequest, RegisterResponse, AgentType
from soccer.ttypes import HeliosChainAction, HeliosBasicMove, HeliosGoalie, HeliosSetPlay
from soccer.ttypes import DoMoveBall, RpcVector2D, TrainerAction
from soccer.ttypes import DoHeliosSubstitute, CoachAction
import os
from utils.PFProcessServer import PFProcessServer
from thrift.server.TServer import TThreadedServer
from typing import Union
from threading import Semaphore
from multiprocessing import Manager, Lock
import logging
from pyrusgeom.vector_2d import Vector2D
import argparse
from src.SampleCoachAgent import SampleCoachAgent
from src.SamplePlayerAgent import SamplePlayerAgent
from src.SampleTrainerAgent import SampleTrainerAgent
logging.basicConfig(level=logging.DEBUG)
manager = Manager()
shared_lock = Lock() # Create a Lock for synchronization
shared_number_of_connections = manager.Value('i', 0)
class GameHandler:
def __init__(self):
self.server_params: Union[ServerParam, None] = None
self.player_params: Union[PlayerParam, None] = None
self.player_types: dict[int, PlayerType] = {}
self.debug_mode: bool = False
self.agent: Union[SampleTrainerAgent, SampleCoachAgent, SamplePlayerAgent] = None
def GetPlayerActions(self, state: State):
logging.debug(f"GetPlayerActions unum {state.register_response.uniform_number} at {state.world_model.cycle}")
actions = self.agent.get_actions(state.world_model)
return actions
def GetCoachActions(self, state: State):
logging.debug(f"GetCoachActions coach at {state.world_model.cycle}")
actions = self.agent.get_actions(state.world_model)
return actions
def GetTrainerActions(self, state: State):
logging.debug(f"GetTrainerActions trainer at {state.world_model.cycle}")
actions = self.agent.get_actions(state.world_model)
return actions
def SendServerParams(self, serverParams: ServerParam):
logging.debug(f"Server params received unum {serverParams.register_response.uniform_number}")
self.agent.set_server_param(serverParams)
res = Empty()
return res
def SendPlayerParams(self, playerParams: PlayerParam):
logging.debug(f"Player params received unum {playerParams.register_response.uniform_number}")
self.agent.set_player_param(playerParams)
res = Empty()
return res
def SendPlayerType(self, playerType: PlayerType):
logging.debug(f"Player type received unum {playerType.register_response.uniform_number}")
self.agent.set_player_types(playerType)
res = Empty()
return res
def SendInitMessage(self, initMessage: InitMessage):
logging.debug(f"Init message received unum {initMessage.register_response.uniform_number}")
self.debug_mode = initMessage.debug_mode
res = Empty()
return res
def Register(self, register_request: RegisterRequest):
logging.debug(f"received register request from team_name: {register_request.team_name} "
f"unum: {register_request.uniform_number} "
f"agent_type: {register_request.agent_type}")
with shared_lock:
shared_number_of_connections.value += 1
logging.debug(f"Number of connections {shared_number_of_connections.value}")
if register_request.agent_type == AgentType.TrainerT:
self.agent = SampleTrainerAgent()
elif register_request.agent_type == AgentType.CoachT:
self.agent = SampleCoachAgent()
else:
self.agent = SamplePlayerAgent()
team_name = register_request.team_name
uniform_number = register_request.uniform_number
agent_type = register_request.agent_type
res = RegisterResponse(client_id=shared_number_of_connections.value,
team_name=team_name,
uniform_number=uniform_number,
agent_type=agent_type)
return res
def SendByeCommand(self, register_response: RegisterResponse):
logging.debug(f"Bye command received unum {register_response.uniform_number}")
with shared_lock:
pass
res = Empty()
return res
def serve(port):
handler = GameHandler()
processor = Game.Processor(handler)
transport = TSocket.TServerSocket(host='0.0.0.0', port=port)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = PFProcessServer(processor, transport, tfactory, pfactory)
# server = TThreadedServer(processor, transport, tfactory, pfactory)
logging.info(f"Starting server on port {port}")
try:
server.serve()
except KeyboardInterrupt:
server.stop()
print("Stopping server")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Run play maker server')
parser.add_argument('-p', '--rpc-port', required=False, help='The port of the server', default=50051)
args = parser.parse_args()
serve(args.rpc_port)