private void set(String uriString, String payload) { //System.out.println("payload\n" + payload); try { URI uri = new URI(uriString); CoapClient client = new CoapClient(uri); client.setEndpoint(endPoint); CoapResponse response = client.put(payload, MediaTypeRegistry.TEXT_PLAIN); if (response != null && response.isSuccess()) { //System.out.println("Yay"); } else { System.out.println("Sending payload to " + uriString + " failed!"); } client.shutdown(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void test() { CoapResponse response = null; try { URI uri = new URI(SERVER_URI); CoapClient client = new CoapClient(uri); client.setEndpoint(new CoapEndpoint(dtlsConnector, NetworkConfig.getStandard())); response = client.get(); } catch (URISyntaxException e) { System.err.println("Invalid URI: " + e.getMessage()); System.exit(-1); } if (response != null) { System.out.println(response.getCode()); System.out.println(response.getOptions()); System.out.println(response.getResponseText()); System.out.println("\nADVANCED\n"); System.out.println(Utils.prettyPrint(response)); } else { System.out.println("No response received."); } }
/** * Creates a {@link CoapClient} that uses the same executor as this resource * and one of the endpoints that this resource belongs to. If no executor is * defined by this resource or any parent, the client will not have an * executor (it still works). If this resource is not yet added to a server * or the server has no endpoints, the client has no specific endpoint and * will use Californium's default endpoint. * * @return the CoAP client */ public CoapClient createClient() { CoapClient client = new CoapClient(); client.setExecutor(getExecutor()); List<Endpoint> endpoints = getEndpoints(); if (!endpoints.isEmpty()) client.setEndpoint(endpoints.get(0)); return client; }
/** * Creates a {@link CoapClient} that uses the same executor as this resource * and one of the endpoints that this resource belongs to. If no executor is * defined by this resource or any parent, the client will not have an * executor (it still works). If this resource is not yet added to a server * or the server has no endpoints, the client has no specific endpoint and * will use Californium's default endpoint. * * @return the CoAP client */ public CoapClient createClient() { CoapClient client = new CoapClient(); client.setExecutor(getExecutor()); List<Endpoint> endpoints = getEndpoints(); if (!endpoints.isEmpty()) client.setEndpoint(endpoints.get(0)); return client; }
String uriString = "coaps://" + ip + "//" + realm + "/" + Arrays.asList(path).stream().collect(Collectors.joining("/")); CoapClient client = new CoapClient(new URI(uriString)); client.setEndpoint(endPoint); CoapHandler handler = new CoapHandler() {
URI uri = new URI("coaps://" + ip + "//" + DEVICES); CoapClient client = new CoapClient(uri); client.setEndpoint(endPoint); CoapResponse response = client.get(); if (response == null) { URI uri = new URI("coaps://" + ip + "//" + GROUPS); CoapClient client = new CoapClient(uri); client.setEndpoint(endPoint); CoapResponse response = client.get(); if (response == null) { URI sceneURI = new URI("coaps://" + ip + "//" + SCENE); CoapClient client = new CoapClient(sceneURI); client.setEndpoint(endPoint); CoapResponse responseRooms = client.get(); if (responseRooms == null) { client.setEndpoint(endPoint); CoapResponse responseMoods = client.get(); if (responseMoods == null) {
private static void testBlockwise(final Mode mode) throws Exception { CoapClient client = new CoapClient(uriOf(URI)); client.setEndpoint(clientEndpoint); testBlockwise(client, mode); }
private static void testSimpleGet(final Mode mode) throws Exception { String uri = uriOf(URI); LOGGER.log(Level.FINE, "Test simple GET to {0}", uri); String currentResponseText = "simple GET"; resource.setResponse(currentResponseText, mode); CoapClient client = new CoapClient(uri); client.setEndpoint(clientEndpoint); CoapResponse response = client.get(); assertThatResponseContainsValue(response, currentResponseText); }
@Test public void testSynchronousPing() throws Exception { final AtomicBoolean sent = new AtomicBoolean(); CoapEndpoint clientEndpoint = new CoapEndpoint(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); clientEndpoint.addInterceptor(new MessageInterceptorAdapter() { @Override public void sendRequest(Request request) { sent.set(true); } }); String uri = TestTools.getUri(serverEndpoint, TARGET); CoapClient client = new CoapClient(uri).useExecutor(); client.setEndpoint(clientEndpoint); // Check that we get the right content when calling get() boolean ping = client.ping(); Assert.assertTrue(ping); Assert.assertTrue("Ping not sent using provided endpoint", sent.get()); }
private void testObserveProactive(final String responseText) throws Exception { String currentResponseText = responseText; int blocks = resource.setNotifies(currentResponseText, Mode.PIGGY_BACKED_RESPONSE); CountDownLatch latch = new CountDownLatch(HOW_MANY_NOTIFICATION_WE_WAIT_FOR + 1); AtomicBoolean isOnErrorInvoked = new AtomicBoolean(); CoapClient client = new CoapClient(uriOf(URI)); client.setEndpoint(clientEndpoint); CoapObserverAndCanceler handler = new CoapObserverAndCanceler(latch, isOnErrorInvoked, true); CoapObserveRelation rel = client.observe(handler); handler.setObserveRelation(rel); if (1 < blocks) { // wait longer, blocks may require retransmission and // blockwise notifies may overlap blocks <<= 2; } // Wait until all the notifications are received and // the relation is canceled latch.await(calculateNotifiesTimeout((HOW_MANY_NOTIFICATION_WE_WAIT_FOR + 1) * blocks), TimeUnit.MILLISECONDS); assertTrue("Client has not received all expected responses, left " + latch.getCount(), 0 == latch.getCount()); assertFalse(isOnErrorInvoked.get()); // should not happen }
/** * Verifies that the client & server clean up the message exchange store after retrieving * a resource body using a blockwise transfer with NON messages. * * @throws Exception if the test fails. */ @Test public void testBlockwiseUsingNONMessages() throws Exception { CoapClient client = new CoapClient(uriOf(URI)).useNONs(); client.setEndpoint(clientEndpoint); testBlockwise(client, Mode.PIGGY_BACKED_RESPONSE); }
/** * Verifies that the client & server clean up the message exchange store after the client has * established an observe relation, received some notifications and then has "forgotten" * about the observation. * * @throws Exception if the test fails. */ @Test public void testObserveReactive() throws Exception { final String uri = uriOf(URI); System.out.println("Test observe relation with a reactive cancelation to " + uri); String currentResponseText = "Hello observer"; resource.setNotifies(currentResponseText, Mode.PIGGY_BACKED_RESPONSE); CountDownLatch latch = new CountDownLatch(HOW_MANY_NOTIFICATION_WE_WAIT_FOR); AtomicBoolean isOnErrorInvoked = new AtomicBoolean(); CoapClient client = new CoapClient(uri); client.setEndpoint(clientEndpoint); CoapObserverAndCanceler handler = new CoapObserverAndCanceler(latch, isOnErrorInvoked, false); CoapObserveRelation rel = client.observe(handler); handler.setObserveRelation(rel); assertTrue("Client has not received all expected responses", latch.await(calculateNotifiesTimeout(HOW_MANY_NOTIFICATION_WE_WAIT_FOR), TimeUnit.MILLISECONDS)); assertFalse(isOnErrorInvoked.get()); // should not happen }
@Test public void testBlockwiseTransferToleratesLostMessages() throws Exception { String uri = getUri(new InetSocketAddress(middleAddress, middlePort), "test"); respPayload = generateRandomPayload(250); CoapClient coapclient = new CoapClient(uri); coapclient.setTimeout(10000); coapclient.setEndpoint(clientEndpoint); middle.drop(5, 6, 8, 9, 15); getResourceAndAssertPayload(coapclient, respPayload); for (int i = 0; i < 5; i++) { int[] numbers = new int[10]; for (int j = 0; j < numbers.length; j++) { numbers[j] = rand.nextInt(16); } middle.reset(); middle.drop(numbers); getResourceAndAssertPayload(coapclient, respPayload); } }
coapClient.setEndpoint(client); Request request = createRequest(GET, path, server);