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