/** * Copyright 2013-2015 Seagate Technology LLC. * * This Source Code Form is subject to the terms of the Mozilla * Public License, v. 2.0. If a copy of the MPL was not * distributed with this file, You can obtain one at * https://mozilla.org/MP:/2.0/. * * This program is distributed in the hope that it will be useful, * but is provided AS-IS, WITHOUT ANY WARRANTY; including without * the implied warranty of MERCHANTABILITY, NON-INFRINGEMENT or * FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public * License for more details. * * See www.openkinetic.org for more project information */ package com.seagate.kinetic.client.io.provider.nio.tcp; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import java.util.logging.Level; import java.util.logging.Logger; import com.seagate.kinetic.client.io.provider.spi.ClientMessageService; import com.seagate.kinetic.common.lib.KineticMessage; /** * * @author chiaming * */ public class NioMessageServiceHandler extends SimpleChannelInboundHandler<KineticMessage> { private static final Logger logger = Logger .getLogger(NioMessageServiceHandler.class.getName()); private ClientMessageService mservice = null; public NioMessageServiceHandler(ClientMessageService mservice) { this.mservice = mservice; } @Override protected void channelRead0(ChannelHandlerContext ctx, KineticMessage message) throws Exception { this.mservice.routeMessage(message); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { logger.log(Level.WARNING, "Unexpected exception from downstream.", cause); this.mservice.close(); ctx.close(); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { // close my message service handler this.mservice.close(); // close connection ctx.close(); } }