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

The following examples show how to use org.eclipse.californium.core.coap.Response#getType() . 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: ReliabilityLayer.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * When we receive a Confirmable response, we acknowledge it and it also
 * counts as acknowledgment for the request. If the response is a duplicate,
 * we stop it here and do not forward it to the upper layer.
 */
@Override
public void receiveResponse(Exchange exchange, Response response) {
	exchange.setFailedTransmissionCount(0);
	
	exchange.getCurrentRequest().setAcknowledged(true);
	LOGGER.finest("Cancel any retransmission");
	exchange.setRetransmissionHandle(null);
	
	if (response.getType() == Type.CON && !exchange.getRequest().isCanceled()) {
		LOGGER.finer("Response is confirmable, send ACK");
		EmptyMessage ack = EmptyMessage.newACK(response);
		sendEmptyMessage(exchange, ack);
	}
	
	if (response.isDuplicate()) {
		LOGGER.fine("Response is duplicate, ignore it");
	} else {
		super.receiveResponse(exchange, response);
	}
}
 
Example 2
Source File: ReliabilityLayer.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * When we receive a Confirmable response, we acknowledge it and it also
 * counts as acknowledgment for the request. If the response is a duplicate,
 * we stop it here and do not forward it to the upper layer.
 */
@Override
public void receiveResponse(Exchange exchange, Response response) {
	exchange.setFailedTransmissionCount(0);
	
	exchange.getCurrentRequest().setAcknowledged(true);
	LOGGER.finest("Cancel any retransmission");
	exchange.setRetransmissionHandle(null);
	
	if (response.getType() == Type.CON && !exchange.getRequest().isCanceled()) {
		LOGGER.finer("Response is confirmable, send ACK");
		EmptyMessage ack = EmptyMessage.newACK(response);
		sendEmptyMessage(exchange, ack);
	}
	
	if (response.isDuplicate()) {
		LOGGER.fine("Response is duplicate, ignore it");
	} else {
		super.receiveResponse(exchange, response);
	}
}
 
Example 3
Source File: ObserveLayer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns true if the specified response is still in transit. A response is
 * in transit if it has not yet been acknowledged, rejected or its current
 * transmission has not yet timed out. 
 */
private boolean isInTransit(Response response) {
	Type type = response.getType();
	boolean acked = response.isAcknowledged();
	boolean timeout = response.isTimedOut();
	boolean result = type == Type.CON && !acked && !timeout;
	return result;
}
 
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: ObserveLayer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Returns true if the specified response is still in transit. A response is
 * in transit if it has not yet been acknowledged, rejected or its current
 * transmission has not yet timed out. 
 */
private boolean isInTransit(Response response) {
	Type type = response.getType();
	boolean acked = response.isAcknowledged();
	boolean timeout = response.isTimedOut();
	boolean result = type == Type.CON && !acked && !timeout;
	return result;
}
 
Example 6
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 7
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 8
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 9
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 10
Source File: Matcher.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
		public void completed(Exchange exchange) {
			
			/* 
			 * Logging in this method leads to significant performance loss.
			 * Uncomment logging code only for debugging purposes.
			 */
			
			if (exchange.getOrigin() == Origin.LOCAL) {
				// this endpoint created the Exchange by issuing a request
				
				KeyMID idByMID = new KeyMID(exchange.getCurrentRequest().getMID(), null, 0);
				KeyToken idByToken = new KeyToken(exchange.getCurrentRequest().getToken());
				
//				LOGGER.fine("Exchange completed: Cleaning up "+idByTok);
				exchangesByToken.remove(idByToken);
				
				// in case an empty ACK was lost
				exchangesByMID.remove(idByMID);
			
			} else { // Origin.REMOTE
				// this endpoint created the Exchange to respond to a request

				Response response = exchange.getCurrentResponse();
				if (response != null && response.getType() != Type.ACK) {
					// only response MIDs are stored for ACK and RST, no reponse Tokens
					KeyMID midKey = new KeyMID(response.getMID(), null, 0);
//					LOGGER.fine("Remote ongoing completed, cleaning up "+midKey);
					exchangesByMID.remove(midKey);
				}
				
				Request request = exchange.getCurrentRequest();
				if (request != null && (request.getOptions().hasBlock1() || response.getOptions().hasBlock2()) ) {
					KeyUri uriKey = new KeyUri(request.getURI(), request.getSource().getAddress(), request.getSourcePort());
					LOGGER.fine("Remote ongoing completed, cleaning up "+uriKey);
					ongoingExchanges.remove(uriKey);
				}
				
				// Remove all remaining NON-notifications if this exchange is an observe relation
				ObserveRelation relation = exchange.getRelation();
				if (relation != null) {
					removeNotificatoinsOf(relation);
				}
			}
		}
 
Example 11
Source File: CoapMessage.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public CoapMessage(Response request, boolean incoming) {
    this(incoming, request.getType(), request.getMID(), request.getTokenString(), request.getOptions(), request
            .getPayload());
    this.code = request.getCode().toString();
}
 
Example 12
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 13
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 14
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 15
Source File: Matcher.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
		public void completed(Exchange exchange) {
			
			/* 
			 * Logging in this method leads to significant performance loss.
			 * Uncomment logging code only for debugging purposes.
			 */
			
			if (exchange.getOrigin() == Origin.LOCAL) {
				// this endpoint created the Exchange by issuing a request
				
				KeyMID idByMID = new KeyMID(exchange.getCurrentRequest().getMID(), null, 0);
				KeyToken idByToken = new KeyToken(exchange.getCurrentRequest().getToken());
				
//				LOGGER.fine("Exchange completed: Cleaning up "+idByTok);
				exchangesByToken.remove(idByToken);
				
				// in case an empty ACK was lost
				exchangesByMID.remove(idByMID);
			
			} else { // Origin.REMOTE
				// this endpoint created the Exchange to respond to a request

				Response response = exchange.getCurrentResponse();
				if (response != null && response.getType() != Type.ACK) {
					// only response MIDs are stored for ACK and RST, no reponse Tokens
					KeyMID midKey = new KeyMID(response.getMID(), null, 0);
//					LOGGER.fine("Remote ongoing completed, cleaning up "+midKey);
					exchangesByMID.remove(midKey);
				}
				
				Request request = exchange.getCurrentRequest();
				if (request != null && (request.getOptions().hasBlock1() || response.getOptions().hasBlock2()) ) {
					KeyUri uriKey = new KeyUri(request.getURI(), request.getSource().getAddress(), request.getSourcePort());
					LOGGER.fine("Remote ongoing completed, cleaning up "+uriKey);
					ongoingExchanges.remove(uriKey);
				}
				
				// Remove all remaining NON-notifications if this exchange is an observe relation
				ObserveRelation relation = exchange.getRelation();
				if (relation != null) {
					removeNotificatoinsOf(relation);
				}
			}
		}
 
Example 16
Source File: CoapMessage.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public CoapMessage(Response request, boolean incoming) {
    this(incoming, request.getType(), request.getMID(), request.getTokenString(), request.getOptions(), request
            .getPayload());
    this.code = request.getCode().toString();
}