-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2p_server.py
203 lines (177 loc) · 5.93 KB
/
p2p_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
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
#!/usr/bin/python
#coding:utf-8
import SocketServer
import socket
import fcntl
import struct
import time
import threading
def get_ip_addr(ifname='eth0'):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl( s.fileno(),0x8915,struct.pack('256s', ifname))[20:24])
interval = 7
HEARTBEAT = '0'
GETLIST = '1'
CONNECTWHO = '2'
LOGIN = '3'
LOGOUT = '4'
FAILED = '5'
OPERATESUCCESS = '6'
PICHEAD = '7'
TRANSMIT = '-'
LIST = [] # items' format: ((ip,port),socket,last_time))
lock = threading.Lock()
class ThreadedUDPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = self.request[0].strip()
sock = self.request[1]
ip, port = self.client_address
if len(data)<40:
print ip,':',port,'-> ',data,' <-'
if data[0] == TRANSMIT:
data = data[1:]
if len(data)<40:
return
for i in range(40):
if data[i] == '#':
addr,dat = data[:i],data[i+1:]
print('recv transmit packet from %s:%s -> length: %d'%(ip,str(port),len(dat)))
dsthost,dstport = addr.split(':')
dsthost = str(dsthost)
dstport = int(dstport)
dat = PICHEAD + str(ip) + ':' + str(port) + '#' + dat
sock.sendto(dat,(dsthost,dstport))
# Transmit(dat,dsthost,int(dstport))
break
elif data[0] == HEARTBEAT:
HeartBeat(((ip,port),sock))
elif data[0] == GETLIST:
lst = GetList(ip,port)
lst = GETLIST + str(lst)
sock.sendto(lst,(ip,port))
elif data[0] == CONNECTWHO:
host,port1 = str(data[2:]).split(':')
port1 = int(port1)
dat = CONNECTWHO + data[1] + str(ip) + ':' + str(port)
print('connect to ')
sock.sendto(dat,(host,port1))
elif data[0] == LOGIN:
if Login(((ip,port),sock)):
sock.sendto(LOGIN,(ip,port))
else:
sock.sendto(FAILED,(ip,port))
elif data[0] == LOGOUT:
Logout(ip,port)
sock.sendto(LOGOUT,(ip,port))
elif data[0] == OPERATESUCCESS:
dstip, dstport = data[1:].strip().split(':')
OperateSuccessTo((ip,port),(dstip,int(dstport)))
else:
pass
class ThreadedUDPServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
pass
def Login(((ip,port),sock)):
for (_ip,_port),_socket,_last_time in LIST:
if ip==_ip:
if port==_port:
return True
else:
pass
tm = time.time()
lock.acquire()
LIST.append(((ip,port),sock,tm))
lock.release()
return True
def Logout(ip,port):
Remove(ip,port)
def OperateSuccessTo((ip,port),(dstip,dstport)):
for (_ip,_port),_socket,_last_time in LIST:
if dstip == _ip and dstport == _port:
dat = OPERATESUCCESS + str(ip) + ':' + str(port)
_socket.sendto(dat,(dstip,int(dstport)))
def Remove(ip,port):
lock.acquire()
for (_ip,_port),_socket,_last_time in LIST:
if ip==_ip and port == _port:
print 'Remove: ',_ip,':',_port
LIST.remove(((_ip,_port),_socket,_last_time))
break
lock.release()
def UpdateTime((ip,port), socket):
lock.acquire()
tm = time.time()
for (_ip,_port),_socket,_last_time in LIST:
if ip==_ip and port==_port:
if socket==_socket:
LIST.remove(((_ip,_port),_socket,_last_time))
LIST.append(((_ip,_port), _socket, tm))
break
lock.release()
def GetList(ip,port):
lst = ''
for (_ip,_port),_socket,_last_time in LIST:
if _ip == ip and _port == port:
continue
lst += str(_ip)+':'+str(_port)+'#'
if len(lst) > 0:
lst = lst[:-1]
return lst
def ConnectTo((ip,port),(dst_ip,dst_port)):
for (_ip,_port),_socket,_last_time in LIST:
if dst_ip==_ip and dst_port==int(_port):
print('connectto')
data = CONNECTWHO + str(ip) + ':' + str(port)
_socket.sendto(data, (dst_ip,int(dst_port)))
return True
else:
return False
def Transmit(data,dst_ip,dst_port):
for (_ip,_port),_socket,_last_time in LIST:
if dst_ip==_ip and dst_port==int(_port):
_socket.sendto(data, (dst_ip,int(dst_port)))
def HeartBeat(((ip,port),socket)):
UpdateTime((ip,port),socket)
SendHeartBeat(((ip,port),socket))
def SendHeartBeat(((ip,port),sock)):
if ip==None or port==None or sock==None:
UpdateTime((ip,port),sock)
return
try:
sock.sendto(HEARTBEAT,(ip,port))
except:
Remove(ip,port)
def ListCheck():
try:
while True:
for (_ip,_port),_socket,_last_time in LIST:
if time.time() - _last_time > 15:
SendHeartBeat(((_ip,_port),_socket))
if time.time() - _last_time > 28:
Remove(_ip,_port)
time.sleep(interval)
except KeyboardInterrupt:
pass
def StartListCheckThrd():
th = threading.Thread(target=ListCheck(),args=())
th.setName('ListCheckDaemon')
th.setDaemon(True)
th.start()
def StartUDPServerThrd():
HOST, PORT = get_ip_addr('eth1'), 9999
ThreadedUDPServer.allow_reuse_address = True
server = ThreadedUDPServer((HOST, PORT), ThreadedUDPRequestHandler)
server.request_queue_size = 20 # max request queue number
server.max_packet_size = 8192 * 20
try:
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
server_thread.daemon = True
server_thread.start()
except KeyboardInterrupt:
print(" UDPServer has stopped! Bye!")
exit(0)
def main():
StartUDPServerThrd() # must start the UDPServerThread first!
StartListCheckThrd()
if __name__ == "__main__":
main()