aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-07-10 10:25:25 -0700
committerAnton Tananaev <anton@traccar.org>2022-07-10 10:25:25 -0700
commit65cb239b78d3e75db565309b0196378e29a439d4 (patch)
tree64419fdbc811e78f629e8da2a34f5ecc950c36c4 /src
parent15ff944349b44ead4d711459d7d0e54838c7f30c (diff)
downloadtrackermap-server-65cb239b78d3e75db565309b0196378e29a439d4.tar.gz
trackermap-server-65cb239b78d3e75db565309b0196378e29a439d4.tar.bz2
trackermap-server-65cb239b78d3e75db565309b0196378e29a439d4.zip
Specify multicast interface
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/traccar/broadcast/MulticastBroadcastService.java22
-rw-r--r--src/main/java/org/traccar/config/Keys.java7
2 files changed, 23 insertions, 6 deletions
diff --git a/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java b/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java
index 81b6cd5ec..877008eda 100644
--- a/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java
+++ b/src/main/java/org/traccar/broadcast/MulticastBroadcastService.java
@@ -30,7 +30,9 @@ import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.MulticastSocket;
+import java.net.NetworkInterface;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Map;
@@ -44,8 +46,9 @@ public class MulticastBroadcastService implements BroadcastService {
private final ObjectMapper objectMapper;
- private final InetAddress address;
+ private final NetworkInterface networkInterface;
private final int port;
+ private final InetSocketAddress group;
private DatagramSocket publisherSocket;
@@ -56,8 +59,15 @@ public class MulticastBroadcastService implements BroadcastService {
public MulticastBroadcastService(Config config, ObjectMapper objectMapper) throws IOException {
this.objectMapper = objectMapper;
- address = InetAddress.getByName(config.getString(Keys.BROADCAST_ADDRESS));
port = config.getInteger(Keys.BROADCAST_PORT);
+ String interfaceName = config.getString(Keys.BROADCAST_INTERFACE);
+ if (interfaceName.indexOf('.') >= 0 || interfaceName.indexOf(':') >= 0) {
+ networkInterface = NetworkInterface.getByInetAddress(InetAddress.getByName(interfaceName));
+ } else {
+ networkInterface = NetworkInterface.getByName(interfaceName);
+ }
+ InetAddress address = InetAddress.getByName(config.getString(Keys.BROADCAST_ADDRESS));
+ group = new InetSocketAddress(address, port);
}
@Override
@@ -106,7 +116,7 @@ public class MulticastBroadcastService implements BroadcastService {
private void sendMessage(BroadcastMessage message) {
try {
byte[] buffer = objectMapper.writeValueAsString(message).getBytes(StandardCharsets.UTF_8);
- DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
+ DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group);
publisherSocket.send(packet);
} catch (IOException e) {
LOGGER.warn("Broadcast failed", e);
@@ -150,11 +160,10 @@ public class MulticastBroadcastService implements BroadcastService {
}
private final Runnable receiver = new Runnable() {
- @SuppressWarnings("deprecation")
@Override
public void run() {
try (MulticastSocket socket = new MulticastSocket(port)) {
- socket.joinGroup(address);
+ socket.joinGroup(group, networkInterface);
while (!service.isShutdown()) {
DatagramPacket packet = new DatagramPacket(receiverBuffer, receiverBuffer.length);
socket.receive(packet);
@@ -162,10 +171,11 @@ public class MulticastBroadcastService implements BroadcastService {
LOGGER.info("Broadcast received: {}", data);
handleMessage(objectMapper.readValue(data, BroadcastMessage.class));
}
- socket.leaveGroup(address);
+ socket.leaveGroup(group, networkInterface);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
+
}
diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java
index 8e11b4013..6f19ee863 100644
--- a/src/main/java/org/traccar/config/Keys.java
+++ b/src/main/java/org/traccar/config/Keys.java
@@ -1445,6 +1445,13 @@ public final class Keys {
"time,position,speed,course,accuracy,result");
/**
+ * Multicast interface. It can be either an IP address or an interface name.
+ */
+ public static final ConfigKey<String> BROADCAST_INTERFACE = new StringConfigKey(
+ "broadcast.interface",
+ List.of(KeyType.CONFIG));
+
+ /**
* Multicast address for broadcasting synchronization events.
*/
public static final ConfigKey<String> BROADCAST_ADDRESS = new StringConfigKey(