use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.
the class AsyncFile method read.
public IPromise<AsyncFileIOEvent> read(long position, int chunkSize, ByteBuffer target) {
if (fileChannel == null)
throw new RuntimeException("file not opened");
Actor sender = Actor.current();
Promise p = new Promise();
if (target == null) {
target = ByteBuffer.allocate(chunkSize);
}
final long bufferStartPos = target.position();
final ByteBuffer finalTarget = target;
fileChannel.read(target, position, target, new CompletionHandler<Integer, ByteBuffer>() {
@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));
}
@Override
public void failed(Throwable exc, ByteBuffer attachment) {
p.reject(exc);
}
});
return p;
}
use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.
the class AsyncServerSocket method receiveLoop.
public void receiveLoop() {
Actor actor = Actor.current();
if (t == null)
t = Thread.currentThread();
else {
if (t != Thread.currentThread()) {
System.out.println("FATAL");
System.exit(-1);
}
}
boolean hadStuff = false;
int iterCount = 10;
do {
try {
selector.selectNow();
Set<SelectionKey> selectionKeys = selector.selectedKeys();
for (Iterator<SelectionKey> iterator = selectionKeys.iterator(); iterator.hasNext(); ) {
SelectionKey key = iterator.next();
try {
if (key == serverkey) {
if (key.isAcceptable()) {
SocketChannel accept = socket.accept();
if (accept != null) {
hadStuff = true;
accept.configureBlocking(false);
SelectionKey newKey = accept.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
AsyncSocketConnection con = connectionFactory.apply(key, accept);
newKey.attach(con);
}
}
} else {
SocketChannel client = (SocketChannel) key.channel();
int written = 0;
if (key.isWritable()) {
AsyncSocketConnection con = (AsyncSocketConnection) key.attachment();
ByteBuffer writingBuffer = con.getWritingBuffer();
if (writingBuffer != null) {
hadStuff = true;
try {
written = con.chan.write(writingBuffer);
if (written < 0) {
iterator.remove();
key.cancel();
// closed
con.writeFinished("disconnected");
} else if (writingBuffer.remaining() == 0) {
iterator.remove();
con.writeFinished(null);
}
} catch (IOException ioe) {
iterator.remove();
key.cancel();
con.writeFinished("disconnected");
}
}
}
if (key.isReadable() && written == 0) {
iterator.remove();
AsyncSocketConnection con = (AsyncSocketConnection) key.attachment();
if (con == null || con.isClosed()) {
Log.Lg.warn(this, "con is null " + key);
} else {
hadStuff = true;
try {
if (!con.readData()) {
// yield ?
}
} catch (Exception ioe) {
if (ioe instanceof EOFException) {
Log.Info(this, "" + ioe);
} else {
Log.Info(this, ioe);
}
con.closed(ioe);
key.cancel();
try {
client.close();
} catch (IOException e) {
Log.Warn(this, e);
}
}
}
}
}
} catch (Throwable e) {
Log.Warn(this, e, "");
}
}
} catch (Throwable e) {
Log.Warn(this, e, "");
Actors.reject(e);
}
} while (iterCount-- > 0 && hadStuff);
if (!isClosed()) {
if (hadStuff) {
actor.execute(() -> receiveLoop());
} else {
actor.delayed(1, () -> receiveLoop());
}
} else {
// close open connections
try {
selector.selectNow();
Actors.SubmitDelayed(TCPServerConnector.DELAY_MS_TILL_CLOSE, () -> {
selector.selectedKeys().forEach(key -> {
try {
key.channel().close();
} catch (IOException e) {
Log.Warn(this, e);
}
});
});
} catch (IOException e) {
Log.Warn(this, e);
}
}
}
use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.
the class UntypedSampleServiceClient method main.
public static void main(String[] args) {
Actor remote = (Actor) new WebSocketConnectable().actorClass(Actor.class).url("ws://localhost:3998").serType(SerializerType.JsonNoRef).connect((x, y) -> System.out.println("disconnect " + x + " " + y)).await();
remote.ask("withPromise", "Hello").then((r, e) -> System.out.println(r + " " + e));
remote.tell("withCallback", "Hi", (Callback) (r, e) -> System.out.println("callback:" + r + " " + e));
remote.ask("withCallbackAndPromise", "Hi CB+P ", (Callback) (r, e) -> System.out.println("callback:" + r + " " + e)).then((r, e) -> System.out.println(r + " " + e));
remote.ask("getSingletonSubservice", "lol").then((subremote, err) -> {
Actor subrem = (Actor) subremote;
subrem.tell("voidFun");
subrem.ask("withCallbackAndPromise", "Hi sub", (Callback) (r, e) -> System.out.println("from sub" + r + " " + e));
});
}
use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.
the class HoardeTest method testGenericAct.
private void testGenericAct(ArrayList toEncode, int threads, int max, boolean verify) throws InterruptedException {
int actcount = 0;
Hoarde<DecAct> oc = new Hoarde<DecAct>(threads, DecAct.class);
oc.each(dec -> dec.init());
AtomicInteger count = new AtomicInteger(0);
AtomicInteger lastVal = new AtomicInteger(-1);
while (actcount < max) {
long tim = System.currentTimeMillis();
for (int i = 0; i < toEncode.size(); i++) {
HashMap<Object, Object> toEnc = (HashMap<Object, Object>) toEncode.get(i);
oc.ordered(act -> {
return act.decode(toEnc);
}).then((r, e) -> {
count.incrementAndGet();
if (verify) {
HashMap o = (HashMap) FSTConfiguration.getDefaultConfiguration().asObject((byte[]) r);
Integer value = (Integer) ((Object[]) o.get("VALUE"))[0];
if (lastVal.get() != value.intValue() - 1)
System.out.println("ERROR " + value + " " + lastVal);
lastVal.set(value.intValue());
}
});
}
while (count.get() < 1000000) {
LockSupport.parkNanos(1000 * 1000);
}
System.out.println("time Actor: " + (System.currentTimeMillis() - tim) + " " + count.get());
count.set(0);
actcount++;
}
oc.each((act) -> act.stop());
}
use of org.nustaq.kontraktor.Actor in project kontraktor by RuedigerMoeller.
the class HttpMonitor method getMonitorableKeys.
public IPromise<String[]> getMonitorableKeys(String simpleClzName) {
ArrayList<String> result = new ArrayList();
monitored.entrySet().forEach((entry) -> {
Monitorable mon = entry.getValue();
if (mon instanceof Actor)
mon = ((Actor) mon).getActor();
if (entry.getValue() != null && mon.getClass().getSimpleName().equals(simpleClzName)) {
result.add(entry.getKey());
}
});
String[] res = new String[result.size()];
result.toArray(res);
return new Promise(res);
}
Aggregations