/** * Formats a {@link CoapResponse} into a readable String representation. * * @param r the CoapResponse * @return the pretty print */ public static String prettyPrint(CoapResponse r) { return prettyPrint(r.advanced()); }
/** * Formats a {@link CoapResponse} into a readable String representation. * * @param r the CoapResponse * @return the pretty print */ public static String prettyPrint(CoapResponse r) { return prettyPrint(r.advanced()); }
@Override public void onLoad(CoapResponse response) { if (response.getCode() == ResponseCode.NOT_FOUND) { CoapObserveRelation cor; synchronized (relationStorage) { if (!testdump.get()) { testdump.set(true); System.out.println("Used Memory: " + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 + "kb (" + relationStorage.size() + " clients)."); } if ((cor = relationStorage.get(new InetSocketAddress(response.advanced().getSource(), response.advanced().getSourcePort()))) != null) { cor.reactiveCancel(); relationStorage.remove(new InetSocketAddress(response.advanced().getSource(), response.advanced().getSourcePort())); cor = null; } if (relationStorage.size() == 0) testdump.set(false); } return; } } @Override public void onError() { }
/** * Checks if the specified response truly is a new notification and if, * invokes the handler's method or drops the notification otherwise. * Ordering and delivery must be done synchronized here to deal with * race conditions in the stack. */ @Override protected void deliver(CoapResponse response) { synchronized (relation) { if (relation.onResponse(response)) { handler.onLoad(response); } else { LOGGER.log(Level.FINER, "Dropping old notification: {0}", response.advanced()); return; } } }
/** * Sets the current response or notification. * * Use {@link #orderer} to filter deprecated responses. * * @param response the response or notification * @return {@code true}, response is accepted by {@link #orderer}, * {@code false} otherwise. */ protected boolean onResponse(CoapResponse response) { if (null != response && orderer.isNew(response.advanced())) { current = response; prepareReregistration(response, 2000); registrationPending.set(false); return true; } else { return false; } }
/** * Checks if the specified response truly is a new notification and if, * invokes the handler's method or drops the notification otherwise. * Ordering and delivery must be done synchronized here to deal with * race conditions in the stack. */ @Override protected void deliver(CoapResponse response) { synchronized (relation) { if (relation.onResponse(response)) { handler.onLoad(response); } else { LOGGER.finer("Dropping old notification: "+response.advanced()); return; } } }
@Override public void onLoad(CoapResponse response) { int counter; synchronized (this) { counter = loadCalls.incrementAndGet(); notifyAll(); } System.out.println("Received " + counter + ". Notification: " + response.advanced()); }
/** * Sets the current response or notification. * * Use {@link #orderer} to filter deprecated responses. * * @param response the response or notification * @return {@code true}, response is accepted by {@link #orderer}, * {@code false} otherwise. */ protected boolean onResponse(CoapResponse response) { if (null != response && orderer.isNew(response.advanced())) { current = response; prepareReregistration(response, 2000); registrationPending.set(false); return true; } else { return false; } }
System.out.println("---------------\nGET " + link2.getURI() + "\n---------------"); CoapResponse response = client.get(); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText());
@Override public void onLoad(final CoapResponse response) { String responseDescription = String.format("onLoad(%d) '%s'", onLoadCounter.incrementAndGet(), response.getResponseText()); System.out.println(responseDescription); if (response.getResponseText().startsWith(CONTENT_1) && response.advanced().getOptions().hasObserve()) { try { assertAwait("assert missing", barrier, 2000, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { } } } });
System.out.println("---------------\nGET /test\n---------------"); response = client.get(); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); System.out.println("---------------\nDELETE /test\n---------------"); response = client.delete(); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); System.out.println("---------------\nPUT /test\n---------------"); response = client.put("", MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); System.out.println("---------------\nPOST /test\n---------------"); response = client.post("non-empty", MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); System.out.println("---------------\nNON-GET /test\n---------------"); response = client.get(); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); System.out.println("---------------\nNON-DELETE /test\n---------------"); response = client.delete(); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText());
private CoapObserveRelation observeAndWait(Request request, CoapHandler handler) { if (request.getOptions().hasObserve()) { Endpoint outEndpoint = getEffectiveEndpoint(request); CoapObserveRelation relation = new CoapObserveRelation(request, outEndpoint); request.addMessageObserver(new ObserveMessageObserverImpl(handler, relation)); CoapResponse response = synchronous(request, outEndpoint); if (response == null || !response.advanced().getOptions().hasObserve()) { relation.setCanceled(true); } return relation; } else { throw new IllegalArgumentException("please make sure that the request has observe option set."); } }
@Override public void onLoad(CoapResponse response) { notifications.incrementAndGet(); String payload = response.getResponseText(); Assert.assertEquals(expected, payload); Assert.assertTrue(response.advanced().getOptions().hasObserve()); } @Override public void onError() {
/** * Sends the specified observe request and waits for the response whereupon * the specified handler is invoked when a notification arrives. * * @param request the request * * @param handler the Response handler * * @return the CoAP observe relation * @throws IllegalArgumentException if the observe option is not set in the * request */ public CoapObserveRelation observeAndWait(Request request, CoapHandler handler) { if (request.getOptions().hasObserve()) { Endpoint outEndpoint = getEffectiveEndpoint(request); CoapObserveRelation relation = new CoapObserveRelation(request, outEndpoint); request.addMessageObserver(new ObserveMessageObserverImpl(handler, relation)); CoapResponse response = synchronous(request, outEndpoint); if (response == null || !response.advanced().getOptions().hasObserve()) { relation.setCanceled(true); } return relation; } else { throw new IllegalArgumentException("please make sure that the request has observe option set."); } }
@Test public void testAdvancedUsesTypeFromRequest() throws Exception { String uri = TestTools.getUri(serverEndpoint, TARGET); CoapClient client = new CoapClient(uri).useExecutor(); // Set NONs but expecting CONs as specified in request client.useNONs(); Request request = new Request(Code.GET, Type.CON); CoapResponse resp = client.advanced(request); Assert.assertEquals(Type.ACK, resp.advanced().getType()); Assert.assertEquals(CONTENT_1, resp.getResponseText()); }
@Test public void testAdvancedUsesUriFromRequest() throws Exception { String nonExistingUri = TestTools.getUri(serverEndpoint, "non-existing"); CoapClient client = new CoapClient(nonExistingUri).useExecutor(); Request request = new Request(Code.GET, Type.CON); String uri = TestTools.getUri(serverEndpoint, TARGET); request.setURI(uri); CoapResponse resp = client.advanced(request); Assert.assertEquals(Type.ACK, resp.advanced().getType()); Assert.assertEquals(CONTENT_1, resp.getResponseText()); }