Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * jndn-management |
| 3 | * Copyright (c) 2015-2016, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU Lesser General Public License, |
| 7 | * version 3, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | package com.intel.jndn.management; |
| 15 | |
| 16 | import com.intel.jndn.management.helpers.FetchHelper; |
| 17 | import net.named_data.jndn.Data; |
| 18 | import net.named_data.jndn.Face; |
| 19 | import net.named_data.jndn.Interest; |
| 20 | import net.named_data.jndn.Name; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.util.logging.Level; |
| 24 | import java.util.logging.Logger; |
| 25 | |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Simplistic checker for live connection to NFD forwarder. |
| 28 | */ |
| 29 | public final class NdnPingClient { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 30 | public static final long DEFAULT_TIMEOUT = 2000; |
| 31 | private static final Logger LOG = Logger.getLogger(NdnPingClient.class.getName()); |
| 32 | |
| 33 | /** |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 34 | * Prevent creation of NdnPingClient instances. |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 35 | */ |
| 36 | private NdnPingClient() { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Ping a forwarder on an existing face to verify that the forwarder is |
| 41 | * working and responding to requests; this version sends a discovery packet |
| 42 | * to /localhost/nfd which should always respond if the requestor is on the |
| 43 | * same machine as the NDN forwarding daemon. |
| 44 | * |
| 45 | * @param face only a localhost Face |
| 46 | * @return true if successful, false otherwise |
| 47 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 48 | public static boolean pingLocal(final Face face) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 49 | return ping(face, new Name("/localhost/nfd")); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Request a name on an existing face to verify the forwarder is working and |
| 54 | * responding to requests. Note that the name must be served or cached on the |
| 55 | * forwarder for this to return true. |
| 56 | * |
| 57 | * @param face a {@link Face} to ping |
| 58 | * @param name a known {@link Name} that the remote node will answer to |
| 59 | * @return true if successful, false otherwise |
| 60 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 61 | public static boolean ping(final Face face, final Name name) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 62 | // build interest |
| 63 | Interest interest = new Interest(name); |
Davide Pesavento | 4821a34 | 2020-11-06 00:15:32 -0500 | [diff] [blame^] | 64 | interest.setCanBePrefix(true); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 65 | interest.setMustBeFresh(true); |
Davide Pesavento | 4821a34 | 2020-11-06 00:15:32 -0500 | [diff] [blame^] | 66 | interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 67 | |
| 68 | // send packet |
| 69 | try { |
| 70 | Data data = FetchHelper.getData(face, interest); |
| 71 | return data != null; |
| 72 | } catch (IOException e) { |
| 73 | LOG.log(Level.INFO, "Error sending ping interest", e); |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 | } |