Major refactoring and improvements
This commit removes dependency on jndn-utils (it requires Java 8, while
jndn-management needs to be compatible with Java 7 to support Android)
Some changes include interface unification with structures in ndn-cxx library
Change-Id: I944ea41e225edc1848657ed574b625c7ec18df5d
diff --git a/src/main/java/com/intel/jndn/management/NdnPingClient.java b/src/main/java/com/intel/jndn/management/NdnPingClient.java
new file mode 100644
index 0000000..ecdaecc
--- /dev/null
+++ b/src/main/java/com/intel/jndn/management/NdnPingClient.java
@@ -0,0 +1,73 @@
+/*
+ * jndn-management
+ * Copyright (c) 2015-2016, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 3, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+package com.intel.jndn.management;
+
+import com.intel.jndn.management.helpers.FetchHelper;
+import net.named_data.jndn.Data;
+import net.named_data.jndn.Face;
+import net.named_data.jndn.Interest;
+import net.named_data.jndn.Name;
+
+import java.io.IOException;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+public class NdnPingClient {
+ public static final long DEFAULT_TIMEOUT = 2000;
+ private static final Logger LOG = Logger.getLogger(NdnPingClient.class.getName());
+
+ /**
+ * Prevent creation of NdnPingClient instances
+ */
+ private NdnPingClient() {
+ }
+
+ /**
+ * Ping a forwarder on an existing face to verify that the forwarder is
+ * working and responding to requests; this version sends a discovery packet
+ * to /localhost/nfd which should always respond if the requestor is on the
+ * same machine as the NDN forwarding daemon.
+ *
+ * @param face only a localhost Face
+ * @return true if successful, false otherwise
+ */
+ public static boolean pingLocal(Face face) {
+ return ping(face, new Name("/localhost/nfd"));
+ }
+
+ /**
+ * Request a name on an existing face to verify the forwarder is working and
+ * responding to requests. Note that the name must be served or cached on the
+ * forwarder for this to return true.
+ *
+ * @param face a {@link Face} to ping
+ * @param name a known {@link Name} that the remote node will answer to
+ * @return true if successful, false otherwise
+ */
+ public static boolean ping(Face face, Name name) {
+ // build interest
+ Interest interest = new Interest(name);
+ interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
+ interest.setMustBeFresh(true);
+
+ // send packet
+ try {
+ Data data = FetchHelper.getData(face, interest);
+ return data != null;
+ } catch (IOException e) {
+ LOG.log(Level.INFO, "Error sending ping interest", e);
+ return false;
+ }
+ }
+}