public void run(){ client = new CoapClient(serverURI); while(isActive()){ if (System.currentTimeMillis() / 1000 > nextExpectedMessage){ client.observe(this); } try{ Thread.sleep(60000); }catch(Exception ex){} } }
CoapObserveRelation relation1 = client.observe( new CoapHandler() { @Override public void onLoad(CoapResponse response) { CoapObserveRelation relation2 = client.observe( new CoapHandler() { @Override public void onLoad(CoapResponse response) { CoapObserveRelation relation13 = client.observe( new CoapHandler() { @Override public void onLoad(CoapResponse response) { CoapObserveRelation relation14 = client.observe( new CoapHandler() { @Override public void onLoad(CoapResponse response) {
/** * Sends an observe request and invokes the specified handler each time * a notification arrives. * * @param handler the Response handler * @return the CoAP observe relation */ public CoapObserveRelation observe(CoapHandler handler) { Request request = newGet().setURI(uri).setObserve(); return observe(request, handler); }
/** * Sends an observe request and invokes the specified handler each time * a notification arrives. * * @param handler the Response handler * @return the CoAP observe relation */ public CoapObserveRelation observe(CoapHandler handler) { Request request = Request.newGet().setURI(uri).setObserve(); return observe(request, handler); }
@Override public void handlePOST(CoapExchange exchange) { CoapClient client = this.createClient(exchange.getRequestText()); CoapObserveRelation relation; relation = client.observe(handler); synchronized(relationStorage) { relationStorage.put(new InetSocketAddress(exchange.getSourceAddress(), exchange.getSourcePort()), relation); } Response response = new Response(ResponseCode.VALID); exchange.respond(response); } }
/** * Sends an observe request with the specified Accept option and invokes the * specified handler each time a notification arrives. * * @param handler the Response handler * @param accept the Accept option * @return the CoAP observe relation */ public CoapObserveRelation observe(CoapHandler handler, int accept) { Request request = Request.newGet().setURI(uri).setObserve(); return observe(accept(request, accept), handler); }
CoapObserveRelation relation = client.observe(handler); watching.add(relation);
/** * Sends an observe request with the specified Accept option and invokes the * specified handler each time a notification arrives. * * @param handler the Response handler * @param accept the Accept option * @return the CoAP observe relation */ public CoapObserveRelation observe(CoapHandler handler, int accept) { Request request = newGet().setURI(uri).setObserve(); return observe(accept(request, accept), handler); }
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 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 case for CoapClient.observe(Request request, CoapHandler handler) * exception handling. * * @throws Exception */ @Test(expected = IllegalArgumentException.class) public void testObserveExceptionHandling() throws Exception { CoapClient client = new CoapClient(uriX); Request request = Request.newGet().setURI(uriX); @SuppressWarnings("unused") CoapObserveRelation rel = client.observe(request, new CoapHandler() { @Override public void onLoad(CoapResponse response) { } @Override public void onError() { } }); }
CoapObserveRelation obs1 = client.observe(new TestHandler("Test Observe") {
@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()); }