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.stream; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; import io.netty.channel.ChannelHandlerContext; import org.anhonesteffort.chnlzr.CapnpUtil; import org.anhonesteffort.chnlbrkr.ChnlBrkrConfig; import org.anhonesteffort.chnlzr.WriteQueuingContext; import org.anhonesteffort.chnlbrkr.chnlzr.IdleChnlzrConnection; import org.capnproto.MessageBuilder; import static org.anhonesteffort.chnlzr.Proto.ChannelRequest; public class ChannelStreamerFactory { private final ChnlBrkrConfig config; public ChannelStreamerFactory(ChnlBrkrConfig config) { this.config = config; } public ListenableFuture<ChannelStreamer> create(IdleChnlzrConnection connection, WriteQueuingContext client, ChannelRequest.Reader request) { MessageBuilder capabilities = CapnpUtil.builder(connection.getCapabilitiesMessage()); SettableFuture<ChannelRequestHandler> requestFuture = SettableFuture.create(); SettableFuture<ChannelStreamer> streamFuture = SettableFuture.create(); ChannelRequestHandler requestHandler = new ChannelRequestHandler(requestFuture, request, connection.getId(), config.chnlzrRequestAnswerTimeoutMs()); connection.getContext().pipeline().replace(connection, "request", requestHandler); Futures.addCallback(requestFuture, new ChannelRequestCallback(streamFuture, client, capabilities)); return streamFuture; } private static class ChannelRequestCallback implements FutureCallback<ChannelRequestHandler> { private final SettableFuture<ChannelStreamer> future; private final WriteQueuingContext client; private final MessageBuilder capabilities; public ChannelRequestCallback(SettableFuture<ChannelStreamer> future, WriteQueuingContext client, MessageBuilder capabilities) { this.future = future; this.capabilities = capabilities; this.client = client; } @Override public void onSuccess(ChannelRequestHandler requestHandler) { ChannelHandlerContext chnlzr = requestHandler.getContext(); ChannelStreamer streamer = new ChannelStreamer(chnlzr, client, capabilities, requestHandler.getState(), requestHandler.getChnlzrId()); chnlzr.pipeline().replace(requestHandler, "stream", streamer); if (!future.set(streamer)) { chnlzr.close(); } } @Override public void onFailure(Throwable throwable) { future.setException(throwable); } } }