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.
* If necessary, add an interceptor that logs the messages,
* e.g., the MessageTracer.
*/
for (MessageInterceptor interceptor:interceptors)
interceptor.receiveEmptyMessage(message);
// MessageInterceptor might have canceled
if (!message.isCanceled()) {
// CoAP Ping
if (message.getType() == Type.CON || message.getType() == Type.NON) {
EmptyMessage rst = EmptyMessage.newRST(message);
LOGGER.info("Responding to ping by " + raw.getInetSocketAddress());
for (MessageInterceptor interceptor:interceptors)
interceptor.sendEmptyMessage(rst);