public void open(Path file, OpenOption... options) throws IOException { if ( fileChannel != null ) throw new RuntimeException("can only open once"); Actor sender = Actor.current(); Set<OpenOption> set = new HashSet<OpenOption>(options.length); Collections.addAll(set, options); fileChannel = AsynchronousFileChannel.open(file, set, new ActorExecutorService(sender), NO_ATTRIBUTES); }
public void open(Path file, OpenOption... options) throws IOException { if ( fileChannel != null ) throw new RuntimeException("can only open once"); Actor sender = Actor.current(); Set<OpenOption> set = new HashSet<OpenOption>(options.length); Collections.addAll(set, options); fileChannel = AsynchronousFileChannel.open(file, set, new ActorExecutorService(sender), NO_ATTRIBUTES); }
public void sendBinary(byte[] message) { if ( DumpProtocol ) { System.out.println("requ:"); System.out.println(new String(message,0)); } Actor executor = Actor.current(); session.getAsyncRemote().sendBinary(ByteBuffer.wrap(message), new SendHandler() { @Override public void onResult(SendResult result) { if ( ! result.isOK() ) { executor.execute( () -> { FSTUtil.<RuntimeException>rethrow(result.getException()); try { close(); } catch (IOException e) { Log.Warn(this,e); } }); } } }); }
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; }
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; }
public void run() { pollThread = Thread.currentThread(); if ( underway ) return; underway = true; try { int count = 1; while( count > 0 ) { // as long there are messages, keep sending them count = onePoll(); if ( sendJobs.size() > 0 ) { if ( count > 0 ) { int debug =1; } else { if ( remoteRefCounter == 0 ) // no remote actors registered { Actor.current().delayed(100, this); // backoff massively } else { Actor.current().delayed(1, this); // backoff a bit (remoteactors present, no messages) } } } else { // no schedule entries (== no clients) Actor.current().delayed(100, this ); } } } finally { underway = false; } }
public void run() { pollThread = Thread.currentThread(); if ( underway ) return; underway = true; try { int count = 1; while( count > 0 ) { // as long there are messages, keep sending them count = onePoll(); if ( sendJobs.size() > 0 ) { if ( count > 0 ) { int debug =1; } else { if ( remoteRefCounter == 0 ) // no remote actors registered { Actor.current().delayed(100, this); // backoff massively } else { Actor.current().delayed(1, this); // backoff a bit (remoteactors present, no messages) } } } else { // no schedule entries (== no clients) Actor.current().delayed(100, this ); } } } finally { underway = false; } }
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; }
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; }
/** * return a future which is completed upon connection close * * @param reg * * @return future completed upon termination of scheduling (disconnect) * */ public IPromise scheduleSendLoop(ConnectionRegistry reg) { Promise promise = new Promise(); sendJobs.add(new ScheduleEntry(reg, promise)); synchronized (this) { if ( ! loopStarted ) { loopStarted = true; Actor.current().execute(this); } } return promise; }
/** * return a future which is completed upon connection close * * @param reg * * @return future completed upon termination of scheduling (disconnect) * */ public IPromise scheduleSendLoop(ConnectionRegistry reg) { Promise promise = new Promise(); sendJobs.add(new ScheduleEntry(reg, promise)); synchronized (this) { if ( ! loopStarted ) { loopStarted = true; Actor.current().execute(this); } } return promise; }
public IPromise<AsyncFileIOEvent> write(long filePosition, ByteBuffer source) { if (fileChannel == null) throw new RuntimeException("file not opened"); Actor sender = Actor.current(); Promise p = new Promise(); final long bufferStartPos = source.position(); final ByteBuffer finalTarget = source; fileChannel.write(source, filePosition, source, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(Integer result, ByteBuffer attachment) { if ( source.remaining() > 0 ) { // just retry (will enqueue new message/job to actor mailbox) fileChannel.write(source,filePosition,source,this); } else { long newPos = filePosition + 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; }
public IPromise<AsyncFileIOEvent> write(long filePosition, ByteBuffer source) { if (fileChannel == null) throw new RuntimeException("file not opened"); Actor sender = Actor.current(); Promise p = new Promise(); final long bufferStartPos = source.position(); final ByteBuffer finalTarget = source; fileChannel.write(source, filePosition, source, new CompletionHandler<Integer, ByteBuffer>() { @Override public void completed(Integer result, ByteBuffer attachment) { if ( source.remaining() > 0 ) { // just retry (will enqueue new message/job to actor mailbox) fileChannel.write(source,filePosition,source,this); } else { long newPos = filePosition + 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; }
public void writeObject(Object o) { if ( theExecutingThread != Thread.currentThread() && myActor != null ) { myActor.execute( () -> { try { writeObject(o); } catch (Exception e) { Log.Warn(this,e); } }); return; } if ( myActor != null ) myActor = Actor.current(); checkThread(); objects.add(o); if (objects.size()>100) { try { flush(); } catch (Exception e) { FSTUtil.<RuntimeException>rethrow(e); } } }
public void writeObject(Object o) { if ( theExecutingThread != Thread.currentThread() && myActor != null ) { myActor.execute( () -> { try { writeObject(o); } catch (Exception e) { Log.Warn(this,e); } }); return; } if ( myActor != null ) myActor = Actor.current(); checkThread(); objects.add(o); if (objects.size()>100) { try { flush(); } catch (Exception e) { FSTUtil.<RuntimeException>rethrow(e); } } }
protected IPromise directWrite(ByteBuffer buf) { checkThread(); if ( myActor == null ) myActor = Actor.current(); if ( writePromise != null ) throw new RuntimeException("concurrent write con:"+chan.isConnected()+" open:"+chan.isOpen()); writePromise = new Promise(); writingBuffer = buf; Promise res = writePromise; try { int written = 0; written = chan.write(buf); if (written<0) { // TODO:closed writeFinished(new IOException("connection closed")); } if ( buf.remaining() > 0 ) { // key.interestOps(SelectionKey.OP_WRITE); } else { writeFinished(null); } } catch (Exception e) { res.reject(e); FSTUtil.rethrow(e); } return res; }
protected IPromise directWrite(ByteBuffer buf) { checkThread(); if ( myActor == null ) myActor = Actor.current(); if ( writePromise != null ) throw new RuntimeException("concurrent write con:"+chan.isConnected()+" open:"+chan.isOpen()); writePromise = new Promise(); writingBuffer = buf; Promise res = writePromise; try { int written = 0; written = chan.write(buf); if (written<0) { // TODO:closed writeFinished(new IOException("connection closed")); } if ( buf.remaining() > 0 ) { // key.interestOps(SelectionKey.OP_WRITE); } else { writeFinished(null); } } catch (Exception e) { res.reject(e); FSTUtil.rethrow(e); } return res; }
Actor.current(); // ensure running in actor thread poller.get().scheduleSendLoop(reg); reg.setFacadeActor(facade);
Actor.current(); // ensure running in actor thread poller.get().scheduleSendLoop(reg); reg.setFacadeActor(facade);
writesocket.setConf(reg.getConf()); Actor.current(); // ensure running in actor thread