aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/broadcast/BroadcastService.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-25 13:33:35 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-25 13:33:35 -0700
commitc53d98c668af9c79767e22964f05c7bf7dc866f2 (patch)
tree42d664009487232e3a21514bb5ab52c7571262a6 /src/main/java/org/traccar/broadcast/BroadcastService.java
parent5a732a26c85785a9b801583f2fff0ce47314aa03 (diff)
downloadtrackermap-server-c53d98c668af9c79767e22964f05c7bf7dc866f2.tar.gz
trackermap-server-c53d98c668af9c79767e22964f05c7bf7dc866f2.tar.bz2
trackermap-server-c53d98c668af9c79767e22964f05c7bf7dc866f2.zip
Integrate broadcast service
Diffstat (limited to 'src/main/java/org/traccar/broadcast/BroadcastService.java')
-rw-r--r--src/main/java/org/traccar/broadcast/BroadcastService.java80
1 files changed, 2 insertions, 78 deletions
diff --git a/src/main/java/org/traccar/broadcast/BroadcastService.java b/src/main/java/org/traccar/broadcast/BroadcastService.java
index 26e38400b..8a2e4bafc 100644
--- a/src/main/java/org/traccar/broadcast/BroadcastService.java
+++ b/src/main/java/org/traccar/broadcast/BroadcastService.java
@@ -15,84 +15,8 @@
*/
package org.traccar.broadcast;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.traccar.LifecycleObject;
-import org.traccar.config.Config;
-import org.traccar.config.Keys;
-
-import java.io.IOException;
-import java.net.DatagramPacket;
-import java.net.DatagramSocket;
-import java.net.InetAddress;
-import java.net.MulticastSocket;
-import java.nio.charset.StandardCharsets;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-
-public class BroadcastService implements LifecycleObject {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(BroadcastService.class);
-
- private final ObjectMapper objectMapper;
-
- private final InetAddress address;
- private final int port;
-
- private DatagramSocket publisherSocket;
-
- private final ExecutorService service = Executors.newSingleThreadExecutor();
- private final byte[] receiverBuffer = new byte[4096];
-
- public BroadcastService(Config config, ObjectMapper objectMapper) throws IOException {
- this.objectMapper = objectMapper;
- address = InetAddress.getByName(config.getString(Keys.BROADCAST_ADDRESS));
- port = config.getInteger(Keys.BROADCAST_PORT);
- }
-
- public void sendMessage(BroadcastMessage message) throws IOException {
- byte[] buffer = objectMapper.writeValueAsString(message).getBytes(StandardCharsets.UTF_8);
- DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
- publisherSocket.send(packet);
- }
-
- private void handleMessage(BroadcastMessage message) {
- if (message.getDeviceStatus() != null) {
- LOGGER.info("Broadcast received device {}", message.getDeviceStatus().getDeviceId());
- } else if (message.getPosition() != null) {
- LOGGER.info("Broadcast received position {}", message.getPosition().getDeviceId());
- }
- }
-
- @Override
- public void start() throws IOException {
- service.submit(receiver);
- publisherSocket = new DatagramSocket();
- }
-
- @Override
- public void stop() {
- publisherSocket.close();
- service.shutdown();
- }
-
- private final Runnable receiver = new Runnable() {
- @Override
- public void run() {
- try (MulticastSocket socket = new MulticastSocket(port)) {
- socket.joinGroup(address);
- while (!service.isShutdown()) {
- DatagramPacket packet = new DatagramPacket(receiverBuffer, receiverBuffer.length);
- socket.receive(packet);
- String data = new String(packet.getData(), 0, packet.getLength(), StandardCharsets.UTF_8);
- handleMessage(objectMapper.readValue(data, BroadcastMessage.class));
- }
- socket.leaveGroup(address);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- };
+public interface BroadcastService extends LifecycleObject, BroadcastInterface {
+ void registerListener(BroadcastInterface listener);
}