use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class CompressionChunkingHandle method process.
@Override
public XFuture<StreamWriter> process(Http2Response response) {
if (lastResponseSent != null)
throw new IllegalStateException("You already sent a response. " + "do not call Actions.redirect or Actions.render more than once. previous response=" + lastResponseSent + " 2nd response=" + response);
lastResponseSent = response;
Compression compression = checkForCompression(response);
ChunkedStream chunkedStream = new ChunkedStream(webSettings.getMaxBodySizeToSend());
Http2Response finalResp = response;
if (Current.isContextSet()) {
// in some exceptional cases like incoming cookies failing to parse, there will be no context
List<OverwritePlatformResponse> callbacks = Current.getContext().getCallbacks();
for (OverwritePlatformResponse callback : callbacks) {
finalResp = (Http2Response) callback.modifyOrReplace(finalResp);
}
}
boolean closeAfterResponding = false;
if (closeAfterResponding(originalRequest))
closeAfterResponding = true;
boolean shouldClose = closeAfterResponding;
return handler.process(response).thenApply(w -> possiblyClose(shouldClose, response, w)).thenApply(w -> new ProxyStreamWriter(shouldClose, compression, chunkedStream, w));
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class IntegTestClientToEchoServer method write.
private void write(Channel channel, String reason) {
// counter++;
// if(counter % 100000 == 0)
// log.info("counter="+counter);
byte[] data = new byte[10240];
ByteBuffer buffer = ByteBuffer.wrap(data);
XFuture<Void> write = channel.write(buffer);
write.thenAccept(p -> write(channel, "wrote data from client")).whenComplete((r, e) -> finished(r, e)).exceptionally(e -> {
logIt(e);
return null;
});
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class BasTCPServerChannel method accept.
/* (non-Javadoc)
* @see api.biz.xsoftware.nio.TCPServerChannel#accept()
*/
public boolean accept(int newSocketNum) throws IOException {
XFuture<Void> future;
try {
// special code...see information in close() method
if (isClosed())
return false;
JdkSocketChannel newChan = channel.accept();
if (newChan == null)
return false;
newChan.configureBlocking(false);
SocketAddress remoteAddress = newChan.getRemoteAddress();
String serverSocketId = id + "." + newSocketNum;
BasTCPChannel tcpChan = new BasTCPChannel(serverSocketId, newChan, remoteAddress, selMgr, router, pool, config);
if (log.isTraceEnabled())
log.trace(tcpChan + "Accepted new incoming connection");
XFuture<DataListener> connectFuture = connectionListener.connected(tcpChan, true);
future = connectFuture.thenCompose(l -> tcpChan.registerForReads(l)).thenApply(c -> null);
} catch (Throwable e) {
future = new XFuture<Void>();
future.completeExceptionally(e);
}
future.exceptionally(t -> {
log.error(this + "Failed to connect", t);
connectionListener.failed(this, t);
return null;
});
return true;
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class TestPermitQueue method testReducePermits.
@Test
public void testReducePermits() throws InterruptedException {
PermitQueue queue = new PermitQueue(2);
XFuture<Long> future1 = new XFuture<Long>();
svc.addToReturn(future1);
svc.addToReturn(future1);
svc.addToReturn(future1);
svc.addToReturn(future1);
queue.runRequest(() -> svc.runFunction(1));
queue.runRequest(() -> svc.runFunction(2));
queue.runRequest(() -> svc.runFunction(3));
queue.runRequest(() -> svc.runFunction(4));
List<Integer> results = svc.getAndClear();
Assert.assertEquals(2, results.size());
Assert.assertEquals(1, results.get(0).intValue());
Assert.assertEquals(2, results.get(1).intValue());
queue.modifyPermitPoolSize(-1);
// release two
queue.releasePermit();
queue.releasePermit();
// only one will run
List<Integer> results2 = svc.getAndClear();
Assert.assertEquals(1, results2.size());
Assert.assertEquals(3, results2.get(0).intValue());
queue.releasePermit();
List<Integer> results3 = svc.getAndClear();
Assert.assertEquals(1, results3.size());
Assert.assertEquals(4, results3.get(0).intValue());
}
use of org.webpieces.util.futures.XFuture in project webpieces by deanhiller.
the class MockChannel method write.
@SuppressWarnings("unchecked")
@Override
public XFuture<Void> write(ByteBuffer b) {
DataWrapper wrapper = dataGen.wrapByteBuffer(b);
List<HttpPayload> parsedData = parser.parse(wrapper);
if (parsedData.size() != 1)
throw new IllegalArgumentException("The impl should be writing out full single payloads each write call");
HttpPayload payload = parsedData.get(0);
return (XFuture<Void>) super.calledMethod(Method.WRITE, payload);
}
Aggregations