-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommunication_channel.py
45 lines (40 loc) · 1.57 KB
/
communication_channel.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
import Queue
# A class for building a communication channel. This class will output a channel
# for the server and the client. This should be used like so:
#
# factory = CommunicationFactory()
#
# # server should get the server_channel
# server_channel = factory.build_for("server")
#
# # client should get the client_channel
# client_channel = factory.build_for("client")
#
# The channels that you receive will be of type +CommunicationChannel+.
class CommunicationFactory:
def __init__(self):
self.client_to_server = Queue.Queue()
self.server_to_client = Queue.Queue()
def build_for(self, build_type):
if build_type == "client":
return CommunicationChannel(self.client_to_server, self.server_to_client)
elif build_type == "server":
return CommunicationChannel(self.server_to_client, self.client_to_server)
else:
raise Exception("Cannot build communication channel for %s." % build_type)
# Queue that acts as the port between the client and the server, or the server
# and the client.
#
# You can put/get messages from the queue whenever you are ready for them.
class CommunicationChannel:
def __init__(self, send_queue, receive_queue):
self._send_queue = send_queue
self._receive_queue = receive_queue
def put(self, message):
#print "QUEUE PUT: "+str(message)
message.message_type._check_valid_message_type()
return self._send_queue.put(message)
def get(self):
msg = self._receive_queue.get()
#print "QUEUE GET: "+str(msg)
return msg