-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_select.py
185 lines (152 loc) · 6.34 KB
/
server_select.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import socket
import select
import queue
import signal
HOST = 'localhost'
PORT = 8888
ENCODING = 'utf-8'
class Server(object):
def __init__(self, host, port):
self.host = host
self.port = port
self.buffer_size = 2048
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# login:connection dictionary
self.login_list = {}
self.inputs = [self.sock]
self.outputs = []
self.message_queues = {}
self.shutdown = False
try:
self.sock.bind((str(self.host), int(self.port)))
self.sock.listen(10)
self.sock.setblocking(False)
except socket.error:
self.shutdown = True
# Trap for SIGINT for exiting
signal.signal(signal.SIGINT, self.sighandler)
self.run()
def run(self):
"""Main server method"""
if self.shutdown:
raise Exception("This server was unable to bind with socket")
print("Server started")
while self.inputs:
try:
# Use select to get lists of sockets ready for IO operations
read, write, exceptional = select.select(self.inputs, self.outputs, [])
except select.error:
break
# Sockets processing
# 1) readable sockets
for socket in read:
# a) processing server socket (incoming connection)
if socket is self.sock:
try:
connection, address = socket.accept()
connection.setblocking(False)
except socket.error:
pass
self.inputs.append(connection)
self.outputs.append(connection)
self.message_queues[connection] = queue.Queue()
# b) processing client socket (incoming messages)
else:
data = socket.recv(self.buffer_size)
self.process_data(data, socket)
# 2) writeable sockets
for socket in write:
if socket in self.inputs:
if not self.message_queues[socket].empty():
data = self.message_queues[socket].get()
socket.send(data)
# 3) sockets where exceptions has occured
for socket in exceptional:
self.inputs.remove(socket)
if socket in self.outputs:
self.outputs.remove(socket)
del self.message_queues[socket]
socket.close()
for login, address in self.login_list.items():
if address == socket:
del self.login_list[login]
break
self.update_login_list()
def process_data(self, data, socket):
"""Process data received by socket"""
if data:
message = data.decode(ENCODING)
message = message.split(';', 3)
# at most 4 splits (don't split the message if it contains ;)
# Processing data
# 1) new user logged in
if message[0] == 'login':
tmp_login = message[1]
while message[1] in self.login_list:
message[1] += '#'
if tmp_login != message[1]:
prompt = 'msg;server;' + message[1] + ';Login ' + tmp_login \
+ ' already in use. Your login changed to ' + message[1] + '\n'
self.message_queues[socket].put(prompt.encode(ENCODING))
self.login_list[message[1]] = socket
print(message[1] + ' has logged in')
# Update list of active users, send it to clients
self.update_login_list()
# 2) user logged out
elif message[0] == 'logout':
print(message[1] + ' has logged out')
self.inputs.remove(socket)
if socket in self.outputs:
self.outputs.remove(socket)
del self.message_queues[socket]
socket.close()
for login, address in self.login_list.items():
if address == socket:
del self.login_list[login]
break
# Update list of active users, send it to clients
self.update_login_list()
# 3) Message from one user to another (msg;origin;target;message)
elif message[0] == 'msg' and message[2] != 'all':
msg = data.decode(ENCODING) + '\n'
data = msg.encode(ENCODING)
target = self.login_list[message[2]]
self.message_queues[target].put(data)
# 4) Message from one user to all users (msg;origin;all;message)
elif message[0] == 'msg':
msg = data.decode(ENCODING) + '\n'
data = msg.encode(ENCODING)
for connection, connection_queue in self.message_queues.items():
if connection != socket:
connection_queue.put(data)
# Empty result in socket ready to be read from == closed connection
else:
self.inputs.remove(socket)
if socket in self.outputs:
self.outputs.remove(socket)
del self.message_queues[socket]
socket.close()
for login, address in self.login_list.items():
if address == socket:
del self.login_list[login]
break
self.update_login_list()
def update_login_list(self):
"""Update login list and send it to active users"""
logins = 'login'
for login in self.login_list:
logins += ';' + login
logins += ';all' + '\n'
logins = logins.encode(ENCODING)
for connection, connection_queue in self.message_queues.items():
connection_queue.put(logins)
def sighandler(self, signum, frame):
"""Handle trapped SIGINT"""
# close the server
print('Shutting down server...')
# close existing client sockets
for connection in self.outputs:
connection.close()
self.sock.close()
if __name__ == '__main__':
server = Server(HOST, PORT)