net.dongliu.prettypb.rpc.client.ClientConnectResponseHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prettypb-rpc Show documentation
Show all versions of prettypb-rpc Show documentation
proto rpc libs, compatible with proto-rpc-pro
/**
* 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 #waitResponse(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);
private CountDownLatch countDownLatch = new CountDownLatch(1);
private volatile ConnectResponse connectResponse;
public ConnectResponse waitResponse(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