/** * @param content the content to check (e.g. /a/b/c) * @param interest the interest to check from (e.g. /a/b) * @return the next component from a Data packet after specified Interest * components (e.g. c); if the Data is not longer than the Interest, return an * empty component. */ private static Name.Component getNextComponentAfterLastInterestComponent(Data content, Interest interest) { if (content.getName().size() > interest.getName().size()) { return content.getName().get(interest.getName().size()); } else { return new Name.Component(); } }
/** * Create a default interest for a given {@link Name} using the client's * passed settings (see {@link #SimpleClient(long, long)}) * * @param name the {@link Name} of the data to retrieve * @return a default interest for the given name */ public Interest getDefaultInterest(Name name) { return new Interest(name, interestLifetime); } }
static Name toMessageName(Name publisherPrefix, long messageId) { Name.Component messageComponent = Name.Component.fromNumberWithMarker(messageId, PubSubNamespace.MESSAGE_ID_MARKER); return new Name(publisherPrefix).append(messageComponent); }
private ControlParameters decodeParameters(Interest interest) { ControlParameters params = new ControlParameters(); try { params.wireDecode(interest.getName().get(CONTROL_PARAMETERS_NAME_OFFSET).getValue()); params.setFaceId(1); params.setOrigin(0); params.setCost(0); } catch (EncodingException e) { throw new IllegalArgumentException("", e); } return params; }
private static void sendAttributes(Face face, Name publisherName) { Data data = new Data(publisherName); data.setContent(new Blob("[attributes here]")); data.getMetaInfo().setFreshnessPeriod(ATTRIBUTES_FRESHNESS_PERIOD); try { face.putData(data); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Failed to publish attributes for publisher: " + publisherName, e); } }
private boolean hasFinalBlockId(Data data) { return data.getMetaInfo().getFinalBlockId().getValue().size() > 0; }
private void resend(Face face, Interest interest, OnData onData, OnTimeout onTimeout) throws IOException { LOGGER.log(Level.INFO, "Resending interest with {0}ms lifetime: {1}", new Object[]{interest.getInterestLifetimeMilliseconds(), interest.getName()}); face.expressInterest(interest, onData, timedOutInterest -> { try { retryInterest(face, timedOutInterest, onData, onTimeout); } catch (IOException e) { onTimeout.onTimeout(interest); } }); } }
@Override public Data onInterest(Name prefix, Interest interest) throws Exception { Data data = new Data(interest.getName()); Blob content = callback.onInterest(prefix, interest); data.setContent(content); return data; } };
private static boolean isAttributesRequest(Name name, Interest interest) { return name.equals(interest.getName()) && interest.getChildSelector() == -1; }
private ControlResponse encodeResponse(ControlParameters params) { ControlResponse response = new ControlResponse(); response.setStatusCode(STATUS_CODE_OK); response.setStatusText("OK"); response.setBodyAsControlParameters(params); return response; }
/** * Synchronized helper method to prevent multiple threads from mashing * totalRetries * * @param context the current request context * @throws IOException when the client cannot perform the necessary network IO */ private synchronized void retryInterest(RetryContext context) throws IOException { LOGGER.info("Retrying interest: " + context.interest.toUri()); context.face.expressInterest(context.interest, context, context); totalRetries++; }
public InMemoryContentStore(int freshnessMs) { this.template = new Data(); this.template.getMetaInfo().setFreshnessPeriod(freshnessMs); this.store = DefaultNameTree.newRootTree(); }
@Override public Name getPrefix() { return new Name(prefix); }
private void retryInterest(Face face, Interest interest, OnData onData, OnTimeout onTimeout) throws IOException { double newLifetime = interest.getInterestLifetimeMilliseconds() * backoffFactor; if (newLifetime < cutoffLifetime) { interest.setInterestLifetimeMilliseconds(newLifetime); resend(face, interest, onData, onTimeout); } else { onTimeout.onTimeout(interest); } }
private static boolean hasSelectors(Interest interest) { return interest.getChildSelector() != -1 || interest.getExclude().size() > 0; }
/** * @param interest the {@link Interest} to check * @return true if the {@link Interest} has no child selector */ private static boolean hasNoChildSelector(Interest interest) { return interest.getChildSelector() < 0; }
@Override public void onData(Interest interest, Data data) { applicationOnData.onData(interest, data); }
@Override public String toString() { String c = (component == null) ? null : component.toEscapedString(); return "DefaultNameTree{" + c + ": " + content + '}'; } }
@Override public Name getPrefix() { return new Name(prefix); }
/** * @return the NDN name of the topic; the return is wrapped so that the original value is immutable and the return * type can be modified */ public Name name() { return new Name(name); }