All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.prettypb.rpc.client.ClientConnectResponseHandler Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
/**
 *   Copyright 2010-2014 Peter Klauser
 *
 *   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.dongliu.prettypb.rpc.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import net.dongliu.prettypb.rpc.protocol.ConnectResponse;
import net.dongliu.prettypb.rpc.protocol.WirePayload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;


/**
 * The ClientConnectResponseHandler waits for a ConnectResponse
 * from the server's {@link net.dongliu.prettypb.rpc.server.RequestHandler} and
 * supplies this to the DuplexTcpClientBootstrap who
 * calls the {@link #blockGetConnectResponse(long)}.
 *
 * Once the server's ConnectResponse has been made, this handler
 * is removed from the Channel pipeline and replaced with the
 * {@link net.dongliu.prettypb.rpc.client.RpcClientHandler} and {@link net.dongliu.prettypb.rpc.server.RpcServerHandler}.
 *
 * @author Peter Klauser
 */
public class ClientConnectResponseHandler extends MessageToMessageDecoder {

    private static Logger logger = LoggerFactory.getLogger(ClientConnectResponseHandler.class);

    CountDownLatch countDownLatch = new CountDownLatch(1);

    private volatile ConnectResponse connectResponse;

    public ConnectResponse blockGetConnectResponse(long timeout)
            throws TimeoutException, InterruptedException {
        if (!countDownLatch.await(timeout, TimeUnit.MILLISECONDS)) {
            throw new TimeoutException("wait connect response timeout");
        }
        return connectResponse;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, WirePayload msg,
                          List out) throws Exception {
        if (msg.hasConnectResponse()) {
            this.connectResponse = msg.getConnectResponse();
            logger.debug("Received ConnectResponse({})", connectResponse.getCorrelationId());
            countDownLatch.countDown();
        } else {
            out.add(msg);
        }

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        this.connectResponse = null;
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        super.exceptionCaught(ctx, cause);
        logger.warn("Exception caught during RPC connection handshake.", cause);
        ctx.close();
    }

}