Java Code Examples for org.eclipse.californium.core.network.Exchange#getCurrentResponse()

The following examples show how to use org.eclipse.californium.core.network.Exchange#getCurrentResponse() . 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 5 votes vote down vote up
/**
 * When we receive a duplicate of a request, we stop it here and do not
 * forward it to the upper layer. If the server has already sent a response,
 * we send it again. If the request has only been acknowledged (but the ACK
 * has gone lost or not reached the client yet), we resent the ACK. If the
 * request has neither been responded, acknowledged or rejected yet, the
 * server has not yet decided what to do with the request and we cannot do
 * anything.
 */
@Override
public void receiveRequest(Exchange exchange, Request request) {
	
	if (request.isDuplicate()) {
		// Request is a duplicate, so resend ACK, RST or response
		if (exchange.getCurrentResponse() != null) {
			LOGGER.fine("Respond with the current response to the duplicate request");
			// Do not restart retransmission cycle
			super.sendResponse(exchange, exchange.getCurrentResponse());
			
		} else if (exchange.getCurrentRequest().isAcknowledged()) {
			LOGGER.fine("The duplicate request was acknowledged but no response computed yet. Retransmit ACK");
			EmptyMessage ack = EmptyMessage.newACK(request);
			sendEmptyMessage(exchange, ack);
		
		} else if (exchange.getCurrentRequest().isRejected()) {
			LOGGER.fine("The duplicate request was rejected. Reject again");
			EmptyMessage rst = EmptyMessage.newRST(request);
			sendEmptyMessage(exchange, rst);

		} else {
			LOGGER.fine("The server has not yet decided what to do with the request. We ignore the duplicate.");
			// The server has not yet decided, whether to acknowledge or
			// reject the request. We know for sure that the server has
			// received the request though and can drop this duplicate here.
		}

	} else {
		// Request is not a duplicate
		exchange.setCurrentRequest(request);
		super.receiveRequest(exchange, request);
	}
}
 
Example 2
Source File: CongestionControlLayer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void checkRemoteEndpointQueue(Exchange exchange) {
	// 0 = empty queue | 1 = response | 2 = request
	if (!getRemoteEndpoint(exchange).getConfirmableQueue().isEmpty()) {
		// We have some exchanges that need to be processed; is it a
		// response or a request?
		Exchange queuedExchange = getRemoteEndpoint(exchange).getConfirmableQueue().poll();
		if (queuedExchange.getCurrentResponse() != null) {
			// it's a response
			sendResponse(queuedExchange, queuedExchange.getCurrentResponse());
		} else if (queuedExchange.getCurrentRequest() != null) {
			// it's a request
			sendRequest(queuedExchange, queuedExchange.getCurrentRequest());
		}
	}
}
 
Example 3
Source File: CongestionControlLayer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void run() {
	if (!endpoint.getNonConfirmableQueue().isEmpty()) {
		endpoint.setProcessingNON(true);

		Exchange exchange = endpoint.getNonConfirmableQueue().poll();

		if (getRemoteEndpoint(exchange).getNonConfirmableCounter() <= MAX_SUCCESSIVE_NONS) {
			getRemoteEndpoint(exchange).increaseNonConfirmableCounter();
			if (exchange.getCurrentRequest().getDestinationPort() != 0) {
				// it's a response
				sendBucketRequest(exchange, exchange.getCurrentRequest());
			} else if (exchange.getCurrentResponse() != null) {
				// it's a request
				sendBucketResponse(exchange, exchange.getCurrentResponse());
			}
		}
		// schedule next transmission of a NON based on the RTO value (rate = 1/RTO)
		executor.schedule(
				new bucketThread(getRemoteEndpoint(exchange)),
				getRemoteEndpoint(exchange).getRTO(),
				TimeUnit.MILLISECONDS);

	} else {
		endpoint.setProcessingNON(false);
	}
}
 
Example 4
Source File: ReliabilityLayer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * When we receive a duplicate of a request, we stop it here and do not
 * forward it to the upper layer. If the server has already sent a response,
 * we send it again. If the request has only been acknowledged (but the ACK
 * has gone lost or not reached the client yet), we resent the ACK. If the
 * request has neither been responded, acknowledged or rejected yet, the
 * server has not yet decided what to do with the request and we cannot do
 * anything.
 */
@Override
public void receiveRequest(Exchange exchange, Request request) {
	
	if (request.isDuplicate()) {
		// Request is a duplicate, so resend ACK, RST or response
		if (exchange.getCurrentResponse() != null) {
			LOGGER.fine("Respond with the current response to the duplicate request");
			// Do not restart retransmission cycle
			super.sendResponse(exchange, exchange.getCurrentResponse());
			
		} else if (exchange.getCurrentRequest().isAcknowledged()) {
			LOGGER.fine("The duplicate request was acknowledged but no response computed yet. Retransmit ACK");
			EmptyMessage ack = EmptyMessage.newACK(request);
			sendEmptyMessage(exchange, ack);
		
		} else if (exchange.getCurrentRequest().isRejected()) {
			LOGGER.fine("The duplicate request was rejected. Reject again");
			EmptyMessage rst = EmptyMessage.newRST(request);
			sendEmptyMessage(exchange, rst);

		} else {
			LOGGER.fine("The server has not yet decided what to do with the request. We ignore the duplicate.");
			// The server has not yet decided, whether to acknowledge or
			// reject the request. We know for sure that the server has
			// received the request though and can drop this duplicate here.
		}

	} else {
		// Request is not a duplicate
		exchange.setCurrentRequest(request);
		super.receiveRequest(exchange, request);
	}
}
 
Example 5
Source File: CongestionControlLayer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void checkRemoteEndpointQueue(Exchange exchange) {
	// 0 = empty queue | 1 = response | 2 = request
	if (!getRemoteEndpoint(exchange).getConfirmableQueue().isEmpty()) {
		// We have some exchanges that need to be processed; is it a
		// response or a request?
		Exchange queuedExchange = getRemoteEndpoint(exchange).getConfirmableQueue().poll();
		if (queuedExchange.getCurrentResponse() != null) {
			// it's a response
			sendResponse(queuedExchange, queuedExchange.getCurrentResponse());
		} else if (queuedExchange.getCurrentRequest() != null) {
			// it's a request
			sendRequest(queuedExchange, queuedExchange.getCurrentRequest());
		}
	}
}
 
Example 6
Source File: CongestionControlLayer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void run() {
	if (!endpoint.getNonConfirmableQueue().isEmpty()) {
		endpoint.setProcessingNON(true);

		Exchange exchange = endpoint.getNonConfirmableQueue().poll();

		if (getRemoteEndpoint(exchange).getNonConfirmableCounter() <= MAX_SUCCESSIVE_NONS) {
			getRemoteEndpoint(exchange).increaseNonConfirmableCounter();
			if (exchange.getCurrentRequest().getDestinationPort() != 0) {
				// it's a response
				sendBucketRequest(exchange, exchange.getCurrentRequest());
			} else if (exchange.getCurrentResponse() != null) {
				// it's a request
				sendBucketResponse(exchange, exchange.getCurrentResponse());
			}
		}
		// schedule next transmission of a NON based on the RTO value (rate = 1/RTO)
		executor.schedule(
				new bucketThread(getRemoteEndpoint(exchange)),
				getRemoteEndpoint(exchange).getRTO(),
				TimeUnit.MILLISECONDS);

	} else {
		endpoint.setProcessingNON(false);
	}
}
 
Example 7
Source File: CongestionControlLayer.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private boolean processMessage(Exchange exchange, Object message) {
	Type messageType;
	messageType = Type.NON;

	// Determine type of message
	if (message.getClass() == Request.class) {
		messageType = exchange.getCurrentRequest().getType();
	}
	if (message.getClass() == Response.class) {
		messageType = exchange.getCurrentResponse().getType();
	}

	// Put into queues for NON or CON messages
	if (messageType == Type.CON) {
		if (!checkNSTART(exchange)) { // Check if NSTART is not reached yet
										// for confirmable transmissions
			return false;
		}
	} else if (getRemoteEndpoint(exchange).getNonConfirmableCounter() > MAX_SUCCESSIVE_NONS) {
		// Every MAX_SUCCESSIVE_NONS + 1 packets, a non-confirmable needs to
		// be converted to a confirmable [CoCoA]
		if (exchange.getCurrentRequest().getDestinationPort() != 0) {
			exchange.getCurrentRequest().setType(Type.CON);
		} else if (exchange.getCurrentResponse() != null) {
			exchange.getCurrentResponse().setType(Type.CON);
		}
		getRemoteEndpoint(exchange).resetNonConfirmableCounter();

		// Check if NSTART is not reached yet for confirmable transmissions
		if (!checkNSTART(exchange)) {
			return false;
		}
	} else {
		// Check of if there's space to queue a NON
		if (getRemoteEndpoint(exchange).getNonConfirmableQueue().size() == EXCHANGELIMIT) {
			// System.out.println("Non-confirmable exchange queue limit reached!");
			// TODO: Drop packet -> Notify upper layers?
		} else {
			getRemoteEndpoint(exchange).getNonConfirmableQueue().add(
					exchange);

			// Check if NONs are already processed, if not, start bucket
			// Thread
			if (!getRemoteEndpoint(exchange).getProcessingNON()) {
				executor.schedule(new bucketThread(
						getRemoteEndpoint(exchange)), 0,
						TimeUnit.MILLISECONDS);
			}
		}
		return false;
	}
	return true;

}
 
Example 8
Source File: CongestionControlLayer.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private boolean processMessage(Exchange exchange, Object message) {
	Type messageType;
	messageType = Type.NON;

	// Determine type of message
	if (message.getClass() == Request.class) {
		messageType = exchange.getCurrentRequest().getType();
	}
	if (message.getClass() == Response.class) {
		messageType = exchange.getCurrentResponse().getType();
	}

	// Put into queues for NON or CON messages
	if (messageType == Type.CON) {
		if (!checkNSTART(exchange)) { // Check if NSTART is not reached yet
										// for confirmable transmissions
			return false;
		}
	} else if (getRemoteEndpoint(exchange).getNonConfirmableCounter() > MAX_SUCCESSIVE_NONS) {
		// Every MAX_SUCCESSIVE_NONS + 1 packets, a non-confirmable needs to
		// be converted to a confirmable [CoCoA]
		if (exchange.getCurrentRequest().getDestinationPort() != 0) {
			exchange.getCurrentRequest().setType(Type.CON);
		} else if (exchange.getCurrentResponse() != null) {
			exchange.getCurrentResponse().setType(Type.CON);
		}
		getRemoteEndpoint(exchange).resetNonConfirmableCounter();

		// Check if NSTART is not reached yet for confirmable transmissions
		if (!checkNSTART(exchange)) {
			return false;
		}
	} else {
		// Check of if there's space to queue a NON
		if (getRemoteEndpoint(exchange).getNonConfirmableQueue().size() == EXCHANGELIMIT) {
			// System.out.println("Non-confirmable exchange queue limit reached!");
			// TODO: Drop packet -> Notify upper layers?
		} else {
			getRemoteEndpoint(exchange).getNonConfirmableQueue().add(
					exchange);

			// Check if NONs are already processed, if not, start bucket
			// Thread
			if (!getRemoteEndpoint(exchange).getProcessingNON()) {
				executor.schedule(new bucketThread(
						getRemoteEndpoint(exchange)), 0,
						TimeUnit.MILLISECONDS);
			}
		}
		return false;
	}
	return true;

}