forked from CLSFramework/playmaker-server-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
87 lines (71 loc) · 2.78 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
from time import sleep
import service_pb2_grpc as pb2_grpc
import service_pb2 as pb2
import google.protobuf
from concurrent import futures
import grpc
from src.SamplePlayerAgent import SamplePlayerAgent
from src.SampleCoachAgent import SampleCoachAgent
from src.SampleTrainerAgent import SampleTrainerAgent
from threading import Semaphore
import os
class Game(pb2_grpc.GameServicer):
def __init__(self):
self.player_agent = SamplePlayerAgent()
self.coach_agent = SampleCoachAgent()
self.trainer_agent = SampleTrainerAgent()
self.number_of_connections = 0
self.lock = Semaphore()
self.running = Semaphore()
def GetPlayerActions(self, request:pb2.State, context):
actions = self.player_agent.get_actions(request.world_model)
return actions
def GetCoachActions(self, request:pb2.State, context):
actions = self.coach_agent.get_actions(request.world_model)
return actions
def GetTrainerActions(self, request:pb2.State, context):
actions = self.trainer_agent.get_actions(request.world_model)
return actions
def SendServerParams(self, request: pb2.ServerParam, context):
self.player_agent.set_params(request)
self.coach_agent.set_params(request)
self.trainer_agent.set_params(request)
return pb2.Empty()
def SendPlayerParams(self, request:pb2.PlayerParam, context):
self.player_agent.set_params(request)
self.coach_agent.set_params(request)
self.trainer_agent.set_params(request)
return pb2.Empty()
def SendPlayerType(self, request: pb2.PlayerType, context):
self.player_agent.set_params(request)
self.coach_agent.set_params(request)
self.trainer_agent.set_params(request)
return pb2.Empty()
def SendInitMessage(self, request, context):
self.player_agent.set_debug_mode(request.debug_mode)
return pb2.Empty()
def GetInitMessage(self, request, context):
with self.lock:
self.number_of_connections += 1
return pb2.InitMessageFromServer()
def SendByeCommand(self, request, context):
with self.lock:
self.number_of_connections -= 1
if self.number_of_connections <= 0:
self.running.release()
return pb2.Empty()
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=22))
game_service = Game()
game_service.running.acquire()
pb2_grpc.add_GameServicer_to_server(game_service, server)
server.add_insecure_port('[::]:50051')
server.start()
print("Server started at port 50051")
game_service.running.acquire()
print("Stopping server")
sleep(1)
os._exit(0)
server.wait_for_termination()
if __name__ == '__main__':
serve()