use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class BasicTest method allTest.
@Test
public void allTest() throws InterruptedException {
ArrayList<IPromise<Object>> al = new ArrayList();
al.add(new Promise());
al.add(new Promise());
new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
al.get(0).resolve();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
al.get(1).resolve();
}).start();
Actors.all(al).await();
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class AsyncSocketConnection method writeFinished.
// error = null => ok
void writeFinished(Object error) {
checkThread();
writingBuffer = null;
Promise wp = this.writePromise;
writePromise = null;
if (!wp.isSettled()) {
if (error != null)
wp.reject(error);
else
wp.resolve();
}
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class TicketMachine method getTicket.
public IPromise<IPromise> getTicket(final Object channelKey) {
List<Ticket> futures = tickets.get(channelKey);
if (futures == null) {
futures = new ArrayList<>(3);
tickets.put(channelKey, futures);
}
Promise<Object> signalFin = new Promise<>();
IPromise signalStart = new Promise();
final Ticket ticket = new Ticket(signalStart, signalFin);
futures.add(ticket);
// System.out.println("get ticket "+ticket+" "+Thread.currentThread().getName());
final List<Ticket> finalFutures = futures;
signalFin.then(new Callback() {
@Override
public void complete(Object result, Object error) {
// System.out.println("rec "+channelKey+" do remove+checknext");
boolean remove = finalFutures.remove(ticket);
if (!remove)
System.err.println("Error failed to remove " + channelKey);
checkNext(channelKey, finalFutures, ticket);
}
});
if (futures.size() == 1) {
// this is the one and only call, start immediately
signalStart.complete(signalFin, null);
}
return signalStart;
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class NIOServerConnector method Publish.
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;
}
use of org.nustaq.kontraktor.Promise in project kontraktor by RuedigerMoeller.
the class HttpMonitor method getMonitorables.
protected IPromise<Object[]> getMonitorables(int depth, Monitorable monitorable) {
// System.out.println("dumpmon " + monitorable);
Promise p = new Promise();
monitorable.$getReport().then((report, err) -> {
Object[] result = new Object[2];
result[0] = report;
monitorable.$getSubMonitorables().then((monitorables, errmon) -> {
if (monitorables.length > 0 && depth >= 1) {
Object[] subResult = new Object[monitorables.length];
result[1] = subResult;
IPromise[] futs = new IPromise[monitorables.length];
for (int i = 0; i < monitorables.length; i++) {
Monitorable submon = monitorables[i];
futs[i] = getMonitorables(depth - 1, submon);
}
all(futs).then((futArr, err0) -> {
IPromise[] futures = (IPromise[]) futArr;
for (int i = 0; i < futures.length; i++) {
IPromise future = futures[i];
subResult[i] = future.get();
}
p.settle(result, null);
});
} else
p.settle(result, null);
});
});
return p;
}
Aggregations