@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));
                }
            }
        }
@Override
        protected void channelRead0(ChannelHandlerContext ctx,
                CoapMessage msg) {
            try {
               
                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) {
                   
                    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));
                }
            }
        }
@override protected void channelread0(channelhandlercontext ctx, coapmessage msg) { try { 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) { 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)); } } }