-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix/ForeFlight: Send unicast to many clients, closes #221
- Loading branch information
Showing
5 changed files
with
243 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/python3 | ||
# Miniature mock-up of interface to ForeFlight | ||
# - Every 5s broadcasts the message {"App":"ForeFlight","GDL90":{"port":4000, "sim":true}} on port 63093 | ||
# - Listens on UDP Port 49002 and outputs whatever it receives | ||
|
||
import sys | ||
import socket | ||
import time | ||
|
||
#--- Output Socket (Broadcast) | ||
sockBcst = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | ||
sockBcst.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | ||
|
||
#--- Listen Socket | ||
sockListen = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | ||
sockListen.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
if sys.platform != "win32": | ||
sockListen.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) | ||
sockListen.settimeout(1.0) | ||
sockListen.bind(('', 49002)) | ||
|
||
#---wait for and print data | ||
tsNextBcst = 0.0 | ||
while 1: | ||
# Every (about) 5s we broadcast our availability to the world | ||
if (time.monotonic() > tsNextBcst): | ||
sockBcst.sendto(b'{"App":"ForeFlight","GDL90":{"port":4000, "sim":true}}', ('<broadcast>', 63093)) | ||
tsNextBcst = time.monotonic() + 5.0 | ||
# Wait for data, timeout is defined about (1s) and would throw an exception | ||
try: | ||
data, addr = sockListen.recvfrom(1024) | ||
print(time.strftime('%H:%M:%S'), addr) | ||
print(data.decode("utf-8")) | ||
except OSError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
#!/usr/bin/python | ||
#!/usr/bin/python3 | ||
|
||
import sys | ||
import socket | ||
|
||
UDP_IP = "localhost" | ||
UDP_PORT = 49003 | ||
if len(sys.argv) < 3: | ||
print ("Usage: udp_send.py <TargetHost> <Port> [msg]") | ||
exit(1) | ||
|
||
UDP_IP = sys.argv[1] # "localhost" | ||
UDP_PORT = int(sys.argv[2]) # 49003 | ||
MESSAGE = "Hello, World!" | ||
if len(sys.argv) >= 4: | ||
MESSAGE = sys.argv[3] | ||
|
||
print "UDP target IP:", UDP_IP | ||
print "UDP target port:", UDP_PORT | ||
print "message:", MESSAGE | ||
print ("UDP target IP:", UDP_IP) | ||
print ("UDP target port:", UDP_PORT) | ||
print ("message:", MESSAGE) | ||
|
||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) # UDP | ||
|
||
# User wants broadcast? We can do that! | ||
if UDP_IP == '255.255.255.255': | ||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | ||
|
||
sock.sendto(MESSAGE.encode('utf-8'), (UDP_IP, UDP_PORT)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.