blob: 4db2179477639fb1221fd3cb7e6a8948a7a39699 [file] [log] [blame]
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -08001/*
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 */
14package com.intel.jndn.management;
15
16import com.intel.jndn.management.helpers.FetchHelper;
17import net.named_data.jndn.Data;
18import net.named_data.jndn.Face;
19import net.named_data.jndn.Interest;
20import net.named_data.jndn.Name;
21
22import java.io.IOException;
23import java.util.logging.Level;
24import java.util.logging.Logger;
25
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080026/**
27 * Simplistic checker for live connection to NFD forwarder.
28 */
29public final class NdnPingClient {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080030 public static final long DEFAULT_TIMEOUT = 2000;
31 private static final Logger LOG = Logger.getLogger(NdnPingClient.class.getName());
32
33 /**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080034 * Prevent creation of NdnPingClient instances.
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080035 */
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 Afanasyeve36e1af2016-02-19 18:06:05 -080048 public static boolean pingLocal(final Face face) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080049 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 Afanasyeve36e1af2016-02-19 18:06:05 -080061 public static boolean ping(final Face face, final Name name) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080062 // build interest
63 Interest interest = new Interest(name);
Davide Pesavento4821a342020-11-06 00:15:32 -050064 interest.setCanBePrefix(true);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080065 interest.setMustBeFresh(true);
Davide Pesavento4821a342020-11-06 00:15:32 -050066 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080067
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}