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