
io.datakernel.csp.ChannelConsumers Maven / Gradle / Ivy
Show all versions of datakernel-csp Show documentation
/*
* Copyright (C) 2015-2019 SoftIndex LLC.
*
* 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 io.datakernel.csp;
import io.datakernel.bytebuf.ByteBuf;
import io.datakernel.common.Recyclable;
import io.datakernel.common.exception.UncheckedException;
import io.datakernel.eventloop.Eventloop;
import io.datakernel.promise.Promise;
import io.datakernel.promise.SettablePromise;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import static io.datakernel.common.Recyclable.deepRecycle;
/**
* Provides additional functionality for managing {@link ChannelConsumer}s.
*/
@SuppressWarnings("WeakerAccess")
public final class ChannelConsumers {
/**
* Passes iterator's values to the {@code output} until it {@code hasNext()},
* then returns a promise of {@code null} as a marker of completion.
*
* If there was an exception while accepting iterator, a promise of
* exception will be returned.
*
* @param output a {@code ChannelConsumer}, which accepts the iterator
* @param it an {@link Iterator} which provides some values
* @param a data type of passed values
* @return a promise of {@code null} as a marker of completion
*/
public static Promise acceptAll(ChannelConsumer output, Iterator extends T> it) {
if (!it.hasNext()) return Promise.complete();
return Promise.ofCallback(cb -> acceptAllImpl(output, it, cb));
}
private static void acceptAllImpl(ChannelConsumer output, Iterator extends T> it, SettablePromise cb) {
while (it.hasNext()) {
Promise accept = output.accept(it.next());
if (accept.isResult()) continue;
accept.whenComplete(($, e) -> {
if (e == null) {
acceptAllImpl(output, it, cb);
} else {
deepRecycle(it);
cb.setException(e);
}
});
return;
}
cb.set(null);
}
public static ChannelConsumer recycling() {
return new RecyclingChannelConsumer<>();
}
public static ChannelConsumer outputStreamAsChannelConsumer(Executor executor, OutputStream outputStream) {
return new AbstractChannelConsumer() {
@Override
protected Promise doAccept(@Nullable ByteBuf buf) {
return Promise.ofBlockingRunnable(executor, () -> {
try {
if (buf != null) {
outputStream.write(buf.array(), buf.head(), buf.readRemaining());
buf.recycle();
} else {
outputStream.close();
}
} catch (IOException e) {
throw new UncheckedException(e);
}
});
}
@Override
protected void onClosed(@NotNull Throwable e) {
executor.execute(() -> {
try {
outputStream.close();
} catch (IOException ignored) {
}
});
}
};
}
public static OutputStream channelConsumerAsOutputStream(Eventloop eventloop, ChannelConsumer channelConsumer) {
return new OutputStream() {
@Override
public void write(int b) throws IOException {
write(new byte[]{(byte) b}, 0, 1);
}
@Override
public void write(@NotNull byte[] b, int off, int len) throws IOException {
submit(ByteBuf.wrap(b, off, off + len));
}
@Override
public void close() throws IOException {
submit(null);
}
private void submit(ByteBuf buf) throws IOException {
CompletableFuture future = eventloop.submit(() -> channelConsumer.accept(buf));
try {
future.get();
} catch (InterruptedException e) {
eventloop.execute(channelConsumer::cancel);
throw new IOException(e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof IOException) throw (IOException) cause;
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
if (cause instanceof Exception) throw new IOException(cause);
throw (Error) cause;
}
}
};
}
}