Fix the subscription to work with pyzmq

This commit is contained in:
Piv
2020-02-18 21:37:49 +10:30
parent c4da2bd0c8
commit 28e691f50c

View File

@@ -7,6 +7,8 @@ import org.zeromq.SocketType;
import org.zeromq.ZContext; import org.zeromq.ZContext;
import org.zeromq.ZMQ; import org.zeromq.ZMQ;
import java.util.Arrays;
/** /**
* Connects to Pi and retrieves updates of the SLAM map, * Connects to Pi and retrieves updates of the SLAM map,
* notifying listeners of changes to the map when they arrive. * notifying listeners of changes to the map when they arrive.
@@ -18,6 +20,7 @@ public class ZmqSlamUpdater extends SlamUpdater {
private String host; private String host;
private String port; private String port;
private boolean running = false; private boolean running = false;
private static final byte[] SLAM_SUBSCRIPTION = "slam_map".getBytes();
public ZmqSlamUpdater(String host, String port) { public ZmqSlamUpdater(String host, String port) {
super(); super();
@@ -39,11 +42,14 @@ public class ZmqSlamUpdater extends SlamUpdater {
// Receive map from zmq and update appropriately. // Receive map from zmq and update appropriately.
try (ZMQ.Socket socket = context.createSocket(SocketType.SUB)) { try (ZMQ.Socket socket = context.createSocket(SocketType.SUB)) {
socket.connect("tcp://" + host + ":" + port); socket.connect("tcp://" + host + ":" + port);
socket.subscribe("slam_map"); socket.subscribe(SLAM_SUBSCRIPTION);
while (running) { while (running) {
byte[] map = socket.recv(); byte[] map = socket.recv();
// Don't want to do the event when we just receive the header.
if (!Arrays.equals(map, SLAM_SUBSCRIPTION)) {
fireMapChanged(SlamScan.parseFrom(map)); fireMapChanged(SlamScan.parseFrom(map));
} }
}
} catch (InvalidProtocolBufferException e) { } catch (InvalidProtocolBufferException e) {
System.out.println("Invalid map found"); System.out.println("Invalid map found");
running = false; running = false;