-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatClient.py
444 lines (369 loc) · 16.6 KB
/
ChatClient.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
from collections import deque
import time
import socket
import sys
import select
import enum
import re
import random
import copy
class Attribute(enum.IntEnum):
__order__ = "Source Destination ProtocolNumber HopCount MessageIDNumber ViewLog Message"
Source = 0
Destination = 1
ProtocolNumber = 2
HopCount = 3
MessageIDNumber = 4
ViewLog = 5
Message = 6
FieldName = [
"SRC",
"DST",
"PNUM",
"HCT",
"MNUM",
"VL",
"MESG"
]
class Protocol(enum.IntEnum):
ErrorMessage = 0
RegistrationRequest = 1
RegistrationResponse = 2
Data = 3
DataConfirmation = 4
RequestPeerRegistry = 5
CurrentPeerRegistry = 6
SerialBroadcast = 7
SerialBroadcastConfirmation = 8
class MessageType(enum.IntEnum):
Direct = 0
Broadcast = 1
Forward = 2
def AppendTag(attributeType, attributeValue):
return FieldName[attributeType] + ":" + attributeValue + ";"
def FormatMessage(attributeValues):
message = ""
for tag, value in zip(Attribute, attributeValues):
message += AppendTag(tag, value)
return message[:-1]
def UpdatePeerRegistry(listOfPeers):
dictOfPeers = {}
for item in listOfPeers:
info = item.split("=", 1)
peerID = info[0]
details = info[1].split("@", 1)
dictOfPeers[peerID] = (details[0], int(details[1]))
return dictOfPeers
def DisplayIDS(peerList, recentlySeenPeers):
peerList.pop('999', None)
print("******************************")
print("Recently Seen Peers:")
for peer in recentlySeenPeers:
sys.stdout.write(peer + ", ")
sys.stdout.write("\b\b \n")
sys.stdout.flush()
print("Known addresses:")
for peer in peerList:
print(peer + "\t" + peerList[peer][0] + "\t" + str(peerList[peer][1]))
print("******************************")
class Message:
def __init__(self, messageString = ""):
self.parsedMessage = dict(item.split(":", 1) for item in messageString.split(";", 7))
self.intendedRecepients = []
self.retryCount = 1
self.timer = 0
self.messageType = None
def Format(self, peerID):
message = ""
if self.messageType != MessageType.Forward:
self.parsedMessage[FieldName[Attribute.Destination]] = peerID
for attrKey in FieldName:
message += attrKey + ":" + self.parsedMessage[attrKey] + ";"
return message[:-1]
def GetTagValue(self, attributeType):
try:
return self.parsedMessage[FieldName[attributeType]]
except:
return None
class ConnectionHandler:
peerList = {}
unknownPeers = {}
recentlySeenPeers = {}
def __init__(self, serverDetails):
self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.client = ("", 0)
self.clientID = None
self.messageID = 100
self.ackMessage = []
self.peerList['999'] = serverDetails #server
self.localInput = deque()
self.networkInput = deque()
self.networkOutput = deque()
def __del__(self):
self.clientSocket.close()
#print("Connection closed.")
def RequestPeerList(self):
idsValues = [self.clientID, "999", str(Protocol.RequestPeerRegistry.value), "1", str(self.messageID), "", "get map"]
requestMessage = Message(FormatMessage(idsValues))
requestMessage.intendedRecepients = ['999']
requestMessage.timer = time.time()
self.networkOutput.append(requestMessage)
self.ackMessage.append(requestMessage)
self.messageID += 1
def AcknowledgeMsg(self, nwInput):
pnum = int(nwInput.GetTagValue(Attribute.ProtocolNumber))
nwInput.parsedMessage[FieldName[Attribute.Source]], nwInput.parsedMessage[FieldName[Attribute.Destination]] = nwInput.parsedMessage[FieldName[Attribute.Destination]], nwInput.parsedMessage[FieldName[Attribute.Source]]
nwInput.parsedMessage[FieldName[Attribute.Message]] = "ACK"
nwInput.parsedMessage[FieldName[Attribute.ProtocolNumber]] = str(pnum + 1)
nwInput.parsedMessage[FieldName[Attribute.HopCount]] = "1"
nwInput.intendedRecepients = [nwInput.parsedMessage[FieldName[Attribute.Destination]]]
self.networkOutput.append(nwInput)
def SendData(self, userInput):
msg = ""
pnum = 7 #default broadcast to all (known) peers
peerID = ""
hct = 1
peers = self.peerList.keys()
peers.remove("999")
VL = ""
messagetype = None
regex = re.compile("^(msg (.*))$")
match = regex.match(userInput)
if match:
regex = re.compile("^(msg [0-9]{3} (.*))$")
match = regex.match(userInput)
if not match:
print("Peer ID format is invalid. Please try again.")
return
peerID = userInput[4:7]
if peerID not in peers:
hct = 9
if len(peers) > 3:
random.shuffle(peers)
peers = peers[0:3]
VL = self.clientID
messagetype = MessageType.Forward
else:
peers = [peerID]
peerID = ""
messagetype = MessageType.Direct
msg = userInput[match.end():].strip()
pnum = 3
else:
msg = userInput[3:].strip()
peerID = ""
messagetype = MessageType.Broadcast
msg = msg.translate(None, ''.join(["\'", "\"", ";", ":"])) #remove all invalid characters from the input
msg = msg[:200] #truncate the input string to 200 characters
dataParams = [self.clientID, peerID, str(pnum), str(hct), str(self.messageID), VL, msg]
dataMessage = Message(FormatMessage(dataParams))
dataMessage.intendedRecepients = peers
dataMessage.messageType = messagetype
dataMessage.timer = time.time()
self.networkOutput.append(dataMessage)
self.ackMessage.append(dataMessage)
self.messageID += 1
def ProcessNetworkInput(self, nwInput):
pnum = int(nwInput.GetTagValue(Attribute.ProtocolNumber))
if pnum in [Protocol.DataConfirmation, Protocol.SerialBroadcastConfirmation, Protocol.RegistrationResponse, Protocol.CurrentPeerRegistry]:
for x in self.ackMessage:
if x.messageType == MessageType.Forward:
continue
source = nwInput.GetTagValue(Attribute.Source)
if source not in x.intendedRecepients or x.GetTagValue(Attribute.MessageIDNumber) != nwInput.GetTagValue(Attribute.MessageIDNumber):
continue
x.intendedRecepients.remove(source)
if not x.intendedRecepients:
self.ackMessage.remove(x)
if pnum in [Protocol.Data, Protocol.SerialBroadcast]:
print(nwInput.GetTagValue(Attribute.Source) + " sent: " + nwInput.GetTagValue(Attribute.Message))
self.AcknowledgeMsg(nwInput)
if Protocol.CurrentPeerRegistry == pnum:
message = nwInput.GetTagValue(Attribute.Message).split("and", 1)
self.recentlySeenPeers = message[0].split("=", 1)[1].split(",")
server = self.peerList['999']
self.peerList = UpdatePeerRegistry(message[1].split(","))
self.peerList['999'] = server
self.peerList.pop(self.clientID, None) # delete your own details
DisplayIDS(dict(self.peerList), self.recentlySeenPeers)
if Protocol.RegistrationResponse == pnum:
self.clientID = nwInput.GetTagValue(Attribute.Destination)
print("Successfully registered. My ID is: " + self.clientID)
if Protocol.ErrorMessage == pnum:
print("Error occured! Received: " + parsedResponse.GetTagValue(Attribute.Messages))
def ProcessForwards(self, nwInput):
nwInput.messageType = MessageType.Forward
pnum = int(nwInput.GetTagValue(Attribute.ProtocolNumber))
if pnum == Protocol.DataConfirmation:
for x in self.ackMessage:
source = nwInput.GetTagValue(Attribute.ViewLog)
if source not in x.intendedRecepients or x.GetTagValue(Attribute.MessageIDNumber) != nwInput.GetTagValue(Attribute.MessageIDNumber):
continue
x.intendedRecepients.remove(source)
if not x.intendedRecepients:
self.ackMessage.remove(x)
return
ackMsg = copy.deepcopy(nwInput)
self.AcknowledgeMsg(ackMsg)
src = nwInput.GetTagValue(Attribute.Source)
dest = nwInput.GetTagValue(Attribute.Destination)
msg = nwInput.GetTagValue(Attribute.Message)
print("Received from " + src + " to " + dest + " - " + msg)
hct = int(nwInput.GetTagValue(Attribute.HopCount))
if hct < 1:
print("********************")
print("Dropped message from " + src + " to " + dest + " - hop count exceeded")
print("MESG: " + msg)
print("********************")
return
VL = nwInput.GetTagValue(Attribute.ViewLog).split(",")
if self.clientID in VL:
print("********************")
print("Dropped message from " + src + " to " + dest + " - peer revisited")
print("MESG: " + msg)
print("********************")
return
hct -= 1
VL.append(self.clientID)
nwInput.parsedMessage[FieldName[Attribute.ViewLog]] = ",".join(VL)
nwInput.parsedMessage[FieldName[Attribute.HopCount]] = str(hct)
peers = self.peerList.keys()
peers.remove("999")
peers = [x for x in peers if x not in VL]
if nwInput.GetTagValue(Attribute.Destination) in peers:
nwInput.intendedRecepients = [dest]
nwInput.timer = time.time()
self.networkOutput.append(nwInput)
self.ackMessage.append(nwInput)
return
if len(peers) > 3:
random.shuffle(peers)
peers = peers[0:3]
nwInput.intendedRecepients = peers
nwInput.timer = time.time()
self.networkOutput.append(nwInput)
self.ackMessage.append(nwInput)
def HandleIO(self):
"""Handle receiving udp packets"""
try:
nwInput = self.networkInput.popleft()
if (nwInput.messageType != MessageType.Forward and nwInput.GetTagValue(Attribute.Destination) == self.clientID) or int(nwInput.GetTagValue(Attribute.ProtocolNumber)) == Protocol.RegistrationResponse:
self.ProcessNetworkInput(nwInput)
else:
self.ProcessForwards(nwInput)
except IndexError:
pass
except Exception as e:
print("Unhandled exception: %s" % e)
raise
"""Handle receiving local input"""
try:
userInput = self.localInput.popleft()
if userInput[:3].lower() in ["msg", "all"]:
self.SendData(userInput)
elif "ids" == userInput.lower():
self.RequestPeerList()
else:
print("Please enter a valid input!")
except IndexError:
pass
except Exception as e:
print("Unhandled exception: %s" % e)
raise
"""Handle retry"""
for x in self.ackMessage:
if time.time() - x.timer < 3: #if recepients still haven't received the previous message
return
x.timer = time.time()
if x.retryCount > 4:
print("*****************************")
for recepient in x.intendedRecepients:
print("ERROR: Gave up sending to " + recepient)
print("*****************************")
if self.clientID == None:
print("Failed to register user.")
sys.exit()
else:
self.ackMessage.remove(x)
continue
self.networkOutput.append(x)
x.retryCount += 1
continue
def RegisterClient(self):
registrationValues = ["000", "999", str(Protocol.RegistrationRequest.value), "1", str(self.messageID), "", "register"]
registrationMessage = Message(FormatMessage(registrationValues))
registrationMessage.intendedRecepients = ['999']
registrationMessage.timer = time.time()
self.networkOutput.append(registrationMessage)
self.ackMessage.append(registrationMessage)
self.messageID += 1
def Process(self):
self.clientSocket.bind(("0.0.0.0", 63683))
watch_for_write = []
watch_for_read = [sys.stdin, self.clientSocket]
self.RegisterClient()
while True:
try:
if len(self.networkOutput) > 0:
watch_for_write = [self.clientSocket]
else:
watch_for_write = []
input_ready, output_ready, except_ready = select.select(watch_for_read, watch_for_write, watch_for_read, 3)
for item in input_ready:
if item == sys.stdin:
data = sys.stdin.readline().strip()
if len(data) > 0:
self.localInput.append(str(data))
if item == self.clientSocket:
data, addr = self.clientSocket.recvfrom(1024)
data = str(data)
if len(data) < 0:
return
regEx = re.compile("^(SRC:[0-9]{3};DST:[0-9]{3};PNUM:[0-9]{1};HCT:[0-9]{1};MNUM:[0-9]{3};VL:(\d+(,\d+)*)?;MESG:(.*))$")
if regEx.match(data): # drop data if it doesn't match the given format
nwData = Message(data)
src = nwData.parsedMessage[FieldName[Attribute.Source]]
if src not in self.peerList:
pnum = int(nwData.GetTagValue(Attribute.ProtocolNumber))
if addr in self.peerList.values() and pnum == Protocol.DataConfirmation:
nwData.parsedMessage[FieldName[Attribute.ViewLog]] = self.peerList.keys()[self.peerList.values().index(addr)]
nwData.messageType = MessageType.Forward
else:
self.unknownPeers[(src, nwData.parsedMessage[FieldName[Attribute.MessageIDNumber]])] = addr
self.networkInput.append(nwData)
else:
print("********************")
print("Dropped message - format mismatch")
print("MESG: " + data)
print("********************")
for item in output_ready:
if item == self.clientSocket:
try:
msg_to_send = self.networkOutput.popleft()
msgID = msg_to_send.parsedMessage[FieldName[Attribute.MessageIDNumber]]
for recepient in msg_to_send.intendedRecepients:
if recepient in self.peerList:
self.clientSocket.sendto(msg_to_send.Format(recepient), self.peerList[recepient])
elif (recepient, msgID) in self.unknownPeers:
self.clientSocket.sendto(msg_to_send.Format(recepient), self.unknownPeers[(recepient, msgID)])
del self.unknownPeers[(recepient, msgID)]
#print(msg_to_send.Format(recepient))
except IndexError:
pass
except Exception as e:
print("Unhandled send exception: %s" % e)
for item in except_ready:
if item == self.clientSocket:
return
# Catch ^C to cancel and end program.
except KeyboardInterrupt:
return
except Exception as e:
print("Unhandled exception 0: %s" % e)
return
self.HandleIO()
def main():
serverConnectionManager = ConnectionHandler(("steel.isi.edu", 63682))
serverConnectionManager.Process()
if __name__ == "__main__":
main()