Java tutorial
/* * 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.chnlzr; import com.google.common.util.concurrent.SettableFuture; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; import org.anhonesteffort.chnlzr.ProtocolErrorException; 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.Capabilities; public class IdleChnlzrConnection extends ChannelHandlerAdapter { private static final Timer timer = new Timer(true); private final SettableFuture<IdleChnlzrConnection> future; private final String id; private final long timeoutMs; private final TimerTask timeoutTask; private ChannelHandlerContext context; private BaseMessage.Reader capabilities; protected IdleChnlzrConnection(SettableFuture<IdleChnlzrConnection> future, String id, long timeoutMs) { this.future = future; this.id = id; this.timeoutMs = timeoutMs; timeoutTask = new TimerTask() { @Override public void run() { if (future.setException(new TimeoutException("chnlzr capabilities receive timeout exceeded"))) { context.close(); } } }; } public String getId() { return id; } public ChannelHandlerContext getContext() { return context; } public Capabilities.Reader getCapabilities() { return capabilities.getCapabilities(); } public BaseMessage.Reader getCapabilitiesMessage() { return capabilities; } @Override public void handlerAdded(ChannelHandlerContext context) { this.context = context; timer.schedule(timeoutTask, timeoutMs); } @Override public void channelRead(ChannelHandlerContext context, Object msg) throws ProtocolErrorException { BaseMessage.Reader chnlzrMsg = (BaseMessage.Reader) msg; switch (chnlzrMsg.getType()) { case CAPABILITIES: timeoutTask.cancel(); capabilities = chnlzrMsg; future.set(this); break; case ERROR: timeoutTask.cancel(); ProtocolErrorException error = new ProtocolErrorException("chnlzr sent error", chnlzrMsg.getError().getCode()); if (future.setException(error)) { context.close(); } else { throw error; } break; default: timeoutTask.cancel(); IllegalStateException ex = new IllegalStateException( "chnlzr sent unexpected " + chnlzrMsg.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("chnlzr connection closed unexpectedly")); } }