processHandler(process -> {
@Override public void start() throws Exception { Command helloWorld = CommandBuilder.command("hello-world"). processHandler(process -> { process.write("hello world\n"); process.end(); }).build(vertx); ShellService service = ShellService.create(vertx, new ShellServiceOptions().setTelnetOptions( new TelnetTermOptions().setHost("localhost").setPort(3000) )); CommandRegistry.getShared(vertx).registerCommand(helloWorld); service.start(ar -> { if (!ar.succeeded()) { ar.cause().printStackTrace(); } }); } }
@Override public void start() throws Exception { Command starwars = CommandBuilder.command("echokeyboard"). processHandler(process -> { // Echo process.stdinHandler(keys -> { process.write(keys.replace('\r', '\n')); }); // Terminate when user hits Ctrl-C process.interruptHandler(v -> { process.end(); }); }).build(vertx); ShellService service = ShellService.create(vertx, new ShellServiceOptions().setTelnetOptions( new TelnetTermOptions().setHost("localhost").setPort(3000) )); CommandRegistry.getShared(vertx).registerCommand(starwars); service.start(ar -> { if (!ar.succeeded()) { ar.cause().printStackTrace(); } }); } }
processHandler(process -> {
processHandler(process -> { URL url; try {
/** * Set the command process handler, the process handler is called when the command is executed. * @param handler the process handler * @return this command object */ public io.vertx.rxjava.ext.shell.command.CommandBuilder processHandler(Handler<io.vertx.rxjava.ext.shell.command.CommandProcess> handler) { delegate.processHandler(new Handler<io.vertx.ext.shell.command.CommandProcess>() { public void handle(io.vertx.ext.shell.command.CommandProcess event) { handler.handle(io.vertx.rxjava.ext.shell.command.CommandProcess.newInstance(event)); } }); return this; }
/** * Set the command process handler, the process handler is called when the command is executed. * @param handler the process handler * @return this command object */ public io.vertx.rxjava.ext.shell.command.CommandBuilder processHandler(Handler<io.vertx.rxjava.ext.shell.command.CommandProcess> handler) { delegate.processHandler(new Handler<io.vertx.ext.shell.command.CommandProcess>() { public void handle(io.vertx.ext.shell.command.CommandProcess event) { handler.handle(io.vertx.rxjava.ext.shell.command.CommandProcess.newInstance(event)); } }); return this; }
@Override public void start(Future<Void> startFuture) throws Exception { CommandBuilder command = CommandBuilder.command("hello"); command.processHandler(process -> { }); registry.registerCommand(command.build(vertx), ar -> { if (ar.succeeded()) { startFuture.complete(); } else { startFuture.fail(ar.cause()); } }); } }, context.asyncAssertSuccess(id -> {
@Test public void testExecuteReadlineBuffered(TestContext context) { TestTtyConnection conn = new TestTtyConnection(vertx); ShellImpl shell = createShell(conn); shell.init().readline(); Async async = context.async(); AtomicInteger count = new AtomicInteger(); commands.add(CommandBuilder.command("read").processHandler(process -> { if (count.incrementAndGet() == 2) { async.complete(); } process.end(0); })); conn.read("read\rread\r"); }
@Test public void testVertx(TestContext context) { TestTtyConnection conn = new TestTtyConnection(vertx); ShellImpl shell = createShell(conn); shell.init().readline(); Async done = context.async(); commands.add(CommandBuilder.command("foo").processHandler(process -> { context.assertEquals(vertx, process.vertx()); done.complete(); })); conn.read("foo\r"); }
@Test public void testSuspendReadyProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); builder.processHandler(process -> context.fail()); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); try { process.suspend(); context.fail(); } catch (Exception ignore) { } }
@Test public void testThrowExceptionInProcess(TestContext context) { commands.add(CommandBuilder.command("foo").processHandler(process -> { throw new RuntimeException(); })); Shell shell = server.createShell(); Job job = shell.createJob("foo"); Async async = context.async(); Pty pty = Pty.create(); job.setTty(pty.slave()).statusUpdateHandler(CommandProcessTest.terminateHandler(code -> { context.assertEquals(1, job.process().exitCode()); async.complete(); })).run(); }
@Test public void testResumeReadyProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); builder.processHandler(process -> context.fail()); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); try { process.resume(); context.fail(); } catch (Exception ignore) { } }
@Test public void testInterruptReadyProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); builder.processHandler(process -> context.fail()); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); try { process.interrupt(); context.fail(); } catch (Exception ignore) { } }
@Test public void testRunRunningProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); Async runningLatch = context.async(); builder.processHandler(process -> runningLatch.complete()); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); process.run(); runningLatch.awaitSuccess(10000); try { process.run(); context.fail(); } catch (IllegalStateException ignore) { } }
@Test public void testResumeRunningProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); Async runningLatch = context.async(); builder.processHandler(process -> runningLatch.complete()); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); process.run(); runningLatch.awaitSuccess(10000); try { process.resume(); context.fail(); } catch (Exception ignore) { } }
@Test public void testRunTerminatedProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); Async terminatedLatch = context.async(); builder.processHandler(CommandProcess::end); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); process.terminatedHandler(exitCode -> terminatedLatch.complete()); process.run(); terminatedLatch.awaitSuccess(10000); try { process.run(); context.fail(); } catch (IllegalStateException ignore) { } }
@Test public void testResumeTerminatedProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); Async terminatedLatch = context.async(); builder.processHandler(CommandProcess::end); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); process.terminatedHandler(exitCode -> terminatedLatch.complete()); process.run(); terminatedLatch.awaitSuccess(10000); try { process.resume(); context.fail(); } catch (IllegalStateException ignore) { } }
@Test public void testRun(TestContext context) { commands.add(CommandBuilder.command("foo").processHandler(process -> { process.end(3); })); Shell shell = server.createShell(); Job job = shell.createJob(CliToken.tokenize("foo")); Async async = context.async(); job.setTty(Pty.create().slave()).statusUpdateHandler(CommandProcessTest.terminateHandler(code -> { context.assertEquals(3, job.process().exitCode()); async.complete(); })).run(); }
@Test public void testInterruptTerminatedProcess(TestContext context) { CommandBuilder builder = CommandBuilder.command("hello"); Async terminatedLatch = context.async(); builder.processHandler(process -> process.end()); Process process = builder.build(vertx).createProcess().setSession(Session.create()).setTty(Pty.create().slave()); process.terminatedHandler(exitCode -> terminatedLatch.complete()); process.run(); terminatedLatch.awaitSuccess(10000); try { process.interrupt(); context.fail(); } catch (Exception ignore) { } }