-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer.py
90 lines (86 loc) · 3.38 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
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
# -*- coding: utf-8 -*-
"""
..::/RADIN\::..
"""
import select
import queue
from collections import namedtuple
from time import sleep
from socket import *
person = namedtuple('person', ['name','id','mode','cnn_ip'])
users_index=0
counter=0
serverPort = 12500
inputs=[]
outputs=[]
exp=[]
users=[]
ss = socket(AF_INET,SOCK_STREAM)
ss.bind(('',serverPort))
ss.listen(5)
inputs.append(ss)
exp.append(ss)
message_queues={}
print ('The server is ready to receive')
while 1:
readable, writable, exceptional = select.select(inputs,outputs,exp)
for sock in readable :
if sock is ss:
connectionSocket, addr = ss.accept()
inputs.append(connectionSocket)
exp.append(connectionSocket)
users.append(person(name="",id=users_index,mode='',cnn_ip=connectionSocket))
message_queues[connectionSocket] = queue.Queue()
users_index+=1
counter+=1
print ('%d user(s) connected to server.'%counter)
else:
msg = sock.recv(1024)
msg = msg.decode(encoding='UTF-8',errors='strict')
if msg.__le__(''): # handling the empty message being sent when a client leave the room
z=[x.id for x in users if x.cnn_ip==sock]
# removing specified person from users list and shifting the list
for x in range(z[0],users_index):
users[x]=users[x]._replace(id=users[x].id-1)
print(''.join([users[z[0]].name,' Left']))
x=person(name=users[z[0]].name,id=users[z[0]].id,mode=users[z[0]].mode,cnn_ip=sock)
users.remove(x)
exceptional.append(sock)
break
if msg[0]=="$": #joining
sock.send(bytes(''.join(['Hi ',msg[1:], ' ! ^_^']),'UTF-8'))
print(''.join([msg[1:], ' Joined']))
z=[x.id for x in users if x.cnn_ip==sock]
users[z[0]]=users[z[0]]._replace(name=msg[1:])
elif msg[0]=="~": #declaring the mode
z=[x.id for x in users if x.cnn_ip==sock]
users[z[0]]=users[z[0]]._replace(mode=msg[1])
elif msg=="&fetch":
#flushing each person's queue or showing the unseen messages according to the index
writable.append(sock)
else :
# adding to the Queue
z=[x.id for x in users if x.cnn_ip==sock]
msg=''.join([users[z[0]].name," : ",msg])
for x in users:
message_queues[x.cnn_ip].put(msg)
#print(users[:]) #checking the object added to the list according to the people
for sock in writable:
l= message_queues[sock].qsize()
sock.send(bytes(str(l),'UTF-8'))
for c in range(0,l):
sock.send(bytes(message_queues[sock].get_nowait(),'UTF-8'))
#need time to send correctly
sleep(0.05)
for sock in exceptional:
# Stop listening for input on the connection
inputs.remove(sock)
exp.remove(sock)
if sock in outputs:
outputs.remove(sock)
sock.close()
# Remove message queue
del message_queues[sock]
users_index-=1
counter-=1
print ('%d user(s) connected to server.'%counter)