Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 4feb2da | 2015-03-03 16:05:29 -0800 | [diff] [blame^] | 2 | * jndn-utils |
| 3 | * Copyright (c) 2015, 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. |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.utils; |
| 15 | |
| 16 | import java.io.IOException; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 17 | import java.util.concurrent.ExecutionException; |
| 18 | import java.util.concurrent.TimeoutException; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 19 | import net.named_data.jndn.Data; |
| 20 | import net.named_data.jndn.Face; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 21 | import net.named_data.jndn.Interest; |
| 22 | import net.named_data.jndn.Name; |
| 23 | import net.named_data.jndn.OnData; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 24 | import net.named_data.jndn.OnTimeout; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 25 | import java.util.logging.Logger; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 28 | * Provide a client to simplify information retrieval over the NDN network. |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 29 | * |
| 30 | * @author Andrew Brown <andrew.brown@intel.com> |
| 31 | */ |
| 32 | public class Client { |
| 33 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 34 | public static final long DEFAULT_SLEEP_TIME = 20; |
| 35 | public static final long DEFAULT_TIMEOUT = 2000; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 36 | private static final Logger logger = Logger.getLogger(Client.class.getName()); |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 37 | private static Client defaultInstance; |
| 38 | |
| 39 | /** |
| 40 | * Singleton access for simpler client use |
| 41 | * |
| 42 | * @return |
| 43 | */ |
| 44 | public static Client getDefault() { |
| 45 | if (defaultInstance == null) { |
| 46 | defaultInstance = new Client(); |
| 47 | } |
| 48 | return defaultInstance; |
| 49 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 50 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 51 | /** |
andrewsbrown | 8372eaa | 2015-02-23 10:08:17 -0800 | [diff] [blame] | 52 | * Asynchronously request the Data for an Interest. This will send the |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 53 | * Interest and return immediately; use futureData.get() to block until the |
| 54 | * Data returns (see FutureData) or manage the event processing independently. |
| 55 | * |
| 56 | * @param face |
| 57 | * @param interest |
| 58 | * @return |
| 59 | */ |
| 60 | public FutureData getAsync(Face face, Interest interest) { |
| 61 | final FutureData futureData = new FutureData(face, interest.getName()); |
| 62 | |
| 63 | // send interest |
| 64 | try { |
| 65 | face.expressInterest(interest, new OnData() { |
| 66 | @Override |
| 67 | public void onData(Interest interest, Data data) { |
| 68 | futureData.resolve(data); |
| 69 | } |
| 70 | }, new OnTimeout() { |
| 71 | @Override |
| 72 | public void onTimeout(Interest interest) { |
| 73 | futureData.reject(new TimeoutException()); |
| 74 | } |
| 75 | }); |
| 76 | } catch (IOException e) { |
| 77 | logger.warning("IO failure while sending interest: " + e); |
| 78 | futureData.reject(e); |
| 79 | } |
| 80 | |
| 81 | return futureData; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Synchronously retrieve the Data for a Name using a default interest (e.g. 2 |
| 86 | * second timeout); this will block until complete (i.e. either data is |
| 87 | * received or the interest times out). |
| 88 | * |
| 89 | * @param face |
| 90 | * @param name |
| 91 | * @return |
| 92 | */ |
| 93 | public FutureData getAsync(Face face, Name name) { |
| 94 | return getAsync(face, getDefaultInterest(name)); |
| 95 | } |
| 96 | |
| 97 | /** |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 98 | * Synchronously retrieve the Data for an Interest; this will block until |
| 99 | * complete (i.e. either data is received or the interest times out). |
| 100 | * |
| 101 | * @param face |
| 102 | * @param interest |
| 103 | * @return Data packet or null |
| 104 | */ |
| 105 | public Data getSync(Face face, Interest interest) { |
| 106 | // setup event |
| 107 | long startTime = System.currentTimeMillis(); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 108 | |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 109 | // get future data |
| 110 | FutureData futureData = getAsync(face, interest); |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 111 | |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 112 | // process eventCount until a response is received or timeout |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 113 | try { |
| 114 | Data data = futureData.get(); |
| 115 | logger.fine("Request time (ms): " + (System.currentTimeMillis() - startTime)); |
| 116 | return data; |
| 117 | } catch (ExecutionException | InterruptedException e) { |
| 118 | logger.warning("Failed to retrieve data: " + e); |
| 119 | return null; |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 120 | } |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 121 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 122 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 123 | /** |
| 124 | * Synchronously retrieve the Data for a Name using a default interest (e.g. 2 |
| 125 | * second timeout); this will block until complete (i.e. either data is |
| 126 | * received or the interest times out). |
| 127 | * |
| 128 | * @param face |
| 129 | * @param name |
| 130 | * @return |
| 131 | */ |
| 132 | public Data getSync(Face face, Name name) { |
| 133 | return getSync(face, getDefaultInterest(name)); |
| 134 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 135 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 136 | /** |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 137 | * Create a default interest for a given Name using some common settings: - |
| 138 | * lifetime: 2 seconds |
| 139 | * |
| 140 | * @param name |
| 141 | * @return |
| 142 | */ |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 143 | public static Interest getDefaultInterest(Name name) { |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 144 | Interest interest = new Interest(name, DEFAULT_TIMEOUT); |
| 145 | return interest; |
| 146 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 147 | } |