/** * Returns the effective endpoint that the specified request is supposed to * be sent over. If an endpoint has explicitly been set to this CoapClient, * this endpoint will be used. If no endpoint has been set, the client will * effectively use a default endpoint of the {@link EndpointManager}. * * @param request the request to be sent * @return the effective endpoint that the request is going o be sent over. */ protected Endpoint getEffectiveEndpoint(Request request) { Endpoint myEndpoint = getEndpoint(); // custom endpoint if (myEndpoint != null) return myEndpoint; // default endpoints if (CoAP.COAP_SECURE_URI_SCHEME.equals(request.getScheme())) { // this is the case when secure coap is supposed to be used return EndpointManager.getEndpointManager().getDefaultSecureEndpoint(); } else { // this is the normal case return EndpointManager.getEndpointManager().getDefaultEndpoint(); } }
/** * Add individual endpoints listening on default CoAP port on all IPv4 addresses of all network interfaces. */ private void addEndpoints() { for (InetAddress addr : EndpointManager.getEndpointManager().getNetworkInterfaces()) { // only binds to IPv4 addresses and localhost if (addr instanceof Inet4Address || addr.isLoopbackAddress()) { InetSocketAddress bindToAddress = new InetSocketAddress(addr, COAP_PORT); addEndpoint(new CoapEndpoint(bindToAddress)); } } }
/** * Gets the default endpoint for implicit use by clients. By default, the * endpoint has a single-threaded executor and is started. It is possible to * send requests over the endpoint and receive responses. It is not possible * to receive requests by default. If a request arrives at the endpoint, the * {@link ClientMessageDeliverer} rejects it. To receive requests, the * endpoint must be added to an instance of {@link CoapServer}. Be careful * with stopping or destroying the default endpoint as it affects all * messages that are supposed to be sent over it. * * @return the default endpoint */ public synchronized Endpoint getDefaultEndpoint() { if (default_endpoint == null) { createDefaultEndpoint(); } return default_endpoint; }
/** * Returns the effective endpoint that the specified request is supposed to * be sent over. If an endpoint has explicitly been set to this CoapClient, * this endpoint will be used. If no endpoint has been set, the client will * effectively use a default endpoint of the {@link EndpointManager}. * * @param request the request to be sent * @return the effective endpoint that the request is going o be sent over. */ protected Endpoint getEffectiveEndpoint(Request request) { Endpoint myEndpoint = getEndpoint(); // custom endpoint if (myEndpoint != null) return myEndpoint; // default endpoints if (CoAP.COAP_SECURE_URI_SCHEME.equals(request.getScheme())) { // this is the case when secure coap is supposed to be used return EndpointManager.getEndpointManager().getDefaultSecureEndpoint(); } else if (CoAP.COAP_TCP_URI_SCHEME.equals(request.getScheme())) { // Running over TCP. return EndpointManager.getEndpointManager().getDefaultTcpEndpoint(); } else if (CoAP.COAP_SECURE_TCP_URI_SCHEME.equals(request.getScheme())) { // Running over TLS. return EndpointManager.getEndpointManager().getDefaultSecureTcpEndpoint(); } else { // this is the normal case return EndpointManager.getEndpointManager().getDefaultEndpoint(); } }
@After public void shutdownServer() { EndpointManager.getEndpointManager().getDefaultEndpoint().removeInterceptor(interceptor); server.destroy(); System.out.printf("End %s", getClass().getSimpleName()); }
/** * Clear the state for deduplication in both default endpoints. */ public static void clear() { EndpointManager it = getEndpointManager(); if (it.default_endpoint != null) it.default_endpoint.clear(); if (it.default_secure_endpoint != null) it.default_secure_endpoint.clear(); }
EndpointManager.getEndpointManager().setDefaultEndpoint(cocoaEndpoint);
EndpointManager.getEndpointManager().setDefaultSecureEndpoint(dtlsEndpoint);
@Before public void initLogger() { System.out.println(System.lineSeparator() + "Start " + getClass().getSimpleName()); EndpointManager.clear(); }
EndpointManager.getEndpointManager().getDefaultEndpoint().addInterceptor(new MessageTracer());
/** * Reset default endpoints. Destroy all default endpoints. */ public static void reset() { EndpointManager it = getEndpointManager(); if (it.default_endpoint != null) { it.default_endpoint.destroy(); it.default_endpoint = null; } if (it.default_secure_endpoint != null) { it.default_secure_endpoint.destroy(); it.default_secure_endpoint = null; } }
@Before public void setupServers() throws Exception { System.out.println("\nStart "+getClass().getSimpleName()); EndpointManager.clear();
/** * Sends the request over the default endpoint to its destination and * expects a response back. * * @return this request * @throws NullPointerException if this request has no destination set. */ public Request send() { validateBeforeSending(); if (CoAP.COAP_SECURE_URI_SCHEME.equals(getScheme())) { // This is the case when secure coap is supposed to be used EndpointManager.getEndpointManager().getDefaultSecureEndpoint().sendRequest(this); } else { // This is the normal case EndpointManager.getEndpointManager().getDefaultEndpoint().sendRequest(this); } return this; }
EndpointManager.getEndpointManager().getDefaultEndpoint().addInterceptor(interceptor);
/** * Clear the state for deduplication in both default endpoints. */ public static void clear() { EndpointManager it = getEndpointManager(); if (it.default_endpoint != null) it.default_endpoint.clear(); if (it.default_secure_endpoint != null) it.default_secure_endpoint.clear(); if (it.default_tcp_endpoint != null) it.default_tcp_endpoint.clear(); if (it.default_secure_tpc_endpoint != null) it.default_secure_tpc_endpoint.clear(); }
public void addEndpoints() { for (InetAddress addr : EndpointManager.getEndpointManager().getNetworkInterfaces()) { // only binds to IPv4 addresses and localhost if (addr instanceof Inet4Address || addr.isLoopbackAddress()) { System.out.println("addr: "+addr.toString()); InetSocketAddress bindToAddress = new InetSocketAddress(addr, port); System.out.println("bindToAddress: "+bindToAddress.toString()); addEndpoint(new CoapEndpoint(bindToAddress)); } } }
@BeforeClass public static void setupServer() { System.out.println(System.lineSeparator() + "Start " + MessageTypeTest.class.getSimpleName()); EndpointManager.clear(); CoapEndpoint endpoint = new CoapEndpoint(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); server = new CoapServer(); server.addEndpoint(endpoint); server.add(new CoapResource(ACC_RESOURCE) { public void handlePOST(CoapExchange exchange) { exchange.accept(); System.out.println("gotit"); exchange.respond(SERVER_RESPONSE); } }); server.add(new CoapResource(NO_ACC_RESOURCE) { public void handlePOST(CoapExchange exchange) { exchange.respond(SERVER_RESPONSE); } }); server.start(); serverPort = endpoint.getAddress().getPort(); }
/** * Gets the default endpoint for implicit use by clients. By default, the * endpoint has a single-threaded executor and is started. It is possible to * send requests over the endpoint and receive responses. It is not possible * to receive requests by default. If a request arrives at the endpoint, the * {@link ClientMessageDeliverer} rejects it. To receive requests, the * endpoint must be added to an instance of {@link CoapServer}. Be careful with * stopping or destroying the default endpoint as it affects all messages * that are supposed to be sent over it. * * @return the default endpoint */ public synchronized Endpoint getDefaultEndpoint() { if (default_endpoint == null) { createDefaultEndpoint(); } return default_endpoint; }
/** * Sends the request over the default endpoint to its destination and * expects a response back. * @return this request */ public Request send() { validateBeforeSending(); if (CoAP.COAP_SECURE_URI_SCHEME.equals(getScheme())) { // This is the case when secure coap is supposed to be used EndpointManager.getEndpointManager().getDefaultSecureEndpoint().sendRequest(this); } else { // This is the normal case EndpointManager.getEndpointManager().getDefaultEndpoint().sendRequest(this); } return this; }
/** * Reset default endpoints. Destroy all default endpoints and clear their * set. */ public static void reset() { EndpointManager it = getEndpointManager(); if (it.default_endpoint != null) { it.default_endpoint.destroy(); it.default_endpoint = null; } if (it.default_secure_endpoint != null) { it.default_secure_endpoint.destroy(); it.default_secure_endpoint = null; } if (it.default_tcp_endpoint != null) { it.default_tcp_endpoint.destroy(); it.default_tcp_endpoint = null; } if (it.default_secure_tpc_endpoint != null) { it.default_secure_tpc_endpoint.destroy(); it.default_secure_tpc_endpoint = null; } }