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 | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 16 | import com.intel.jndn.utils.client.SegmentedFutureData; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 17 | import java.io.IOException; |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
| 20 | import java.util.concurrent.ExecutionException; |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 21 | import java.util.concurrent.Future; |
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; |
| 28 | import net.named_data.jndn.encoding.EncodingException; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Provide a client to simplify retrieving segmented Data packets over the NDN |
| 32 | * network. This class expects the Data producer to follow the NDN naming |
| 33 | * conventions (see http://named-data.net/doc/tech-memos/naming-conventions.pdf) |
| 34 | * and produce Data packets with a valid segment as the last component of their |
| 35 | * name; additionally, at least the first packet should set the FinalBlockId of |
| 36 | * the packet's MetaInfo (see |
| 37 | * http://named-data.net/doc/ndn-tlv/data.html#finalblockid). |
| 38 | * |
| 39 | * @author Andrew Brown <andrew.brown@intel.com> |
| 40 | */ |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 41 | public class SegmentedClient implements Client { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 42 | |
| 43 | private static SegmentedClient defaultInstance; |
| 44 | private static final Logger logger = Logger.getLogger(SegmentedClient.class.getName()); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 47 | * Singleton access for simpler client use. |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 48 | * |
| 49 | * @return |
| 50 | */ |
| 51 | public static SegmentedClient getDefault() { |
| 52 | if (defaultInstance == null) { |
| 53 | defaultInstance = new SegmentedClient(); |
| 54 | } |
| 55 | return defaultInstance; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Asynchronously send Interest packets for a segmented result; will block |
| 60 | * until the first packet is received and then send remaining interests until |
| 61 | * the specified FinalBlockId. |
| 62 | * |
| 63 | * @param face |
| 64 | * @param interest should include either a ChildSelector or an initial segment |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 65 | * number; the initial segment number will be cut off in the de-segmented |
| 66 | * packet. |
andrewsbrown | 0cf35f9 | 2015-03-09 12:00:00 -0700 | [diff] [blame] | 67 | * @return a list of FutureData packets; if the first segment fails, the list |
| 68 | * will contain one FutureData with the failure exception |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 69 | */ |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 70 | @Override |
| 71 | public Future<Data> getAsync(Face face, Interest interest) { |
| 72 | List<Future<Data>> segments = getAsyncList(face, interest); |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 73 | Name name = hasSegment(interest.getName()) ? interest.getName().getPrefix(-1) : interest.getName(); |
| 74 | return new SegmentedFutureData(name, segments); |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 75 | } |
| 76 | |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 77 | /** |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 78 | * Asynchronously send Interest packets for a segmented result; will block |
| 79 | * until the first packet is received and then send remaining interests until |
| 80 | * the specified FinalBlockId. |
| 81 | * |
| 82 | * @param face |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 83 | * @param name the {@link Name} of the packet to retrieve using a default |
| 84 | * interest |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 85 | * @return an aggregated data packet from all received segments |
| 86 | */ |
| 87 | public Future<Data> getAsync(Face face, Name name) { |
| 88 | return getAsync(face, SimpleClient.getDefaultInterest(name)); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Asynchronously send Interest packets for a segmented result; will block |
| 93 | * until the first packet is received and then send remaining interests until |
| 94 | * the specified FinalBlockId. |
| 95 | * |
| 96 | * @param face |
| 97 | * @param interest should include either a ChildSelector or an initial segment |
| 98 | * number |
| 99 | * @return a list of FutureData packets; if the first segment fails, the list |
| 100 | * will contain one FutureData with the failure exception |
| 101 | */ |
| 102 | public List<Future<Data>> getAsyncList(Face face, Interest interest) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 103 | // get first segment; default 0 or use a specified start segment |
| 104 | long firstSegment = 0; |
| 105 | boolean specifiedSegment = false; |
| 106 | try { |
| 107 | firstSegment = interest.getName().get(-1).toSegment(); |
| 108 | specifiedSegment = true; |
| 109 | } catch (EncodingException e) { |
| 110 | // check for interest selector if no initial segment found |
| 111 | if (interest.getChildSelector() == -1) { |
andrewsbrown | 0cf35f9 | 2015-03-09 12:00:00 -0700 | [diff] [blame] | 112 | logger.log(Level.WARNING, "No child selector set for a segmented Interest; this may result in incorrect retrieval."); |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 113 | // allow this interest to pass without a segment marker since it may still succeed |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | // setup segments |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 118 | final List<Future<Data>> segments = new ArrayList<>(); |
| 119 | segments.add(SimpleClient.getDefault().getAsync(face, interest)); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 120 | |
| 121 | // retrieve first packet to find last segment value |
| 122 | long lastSegment; |
| 123 | try { |
| 124 | lastSegment = segments.get(0).get().getMetaInfo().getFinalBlockId().toSegment(); |
| 125 | } catch (ExecutionException | InterruptedException | EncodingException e) { |
andrewsbrown | 0cf35f9 | 2015-03-09 12:00:00 -0700 | [diff] [blame] | 126 | logger.log(Level.SEVERE, "Failed to retrieve first segment: ", e); |
| 127 | return segments; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // cut interest segment off |
| 131 | if (specifiedSegment) { |
| 132 | interest.setName(interest.getName().getPrefix(-1)); |
| 133 | } |
| 134 | |
| 135 | // send interests in remaining segments |
| 136 | for (long i = firstSegment + 1; i <= lastSegment; i++) { |
| 137 | Interest segmentedInterest = new Interest(interest); |
| 138 | segmentedInterest.getName().appendSegment(i); |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 139 | Future<Data> futureData = SimpleClient.getDefault().getAsync(face, segmentedInterest); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 140 | segments.add((int) i, futureData); |
| 141 | } |
| 142 | |
| 143 | return segments; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Asynchronously send Interests for a segmented Data packet using a default |
| 148 | * interest (e.g. 2 second timeout); this will block until complete (i.e. |
andrewsbrown | 0cf35f9 | 2015-03-09 12:00:00 -0700 | [diff] [blame] | 149 | * either data is received or the interest times out). See getAsync(Face face) |
| 150 | * for more information. |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 151 | * |
| 152 | * @param face |
| 153 | * @param name |
| 154 | * @return |
| 155 | */ |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 156 | public List<Future<Data>> getAsyncList(Face face, Name name) { |
| 157 | return getAsyncList(face, SimpleClient.getDefaultInterest(name)); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Retrieve a segmented Data packet; will block until all segments are |
| 162 | * received and will re-assemble these. |
| 163 | * |
| 164 | * @param face |
| 165 | * @param interest should include either a ChildSelector or an initial segment |
| 166 | * number |
andrewsbrown | 0cf35f9 | 2015-03-09 12:00:00 -0700 | [diff] [blame] | 167 | * @return a Data packet; the name will inherit from the sent Interest, not |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 168 | * the returned packets and the content will be a concatenation of all of the |
| 169 | * packet contents. |
| 170 | * @throws java.io.IOException |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 171 | */ |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 172 | @Override |
andrewsbrown | b005ee6 | 2015-03-31 14:45:54 -0700 | [diff] [blame] | 173 | public Data getSync(Face face, Interest interest) throws IOException { |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 174 | try { |
| 175 | return getAsync(face, interest).get(); |
| 176 | } catch (ExecutionException | InterruptedException e) { |
| 177 | logger.log(Level.WARNING, "Failed to retrieve data.", e); |
| 178 | throw new IOException("Failed to retrieve data.", e); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 179 | } |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Synchronously retrieve the Data for a Name using a default interest (e.g. 2 |
| 184 | * second timeout); this will block until complete (i.e. either data is |
andrewsbrown | 0cf35f9 | 2015-03-09 12:00:00 -0700 | [diff] [blame] | 185 | * received or the interest times out). See getSync(Face face) for more |
| 186 | * information. |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 187 | * |
| 188 | * @param face |
| 189 | * @param name |
| 190 | * @return |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 191 | * @throws java.io.IOException |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 192 | */ |
andrewsbrown | 69d5329 | 2015-03-17 19:37:34 +0100 | [diff] [blame] | 193 | public Data getSync(Face face, Name name) throws IOException { |
| 194 | return getSync(face, SimpleClient.getDefaultInterest(name)); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Check if a name ends in a segment component; uses marker value found in the |
| 199 | * NDN naming conventions (see |
| 200 | * http://named-data.net/doc/tech-memos/naming-conventions.pdf). |
| 201 | * |
| 202 | * @param name |
| 203 | * @return |
| 204 | */ |
| 205 | public static boolean hasSegment(Name name) { |
| 206 | return name.get(-1).getValue().buf().get(0) == 0x00; |
| 207 | } |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 208 | } |