/** * Cancels all observe relations that this endpoint has established with * resources from this server. */ public void cancelAll() { for (ObserveRelation relation:relations) relation.cancel(); }
@Override public void onTimeout() { ObserveRelation relation = exchange.getRelation(); LOGGER.log( Level.INFO, "Notification for token [{0}] timed out. Canceling all relations with source [{1}]", new Object[]{ relation.getExchange().getRequest().getTokenString(), relation.getSource() }); relation.cancelAll(); }
@Override public void onAcknowledgement() { synchronized (exchange) { ObserveRelation relation = exchange.getRelation(); final Response next = relation.getNextControlNotification(); relation.setCurrentControlNotification(next); // next may be null relation.setNextControlNotification(null); if (next != null) { LOGGER.fine("Notification has been acknowledged, send the next one"); /* * The matcher must be able to find the NON notifications to remove * them from the exchangesByMID hashmap */ if (next.getType() == Type.NON) { relation.addNotification(next); } // Create a new task for sending next response so that we // can leave the sync-block executor.execute(new Runnable() { public void run() { ObserveLayer.super.sendResponse(exchange, next); } }); } } }
/** * Adds the specified observe relation. * * @param relation the observe relation * @return true, if a old relation was replaced by the provided one, * false, if the provided relation was added. */ public boolean add(ObserveRelation relation) { if (relation == null) throw new NullPointerException(); ObserveRelation previous = observeRelations.put(relation.getKey(), relation); if (null != previous) { previous.cancel(); return true; } return false; }
+ request.getSourcePort() + " and resource " + resource.getURI()); final ObservingEndpoint remote = observeManager.findObservingEndpoint(source); final ObserveRelation relation = new ObserveRelation(remote, resource, exchange); remote.addObserveRelation(relation); exchange.setRelation(relation); final ObserveRelation relation = observeManager.getRelation(source, request.getToken()); if (relation != null) { relation.cancel();
if (relation != null && relation.isEstablished()) { LOGGER.fine("Response has error code "+response.getCode()+" and must be sent as CON"); response.setType(Type.CON); relation.cancel(); } else { if (relation.check()) { LOGGER.fine("The observe relation check requires the notification to be sent as CON"); response.setType(Type.CON); Response current = relation.getCurrentControlNotification(); if (current != null && isInTransit(current)) { LOGGER.fine("A former notification is still in transit. Postpone " + response); relation.setNextControlNotification(response); relation.setCurrentControlNotification(response); relation.setNextControlNotification(null); relation.addNotification(response);
/** * Remove all observe relations to CoAP clients and notify them that the * observe relation has been canceled. * * @param code * the error code why the relation was terminated * (e.g., 4.04 after deletion) */ public void clearAndNotifyObserveRelations(ResponseCode code) { /* * draft-ietf-core-observe-08, chapter 3.2 Notification states: * In the event that the resource changes in a way that would cause * a normal GET request at that time to return a non-2.xx response * (for example, when the resource is deleted), the server sends a * notification with a matching response code and removes the client * from the list of observers. * This method is called, when the resource is deleted. */ for (ObserveRelation relation:observeRelations) { relation.cancel(); relation.getExchange().sendResponse(new Response(code)); } }
@Override public void sendResponse(final Exchange exchange, final Response response) { final ObserveRelation relation = exchange.getRelation(); if (relation != null && relation.isEstablished()) { if (!response.getOptions().hasObserve()) { /* response for cancel request */ relation.cancel(); response.setLast(true); } else { response.setLast(false); } } // else no observe was requested or the resource does not allow it lower().sendResponse(exchange, response); }
private void removeNotificationsOf(final ObserveRelation relation) { LOGGER.log(Level.FINE, "Removing all remaining NON-notifications of observe relation with {0}", relation.getSource()); for (Iterator<Response> iterator = relation.getNotificationIterator(); iterator.hasNext(); ) { Response previous = iterator.next(); // notifications are local MID namespace if (previous.hasMID()) { KeyMID idByMID = KeyMID.fromOutboundMessage(previous); exchangeStore.remove(idByMID, relation.getExchange()); } else { previous.cancel(); } iterator.remove(); } }
public ObserveRelation getObserveRelation(byte[] token) { for (ObserveRelation relation:relations) { if (Arrays.equals(relation.getExchange().getRequest().getToken(), token)) { return relation; } } return null; } }
new Object[]{request.getSource(), request.getSourcePort(), resource.getURI()}); ObservingEndpoint remote = observeManager.findObservingEndpoint(source); ObserveRelation relation = new ObserveRelation(remote, resource, exchange); remote.addObserveRelation(relation); exchange.setRelation(relation); relation.cancel();
if (relation != null && relation.isEstablished()) { LOGGER.log(Level.FINE, "Response has error code {0} and must be sent as CON", response.getCode()); response.setType(Type.CON); relation.cancel(); } else { if (relation.check()) { LOGGER.fine("The observe relation check requires the notification to be sent as CON"); response.setType(Type.CON); Response current = relation.getCurrentControlNotification(); if (current != null && isInTransit(current)) { LOGGER.log(Level.FINE, "A former notification is still in transit. Postpone {0}", response); relation.setNextControlNotification(response); relation.setCurrentControlNotification(response); relation.setNextControlNotification(null); relation.addNotification(response);
/** * Remove all observe relations to CoAP clients and notify them that the * observe relation has been canceled. * * @param code * the error code why the relation was terminated * (e.g., 4.04 after deletion) */ public void clearAndNotifyObserveRelations(ResponseCode code) { /* * draft-ietf-core-observe-08, chapter 3.2 Notification states: * In the event that the resource changes in a way that would cause * a normal GET request at that time to return a non-2.xx response * (for example, when the resource is deleted), the server sends a * notification with a matching response code and removes the client * from the list of observers. * This method is called, when the resource is deleted. */ for (ObserveRelation relation:observeRelations) { relation.cancel(); relation.getExchange().sendResponse(new Response(code)); } }
/** * Adds the specified observe relation. * * @param relation the observe relation * @return true, if a old relation was replaced by the provided one, * false, if the provided relation was added. */ public boolean add(ObserveRelation relation) { if (relation == null) throw new NullPointerException(); ObserveRelation previous = observeRelations.put(relation.getKey(), relation); if (null != previous) { previous.cancel(); return true; } return false; }
public ObserveRelation getObserveRelation(byte[] token) { for (ObserveRelation relation:relations) { if (Arrays.equals(relation.getExchange().getRequest().getToken(), token)) { return relation; } } return null; } }
ObserveRelation relation = new ObserveRelation(remote, resource, exchange); remote.addObserveRelation(relation); exchange.setRelation(relation); if (relation!=null) relation.cancel();
/** * Cancel all observe relations to CoAP clients. */ public void clearObserveRelations() { for (ObserveRelation relation:observeRelations) { relation.cancel(); } }
@Override public void onTimeout() { ObserveRelation relation = exchange.getRelation(); LOGGER.info("Notification " + relation.getExchange().getRequest().getTokenString() + " timed out. Cancel all relations with source " + relation.getSource()); relation.cancelAll(); }
@Override public void onAcknowledgement() { synchronized (exchange) { ObserveRelation relation = exchange.getRelation(); final Response next = relation.getNextControlNotification(); relation.setCurrentControlNotification(next); // next may be null relation.setNextControlNotification(null); if (next != null) { LOGGER.fine("Notification has been acknowledged, send the next one"); /* * The matcher must be able to find the NON notifications to remove * them from the exchangesByMID hashmap */ if (next.getType() == Type.NON) { relation.addNotification(next); } // Create a new task for sending next response so that we can leave the sync-block executor.execute(new Runnable() { public void run() { ObserveLayer.super.sendResponse(exchange, next); } }); } } }
@Override public boolean accept(ObserveRelation relation) { String relationURI = relation.getExchange().getRequest().getOptions().getUriPathString(); boolean result = relationURI.equals(notifyURI); if (LOG.isTraceEnabled()) { LOG.trace("observe " + relationURI + " / " + notifyURI + ": " + result); } return result; }