-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
executable file
·51 lines (39 loc) · 1.95 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
import zmq
import flatbuffers
from infcomp.socket_wrapper import Socket
from infcomp.parse_flatbuffers import parse_message_body
import infcomp.protocol.ReplyProposal as ReplyProposal
import infcomp.protocol.ReplyStartInference as ReplyStartInference
import infcomp.protocol.ReplyFinishInference as ReplyFinishInference
from infcomp.protocol.MessageBody import MessageBody
from infcomp.actions import parse_action
from infcomp.protocol.Distribution import Distribution
class Server(Socket):
def __init__(self, address):
super().__init__(address, zmq.REP)
def get_action(self):
message_body = parse_message_body(self.receive())
return parse_action(message_body)
def send_proposal(self, proposal, params):
builder = flatbuffers.Builder(64)
if proposal is None:
distribution = None
distribution_type = Distribution.NONE
else:
distribution, distribution_type = proposal.serialize(builder, params)
ReplyProposal.ReplyProposalStart(builder)
if distribution is not None:
ReplyProposal.ReplyProposalAddDistribution(builder, distribution)
ReplyProposal.ReplyProposalAddDistributionType(builder, distribution_type)
message_body = ReplyProposal.ReplyProposalEnd(builder)
self._send_message(builder, message_body, MessageBody().ReplyProposal)
def reply_start_inference(self):
builder = flatbuffers.Builder(64)
ReplyStartInference.ReplyStartInferenceStart(builder)
message_body = ReplyStartInference.ReplyStartInferenceEnd(builder)
self._send_message(builder, message_body, MessageBody().ReplyStartInference)
def reply_finish_inference(self):
builder = flatbuffers.Builder(64)
ReplyFinishInference.ReplyFinishInferenceStart(builder)
message_body = ReplyFinishInference.ReplyFinishInferenceEnd(builder)
self._send_message(builder, message_body, MessageBody().ReplyFinishInference)