@Override public void deliverResponse(Exchange exchange, Response response) { if (response == null) { throw new RuntimeException("Response must not be null"); } else if (exchange == null) { throw new RuntimeException("Exchange must not be null"); } else if (exchange.getRequest() == null) { throw new IllegalArgumentException("Exchange does not contain request"); } else { exchange.getRequest().setResponse(response); } }
@Override public void handleRequest(Exchange exchange) { try { super.handleRequest(exchange); } catch (InvalidRequestException e) { LOG.debug("InvalidRequestException while handling request({}) on the /rd resource", exchange.getRequest(), e); Response response = new Response(ResponseCode.BAD_REQUEST); response.setPayload(e.getMessage()); exchange.sendResponse(response); } catch (RuntimeException e) { LOG.error("Exception while handling request({}) on the /rd resource", exchange.getRequest(), e); exchange.sendResponse(new Response(ResponseCode.INTERNAL_SERVER_ERROR)); } }
@Override public void run() { if (!exchange.isComplete()) { if (exchange.getRequest() == null) { LOGGER.log(Level.INFO, "Block1 transfer timed out: {0}", exchange.getCurrentRequest()); } else { LOGGER.log(Level.INFO, "Block2 transfer timed out: {0}", exchange.getRequest()); } exchange.setComplete(); } } }
@Override public void handleRequest(Exchange exchange) { exchange.sendAccept(); Response response = forwardRequest(exchange.getRequest()); exchange.sendResponse(response); }
@Override public void run() { if(exchange.getResponse() == null) { exchange.getCurrentRequest().setCanceled(true); } exchange.setComplete(); } }, multicastMaxLifetime, TimeUnit.MILLISECONDS);
@Override public void deliverRequest(final Exchange exchange) { Request request = exchange.getRequest(); List<String> path = request.getOptions().getUriPath(); final Resource resource = findResource(path); if (resource != null) { checkForObserveOption(exchange, resource); // Get the executor and let it process the request Executor executor = resource.getExecutor(); if (executor != null) { exchange.setCustomExecutor(); executor.execute(new Runnable() { public void run() { resource.handleRequest(exchange); } }); } else { resource.handleRequest(exchange); } } else { LOGGER.info("Did not find resource " + path.toString() + " requested by " + request.getSource()+":"+request.getSourcePort()); exchange.sendResponse(new Response(ResponseCode.NOT_FOUND)); } }
final Request request = exchange.getRequest(); if (request.getCode() != Code.GET) { return; final ObserveRelation relation = new ObserveRelation(remote, resource, exchange); remote.addObserveRelation(relation); exchange.setRelation(relation);
@Override public void handleRequest(Exchange exchange) { try { super.handleRequest(exchange); } catch (Exception e) { LOG.error("Exception while handling a request on the /bs resource", e); exchange.sendResponse(new Response(ResponseCode.INTERNAL_SERVER_ERROR)); } }
/** * 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(final Exchange exchange, final 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); } }
@Override public void onCancel() { if (!exchange.isComplete()) { LOGGER.log(Level.FINE, "completing canceled request [MID={0}, token={1}]", new Object[]{ exchange.getRequest().getMID(), exchange.getRequest().getTokenString() }); exchange.setComplete(); } } }
@Override public void receiveRequest(Exchange exchange, Request request) { // if there is no BlockwiseLayer we still have to set it if (exchange.getRequest() == null) exchange.setRequest(request); if (deliverer != null) { deliverer.deliverRequest(exchange); } else { LOGGER.severe("Top of CoAP stack has no deliverer to deliver request"); } }
@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(); }
exchange.setRequest(request); exchange.sendResponse(response); return; } else {
@Override public void deliverRequest(Exchange exchange) { System.out.println("server received request"); exchange.sendAccept(); try { Thread.sleep(500); } catch (Exception e) {} Response response = new Response(ResponseCode.CONTENT); response.setConfirmable(false); response.setPayload(SERVER_RESPONSE); exchange.sendResponse(response); } @Override
@Override public boolean registerOutboundRequestWithTokenOnly(final Exchange exchange) { if (exchange == null) { throw new NullPointerException("exchange must not be null"); } else if (exchange.getCurrentRequest() == null) { throw new IllegalArgumentException("exchange does not contain a request"); } else { registerWithToken(exchange); return true; } }
exchange.setRequestBlockStatus(null); exchange.setCurrentResponse(error); super.sendResponse(exchange, error); return; exchange.setCurrentResponse(piggybacked); super.sendResponse(exchange, piggybacked); exchange.setBlock1ToAck(block1); exchange.setRequest(assembled); super.receiveRequest(exchange, assembled); error.getOptions().setBlock1(block1.getSzx(), block1.isM(), block1.getNum()); error.setPayload("Wrong block number"); exchange.setCurrentResponse(error); } else if (exchange.getResponse()!=null && request.getOptions().hasBlock2()) { Response response = exchange.getResponse(); BlockwiseStatus status = findResponseBlockStatus(exchange, response); status.setCurrentNum(block2.getNum()); exchange.setResponseBlockStatus(null); exchange.setBlockCleanupHandle(null); } else { LOGGER.fine("Ongoing is continuing "+status);
@Test public void testDiscoveryMultiFiltering() { Request request = Request.newGet(); request.setURI("coap://localhost/.well-known/core?rt=light-lux&rt=temprature-cel"); Exchange exchange = new Exchange(request, Origin.REMOTE); exchange.setRequest(request); exchange.setEndpoint(new DummyEndpoint()); DiscoveryResource discovery = new DiscoveryResource(root); discovery.handleRequest(exchange); System.out.println(exchange.getResponse().getPayloadString()); Assert.assertEquals(ResponseCode.BAD_OPTION, exchange.getResponse().getCode()); }
private Exchange sendRequest(final TcpMatcher matcher, final CorrelationContext ctx) { Request request = Request.newGet(); request.setDestination(dest.getAddress()); request.setDestinationPort(dest.getPort()); Exchange exchange = new Exchange(request, Origin.LOCAL); exchange.setRequest(request); matcher.sendRequest(exchange, request); exchange.setCorrelationContext(ctx); return exchange; }
/** * If we receive an ACK or RST, we mark the outgoing request or response * as acknowledged or rejected respectively and cancel its retransmission. */ @Override public void receiveEmptyMessage(final Exchange exchange, final EmptyMessage message) { exchange.setFailedTransmissionCount(0); // TODO: If this is an observe relation, the current response might not // be the one that is being acknowledged. The current response might // already be the next NON notification. if (message.getType() == Type.ACK) { if (exchange.getOrigin() == Origin.LOCAL) { exchange.getCurrentRequest().setAcknowledged(true); } else { exchange.getCurrentResponse().setAcknowledged(true); } } else if (message.getType() == Type.RST) { if (exchange.getOrigin() == Origin.LOCAL) { exchange.getCurrentRequest().setRejected(true); } else { exchange.getCurrentResponse().setRejected(true); } } else { LOGGER.warning("Empty messgae was not ACK nor RST: "+message); } LOGGER.finer("Cancel retransmission"); exchange.setRetransmissionHandle(null); super.receiveEmptyMessage(exchange, message); }
/** * If we receive an ACK or RST, we mark the outgoing request or response as * acknowledged or rejected respectively and cancel its retransmission. */ @Override public void receiveEmptyMessage(final Exchange exchange, final EmptyMessage message) { exchange.setFailedTransmissionCount(0); // TODO: If this is an observe relation, the current response might not // be the one that is being acknowledged. The current response might // already be the next NON notification. if (message.getType() == Type.ACK) { if (exchange.isOfLocalOrigin()) { exchange.getCurrentRequest().setAcknowledged(true); } else { exchange.getCurrentResponse().setAcknowledged(true); } } else if (message.getType() == Type.RST) { if (exchange.isOfLocalOrigin()) { exchange.getCurrentRequest().setRejected(true); } else { exchange.getCurrentResponse().setRejected(true); } } else { LOGGER.log(Level.WARNING, "received empty message that is neither ACK nor RST: {0}", message); } exchange.setRetransmissionHandle(null); upper().receiveEmptyMessage(exchange, message); }