Java Code Examples for org.eclipse.californium.core.coap.Response#setMID()

The following examples show how to use org.eclipse.californium.core.coap.Response#setMID() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ObserveLayer.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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");
			// this is not a self replacement, hence a new MID
			next.setMID(Message.NONE);
			// 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);
				}
			});
		}
	}
}
 
Example 2
Source File: ObserveLayer.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@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");
			// this is not a self replacement, hence a new MID
			next.setMID(Message.NONE);
			// 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);
				}
			});
		}
	}
}
 
Example 3
Source File: ObserveLayer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onRetransmission() {
	synchronized (exchange) {
		ObserveRelation relation = exchange.getRelation();
		final Response next = relation.getNextControlNotification();
		if (next != null) {
			LOGGER.fine("The notification has timed out and there is a fresher notification for the retransmission");
			// Cancel the original retransmission and send the fresh notification here
			response.cancel();
			// use the same MID
			next.setMID(response.getMID());
			// Convert all notification retransmissions to CON
			if (next.getType() != Type.CON) {
				next.setType(Type.CON);
				prepareSelfReplacement(exchange, next);
			}
			relation.setCurrentControlNotification(next);
			relation.setNextControlNotification(null);
			// 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);
				}
			});
		}
	}
}
 
Example 4
Source File: ObserveLayer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void onRetransmission() {
	synchronized (exchange) {
		ObserveRelation relation = exchange.getRelation();
		final Response next = relation.getNextControlNotification();
		if (next != null) {
			LOGGER.fine("The notification has timed out and there is a fresher notification for the retransmission");
			// Cancel the original retransmission and send the fresh notification here
			response.cancel();
			// use the same MID
			next.setMID(response.getMID());
			// Convert all notification retransmissions to CON
			if (next.getType() != Type.CON) {
				next.setType(Type.CON);
				prepareSelfReplacement(exchange, next);
			}
			relation.setCurrentControlNotification(next);
			relation.setNextControlNotification(null);
			// 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);
				}
			});
		}
	}
}
 
Example 5
Source File: ReliabilityLayer.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Makes sure that the response type is correct. The response type for a NON
 * can be NON or CON. The response type for a CON should either be an ACK
 * with a piggy-backed response or, if an empty ACK has already be sent, a
 * CON or NON with a separate response.
 */
@Override
public void sendResponse(final Exchange exchange, final Response response) {

	LOGGER.finer("Send response, failed transmissions: "+exchange.getFailedTransmissionCount());

	// If a response type is set, we do not mess around with it.
	// Only if none is set, we have to decide for one here.
	
	Type respType = response.getType();
	if (respType == null) {
		Type reqType = exchange.getCurrentRequest().getType();
		if (reqType == Type.CON) {
			if (exchange.getCurrentRequest().isAcknowledged()) {
				// send separate response
				response.setType(Type.CON);
			} else {
				exchange.getCurrentRequest().setAcknowledged(true);
				// send piggy-backed response
				response.setType(Type.ACK);
				response.setMID(exchange.getCurrentRequest().getMID());
			}
		} else {
			// send NON response
			response.setType(Type.NON);
		}
		
		LOGGER.finest("Switched response message type from "+respType+" to "+response.getType()+" (request was "+reqType+")");
	
	} else if (respType == Type.ACK || respType == Type.RST) {
		response.setMID(exchange.getCurrentRequest().getMID());
	}
	
	if (response.getType() == Type.CON) {
		LOGGER.finer("Scheduling retransmission for " + response);
		prepareRetransmission(exchange, new RetransmissionTask(exchange, response) {
			public void retransmit() {
				sendResponse(exchange, response);
			}
		});
	}
	super.sendResponse(exchange, response);
}
 
Example 6
Source File: ObserveLayer.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void sendResponse(final Exchange exchange, Response response) {
	final ObserveRelation relation = exchange.getRelation();
	if (relation != null && relation.isEstablished()) {
		
		if (exchange.getRequest().isAcknowledged() || exchange.getRequest().getType()==Type.NON) {
			// Transmit errors as CON
			if (!ResponseCode.isSuccess(response.getCode())) {
				LOGGER.fine("Response has error code "+response.getCode()+" and must be sent as CON");
				response.setType(Type.CON);
				relation.cancel();
			} else {
				// Make sure that every now and than a CON is mixed within
				if (relation.check()) {
					LOGGER.fine("The observe relation check requires the notification to be sent as CON");
					response.setType(Type.CON);
				} else {
					// By default use NON, but do not override resource decision
					if (response.getType()==null) response.setType(Type.NON);
				}
			}
		}
		
		// This is a notification
		response.setLast(false);
		
		/*
		 * The matcher must be able to find the NON notifications to remove
		 * them from the exchangesByMID hashmap
		 */
		if (response.getType() == Type.NON) {
			relation.addNotification(response);
		}
		
		/*
		 * Only one Confirmable message is allowed to be in transit. A CON
		 * is in transit as long as it has not been acknowledged, rejected,
		 * or timed out. All further notifications are postponed here. If a
		 * former CON is acknowledged or timeouts, it starts the freshest
		 * notification (In case of a timeout, it keeps the retransmission
		 * counter). When a fresh/younger notification arrives but must be
		 * postponed we forget any former notification.
		 */
		if (response.getType() == Type.CON) {
			prepareSelfReplacement(exchange, response);
		}
		
		// The decision whether to postpone this notification or not and the
		// decision which notification is the freshest to send next must be
		// synchronized
		synchronized (exchange) {
			Response current = relation.getCurrentControlNotification();
			if (current != null && isInTransit(current)) {
				LOGGER.fine("A former notification is still in transit. Postpone " + response);
				// use the same MID
				response.setMID(current.getMID());
				relation.setNextControlNotification(response);
				// do not send now
				return;
			} else {
				relation.setCurrentControlNotification(response);
				relation.setNextControlNotification(null);
			}
		}

	} // else no observe was requested or the resource does not allow it
	super.sendResponse(exchange, response);
}
 
Example 7
Source File: Matcher.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void sendResponse(Exchange exchange, Response response) {
	
	// ensure MID is set
	if (response.getMID() == Message.NONE) {
		response.setMID(currendMID.getAndIncrement()%(1<<16));
	}
	
	// ensure Token is set
	response.setToken(exchange.getCurrentRequest().getToken());

	// If this is a CON notification we now can forget all previous NON notifications
	if (response.getType() == Type.CON || response.getType() == Type.ACK) {
		ObserveRelation relation = exchange.getRelation();
		if (relation != null) {
			removeNotificatoinsOf(relation);
		}
	}
	
	// Blockwise transfers are identified by URI and remote endpoint
	if (response.getOptions().hasBlock2()) {
		Request request = exchange.getCurrentRequest();
		KeyUri idByUri = new KeyUri(request.getURI(), response.getDestination().getAddress(), response.getDestinationPort());
		// Observe notifications only send the first block, hence do not store them as ongoing
		if (exchange.getResponseBlockStatus()!=null && !response.getOptions().hasObserve()) {
			// Remember ongoing blockwise GET requests
			if (ongoingExchanges.put(idByUri, exchange)==null) {
				LOGGER.fine("Ongoing Block2 started late, storing "+idByUri + " for " + request);
			} else {
				LOGGER.fine("Ongoing Block2 continued, storing "+idByUri + " for " + request);
			}
		} else {
			LOGGER.fine("Ongoing Block2 completed, cleaning up "+idByUri + " for " + request);
			ongoingExchanges.remove(idByUri);
		}
	}
	
	// Insert CON and NON to match ACKs and RSTs to the exchange.
	// Do not insert ACKs and RSTs.
	if (response.getType() == Type.CON || response.getType() == Type.NON) {
		KeyMID idByMID = new KeyMID(response.getMID(), null, 0);
		exchangesByMID.put(idByMID, exchange);
	}
	
	// Only CONs and Observe keep the exchange active
	if (response.getType() != Type.CON && response.isLast()) {
		exchange.setComplete();
	}
}
 
Example 8
Source File: ReliabilityLayer.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Makes sure that the response type is correct. The response type for a NON
 * can be NON or CON. The response type for a CON should either be an ACK
 * with a piggy-backed response or, if an empty ACK has already be sent, a
 * CON or NON with a separate response.
 */
@Override
public void sendResponse(final Exchange exchange, final Response response) {

	LOGGER.finer("Send response, failed transmissions: "+exchange.getFailedTransmissionCount());

	// If a response type is set, we do not mess around with it.
	// Only if none is set, we have to decide for one here.
	
	Type respType = response.getType();
	if (respType == null) {
		Type reqType = exchange.getCurrentRequest().getType();
		if (reqType == Type.CON) {
			if (exchange.getCurrentRequest().isAcknowledged()) {
				// send separate response
				response.setType(Type.CON);
			} else {
				exchange.getCurrentRequest().setAcknowledged(true);
				// send piggy-backed response
				response.setType(Type.ACK);
				response.setMID(exchange.getCurrentRequest().getMID());
			}
		} else {
			// send NON response
			response.setType(Type.NON);
		}
		
		LOGGER.finest("Switched response message type from "+respType+" to "+response.getType()+" (request was "+reqType+")");
	
	} else if (respType == Type.ACK || respType == Type.RST) {
		response.setMID(exchange.getCurrentRequest().getMID());
	}
	
	if (response.getType() == Type.CON) {
		LOGGER.finer("Scheduling retransmission for " + response);
		prepareRetransmission(exchange, new RetransmissionTask(exchange, response) {
			public void retransmit() {
				sendResponse(exchange, response);
			}
		});
	}
	super.sendResponse(exchange, response);
}
 
Example 9
Source File: ObserveLayer.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void sendResponse(final Exchange exchange, Response response) {
	final ObserveRelation relation = exchange.getRelation();
	if (relation != null && relation.isEstablished()) {
		
		if (exchange.getRequest().isAcknowledged() || exchange.getRequest().getType()==Type.NON) {
			// Transmit errors as CON
			if (!ResponseCode.isSuccess(response.getCode())) {
				LOGGER.fine("Response has error code "+response.getCode()+" and must be sent as CON");
				response.setType(Type.CON);
				relation.cancel();
			} else {
				// Make sure that every now and than a CON is mixed within
				if (relation.check()) {
					LOGGER.fine("The observe relation check requires the notification to be sent as CON");
					response.setType(Type.CON);
				} else {
					// By default use NON, but do not override resource decision
					if (response.getType()==null) response.setType(Type.NON);
				}
			}
		}
		
		// This is a notification
		response.setLast(false);
		
		/*
		 * The matcher must be able to find the NON notifications to remove
		 * them from the exchangesByMID hashmap
		 */
		if (response.getType() == Type.NON) {
			relation.addNotification(response);
		}
		
		/*
		 * Only one Confirmable message is allowed to be in transit. A CON
		 * is in transit as long as it has not been acknowledged, rejected,
		 * or timed out. All further notifications are postponed here. If a
		 * former CON is acknowledged or timeouts, it starts the freshest
		 * notification (In case of a timeout, it keeps the retransmission
		 * counter). When a fresh/younger notification arrives but must be
		 * postponed we forget any former notification.
		 */
		if (response.getType() == Type.CON) {
			prepareSelfReplacement(exchange, response);
		}
		
		// The decision whether to postpone this notification or not and the
		// decision which notification is the freshest to send next must be
		// synchronized
		synchronized (exchange) {
			Response current = relation.getCurrentControlNotification();
			if (current != null && isInTransit(current)) {
				LOGGER.fine("A former notification is still in transit. Postpone " + response);
				// use the same MID
				response.setMID(current.getMID());
				relation.setNextControlNotification(response);
				// do not send now
				return;
			} else {
				relation.setCurrentControlNotification(response);
				relation.setNextControlNotification(null);
			}
		}

	} // else no observe was requested or the resource does not allow it
	super.sendResponse(exchange, response);
}
 
Example 10
Source File: Matcher.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void sendResponse(Exchange exchange, Response response) {
	
	// ensure MID is set
	if (response.getMID() == Message.NONE) {
		response.setMID(currendMID.getAndIncrement()%(1<<16));
	}
	
	// ensure Token is set
	response.setToken(exchange.getCurrentRequest().getToken());

	// If this is a CON notification we now can forget all previous NON notifications
	if (response.getType() == Type.CON || response.getType() == Type.ACK) {
		ObserveRelation relation = exchange.getRelation();
		if (relation != null) {
			removeNotificatoinsOf(relation);
		}
	}
	
	// Blockwise transfers are identified by URI and remote endpoint
	if (response.getOptions().hasBlock2()) {
		Request request = exchange.getCurrentRequest();
		KeyUri idByUri = new KeyUri(request.getURI(), response.getDestination().getAddress(), response.getDestinationPort());
		// Observe notifications only send the first block, hence do not store them as ongoing
		if (exchange.getResponseBlockStatus()!=null && !response.getOptions().hasObserve()) {
			// Remember ongoing blockwise GET requests
			if (ongoingExchanges.put(idByUri, exchange)==null) {
				LOGGER.fine("Ongoing Block2 started late, storing "+idByUri + " for " + request);
			} else {
				LOGGER.fine("Ongoing Block2 continued, storing "+idByUri + " for " + request);
			}
		} else {
			LOGGER.fine("Ongoing Block2 completed, cleaning up "+idByUri + " for " + request);
			ongoingExchanges.remove(idByUri);
		}
	}
	
	// Insert CON and NON to match ACKs and RSTs to the exchange.
	// Do not insert ACKs and RSTs.
	if (response.getType() == Type.CON || response.getType() == Type.NON) {
		KeyMID idByMID = new KeyMID(response.getMID(), null, 0);
		exchangesByMID.put(idByMID, exchange);
	}
	
	// Only CONs and Observe keep the exchange active
	if (response.getType() != Type.CON && response.isLast()) {
		exchange.setComplete();
	}
}