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 | |
| 26 | public class NdnPingClient { |
| 27 | public static final long DEFAULT_TIMEOUT = 2000; |
| 28 | private static final Logger LOG = Logger.getLogger(NdnPingClient.class.getName()); |
| 29 | |
| 30 | /** |
| 31 | * Prevent creation of NdnPingClient instances |
| 32 | */ |
| 33 | private NdnPingClient() { |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Ping a forwarder on an existing face to verify that the forwarder is |
| 38 | * working and responding to requests; this version sends a discovery packet |
| 39 | * to /localhost/nfd which should always respond if the requestor is on the |
| 40 | * same machine as the NDN forwarding daemon. |
| 41 | * |
| 42 | * @param face only a localhost Face |
| 43 | * @return true if successful, false otherwise |
| 44 | */ |
| 45 | public static boolean pingLocal(Face face) { |
| 46 | return ping(face, new Name("/localhost/nfd")); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Request a name on an existing face to verify the forwarder is working and |
| 51 | * responding to requests. Note that the name must be served or cached on the |
| 52 | * forwarder for this to return true. |
| 53 | * |
| 54 | * @param face a {@link Face} to ping |
| 55 | * @param name a known {@link Name} that the remote node will answer to |
| 56 | * @return true if successful, false otherwise |
| 57 | */ |
| 58 | public static boolean ping(Face face, Name name) { |
| 59 | // build interest |
| 60 | Interest interest = new Interest(name); |
| 61 | interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT); |
| 62 | interest.setMustBeFresh(true); |
| 63 | |
| 64 | // send packet |
| 65 | try { |
| 66 | Data data = FetchHelper.getData(face, interest); |
| 67 | return data != null; |
| 68 | } catch (IOException e) { |
| 69 | LOG.log(Level.INFO, "Error sending ping interest", e); |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | } |