61 lines
1.5 KiB
Java
61 lines
1.5 KiB
Java
package com.example.carcontroller.SLAM;
|
|
|
|
import com.example.carcontroller.SlamScan;
|
|
import com.google.protobuf.InvalidProtocolBufferException;
|
|
|
|
import org.zeromq.SocketType;
|
|
import org.zeromq.ZContext;
|
|
import org.zeromq.ZMQ;
|
|
|
|
/**
|
|
* Connects to Pi and retrieves updates of the SLAM map,
|
|
* notifying listeners of changes to the map when they arrive.
|
|
* Uses 0MQ for the transport.
|
|
*/
|
|
public class ZmqSlamUpdater extends SlamUpdater {
|
|
|
|
private ZContext context;
|
|
private String host;
|
|
private String port;
|
|
private boolean running = false;
|
|
|
|
public ZmqSlamUpdater(String host, String port) {
|
|
super();
|
|
this.host = host;
|
|
this.port = port;
|
|
init();
|
|
}
|
|
|
|
@Override
|
|
protected void init() {
|
|
super.init();
|
|
context = new ZContext();
|
|
}
|
|
|
|
|
|
@Override
|
|
public void run() {
|
|
// Should send a gRPC message to start listening...
|
|
|
|
|
|
running = true;
|
|
// Receive map from zmq and update appropriately.
|
|
try (ZMQ.Socket socket = context.createSocket(SocketType.PAIR)) {
|
|
socket.connect("tcp://" + host + ":" + port);
|
|
socket.send("Hi");
|
|
while (running) {
|
|
byte[] map = socket.recv();
|
|
fireMapChanged(SlamScan.parseFrom(map));
|
|
}
|
|
} catch (InvalidProtocolBufferException e) {
|
|
System.out.println("Invalid map found");
|
|
running = false;
|
|
}
|
|
}
|
|
|
|
|
|
public void stop() {
|
|
running = false;
|
|
}
|
|
}
|