Andrew Brown | db45705 | 2015-02-21 15:41:58 -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 | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.utils; |
| 15 | |
andrewsbrown | 629816c | 2015-04-07 09:04:21 -0700 | [diff] [blame] | 16 | import com.intel.jndn.utils.client.FutureData; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 17 | import com.intel.jndn.utils.client.SegmentedFutureData; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 18 | import java.io.IOException; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 19 | import java.util.concurrent.ExecutionException; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 20 | import java.util.concurrent.Future; |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 21 | import java.util.concurrent.TimeoutException; |
andrewsbrown | 0cf35f9 | 2015-03-09 12:00:00 -0700 | [diff] [blame] | 22 | import java.util.logging.Level; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 23 | import java.util.logging.Logger; |
| 24 | import net.named_data.jndn.Data; |
| 25 | import net.named_data.jndn.Face; |
| 26 | import net.named_data.jndn.Interest; |
| 27 | import net.named_data.jndn.Name; |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 28 | import net.named_data.jndn.OnData; |
| 29 | import net.named_data.jndn.OnTimeout; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 30 | import net.named_data.jndn.encoding.EncodingException; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Provide a client to simplify retrieving segmented Data packets over the NDN |
| 34 | * network. This class expects the Data producer to follow the NDN naming |
| 35 | * conventions (see http://named-data.net/doc/tech-memos/naming-conventions.pdf) |
| 36 | * and produce Data packets with a valid segment as the last component of their |
| 37 | * name; additionally, at least the first packet should set the FinalBlockId of |
| 38 | * the packet's MetaInfo (see |
| 39 | * http://named-data.net/doc/ndn-tlv/data.html#finalblockid). |
| 40 | * |
| 41 | * @author Andrew Brown <andrew.brown@intel.com> |
| 42 | */ |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 43 | public class SegmentedClient implements Client { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 44 | |
| 45 | private static SegmentedClient defaultInstance; |
| 46 | private static final Logger logger = Logger.getLogger(SegmentedClient.class.getName()); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 49 | * Singleton access for simpler client use. |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 50 | * |
| 51 | * @return |
| 52 | */ |
| 53 | public static SegmentedClient getDefault() { |
| 54 | if (defaultInstance == null) { |
| 55 | defaultInstance = new SegmentedClient(); |
| 56 | } |
| 57 | return defaultInstance; |
| 58 | } |
| 59 | |
| 60 | /** |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 61 | * Asynchronously send Interest packets for a segmented result; will not |
| 62 | * block, but will wait for the first packet to return before sending |
| 63 | * remaining interests until using the specified FinalBlockId. Will retrieve |
| 64 | * non-segmented packets as well. |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 65 | * |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 66 | * @param face the {@link Face} on which to retrieve the packets |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 67 | * @param interest should include either a ChildSelector or an initial segment |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 68 | * number; the initial segment number will be cut off in the de-segmented |
| 69 | * packet. |
andrewsbrown | 0cf35f9 | 2015-03-09 12:00:00 -0700 | [diff] [blame] | 70 | * @return a list of FutureData packets; if the first segment fails, the list |
| 71 | * will contain one FutureData with the failure exception |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 72 | */ |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 73 | @Override |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 74 | public Future<Data> getAsync(final Face face, Interest interest) { |
| 75 | final long firstSegmentId = parseFirstSegmentId(interest); |
| 76 | final SegmentedFutureData segmentedData = new SegmentedFutureData(face, interest.getName()); |
| 77 | final FutureData firstData = new FutureData(face, interest.getName()); |
| 78 | segmentedData.add(0, firstData); |
| 79 | |
| 80 | // send first interest |
| 81 | logger.log(Level.FINER, "Sending first interest for: " + interest.getName().toUri()); |
| 82 | try { |
| 83 | face.expressInterest(interest, new OnData() { |
| 84 | @Override |
| 85 | public void onData(Interest interest, Data data) { |
andrewsbrown | 73065d0 | 2015-05-11 13:09:56 -0700 | [diff] [blame] | 86 | // request subsequent segments using FinalBlockId and the Interest template |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 87 | try { |
| 88 | long lastSegmentId = parseLastSegmentId(data); |
| 89 | Interest template = new Interest(interest); |
| 90 | template.setName(removeSegment(data.getName())); |
| 91 | requestRemainingSegments(face, segmentedData, template, firstSegmentId + 1, lastSegmentId); |
| 92 | } catch (EncodingException ex) { |
andrewsbrown | 73065d0 | 2015-05-11 13:09:56 -0700 | [diff] [blame] | 93 | logger.log(Level.FINER, "No segment ID found in FinalBlockId, assuming first packet is only packet."); |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 94 | } |
andrewsbrown | 73065d0 | 2015-05-11 13:09:56 -0700 | [diff] [blame] | 95 | |
| 96 | // resolve the first data |
| 97 | firstData.resolve(data); |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 98 | } |
| 99 | }, new OnTimeout() { |
| 100 | @Override |
| 101 | public void onTimeout(Interest interest) { |
| 102 | segmentedData.reject(new TimeoutException()); |
| 103 | } |
| 104 | }); |
| 105 | } catch (IOException e) { |
| 106 | logger.log(Level.FINE, "IO failure while sending interest: ", e); |
| 107 | segmentedData.reject(e); |
| 108 | } |
| 109 | |
| 110 | return segmentedData; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 111 | } |
| 112 | |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 113 | /** |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 114 | * @param interest the request {@link Interest} |
| 115 | * @return the first segment the interest is requesting, or 0 if none found |
| 116 | */ |
| 117 | private long parseFirstSegmentId(Interest interest) { |
| 118 | try { |
| 119 | return interest.getName().get(-1).toSegment(); |
| 120 | } catch (EncodingException e) { |
| 121 | if (interest.getChildSelector() == -1) { |
| 122 | logger.log(Level.WARNING, "No child selector set for a segmented Interest; this may result in incorrect retrieval."); |
| 123 | // allow this interest to pass without a segment marker since it may still succeed |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param firstData the first returned {@link Data} |
| 131 | * @return the last segment number available as specified in the FinalBlockId |
| 132 | * @throws EncodingException |
| 133 | */ |
| 134 | private long parseLastSegmentId(Data firstData) throws EncodingException { |
| 135 | return firstData.getMetaInfo().getFinalBlockId().toSegment(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Send interests for remaining segments; adding them to the segmented future |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 140 | * |
| 141 | * @param face |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 142 | * @param segmentedData |
| 143 | * @param template |
| 144 | * @param fromSegment |
| 145 | * @param toSegment |
| 146 | */ |
| 147 | private void requestRemainingSegments(Face face, SegmentedFutureData segmentedData, Interest template, long fromSegment, long toSegment) { |
| 148 | // send interests in remaining segments |
| 149 | for (long i = fromSegment; i <= toSegment; i++) { |
| 150 | Interest segmentedInterest = new Interest(template); |
| 151 | segmentedInterest.getName().appendSegment(i); |
| 152 | Future<Data> futureData = SimpleClient.getDefault().getAsync(face, segmentedInterest); |
| 153 | segmentedData.add((int) i, futureData); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Asynchronously send Interest packets for a segmented result; see {@link #getAsync(net.named_data.jndn.Face, net.named_data.jndn.Interest) |
| 159 | * } for more information. |
| 160 | * |
| 161 | * @param face the {@link Face} on which to retrieve the packets |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 162 | * @param name the {@link Name} of the packet to retrieve using a default |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 163 | * interest; may optionally end with the segment component of the first |
| 164 | * segment to retrieve |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 165 | * @return an aggregated data packet from all received segments |
| 166 | */ |
| 167 | public Future<Data> getAsync(Face face, Name name) { |
| 168 | return getAsync(face, SimpleClient.getDefaultInterest(name)); |
| 169 | } |
| 170 | |
| 171 | /** |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 172 | * Retrieve a segmented Data packet; see {@link #getAsync(net.named_data.jndn.Face, net.named_data.jndn.Interest) |
| 173 | * } for more information. This method will block and call |
| 174 | * {@link Face#processEvents()} until the sent interests timeout or the |
| 175 | * corresponding data packets are retrieved. |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 176 | * |
| 177 | * @param face |
| 178 | * @param interest should include either a ChildSelector or an initial segment |
| 179 | * number |
andrewsbrown | 0cf35f9 | 2015-03-09 12:00:00 -0700 | [diff] [blame] | 180 | * @return a Data packet; the name will inherit from the sent Interest, not |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 181 | * the returned packets and the content will be a concatenation of all of the |
| 182 | * packet contents. |
| 183 | * @throws java.io.IOException |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 184 | */ |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 185 | @Override |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 186 | public Data getSync(Face face, Interest interest) throws IOException { |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 187 | try { |
| 188 | return getAsync(face, interest).get(); |
| 189 | } catch (ExecutionException | InterruptedException e) { |
andrewsbrown | 2d7ee8d | 2015-04-15 12:40:58 -0700 | [diff] [blame] | 190 | String message = "Failed to retrieve data: " + interest.toUri(); |
| 191 | logger.log(Level.FINE, message, e); |
| 192 | throw new IOException(message, e); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 193 | } |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Synchronously retrieve the Data for a Name using a default interest (e.g. 2 |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 198 | * second timeout). This method will block and call |
| 199 | * {@link Face#processEvents()} until the sent interests timeout or the |
| 200 | * corresponding data packets are retrieved. |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 201 | * |
| 202 | * @param face |
| 203 | * @param name |
| 204 | * @return |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 205 | * @throws java.io.IOException |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 206 | */ |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 207 | public Data getSync(Face face, Name name) throws IOException { |
| 208 | return getSync(face, SimpleClient.getDefaultInterest(name)); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Check if a name ends in a segment component; uses marker value found in the |
| 213 | * NDN naming conventions (see |
| 214 | * http://named-data.net/doc/tech-memos/naming-conventions.pdf). |
| 215 | * |
| 216 | * @param name |
| 217 | * @return |
| 218 | */ |
| 219 | public static boolean hasSegment(Name name) { |
| 220 | return name.get(-1).getValue().buf().get(0) == 0x00; |
| 221 | } |
andrewsbrown | d403db8 | 2015-05-11 13:02:28 -0700 | [diff] [blame] | 222 | |
| 223 | /** |
| 224 | * @param name the {@link Name} to check |
| 225 | * @return a new instance of {@link Name} with no segment component appended |
| 226 | */ |
| 227 | public static Name removeSegment(Name name) { |
| 228 | return hasSegment(name) ? name.getPrefix(-1) : new Name(name); |
| 229 | } |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 230 | } |