org.bidib.jbidibc.Ping Maven / Gradle / Ivy
package org.bidib.jbidibc;
import java.util.Arrays;
import org.bidib.jbidibc.core.BidibMessageProcessor;
import org.bidib.jbidibc.core.DefaultMessageListener;
import org.bidib.jbidibc.core.node.BidibNode;
import org.bidib.jbidibc.messages.Node;
import org.bidib.jbidibc.messages.exception.PortNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
/**
* This commands sends the Ping message to the specified node.
*
*/
@Command
public class Ping extends BidibNodeCommand {
private static final Logger LOGGER = LoggerFactory.getLogger(Ping.class);
@Option(names = { "-maxPings" }, description = "maximum number of pings, e.g. 10 to stop after 10 pings")
private int maxPings = -1;
public static void main(String[] args) {
run(new Ping(), args);
}
@Override
public Integer call() {
int result = 20;
try {
openPort(getPortName(), null);
Node node = findNode();
if (node != null) {
BidibNode bidibNode = getBidib().getNode(node);
String nodeId = String.format("0x%014x", node.getUniqueId() & 0xffffffffffffffL);
final PingResultHelper pingResultHelper = new PingResultHelper();
final BidibMessageProcessor bidibMessageProcessor = getBidib().getBidibMessageProcessor();
if (bidibMessageProcessor != null) {
bidibMessageProcessor.addMessageListener(new DefaultMessageListener() {
@Override
public void pong(byte[] address, int messageNum, int marker) {
LOGGER.info("Received current pong marker: {}", marker);
pingResultHelper.setPongMarker(marker);
synchronized (pingResultHelper) {
pingResultHelper.notifyAll();
}
}
});
}
System.out.println("PING " + nodeId + " (" + Arrays.toString(node.getAddr()) + ")."); // NOSONAR
byte marker = 1;
while (true) {
final long now = System.currentTimeMillis();
bidibNode.ping(new byte[] { marker });
// wait for repsonse
synchronized (pingResultHelper) {
LOGGER.info("Wait for response.");
if (pingResultHelper.getPongMarker() == null) {
pingResultHelper.wait(3000L);
}
}
System.out
.println("got response from " + nodeId + " (" + Arrays.toString(node.getAddr()) // NOSONAR
+ "): seq=" + pingResultHelper.getPongMarker() + " time="
+ (System.currentTimeMillis() - now) + "ms");
if (maxPings > -1) {
maxPings--;
if (maxPings <= 0) {
LOGGER.info("finished pings.");
break;
}
}
marker++;
}
result = 0;
}
else {
System.err.println("node with unique id \"" + getNodeIdentifier() + "\" not found"); // NOSONAR
}
getBidib().close();
}
catch (
PortNotFoundException ex) {
System.err
.println("The provided port was not found: " + ex.getMessage() // NOSONAR
+ ". Verify that the BiDiB device is connected.");
}
catch (
Exception e) {
System.err.println("Ping node failed: " + e); // NOSONAR
}
return result;
}
private static final class PingResultHelper {
private Integer pongMarker;
private Object lock = new Object();
public void setPongMarker(int pongMarker) {
synchronized (lock) {
this.pongMarker = pongMarker;
}
}
public Integer getPongMarker() {
synchronized (lock) {
return pongMarker;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy