@Override public long expressInterest(Interest interest, OnData onData, OnTimeout onTimeout, OnNetworkNack onNetworkNack, WireFormat wireFormat) throws IOException { sentInterests.add(interest); return super.expressInterest(interest, onData, onTimeout, onNetworkNack, wireFormat); }
private Data getData(final Interest interest) throws IOException { this.state = new State(); this.face.expressInterest(interest, this, this); while (!state.isDone) { try { face.processEvents(); } catch (EncodingException e) { LOG.log(Level.INFO, "Decoding error: " + e.getMessage(), e); } try { Thread.sleep(SLEEP_TIMEOUT); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } if (state.response == null) { throw new IOException("Communication with NFD failed"); } return state.response; }
@Override public void putData(Data data, WireFormat wireFormat) throws IOException { sentDatas.add(data); super.putData(data, wireFormat); }
@Override public Cancellation observeNewAnnouncements(On<Long> onAdded, On<Long> onRemoved, On<Exception> onError) throws IOException { LOGGER.log(Level.INFO, "Observing new announcements: {0}", broadcastPrefix); CompletableFuture<Void> future = new CompletableFuture<>(); OnRegistration onRegistration = new OnRegistration(future); OnAnnouncement onAnnouncement = new OnAnnouncement(onAdded, onRemoved, onError); try { long registeredPrefix = face.registerPrefix(broadcastPrefix, onAnnouncement, (OnRegisterFailed) onRegistration, onRegistration); return () -> face.removeRegisteredPrefix(registeredPrefix); } catch (SecurityException e) { throw new IOException("Failed while using transport security key chain", e); } }
/** * {@inheritDoc} */ @Override public Data getSync(Face face, Interest interest) throws IOException { CompletableFuture<Data> future = getAsync(face, interest); try { // process events until complete while (!future.isDone()) { synchronized (face) { face.processEvents(); } if (sleepTime > 0) { try { Thread.sleep(sleepTime); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } return future.get(); } catch (InterruptedException | ExecutionException | EncodingException e) { logger.log(Level.FINE, "Failed to retrieve data.", e); throw new IOException("Failed to retrieve data.", e); } }
/** * Open the publisher, registering prefixes and announcing group entry. If the publisher is already open, this method * immediately returns * * @throws IOException if prefix registration or group announcement fails */ synchronized void open() throws IOException { if (opened) { return; } opened = true; CompletableFuture<Void> future = new CompletableFuture<>(); OnRegistration onRegistration = new OnRegistration(future); try { registrationId = face.registerPrefix(prefix, this, (OnRegisterFailed) onRegistration, onRegistration); // assumes face.processEvents is driven concurrently elsewhere future.get(10, TimeUnit.SECONDS); announcementService.announceEntrance(publisherId); } catch (IOException | SecurityException | InterruptedException | ExecutionException | TimeoutException e) { throw new IOException("Failed to register NDN prefix; pub-sub IO will be impossible", e); } }
/** * TODO this should not clear content store or remove registered prefix; do that in the future to allow subscribers * to retrieve still-alive messages * * @throws IOException if the group exit announcement fails */ @Override public synchronized void close() throws IOException { if (opened) { face.removeRegisteredPrefix(registrationId); contentStore.clear(); announcementService.announceExit(publisherId); } }
face.makeCommandInterest(interest); } catch (SecurityException e) { throw new IllegalArgumentException("Failed to make command interest; ensure command signing info is set on the " +
/** * {@inheritDoc} */ @Override public void run() { if (!isRegistered()) { try { register(); } catch (IOException ex) { throw new RuntimeException("Failed to register prefix, aborting.", ex); } } // continuously serve packets while (true) { try { synchronized (face) { face.processEvents(); } } catch (IOException ex) { logger.log(Level.SEVERE, "Failed to process events.", ex); } catch (EncodingException ex) { logger.log(Level.SEVERE, "Failed to parse bytes.", ex); } } } }
/** * Register a prefix for responding to interests. * * @throws java.io.IOException if IO fails */ public void register() throws IOException { try { registeredPrefixId = face.registerPrefix(prefix, this, new OnRegisterFailed() { @Override public void onRegisterFailed(Name prefix) { registeredPrefixId = UNREGISTERED; logger.log(Level.SEVERE, "Failed to register prefix: " + prefix.toUri()); } }, new ForwardingFlags()); logger.log(Level.FINER, "Registered a new prefix: " + prefix.toUri()); } catch (net.named_data.jndn.security.SecurityException e) { throw new IOException("Failed to communicate to face due to security error", e); } }
@Override public long expressInterest(Name name, Interest interestTemplate, OnData onData, OnTimeout onTimeout, OnNetworkNack onNetworkNack, WireFormat wireFormat) throws IOException { sentInterests.add(getInterestCopy(name, interestTemplate)); return super.expressInterest(name, interestTemplate, onData, onTimeout, onNetworkNack, wireFormat); }
@Override public void push(Face face, Interest interest) throws IOException { Optional<NameTree<Blob>> leaf = getWithSelectors(interest); if (leaf.isPresent() && leaf.get().content().isPresent()) { Data t = new Data(template); t.setName(leaf.get().fullName()); ByteArrayInputStream b = new ByteArrayInputStream(leaf.get().content().get().getImmutableArray()); for (Data d : SegmentationHelper.segment(t, b)) { face.putData(d); } } }
/** * 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++; }
/** * {@inheritDoc} */ @Override public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter){ try { Data data = callback.onInterest(prefix, interest); data = processPipeline(data); face.putData(data); } catch (Exception e) { logger.log(Level.FINE, "Failed to send data for: " + interest.toUri(), e); } } }
@Override public void onTimeout(final Interest interest) { state.nRetries--; if (state.nRetries > 0) { try { face.expressInterest(new Interest(interest), this, this); } catch (IOException e) { LOG.log(Level.INFO, "Error while expressing interest: " + e.toString(), e); } } else { state.isDone = true; } }
@Override public void push(Face face, Name name) throws IOException { Optional<Blob> blob = get(name); if (blob.isPresent()) { Data t = new Data(template); t.setName(name); ByteArrayInputStream b = new ByteArrayInputStream(blob.get().getImmutableArray()); for (Data d : SegmentationHelper.segment(t, b)) { face.putData(d); } } }
@Override public void announceExit(long id) throws IOException { LOGGER.log(Level.INFO, "Announcing publisher exit: {0} from {1}", new Object[]{id, broadcastPrefix}); Name name = PubSubNamespace.toAnnouncement(broadcastPrefix, id, PubSubNamespace.Announcement.EXIT); Interest interest = new Interest(name); face.expressInterest(interest, null); }
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); } }
@Override public void announceEntrance(long id) throws IOException { LOGGER.log(Level.INFO, "Announcing publisher entrance: {0} to {1}", new Object[]{id, broadcastPrefix}); Name name = PubSubNamespace.toAnnouncement(broadcastPrefix, id, PubSubNamespace.Announcement.ENTRANCE); Interest interest = new Interest(name); face.expressInterest(interest, null); }
/** * {@inheritDoc} */ @Override public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) { logger.finer("Serving packet for: " + interest.toUri()); if (interest.getChildSelector() == -1) { try { interest.getName().get(-1).toSegment(); } catch (EncodingException e) { interest.setChildSelector(Interest.CHILD_SELECTOR_LEFT); } } try { Data data = repository.get(interest); data = processPipeline(data); face.putData(data); } catch (Exception e) { logger.log(Level.FINE, "Failed to find data satisfying: " + interest.toUri(), e); } }