-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
144 lines (131 loc) · 4.1 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
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
import numpy as np
import cv2 as cv
import socket
from pickle import loads, dumps
import sys
from tempfile import TemporaryFile as tmpFile
from PIL import Image
import matplotlib.pyplot as plt
import time
import threading
#import socket.timeout as TimeoutException
BUFFER_SIZE = 2048
user_list = {}
def writeLog(s, file=None):
if file == None:
print(s)
else:
file.write(s+'\n')
return
class user:
def __init__(self, addr, seq, rtt, wind):
self.addr = addr
self.seq = seq
self.rtt = 0
self.wind = wind
def push(self, sock, data):
data[0] = self.seq
sock.sendto(dumps(data), self.addr)
self.seq = (self.seq+1)%self.wind
return
def getSock(IP, port, UDP=False, timeout=None):
typ = socket.SOCK_DGRAM if UDP else socket.SOCK_STREAM
t = socket.socket(socket.AF_INET, typ)
t.bind((IP, port))
t.settimeout(timeout)
return t
def calculateWindowSize(rtt):
if rtt < 1:
rtt = 5
return rtt*15
def sendWindowSize(sock, user_data, probe_count=5):
writeLog("Sending window size")
seq_no = user_data.seq
wind_size = user_data.wind
sock.settimeout(user_data.rtt+0.1)
data = None
for i in range(probe_count):
sock.sendto(dumps([seq_no+i,9,wind_size]),user_data.addr)
start = time.time()
while(time.time()-start < 30):
try:
data = sock.recv(BUFFER_SIZE)
data = loads(data)
if seq_no<=data[0]<seq_no+probe_count and data[1]==9:
return 1
except socket.timeout:
# continue in case of timeout
pass
except Exception as e:
s = "Exception occurred in sendWindowSize: "+e
writeLog(s)
return -1
def listen(sock):
seq_no = -1
rtt = -1
while(True):
#print('Listening')
try:
data, client = sock.recvfrom(BUFFER_SIZE)
data = loads(data)
seq_no = data[0]
if data[1] == 8:
rtt = 2*(time.time()-data[2])
if client not in user_list:
tmp_usr = user(client, data[0], rtt, calculateWindowSize(rtt))
writeLog("Someone new is here, sending ACK")
sock.sendto(dumps([data[0],8,1]),client)
writeLog("ACK sent and sending window size")
stat = sendWindowSize(sock, tmp_usr)
writeLog("Window size set, adding new user: "+str(stat))
if stat == 1:
user_list[client] = tmp_usr
print("New user added")
else:
sock.sendto(dumps([data[0],8,1]),client)
elif data[1] == 9:
# ACK for window_size
if client not in user_list:
# terminate connection
pass
elif data[1] == 10:
writeLog("Removing user")
if client in user_list:
del(user_list[client])
else:
# send NAK
pass
except Exception as e:
print("An exception occured:", e)
pass
print('Returning')
return
def capAndSend(sock, src):
cap = cv.VideoCapture(src)
while(True):
ret, frame = cap.read()
img = Image.fromarray(frame)
f = tmpFile()
img.save(f, 'JPEG')
f.seek(0)
data = [-1,2,f.read()]
for each in user_list.values():
each.push(sock, data)
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()
def broadcast(sock, src=0):
writeLog("In BroadCast")
t = threading.Thread(target=listen, args=(sock,))
s = threading.Thread(target=capAndSend, args=(sock, src))
t.start()
s.start()
t.join()
print("Join 1")
print("Join 2")
return
ip = '127.0.0.1'
port = 64000
s = getSock(ip, port, UDP=True, timeout=40)
broadcast(s)