Skip to content

Commit

Permalink
Fix/ForeFlight: Send unicast to many clients, closes #221
Browse files Browse the repository at this point in the history
  • Loading branch information
TwinFan committed Jan 30, 2024
1 parent 9e951df commit 6179f71
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 182 deletions.
35 changes: 35 additions & 0 deletions Data/ForeFlight/ForeFlight_Sim.py
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
24 changes: 18 additions & 6 deletions Data/RealTraffic/udp_send.py
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))
12 changes: 4 additions & 8 deletions Include/LTForeFlight.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
#define FF_CHECK_POPUP "Open ForeFlight's web site about the Mobile EFB"

#define FOREFLIGHT_NAME "ForeFlight"
#define FF_LOCALHOST "0.0.0.0"
constexpr size_t FF_NET_BUF_SIZE = 512;

// sending intervals in milliseonds
Expand All @@ -50,6 +49,7 @@ constexpr std::chrono::milliseconds FF_INTVL = std::chrono::milliseconds(

#define MSG_FF_LISTENING "ForeFlight: Waiting for a ForeFlight device to broadcast its address..."
#define MSG_FF_SENDING "ForeFlight: Starting to send to %s"
#define MSG_FF_NOT_SENDING "ForeFlight: No longer sending to %s"
#define MSG_FF_STOPPED "ForeFlight: Stopped"

//
Expand All @@ -64,13 +64,9 @@ class ForeFlightSender : public LTOutputChannel
FF_STATE_DISCOVERY, ///< Waiting for a ForeFlight device to broadcast its address on the network
FF_STATE_SENDING, ///< Actually sending data to a discovered device
} state = FF_STATE_NONE;
std::string ffAddr; ///< Address of the ForeFlight app we are sending to
XPMP2::UDPReceiver udp; ///< UDP socket for receiving and sending UDP datagrams from/to ForeFlight
// time points last sent something
std::chrono::steady_clock::time_point nextGPS;
std::chrono::steady_clock::time_point nextAtt;
std::chrono::steady_clock::time_point nextTraffic;
std::chrono::steady_clock::time_point lastStartOfTraffic;
std::string ffAddr; ///< Addresses of the ForeFlight apps we are sending to
/// UDP sockets for sending UDP datagrams from/to ForeFlight apps
std::map<XPMP2::SockAddrTy, XPMP2::UDPReceiver> mapUdp;

public:
ForeFlightSender ();
Expand Down
2 changes: 1 addition & 1 deletion Lib/XPMP2
Submodule XPMP2 updated 2 files
+0 −9 src/Network.cpp
+1 −12 src/Network.h
Loading

0 comments on commit 6179f71

Please sign in to comment.