org.anhonesteffort.chnlbrkr.stream.ChannelRequestHandler.java Source code

Java tutorial

Introduction

Here is the source code for org.anhonesteffort.chnlbrkr.stream.ChannelRequestHandler.java

Source

/*
 * Copyright (C) 2015 An Honest Effort LLC, coping.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.anhonesteffort.chnlbrkr.stream;

import com.google.common.util.concurrent.SettableFuture;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import org.anhonesteffort.chnlzr.CapnpUtil;
import org.anhonesteffort.chnlzr.ProtocolErrorException;
import org.capnproto.MessageBuilder;

import java.net.ConnectException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeoutException;

import static org.anhonesteffort.chnlzr.Proto.BaseMessage;
import static org.anhonesteffort.chnlzr.Proto.ChannelRequest;

public class ChannelRequestHandler extends ChannelHandlerAdapter {

    private static final Timer timer = new Timer(true);

    private final SettableFuture<ChannelRequestHandler> future;
    private final ChannelRequest.Reader request;
    private final String chnlzrId;
    private final TimerTask timeoutTask;
    private final long timeoutMs;

    private ChannelHandlerContext context;
    private MessageBuilder state;

    public ChannelRequestHandler(SettableFuture<ChannelRequestHandler> future, ChannelRequest.Reader request,
            String chnlzrId, long timeoutMs) {
        this.future = future;
        this.request = request;
        this.chnlzrId = chnlzrId;
        this.timeoutMs = timeoutMs;
        timeoutTask = new TimerTask() {
            @Override
            public void run() {
                if (future.setException(
                        new TimeoutException(chnlzrId + " chnlzr request response timeout exceeded"))) {
                    context.close();
                }
            }
        };
    }

    public String getChnlzrId() {
        return chnlzrId;
    }

    public ChannelHandlerContext getContext() {
        return context;
    }

    public MessageBuilder getState() {
        return state;
    }

    @Override
    public void handlerAdded(ChannelHandlerContext context) {
        this.context = context;
        context.writeAndFlush(CapnpUtil.channelRequest(request));
        timer.schedule(timeoutTask, timeoutMs);
    }

    @Override
    public void channelRead(ChannelHandlerContext context, Object msg) throws ProtocolErrorException {
        BaseMessage.Reader message = (BaseMessage.Reader) msg;

        switch (message.getType()) {
        case CHANNEL_STATE:
            timeoutTask.cancel();
            state = CapnpUtil.builder(message);
            future.set(this);
            break;

        case ERROR:
            timeoutTask.cancel();
            ProtocolErrorException error = new ProtocolErrorException(chnlzrId + " chnlzr sent error",
                    message.getError().getCode());
            if (future.setException(error)) {
                context.close();
            } else {
                throw error;
            }
            break;

        default:
            timeoutTask.cancel();
            IllegalStateException ex = new IllegalStateException(
                    chnlzrId + " chnlzr sent unexpected " + message.getType().name());
            if (future.setException(ex)) {
                context.close();
            } else {
                throw ex;
            }
            break;
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext context, Throwable cause) throws Exception {
        timeoutTask.cancel();
        context.close();

        if (!future.setException(cause)) {
            super.exceptionCaught(context, cause);
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext context) {
        timeoutTask.cancel();
        future.setException(new ConnectException(chnlzrId + " chnlzr connection closed unexpectedly"));
    }

}