Package net.hasor.rsf.runtime.server

Source Code of net.hasor.rsf.runtime.server.ServerHandler

/*
* 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.runtime.server;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import net.hasor.rsf.general.ProtocolStatus;
import net.hasor.rsf.net.netty.NetworkChanne;
import net.hasor.rsf.protocol.message.RequestMsg;
import net.hasor.rsf.protocol.message.ResponseMsg;
import net.hasor.rsf.protocol.toos.TransferUtils;
import net.hasor.rsf.runtime.RsfContext;
/**
* 提供服务的Handler(只处理RequestMsg)
* @version : 2014年11月4日
* @author 赵永春(zyc@hasor.net)
*/
public class ServerHandler extends ChannelInboundHandlerAdapter {
    public static final AttributeKey<NetworkChanne> NetworkChanneKey = new AttributeKey<NetworkChanne>("NetworkChanne");
    private RsfContext                              rsfContext       = null;
    //
    public ServerHandler(RsfContext rsfContext) {
        this.rsfContext = rsfContext;
    }
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        if (channel.attr(NetworkChanneKey).get() == null)
            channel.attr(NetworkChanneKey).set(new NetworkChanne(channel));
        super.channelActive(ctx);
    }
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof RequestMsg == false)
            return;
        RequestMsg requestMsg = (RequestMsg) msg;
        requestMsg.setReceiveTime(System.currentTimeMillis());
        //
        try {
            Executor exe = this.rsfContext.getCallExecute(requestMsg.getServiceName());
            exe.execute(new InnerInvokeHandler(this.rsfContext, requestMsg, ctx.channel()));
            //
            ResponseMsg pack = TransferUtils.buildStatus(//
                    requestMsg.getVersion(), //协议版本
                    requestMsg.getRequestID(),//请求ID
                    ProtocolStatus.Accepted);//回应ACK
            ctx.pipeline().writeAndFlush(pack);
        } catch (RejectedExecutionException e) {
            ResponseMsg pack = TransferUtils.buildStatus(//
                    requestMsg.getVersion(), //协议版本
                    requestMsg.getRequestID(),//请求ID
                    ProtocolStatus.ChooseOther);//服务器资源紧张
            ctx.pipeline().writeAndFlush(pack);
        }
    }
}
TOP

Related Classes of net.hasor.rsf.runtime.server.ServerHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.