private synchronized CoapClient getClient(Exchange exchange) { if (client == null) { URI uri = exchange.getIn().getHeader(CoAPConstants.COAP_URI, URI.class); if (uri == null) { uri = endpoint.getUri(); } client = new CoapClient(uri); } return client; } }
public OdlOnem2mCoapClient(String uri) { coapClient = new CoapClient(uri); coapClient.useCONs(); } public CoapResponse sendRequest(OdlOnem2mCoapRequestPrimitive onem2mRequest) {
/** * Create a CoAP client for the given URL. * * @param relativeUrl * @return */ private static CoapClient createClientFor(String relativeUrl) { return new CoapClient("coap://" + COAP_SERVER + ":" + COAP_PORT + "/" + relativeUrl); }
public void run(){ client = new CoapClient(serverURI); while(isActive()){ if (System.currentTimeMillis() / 1000 > nextExpectedMessage){ client.observe(this); } try{ Thread.sleep(60000); }catch(Exception ex){} } }
/** * Create a CoAP client based on extracted parameters. * * @param parameters * @return */ protected CoapClient createCoapClient(CoapParameters parameters) { return new CoapClient( "coap://" + parameters.getHostname() + ":" + parameters.getPort() + "/" + parameters.getUrl()); } }
/** * Create a CoAP client based on extracted parameters. * * @param parameters * @return */ protected CoapClient createCoapClient(CoapParameters parameters) { return new CoapClient( "coap://" + parameters.getHostname() + ":" + parameters.getPort() + "/" + parameters.getUrl()); } }
/** * 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; }
@Override public void doInit(Properties messageAttributes) throws PerfCakeException { client = new CoapClient(safeGetTarget(messageAttributes)); switch (requestType) { case CONFIRMABLE: client.useCONs(); break; case NON_CONFIRMABLE: client.useNONs(); break; } }
private static void testBlockwise(final Mode mode) throws Exception { CoapClient client = new CoapClient(uriOf(URI)); client.setEndpoint(clientEndpoint); testBlockwise(client, mode); }
@Before public void startupServer() { resource.setContent(CONTENT_1); client = new CoapClient(uri).useExecutor(); }
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()); }
/** * 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); }
@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); } }
@Test(expected = IllegalStateException.class) public void testObserveClientReregisterAfterReject() throws Exception { resourceX.setObserveType(Type.NON); resourceX.rejectNextGet(); CoapClient client = new CoapClient(uriX); CountingHandler handler = new CountingHandler(); CoapObserveRelation rel = client.observeAndWait(handler); assertTrue("Not rejected", rel.isCanceled()); rel.reregister(); }
@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(expected = IllegalStateException.class) public void testObserveClientReregisterAfterTimeout() throws Exception { resourceX.setObserveType(Type.NON); resourceX.delayNextGet(100); CoapClient client = new CoapClient(uriX); long timeout = client.getTimeout(); client.setTimeout(1); CountingHandler handler = new CountingHandler(); CoapObserveRelation rel = client.observeAndWait(handler); assertTrue("No timeout", rel.isCanceled()); client.setTimeout(timeout); rel.reregister(); }
@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()); }
@Test public void testObserveClientReregisterBeforeTimeout() throws Exception { resourceX.setObserveType(Type.NON); resourceX.delayNextGet(100); CoapClient client = new CoapClient(uriX); CountingHandler handler = new CountingHandler(); CoapObserveRelation rel = client.observe(handler); assertFalse("Timeout", rel.isCanceled()); assertFalse("reregister not ignored", rel.reregister()); assertTrue(handler.waitForLoadCalls(1, 1000, TimeUnit.MILLISECONDS)); resourceX.changed("client"); // assert notify received assertTrue(handler.waitForLoadCalls(2, 1000, TimeUnit.MILLISECONDS)); assertFalse("Response not received", rel.isCanceled()); }