// TODO: Re-architecturing required
@Override
        protected void channelRead0(ChannelHandlerContext ctx,
                CoapMessage msg) {
            try {
                // Find proper device and raise event.
                Device targetDevice = ctx.channel().attr(keyDevice).get();
                if (targetDevice == null) {
                    throw new InternalServerErrorException(
                            "Unable to find device");
                }
                if (msg instanceof CoapRequest) {
                    onRequestReceived(targetDevice, (CoapRequest) msg);
                } else if (msg instanceof CoapResponse) {
                    // TODO: Re-architecturing required
                    IRequestChannel reqChannel = ((CoapDevice) targetDevice)
                            .getRequestChannel();
                    CoapClient coapClient = (CoapClient) reqChannel;
                    coapClient.onResponseReceived(msg);
                }
            } catch (ServerException e) {
                ctx.writeAndFlush(MessageBuilder.createResponse(msg,
                        e.getErrorResponse()));
                Log.f(ctx.channel(), e);
            } catch (ClientException e) {
                Log.f(ctx.channel(), e);
            } catch (Throwable t) {
                Log.f(ctx.channel(), t);
                if (msg instanceof CoapRequest) {
                    ctx.writeAndFlush(MessageBuilder.createResponse(msg,
                            ResponseStatus.INTERNAL_SERVER_ERROR));
                }
            }
        }
onRequestReceived(targetDevice, (CoapRequest) msg);
} else if (msg instanceof CoapResponse) {
// TODO: Re-architecturing required
IRequestChannel reqChannel = ((CoapDevice) targetDevice)
.getRequestChannel();
try {
// Find proper device and raise event.
Device targetDevice = ctx.channel().attr(keyDevice).get();
if (targetDevice == null) {
throw new InternalServerErrorException(
"Unable to find device");
}
if (msg instanceof CoapRequest) {
onRequestReceived(targetDevice, (CoapRequest) msg);
} else if (msg instanceof CoapResponse) {
// TODO: Re-architecturing required
IRequestChannel reqChannel = ((CoapDevice) targetDevice)
.getRequestChannel();
CoapClient coapClient = (CoapClient) reqChannel;
coapClient.onResponseReceived(msg);
}
} catch (ServerException e) {
ctx.writeAndFlush(MessageBuilder.createResponse(msg,
e.getErrorResponse()));
Log.f(ctx.channel(), e);
} catch (ClientException e) {
@Override
protected void channelRead0(ChannelHandlerContext ctx,
CoapMessage msg) {
try {
// Find proper device and raise event.
Device targetDevice = ctx.channel().attr(keyDevice).get();
if (targetDevice == null) {
throw new InternalServerErrorException(
"Unable to find device");
}
if (msg instanceof CoapRequest) {
onRequestReceived(targetDevice, (CoapRequest) msg);
} else if (msg instanceof CoapResponse) {
// TODO: Re-architecturing required
IRequestChannel reqChannel = ((CoapDevice) targetDevice)
.getRequestChannel();
CoapClient coapClient = (CoapClient) reqChannel;
coapClient.onResponseReceived(msg);
}
} catch (ServerException e) {
ctx.writeAndFlush(MessageBuilder.createResponse(msg,
e.getErrorResponse()));
Log.f(ctx.channel(), e);
} catch (ClientException e) {
Log.f(ctx.channel(), e);
} catch (Throwable t) {
Log.f(ctx.channel(), t);
if (msg instanceof CoapRequest) {
ctx.writeAndFlush(MessageBuilder.createResponse(msg,
ResponseStatus.INTERNAL_SERVER_ERROR));
}
}
}