forked from aniskoubaa/gaitech_edu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WS_UDP_forwarder.py
99 lines (73 loc) · 2.3 KB
/
WS_UDP_forwarder.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
import socket # Import socket module
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket # pip install git+https://github.com/dpallot/simple-websocket-server.git
import base64
import time
import threading
# Load an color image in grayscale
ws_stream_port = 1111
wscmdport=9090
UDP_IP = "127.0.0.1"
UDP_PORT = 25500
MAX_SIZE=65000
robot_UDP_Port = 1109
WS = None
data=''
addr=None;
sockToDrone = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
websocket = None;
websocket_connection= False;
class Simplecmd(WebSocket):
def handleMessage(self):
# print(self.data)
try:
# print('connected to robot ')
global addr;
print('sent to robot: ',addr)
sockToDrone.sendto(self.data,addr)
except socket.error as e:
print(e)
def handleConnected(self):
global websocket,websocket_connection
websocket = self;
websocket_connection= True;
print(self.address, 'connected...')
def handleError(self, error):
print (error)
def handleClose(self):
global websocket,websocket_connection
websocket = None;
websocket_connection= False;
print(self.address, 'closed')
class WSThread ():
def __init__(self,port, type , data_rate=1.0):
self.type = type
self.port = port
self.data_rate = data_rate
t = threading.Thread(target=self.run)
t.setName('WSThread')
t.start()
def run ( self ):
server = SimpleWebSocketServer('', self.port, self.type)
server.serveforever()
class UDPThread():
def __init__(self,websocket ,data , data_rate=1.0):
self.data = data
self.websocket = websocket
self.data_rate = data_rate
t = threading.Thread(target=self.run)
t.setName('UDPThread')
t.start()
def run ( self ):
self.websocket.sendMessage(u''+self.data);
print ('sent to user')
wsc= WSThread(wscmdport, Simplecmd)
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
#global addr;
data, addr = sock.recvfrom(MAX_SIZE)
print(addr)
# print(data)
print(websocket_connection)
if(websocket_connection):
UDPThread(websocket,data)