* The endpoint's executor executes this method to convert the raw bytes
* into a message, look for an associated exchange and forward it to
* the stack of layers.
*/
private void receiveMessage(RawData raw) {
DataParser parser = new DataParser(raw.getBytes());
if (parser.isRequest()) {
// This is a request
Request request;
try {
request = parser.parseRequest();
} catch (IllegalStateException e) {
String log = "message format error caused by " + raw.getInetSocketAddress();
if (!parser.isReply()) {
// manually build RST from raw information
EmptyMessage rst = new EmptyMessage(Type.RST);
rst.setDestination(raw.getAddress());
rst.setDestinationPort(raw.getPort());
rst.setMID(parser.getMID());
for (MessageInterceptor interceptor:interceptors)
interceptor.sendEmptyMessage(rst);
connector.send(serializer.serialize(rst));
log += " and reseted";
}
LOGGER.info(log);
return;
}
request.setSource(raw.getAddress());
request.setSourcePort(raw.getPort());
/*
* Logging here causes significant performance loss.
* If necessary, add an interceptor that logs the messages,
* e.g., the MessageTracer.
*/
for (MessageInterceptor interceptor:interceptors)
interceptor.receiveRequest(request);
// MessageInterceptor might have canceled
if (!request.isCanceled()) {
Exchange exchange = matcher.receiveRequest(request);
if (exchange != null) {
exchange.setEndpoint(CoAPEndpoint.this);
coapstack.receiveRequest(exchange, request);
}
}
} else if (parser.isResponse()) {
// This is a response
Response response = parser.parseResponse();
response.setSource(raw.getAddress());
response.setSourcePort(raw.getPort());
/*
* Logging here causes significant performance loss.
* If necessary, add an interceptor that logs the messages,
* e.g., the MessageTracer.
*/
for (MessageInterceptor interceptor:interceptors)
interceptor.receiveResponse(response);
// MessageInterceptor might have canceled
if (!response.isCanceled()) {
Exchange exchange = matcher.receiveResponse(response);
if (exchange != null) {
exchange.setEndpoint(CoAPEndpoint.this);
response.setRTT(System.currentTimeMillis() - exchange.getTimestamp());
coapstack.receiveResponse(exchange, response);
}
}
} else if (parser.isEmpty()) {
// This is an empty message
EmptyMessage message = parser.parseEmptyMessage();
message.setSource(raw.getAddress());
message.setSourcePort(raw.getPort());
/*
* Logging here causes significant performance loss.