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