/** * similar all but map promises to their content * * returns a future which is settled once all promises provided are settled * */ public static <T> IPromise<List<T>> allMapped(List<IPromise<T>> futures) { Promise returned = new Promise(); Promise res = new Promise(); awaitSettle(futures, res); res.then( (r, e) -> { if (r!=null) { returned.resolve(((List<Promise>)r).stream().map(p -> p.get()).collect(Collectors.toList())); } else { returned.complete(null,e); } }); return returned; }
/** * see IPromise (inheriting Callback) interface */ @Override public T await(long timeoutMillis) { awaitPromise(timeoutMillis); return awaitHelper(); }
public static Promise<ActorServer> Publish(Actor facade, int port, Coding coding, Consumer<Actor> disconnectHandler) { Promise finished = new Promise(); try { ActorServer publisher = new ActorServer(new NIOServerConnector(port), facade, coding); facade.execute(() -> { try { publisher.start(disconnectHandler); finished.resolve(publisher); } catch (Exception e) { finished.reject(e); } }); } catch (Exception e) { e.printStackTrace(); return new Promise(null,e); } return finished; }
url = url.substring(0,idx); Promise res = new Promise(); try { if ( cacheContentShortTime > 0 ) { Promise<String> httpResponsePromise = contentCache.get(url); if ( httpResponsePromise != null ) { if ( httpResponsePromise.isSettled() ) { return complete(httpResponsePromise.get(),httpResponsePromise.getError()); } else { try { httpResponsePromise.await(TimeUnit.MINUTES.toMillis(1)); } catch (Exception e) { return complete(httpResponsePromise.get(),httpResponsePromise.getError()); res.reject(err); return; res.reject(response.getStatusLine().getStatusCode()); return; res.resolve(readContentString(response)); } catch (Throwable e) { Log.Warn(this,e); res.reject(e); res.reject(t);
/** * similar to es6 Promise.race method, however non-IPromise objects are not allowed * * returns a future which is settled once one of the futures provided gets settled * */ public static <T> IPromise<T> race( IPromise<T>... futures ) { Promise p = new Promise(); AtomicBoolean fin = new AtomicBoolean(false); for (int i = 0; i < futures.length; i++) { futures[i].then( (r,e) -> { if ( fin.compareAndSet(false,true) ) { p.complete(r, e); } }); } return p; }
public IPromise<T> connect(int qsiz, Consumer<Actor> discon) Promise<T> result = new Promise<>(); try { client.connect( writesocket -> { objectSink.sinkClosed(); }); result.resolve((T) facadeProxy); return objectSink; }); } catch (Exception e) { if ( ! result.isSettled() ) result.reject(e); else e.printStackTrace();
/** * abbreviation for Promise creation to make code more concise * * @param res * @param <T> * @return */ public static <T> IPromise<T> resolve( T res ) { return new Promise<>(res); }
/** * calls given function round robin. typical use: * * hoarde.ordered( actor -> actor.decode(byte[]) ).onResult( decodedObj -> businesslogic(decodedObj) ); * * after * @param toCall * @return */ public IPromise ordered(Function<T, IPromise> toCall) { final IPromise result = toCall.apply((T) actors[index]); index++; if (index==actors.length) index = 0; if ( prev == null ) { prev = new Promise(); result.then(prev); return prev; } else { Promise p = new Promise(); prev.getNext().finallyDo((res, err) -> result.then((res1, err1) -> p.complete(res1, err1))); prev = p; return p; } }
@Override public void failed(Throwable exc, ByteBuffer attachment) { p.reject(exc); } });
/** * see IPromise (inheriting Callback) interface */ public void timedOut( Timeout to ) { if (!hadResult ) { complete(null, to); } }
@Override public void completed(Integer result, ByteBuffer attachment) { // FIXME: how to handle incomplete read. (currently burden on reader) long newPos = position + finalTarget.limit() - bufferStartPos; if (result < 0) newPos = -1; attachment.flip(); p.resolve(new AsyncFileIOEvent(newPos, result, finalTarget)); }
public IPromise connect(String host, int port, BiFunction<SelectionKey,SocketChannel,AsyncSocketConnection> connectionFactory) { if ( connectFuture != null ) { throw new RuntimeException("illegal state, connect is underway"); } connectFuture = new Promise<>(); this.connectionFactory = connectionFactory; try { channel = SocketChannel.open(); channel.configureBlocking(false); selector = Selector.open(); channel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE ); channel.connect(new InetSocketAddress(host, port)); Actor.current().execute(this); } catch (Exception e) { connectFuture.reject(e); connectFuture = null; } return connectFuture; }
private T awaitHelper() { if ( Actor.isError(getError()) ) { if ( getError() instanceof Throwable ) { FSTUtil.<RuntimeException>rethrow((Throwable) getError()); return null; // never reached } else { if ( getError() == Timeout.INSTANCE ) { throw new KTimeoutException(); } throw new AwaitException(getError()); } } else { return get(); } }
int idleCount = 0; dt.__stack.add(this); while( ! isSettled() ) { if ( ! dt.pollQs() ) { idleCount++; idleCount = 0; if ( endtime != 0 && System.currentTimeMillis() > endtime && ! isSettled() ) { timedOut(Timeout.INSTANCE); break; then( (res, err) -> { latch.countDown(); }); timedOut(Timeout.INSTANCE); return this;
public static Promise<ActorServer> Publish(Actor facade, int port, Coding coding, Consumer<Actor> disconnectHandler) { Promise finished = new Promise(); try { ActorServer publisher = new ActorServer(new NIOServerConnector(port), facade, coding); facade.execute(() -> { try { publisher.start(disconnectHandler); finished.resolve(publisher); } catch (Exception e) { finished.reject(e); } }); } catch (Exception e) { e.printStackTrace(); return new Promise(null,e); } return finished; }
/** * similar to es6 Promise.race method, however non-IPromise objects are not allowed * * returns a future which is settled once one of the futures provided gets settled * */ public static <T> IPromise<T> race( IPromise<T>... futures ) { Promise p = new Promise(); AtomicBoolean fin = new AtomicBoolean(false); for (int i = 0; i < futures.length; i++) { futures[i].then( (r,e) -> { if ( fin.compareAndSet(false,true) ) { p.complete(r, e); } }); } return p; }