use of nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes in project mule-coap-connector by teslanet-nl.
the class Client method doRequest.
// TODO add custom timeout
/**
* Issue a request on a CoAP resource residing on a server.
* @param builder Builder containing request parameters.
* @param options The request options.
* @param handlerBuilder Builder containing the name of the handle when the response is handled asynchronously
* @return The result of the request including payload and attributes.
* @throws InternalInvalidHandlerNameException When the handlerName is not null but does not reference an existing handler.
* @throws InternalRequestException When the Request could not be issued.
* @throws InternalResponseException When the received response appears to be invalid and cannot be processed.
* @throws InternalEndpointException When CoAP communication failed.
* @throws InternalInvalidRequestCodeException When the request code has invalid value.
* @throws InternalNoResponseException When timeout has occurred.
* @throws InternalClientErrorResponseException When response indicates client error.
* @throws InternalInvalidResponseCodeException When response indicates other error.
* @throws InternalServerErrorResponseException When response indicates server error.
* @throws InternalUriException When given request uri parameters are invalid.
*/
Result<InputStream, CoAPResponseAttributes> doRequest(RequestParams builder, RequestOptions options, ResponseHandlerParams handlerBuilder) throws InternalInvalidHandlerNameException, InternalRequestException, InternalResponseException, InternalEndpointException, InternalInvalidRequestCodeException, InternalNoResponseException, InternalClientErrorResponseException, InternalInvalidResponseCodeException, InternalServerErrorResponseException, InternalUriException {
Result<InputStream, CoAPResponseAttributes> result = null;
CoapHandler handler = null;
Request request = new CoapRequestBuilderImpl(builder).build();
try {
MessageUtils.copyOptions(options, request.getOptions(), transformationService);
} catch (InternalInvalidOptionValueException e) {
throw new InternalRequestException("cannot process request options", e);
}
if (handlerBuilder != null) {
// asynchronous request
SourceCallback<InputStream, CoAPResponseAttributes> callback;
callback = getHandler(handlerBuilder.getResponseHandler());
handler = createCoapHandler(handlerBuilder.getResponseHandler(), request.getURI(), builder.getRequestCode(), callback);
coapClient.advanced(handler, request);
// nothing to return
} else {
// send out synchronous request
result = executeSynchronous(builder.getRequestCode(), request);
}
return result;
}
use of nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes in project mule-coap-connector by teslanet-nl.
the class Client method startObserver.
/**
* Start observing a resource on a CoAP server.
* @param params The observer parameters.
* @param handlerBuilder The response handler parameters.
* @throws InternalInvalidObserverException When the observer parameters are invalid.
* @throws InternalUriException When the uri parameters of the resource to observe are invalid.
* @throws InternalInvalidHandlerNameException When the handler parameters are invalid.
*/
synchronized void startObserver(ObserverAddParams params, ResponseHandlerParams handlerBuilder) throws InternalInvalidObserverException, InternalUriException, InternalInvalidHandlerNameException {
SourceCallback<InputStream, CoAPResponseAttributes> handler = getHandler(handlerBuilder.getResponseHandler());
CoapRequestBuilderImpl requestBuilder = new CoapRequestBuilderImpl(params);
URI uri = requestBuilder.buildResourceUri();
ObserveRelation relation = getRelation(uri);
if (relation != null) {
// only one observe relation allowed per uri
// TODO proactive or not, configurable?
relation.stop();
removeRelation(uri);
}
relation = new ObserveRelation("CoAP Observer { " + getClientName() + "::" + uri + " }", coapClient, requestBuilder, (requestUri, requestCode, response) -> this.processMuleFlow(requestUri, requestCode, response, handler));
addRelation(uri, relation);
relation.start();
}
use of nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes in project mule-coap-connector by teslanet-nl.
the class ObserveTest method testPermanentObserve.
/**
* Test permanent observe
* @throws Exception should not happen in this test
*/
@Test(timeout = 100000L)
public void testPermanentObserve() throws Exception {
final MuleEventSpy spy = new MuleEventSpy("permanent");
spy.clear();
// let asynchronous work happen
pauze();
for (int i = 1; i < contents.size(); i++) {
pauze();
Event result = flowRunner("do_put_permanent").withPayload(contents.get(i)).run();
Message response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
await().atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size();
});
int obsOffset = 0;
for (int i = 0; i < spy.getEvents().size(); i++) {
Message response = (Message) spy.getEvents().get(i).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
if (i == 0)
obsOffset = attributes.getOptions().getObserve().intValue();
assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload());
assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
}
}
use of nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes in project mule-coap-connector by teslanet-nl.
the class ObserveTest method testTemporaryObserve.
/**
* Test temporary observe
* @throws Exception should not happen in this test
*/
@Test(timeout = 100000L)
public void testTemporaryObserve() throws Exception {
Event result;
Message response;
MuleEventSpy spy = new MuleEventSpy("temporary");
spy.clear();
// let asynchronous work happen
pauze();
assertEquals("unexpected observation on start test", 0, spy.getEvents().size());
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("1st series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
assertEquals("unexpected observation after 1st test series", 0, spy.getEvents().size());
result = flowRunner("start_temporary").withPayload("nothing_important").run();
response = result.getMessage();
await("missing observation start response").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == 1;
});
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("2nd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
await("number of observation after 2nd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size();
});
result = flowRunner("stop_temporary").withPayload("nothing_important").run();
response = result.getMessage();
await("observation stop response").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size() + 1;
});
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("3rd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
await("number of observation after 3rd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size() + 1;
});
int obsOffset = 0;
for (int i = 1; i < contents.size(); i++) {
response = (Message) spy.getEvents().get(i).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
if (i == 1)
obsOffset = attributes.getOptions().getObserve().intValue() - 1;
assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload().getValue());
assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
}
}
use of nl.teslanet.mule.connectors.coap.api.CoAPResponseAttributes in project mule-coap-connector by teslanet-nl.
the class ObserveTest method testTemporaryObserveNon.
/**
* Test temporary observe using NON requests
* @throws Exception should not happen in this test
*/
@Test(timeout = 100000L)
public void testTemporaryObserveNon() throws Exception {
Event result;
Message response;
MuleEventSpy spy = new MuleEventSpy("temporary");
spy.clear();
// let asynchronous work happen
pauze();
assertEquals("unexpected observation on start test", 0, spy.getEvents().size());
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("1st series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
assertEquals("unexpected observation after 1st test series", 0, spy.getEvents().size());
result = flowRunner("start_temporary").withPayload("nothing_important").withVariable("msgType", "NON_CONFIRMABLE").run();
response = result.getMessage();
await("missing observation start response").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == 1;
});
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("2nd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
await("number of observation after 2nd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size();
});
result = flowRunner("stop_temporary").withPayload("nothing_important").withVariable("msgType", "NON_CONFIRMABLE").run();
response = result.getMessage();
await("observation stop response").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size() + 1;
});
for (int i = 1; i < contents.size(); i++) {
pauze();
result = flowRunner("do_put_temporary").withPayload(contents.get(i)).run();
response = result.getMessage();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
assertEquals("3rd series put nr: " + i + " gave wrong response", ResponseCode.CHANGED.name(), attributes.getResponseCode());
}
await("number of observation after 3rd test series").atMost(10, TimeUnit.SECONDS).until(() -> {
return spy.getEvents().size() == contents.size() + 1;
});
int obsOffset = 0;
for (int i = 1; i < contents.size(); i++) {
response = (Message) spy.getEvents().get(i).getContent();
assertTrue("wrong attributes class", response.getAttributes().getValue() instanceof CoAPResponseAttributes);
CoAPResponseAttributes attributes = (CoAPResponseAttributes) response.getAttributes().getValue();
if (i == 1)
obsOffset = attributes.getOptions().getObserve().intValue() - 1;
assertNotEquals("observation nr: " + i + " is empty", null, response.getPayload().getValue());
assertTrue("observation nr: " + i + " indicates failure", attributes.isSuccess());
assertEquals("observation nr: " + i + " has wrong content", contents.get(i), new String((byte[]) response.getPayload().getValue()));
assertEquals("observation nr: " + i + " has wrong observe option", obsOffset + i, attributes.getOptions().getObserve().intValue());
}
}
Aggregations