-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.py
192 lines (156 loc) · 5.99 KB
/
room.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
186
187
188
189
190
191
192
import json
import random
import websockets
from websockets import WebSocketServerProtocol
import asyncio
import utils
import os
import signal
from logger import Logger
# type - 3 canvas draws
# type - 4 new member added
# type - 5 request canvas
# type - 6 canvas recieve
# type - 7 canvas update
# type - 8 member removed
# create a echo server class
class Room:
Clients = set()
LoggerObj = Logger.getInstance() # start the server
def __init__(self, roomId, host, port,serverPipe):
self.roomId = roomId
self.host = host
self.port = port
self.pipe = serverPipe
Room.LoggerObj.info(f"[+] Room {roomId} created",None,roomId)
async def register(self, websocket):
"""
Register a new client in the room.
:param websocket: the client websocket
return: None
"""
Room.Clients.add(websocket)
Room.LoggerObj.debug("[+] Clients in the room ",Room.Clients,self.roomId)
Room.LoggerObj.info(f"[+] {websocket} has joined the room",None,self.roomId)
await self.sendDict(websocket,json.dumps({"status":"200","type": "2", "message": "Welcome to the chat!"}))
await self.send_all({"status":"200","type": "4", "message": f"{websocket} has joined the chat","noOfClients":len(Room.Clients)})
#print("Okay its now joined")
return
async def unregister(self, websocket):
"""
Remove a client from the room.
:param websocket: the client websocket
return: None
"""
Room.Clients.remove(websocket)
Room.LoggerObj.info(f"{websocket} has left the chat",None,self.roomId)
await self.send_all({"status":"200","type": "8", "message": f"{websocket} has left the chat","noOfClients":len(Room.Clients)})
if len(Room.Clients) == 0:
Room.LoggerObj.info("[+] No clients in the room",None,self.roomId)
self.pipe.send((self.port,self.roomId))
os.kill(os.getpid(), signal.SIGINT)
return
async def send_all(self, message,websocket=None):
"""
Send a message to all clients in the room.
:param message: the message to send
return: None
"""
for client in Room.Clients:
if client != websocket:
await client.send(json.dumps(message))
return
async def handle_message(self, websocket, message):
"""
Function to handle sending messages to clients.
:param websocket: the client websocket
:param message: the message to send
return: Nones
"""
await self.send_all(f"[!] {websocket} says: {message}")
async def sendDict(self,websocket:WebSocketServerProtocol,data):
"""
Send a json response to the client.
:param websocket: the client websocket
:param data: the data to send
return: None
"""
await websocket.send(json.dumps(data))
async def request_canvas(self,websocket:WebSocketServerProtocol):
"""
Request the canvas from the clients to retrieve board content to new client.
:param websocket: the client websocket
return: None
"""
flag = False
for client in Room.Clients:
if client != websocket:
flag = True
#print("sendd req",websocket.id)
await client.send(json.dumps({"status":"200","type": "5", "message": "Requesting canvas data","forClient":str(websocket.id)}))
break
if not flag:
await self.sendDict(websocket,json.dumps({"status":"200","type": "7", "message": "No canvas data available"}))
#print("Is data available",flag)
return
async def send_canvasData(self,websocket:WebSocketServerProtocol,data):
"""
Send the exsisiting canvas data to the new client.
:param websocket: the client websocket
:param data: the data to send
return: None
"""
toClient = data["forClient"]
for client in Room.Clients:
if str(client.id) == toClient:
await client.send(json.dumps({"status":"200","type": "6", "message": "Sending canvas data","canvas":data["canvas"]}))
break
#await toClient.send(json.dumps(data))
return
async def distribute(self,websocket:WebSocketServerProtocol):
"""
Handle the client requests.
:param websocket: the client websocket
return: None
"""
async for data in websocket:
data = json.loads(data)
if data["type"] == "3":
await self.send_all(data,websocket)
elif data["type"] == "5":
await self.request_canvas(websocket)
elif data["type"] == "6":
await self.send_canvasData(websocket,data)
Room.LoggerObj.debug(f"[+] {websocket} sent {data}",None,self.roomId)
return
async def handle_request(self, websocket, path):
"""
Handle client requests.
:param websocket: the client websocket
:param path: the path of the client
return: None
"""
await self.register(websocket)
try:
await self.distribute(websocket)
#Room.LoggerObj.DEBUG("[+] Waiting for messages")
except Exception as e:
Room.LoggerObj.error("[-] Error",e,self.roomId)
finally:
await self.unregister(websocket)
def main(roomId,host, port, serverPipe):
"""
Main function to start the server.
:param roomId: the room id
:param host: the host of the server
:param port: the port of the server
:param serverPipe: the pipe to communicate with the server
return: None
"""
# create a Room
# print(serverPipe)
room_ = Room(roomId,host,port,serverPipe)
start_room = websockets.serve(room_.handle_request, host, port)
asyncio.get_event_loop().run_until_complete(start_room)
asyncio.get_event_loop().run_forever()
return