System.err.println("URI not specified");
System.exit(ERR_MISSING_URI);
}
// create request according to specified method
Request request = newRequest(method);
// set request URI
if (method.equals("DISCOVER") && (uri.getPath() == null || uri.getPath().isEmpty() || uri.getPath().equals("/"))) {
// add discovery resource path to URI
try {
uri = new URI(uri.getScheme(), uri.getAuthority(), DISCOVERY_RESOURCE, uri.getQuery());
} catch (URISyntaxException e) {
System.err.println("Failed to parse URI: " + e.getMessage());
System.exit(ERR_BAD_URI);
}
}
request.setURI(uri);
request.setPayload(payload);
request.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
if (request.getScheme().equals(CoAP.COAP_SECURE_URI_SCHEME)) {
dtlsEndpoint = new CoAPEndpoint(new DTLSConnector(new InetSocketAddress(0)), NetworkConfig.getStandard());
dtlsEndpoint.setMessageDeliverer(new ClientMessageDeliverer());
dtlsEndpoint.start();
EndpointManager.getEndpointManager().setDefaultSecureEndpoint(dtlsEndpoint);
}
// execute request
try {
request.send();
// loop for receiving multiple responses
do {
// receive response
Response response = null;
try {
response = request.waitForResponse();
} catch (InterruptedException e) {
System.err.println("Failed to receive response: " + e.getMessage());
System.exit(ERR_RESPONSE_FAILED);
}