-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc-server.py
69 lines (54 loc) · 2.26 KB
/
grpc-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
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 RLock
lock = RLock()
class Game(pb2_grpc.GameServicer):
def __init__(self):
self.player_agent = SamplePlayerAgent()
self.coach_agent = SampleCoachAgent()
self.trainer_agent = SampleTrainerAgent()
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)
print("Debug mode: ", request.debug_mode)
return pb2.Empty()
def GetInitMessage(self, request, context):
return pb2.InitMessageFromServer()
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=22))
pb2_grpc.add_GameServicer_to_server(Game(), server)
server.add_insecure_port('[::]:50051')
server.start()
print("Server started at port 50051")
server.wait_for_termination()
if __name__ == '__main__':
serve()