org.apache.sshd.common.util.buffer.ByteArrayBuffer Java Examples

The following examples show how to use org.apache.sshd.common.util.buffer.ByteArrayBuffer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WindowAdjustTest.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void start(Environment env) throws IOException {
    log.info("Starting");

    ExecutorService service = ThreadUtils.newSingleThreadExecutor(getClass().getSimpleName() + "-" + POOL_COUNT.incrementAndGet());
    executorHolder.set(service);

    futureHolder.set(service.submit(new Runnable() {
        @SuppressWarnings("synthetic-access")
        @Override
        public void run() {
            log.info("Start heavy load sending " + sendCount + " messages of " + msg.length + " bytes");
            for (int i = 0; i < sendCount; i++) {
                pendingWrapper.write(new ByteArrayBuffer(msg));
            }
            log.info("Sending EOF signal");
            pendingWrapper.write(new ByteArrayBuffer(new byte[]{eofSignal}));
        }
    }));
    log.info("Started");
}
 
Example #2
Source File: TtyCommand.java    From aesh-readline with Apache License 2.0 6 votes vote down vote up
@Override
public void setIoOutputStream(IoOutputStream out) {
    this.ioOut = out;
    this.out = (byte[] bytes) -> {
        if(writeFuture == null)
            writeFuture = out.write(new ByteArrayBuffer(bytes));
        else if(writeFuture.isWritten())
            writeFuture = out.write(new ByteArrayBuffer(bytes));
        else {
            try {
                writeFuture.await();
                writeFuture = out.write(new ByteArrayBuffer(bytes));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
}
 
Example #3
Source File: WindowAdjustTest.java    From termd with Apache License 2.0 6 votes vote down vote up
@Override
public void start(Environment env) throws IOException {
    log.info("Starting");

    ExecutorService service = ThreadUtils.newSingleThreadExecutor(getClass().getSimpleName() + "-" + POOL_COUNT.incrementAndGet());
    executorHolder.set(service);

    futureHolder.set(service.submit(new Runnable() {
        @SuppressWarnings("synthetic-access")
        @Override
        public void run() {
            log.info("Start heavy load sending " + sendCount + " messages of " + msg.length + " bytes");
            for (int i = 0; i < sendCount; i++) {
                pendingWrapper.write(new ByteArrayBuffer(msg));
            }
            log.info("Sending EOF signal");
            pendingWrapper.write(new ByteArrayBuffer(new byte[]{eofSignal}));
        }
    }));
    log.info("Started");
}
 
Example #4
Source File: TtyCommand.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void setIoOutputStream(final IoOutputStream out) {
  this.ioOut = out;
  this.out = new Consumer<byte[]>() {
    @Override
    public void accept(byte[] bytes) {
      out.write(new ByteArrayBuffer(bytes));
    }
  };
}
 
Example #5
Source File: NettyIoSession.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  ByteBuf buf = (ByteBuf) msg;
  byte[] bytes = new byte[buf.readableBytes()];
  buf.getBytes(0, bytes);
  acceptor.factory.handlerBridge.messageReceived(handler, NettyIoSession.this, new ByteArrayBuffer(bytes));
}
 
Example #6
Source File: AsyncEchoShellFactory.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public int data(final ChannelSession channel, byte[] buf, int start, int len) throws IOException {
    buffer.append(new String(buf, start, len));
    for (int i = 0; i < buffer.length(); i++) {
        if (buffer.charAt(i) == '\n') {
            final String s = buffer.substring(0, i + 1);
            final byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
            out.write(new ByteArrayBuffer(bytes)).addListener(new SshFutureListener<IoWriteFuture>() {
                @Override
                public void operationComplete(IoWriteFuture future) {
                    Session session = channel.getSession();
                    if (future.isWritten()) {
                        try {
                            Window wLocal = channel.getLocalWindow();
                            wLocal.consumeAndCheck(bytes.length);
                        } catch (IOException e) {
                            session.exceptionCaught(e);
                        }
                    } else {
                        Throwable t = future.getException();
                        session.exceptionCaught(t);
                    }
                }
            });
            buffer = new StringBuilder(buffer.substring(i + 1));
            i = 0;
        }
    }
    return 0;
}
 
Example #7
Source File: NettyIoSession.java    From aesh-readline with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    byte[] bytes = new byte[buf.readableBytes()];
    buf.getBytes(0, bytes);
    acceptor.factory.handlerBridge.messageReceived(handler, NettyIoSession.this, new ByteArrayBuffer(bytes));
}
 
Example #8
Source File: NettyIoSession.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  ByteBuf buf = (ByteBuf) msg;
  byte[] bytes = new byte[buf.readableBytes()];
  buf.getBytes(0, bytes);
  acceptor.factory.handlerBridge.messageReceived(handler, NettyIoSession.this, new ByteArrayBuffer(bytes));
}
 
Example #9
Source File: AsyncEchoShellFactory.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public int data(final ChannelSession channel, byte[] buf, int start, int len) throws IOException {
    buffer.append(new String(buf, start, len));
    for (int i = 0; i < buffer.length(); i++) {
        if (buffer.charAt(i) == '\n') {
            final String s = buffer.substring(0, i + 1);
            final byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
            out.write(new ByteArrayBuffer(bytes)).addListener(new SshFutureListener<IoWriteFuture>() {
                @Override
                public void operationComplete(IoWriteFuture future) {
                    Session session = channel.getSession();
                    if (future.isWritten()) {
                        try {
                            Window wLocal = channel.getLocalWindow();
                            wLocal.consumeAndCheck(bytes.length);
                        } catch (IOException e) {
                            session.exceptionCaught(e);
                        }
                    } else {
                        Throwable t = future.getException();
                        session.exceptionCaught(t);
                    }
                }
            });
            buffer = new StringBuilder(buffer.substring(i + 1));
            i = 0;
        }
    }
    return 0;
}
 
Example #10
Source File: WindowTest.java    From termd with Apache License 2.0 4 votes vote down vote up
@Test
public void testWindowConsumptionWithAsyncStreams() throws Exception {
    sshd.setShellFactory(new AsyncEchoShellFactory());
    PropertyResolverUtils.updateProperty(sshd, FactoryManager.WINDOW_SIZE, 1024);
    PropertyResolverUtils.updateProperty(client, FactoryManager.WINDOW_SIZE, 1024);

    client.start();

    try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {
        session.addPasswordIdentity(getCurrentTestName());
        session.auth().verify(5L, TimeUnit.SECONDS);

        try (ChannelShell channel = session.createShellChannel()) {
            channel.setStreaming(ClientChannel.Streaming.Async);
            channel.open().verify(5L, TimeUnit.SECONDS);

            try (Channel serverChannel = sshd.getActiveSessions().iterator().next().getService(ServerConnectionService.class).getChannels().iterator().next()) {
                Window clientLocal = channel.getLocalWindow();
                Window clientRemote = channel.getRemoteWindow();
                Window serverLocal = serverChannel.getLocalWindow();
                Window serverRemote = serverChannel.getRemoteWindow();

                final String message = "0123456789\n";
                final byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
                final int nbMessages = 500;
                IoOutputStream output = channel.getAsyncIn();
                IoInputStream input = channel.getAsyncOut();
                for (int i = 0; i < nbMessages; i++) {
                    Buffer buffer = new ByteArrayBuffer(bytes);
                    output.write(buffer).verify(5L, TimeUnit.SECONDS);

                    waitForWindowNotEquals(clientLocal, serverRemote, "client local", "server remote", TimeUnit.SECONDS.toMillis(3L));

                    Buffer buf = new ByteArrayBuffer(16);
                    IoReadFuture future = input.read(buf);
                    future.verify(5L, TimeUnit.SECONDS);
                    assertEquals("Mismatched available data at line #" + i, message.length(), buf.available());
                    assertEquals("Mismatched data at line #" + i, message, new String(buf.array(), buf.rpos(), buf.available()));

                    waitForWindowEquals(clientLocal, serverRemote, "client local", "server remote", TimeUnit.SECONDS.toMillis(3L));
                    waitForWindowEquals(clientRemote, serverLocal, "client remote", "server local", TimeUnit.SECONDS.toMillis(3L));
                }
            }
        }
    } finally {
        client.stop();
    }
}
 
Example #11
Source File: TtyCommand.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public void setIoOutputStream(IoOutputStream out) {
  this.ioOut = out;
  this.out = bytes -> out.write(new ByteArrayBuffer(bytes));
}
 
Example #12
Source File: WindowTest.java    From termd with Apache License 2.0 4 votes vote down vote up
@Test
public void testWindowConsumptionWithAsyncStreams() throws Exception {
    sshd.setShellFactory(new AsyncEchoShellFactory());
    PropertyResolverUtils.updateProperty(sshd, FactoryManager.WINDOW_SIZE, 1024);
    PropertyResolverUtils.updateProperty(client, FactoryManager.WINDOW_SIZE, 1024);

    client.start();

    try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {
        session.addPasswordIdentity(getCurrentTestName());
        session.auth().verify(5L, TimeUnit.SECONDS);

        try (ChannelShell channel = session.createShellChannel()) {
            channel.setStreaming(ClientChannel.Streaming.Async);
            channel.open().verify(5L, TimeUnit.SECONDS);

            try (Channel serverChannel = sshd.getActiveSessions().iterator().next().getService(ServerConnectionService.class).getChannels().iterator().next()) {
                Window clientLocal = channel.getLocalWindow();
                Window clientRemote = channel.getRemoteWindow();
                Window serverLocal = serverChannel.getLocalWindow();
                Window serverRemote = serverChannel.getRemoteWindow();

                final String message = "0123456789\n";
                final byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
                final int nbMessages = 500;
                IoOutputStream output = channel.getAsyncIn();
                IoInputStream input = channel.getAsyncOut();
                for (int i = 0; i < nbMessages; i++) {
                    Buffer buffer = new ByteArrayBuffer(bytes);
                    output.write(buffer).verify(5L, TimeUnit.SECONDS);

                    waitForWindowNotEquals(clientLocal, serverRemote, "client local", "server remote", TimeUnit.SECONDS.toMillis(3L));

                    Buffer buf = new ByteArrayBuffer(16);
                    IoReadFuture future = input.read(buf);
                    future.verify(5L, TimeUnit.SECONDS);
                    assertEquals("Mismatched available data at line #" + i, message.length(), buf.available());
                    assertEquals("Mismatched data at line #" + i, message, new String(buf.array(), buf.rpos(), buf.available()));

                    waitForWindowEquals(clientLocal, serverRemote, "client local", "server remote", TimeUnit.SECONDS.toMillis(3L));
                    waitForWindowEquals(clientRemote, serverLocal, "client remote", "server local", TimeUnit.SECONDS.toMillis(3L));
                }
            }
        }
    } finally {
        client.stop();
    }
}