-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatServer.py
102 lines (76 loc) · 2.92 KB
/
ChatServer.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
import random
import time
import zmq
from Exchange.Exchange import Exchange
class MessageRouter:
def __init__(self):
self.chat = Chat()
self.exchange = Exchange()
self.story = Story()
self.flush = {}
self.default_identifier = "chat"
self.identifier_to_destination = {
"flush": None,
"exit": None,
"chat": self.chat,
"exchange": self.exchange,
"story": self.story,
}
def process_incoming_message(self, message):
header, rest = message.split(">>")
client_id = header.split("=")[1]
sub_messages = rest.split(";;")
replies = []
for sub_message in sub_messages:
message_split = sub_message.split("::")
if len(message_split) == 1:
identifier = self.default_identifier
message_to_destination = sub_message
else:
identifier = message_split[0]
message_to_destination = message_split[1]
if identifier not in self.identifier_to_destination:
identifier = self.default_identifier
if identifier == "flush":
print("flushing for client", client_id)
flush_replies = self.get_flush_replies(client_id)
replies += flush_replies
else:
print("current identifier:", identifier)
destination = self.identifier_to_destination[identifier]
reply = identifier + "::" + destination.process_incoming_message(message_to_destination)
replies.append(reply)
return header + ">>" + ";;".join(replies)
def get_flush_replies(self, client_id):
replies = self.flush.get(client_id, [])
return ["flush::" + reply for reply in replies]
class Chat:
def __init__(self):
pass
def process_incoming_message(self, message):
print("received:", message)
reply = input("Reply to send to client: ")
return reply
class Story:
def __init__(self):
self.string = ""
def process_incoming_message(self, message):
self.string += message
return self.string
def run_server(context, ip, port, message_router):
socket = context.socket(zmq.REP)
socket.bind("tcp://{0}:{1}".format(ip, port))
while True:
print("waiting to receive request ...")
received = socket.recv().decode("utf-8")
reply = message_router.process_incoming_message(received)
print("sending reply:", reply)
socket.send_string(reply)
if __name__ == "__main__":
localhost_ip = "127.0.0.1"
wesley_private_ip = "192.168.1.7" # from `ipconfig /all`
wesley_public_ip = "71.57.35.2" # from googling "what is my ip"
context = zmq.Context()
server_port = "5000"
message_router = MessageRouter()
run_server(context, wesley_private_ip, server_port, message_router)