Java tutorial
/* * Copyright 2008-2009 the original (zyc@hasor.net). * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.hasor.rsf.remoting.transport.provider; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import java.util.concurrent.Executor; import java.util.concurrent.RejectedExecutionException; import net.hasor.rsf.adapter.AbstractRsfContext; import net.hasor.rsf.constants.ProtocolStatus; import net.hasor.rsf.remoting.transport.connection.NetworkConnection; import net.hasor.rsf.remoting.transport.protocol.message.RequestMsg; import net.hasor.rsf.remoting.transport.protocol.message.ResponseMsg; import net.hasor.rsf.utils.TransferUtils; import org.more.logger.LoggerHelper; /** * ? RSF ??? request/response * @version : 2014114 * @author (zyc@hasor.net) */ public class RsfProviderHandler extends ChannelInboundHandlerAdapter { private AbstractRsfContext rsfContext; private String serializeType; // public RsfProviderHandler(AbstractRsfContext rsfContext) { this.rsfContext = rsfContext; this.serializeType = rsfContext.getSettings().getDefaultSerializeType(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof RequestMsg == false) { return; } //request?response RequestMsg requestMsg = (RequestMsg) msg; requestMsg.setReceiveTime(System.currentTimeMillis()); LoggerHelper.logFinest("received request(%s) full = %s", requestMsg.getRequestID(), requestMsg); // try { Executor exe = this.rsfContext.getCallExecute(requestMsg.getServiceName()); NetworkConnection conn = NetworkConnection.getConnection(ctx.channel()); exe.execute(new InnerRequestHandler(this.rsfContext, requestMsg, conn)); // ResponseMsg pack = TransferUtils.buildStatus(// requestMsg.getVersion(), //?? requestMsg.getRequestID(), //ID ProtocolStatus.Accepted, //?? this.serializeType, //? this.rsfContext.getSettings().getServerOption());//? ctx.pipeline().writeAndFlush(pack); } catch (RejectedExecutionException e) { ResponseMsg pack = TransferUtils.buildStatus(// requestMsg.getVersion(), //?? requestMsg.getRequestID(), //ID ProtocolStatus.ChooseOther, //?? this.serializeType, //? this.rsfContext.getSettings().getServerOption());//? ctx.pipeline().writeAndFlush(pack); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.channel().close(); NetworkConnection conn = NetworkConnection.getConnection(ctx.channel()); if (conn != null) { LoggerHelper.logSevere("exceptionCaught, host = %s. , msg = %s.", conn.getHostAddress(), cause.getMessage()); this.rsfContext.getRequestManager().getClientManager().unRegistered(conn.getHostAddress()); } } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { ctx.channel().close(); NetworkConnection conn = NetworkConnection.getConnection(ctx.channel()); if (conn != null) { LoggerHelper.logInfo("remote close, host = %s.", conn.getHostAddress()); this.rsfContext.getRequestManager().getClientManager().unRegistered(conn.getHostAddress()); } } }