io.termd.core.tty.TtyEvent Java Examples

The following examples show how to use io.termd.core.tty.TtyEvent. 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: ShellTest.java    From vertx-shell with Apache License 2.0 6 votes vote down vote up
public void testExit(TestContext context) throws Exception {
  commands.add(Command.create(vertx, Sleep.class));
  for (String cmd : Arrays.asList("exit", "logout")) {
    TestTtyConnection conn = new TestTtyConnection(vertx);
    ShellImpl adapter = createShell(conn);
    adapter.init().readline();
    conn.read("sleep 10000\r");
    long now = System.currentTimeMillis();
    while (adapter.jobController().jobs().size() == 0 || ((JobImpl) adapter.jobController().jobs().iterator().next()).actualStatus() != ExecStatus.RUNNING) {
      context.assertTrue(System.currentTimeMillis() - now < 2000);
      Thread.sleep(1);
    }
    conn.sendEvent(TtyEvent.SUSP);
    conn.read("bg\r");
    conn.read(cmd + "\r");
    context.assertTrue(conn.isClosed());
    now = System.currentTimeMillis();
    while (adapter.jobController().jobs().size() > 0) {
      context.assertTrue((System.currentTimeMillis() - now) < 2000);
      Thread.sleep(10);
    }
  }
}
 
Example #2
Source File: ShellTest.java    From vertx-shell with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuspendProcess(TestContext context) throws Exception {
  TestTtyConnection conn = new TestTtyConnection(vertx);
  ShellImpl shell = createShell(conn);
  shell.init().readline();
  Async done = context.async();
  Async latch2 = context.async();
  commands.add(CommandBuilder.command("foo").processHandler(process -> {
    Job job = shell.jobController().getJob(1);
    process.suspendHandler(v -> {
      context.assertEquals(ExecStatus.STOPPED, job.status());
      context.assertNull(shell.jobController().foregroundJob());
      done.complete();
    });
    latch2.complete();
  }));
  conn.read("foo\r");
  latch2.awaitSuccess(10000);
  conn.sendEvent(TtyEvent.SUSP);
}
 
Example #3
Source File: TestTtyConnection.java    From vertx-shell with Apache License 2.0 6 votes vote down vote up
public void sendEvent(TtyEvent event) {
  int c;
  switch (event) {
    case INTR:
      c = 'C' - 64;
      break;
    case SUSP:
      c = 'Z' - 64;
      break;
    case EOF:
      c = 'D' - 64;
      break;
    default:
      throw new AssertionError();
  }
  context.runOnContext(v -> {
    eventHandler.accept(event, c);
  });
}
 
Example #4
Source File: TtyBridge.java    From termd with Apache License 2.0 6 votes vote down vote up
private void onNewLine(TtyConnection conn, Readline readline, String line) {
  if (processStdinListener != null) {
    processStdinListener.accept(line);
  }
  if (line == null) {
    conn.close();
    return;
  }
  PtyMaster task = new PtyMaster(line, buffer -> onStdOut(conn, buffer), empty -> doneHandler(conn, readline));
  conn.setEventHandler((event,cp) -> {
    if (event == TtyEvent.INTR) {
      task.interruptProcess();
    }
  });
  if (processListener != null) {
    processListener.accept(task);
  }
  task.start();
}
 
Example #5
Source File: ReadlineTest.java    From termd with Apache License 2.0 6 votes vote down vote up
@Test
public void testEventHandler() {
  TestTerm term = new TestTerm(this);
  final LinkedList<TtyEvent> events = new LinkedList<TtyEvent>();
  BiConsumer<TtyEvent, Integer> handler = new BiConsumer<TtyEvent, Integer>() {
    @Override
    public void accept(TtyEvent event, Integer cp) {
      events.add(event);
    }
  };
  term.eventHandler = handler;
  Supplier<String> line = term.readlineComplete();
  term.read(CTRL_C_KEY);
  term.read('\r');
  assertEquals("", line.get());
  assertEquals(Collections.emptyList(), events);
  term.eventHandler.accept(TtyEvent.EOF, 4);
  assertEquals(Collections.singletonList(TtyEvent.EOF), events);
}
 
Example #6
Source File: ReadlineTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testEOF() {
  TestTerm term = new TestTerm(this);
  final LinkedList<String> lines = new LinkedList<String>();
  term.readline(new Consumer<String>() {
    @Override
    public void accept(String s) {
      lines.add(s);
    }
  });
  term.read(TtyEvent.EOF.codePoint());
  assertEquals(Collections.singletonList(null), lines);
}
 
Example #7
Source File: ShellTest.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetStdinOnResumeToForeground(TestContext context) throws Exception {
  Async fooRunning = context.async();
  Async fooSusp = context.async();
  Async fooResumed = context.async();
  Async readLatch = context.async();
  commands.add(
      CommandBuilder.command("foo").processHandler(process -> {
        process.suspendHandler(v -> fooSusp.complete());
        process.resumeHandler(v -> fooResumed.complete());
        process.stdinHandler(line -> {
          context.assertEquals("foo_msg", line);
          readLatch.complete();
        });
        fooRunning.complete();
      }));
  TestTtyConnection conn = new TestTtyConnection(vertx);
  ShellImpl shell = createShell(conn);
  shell.init().readline();
  conn.read("foo\r");
  fooRunning.awaitSuccess(2000);
  conn.sendEvent(TtyEvent.SUSP);
  fooSusp.awaitSuccess(2000);
  conn.read("fg\r");
  fooResumed.awaitSuccess(2000);
  conn.read("foo_msg");
}
 
Example #8
Source File: ShellTest.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetStdinOnBackgroundToForeground(TestContext context) throws Exception {
  Async fooRunning = context.async();
  Async fooSusp = context.async();
  Async fooResumed = context.async();
  Async fooToForeground = context.async();
  Async readLatch = context.async();
  commands.add(
      CommandBuilder.command("foo").processHandler(process -> {
        process.suspendHandler(v -> fooSusp.complete());
        process.resumeHandler(v -> fooResumed.complete());
        process.foregroundHandler(v -> fooToForeground.complete());
        process.stdinHandler(line -> {
          context.assertEquals("foo_msg", line);
          readLatch.complete();
        });
        fooRunning.complete();
      }));
  TestTtyConnection conn = new TestTtyConnection(vertx);
  ShellImpl shell = createShell(conn);
  shell.init().readline();
  conn.read("foo\r");
  fooRunning.awaitSuccess(2000);
  conn.sendEvent(TtyEvent.SUSP);
  fooSusp.awaitSuccess(2000);
  conn.read("bg\r");
  fooResumed.awaitSuccess(2000);
  conn.read("fg\r");
  fooToForeground.awaitSuccess(20000000);
  conn.read("foo_msg");
}
 
Example #9
Source File: ShellTest.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
@Test
public void testEndInBackground(TestContext context) throws Exception {
  Async fooRunning = context.async();
  Async fooSusp = context.async();
  Async fooResumed = context.async();
  Async endLatch = context.async();
  AtomicReference<CommandProcess> cmdProcess = new AtomicReference<>();
  AtomicReference<Context> cmdContext = new AtomicReference<>();
  commands.add(
      CommandBuilder.command("foo").processHandler(process -> {
        cmdProcess.set(process);
        cmdContext.set(Vertx.currentContext());
        process.suspendHandler(v -> fooSusp.complete());
        process.resumeHandler(v -> fooResumed.complete());
        process.endHandler(v -> {
          endLatch.complete();
        });
        fooRunning.complete();
      }));
  TestTtyConnection conn = new TestTtyConnection(vertx);
  ShellImpl shell = createShell(conn);
  shell.init().readline();
  conn.read("foo\r");
  fooRunning.awaitSuccess(2000);
  conn.sendEvent(TtyEvent.SUSP);
  fooSusp.awaitSuccess(2000);
  conn.read("bg\r");
  fooResumed.awaitSuccess(2000);
  cmdContext.get().runOnContext(v -> {
    cmdProcess.get().end();
  });
  long now = System.currentTimeMillis();
  while (shell.jobController().jobs().size() > 0) {
    context.assertTrue(System.currentTimeMillis() - now < 2000);
    Thread.sleep(1);
  }
  conn.read("exit\r");
  conn.getCloseLatch().await(2, TimeUnit.SECONDS);
}
 
Example #10
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(TtyEvent event, Integer cp) {
  switch (event) {
    case INTR:
      if (running) {
        // Ctrl-C interrupt : we use Thread interrupts to signal the command to stop
        interrupt();
      }
  }
}
 
Example #11
Source File: Plasma.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(TtyConnection conn) {
  conn.setEventHandler((event, key) -> {
    if (event == TtyEvent.INTR) {
      interrupted = true;
    }
  });
  if (conn.size() != null) {
    run(conn);
  } else {
    conn.setSizeHandler(size -> run(conn));
  }
}
 
Example #12
Source File: EventHandler.java    From arthas with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(TtyEvent event, Integer key) {
    switch (event) {
        case INTR:
            term.handleIntr(key);
            break;
        case EOF:
            term.handleEof(key);
            break;
        case SUSP:
            term.handleSusp(key);
            break;
    }
}
 
Example #13
Source File: ReadlineTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testEventHandler() {
  TestTerm term = new TestTerm(this);
  LinkedList<TtyEvent> events = new LinkedList<>();
  BiConsumer<TtyEvent, Integer> handler = (event,cp) -> events.add(event);
  term.eventHandler = handler;
  Supplier<String> line = term.readlineComplete();
  term.read(CTRL_C_KEY);
  term.read('\r');
  assertEquals("", line.get());
  assertEquals(Collections.emptyList(), events);
  term.eventHandler.accept(TtyEvent.EOF, 4);
  assertEquals(Collections.singletonList(TtyEvent.EOF), events);
}
 
Example #14
Source File: ReadlineTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testEOF() {
  TestTerm term = new TestTerm(this);
  LinkedList<String> lines = new LinkedList<>();
  term.readline(lines::add);
  term.read(TtyEvent.EOF.codePoint());
  assertEquals(Collections.singletonList(null), lines);
}
 
Example #15
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(TtyEvent event, Integer cp) {
  switch (event) {
    case INTR:
      if (running) {
        // Ctrl-C interrupt : we use Thread interrupts to signal the command to stop
        interrupt();
      }
  }
}
 
Example #16
Source File: Screencaster.java    From termd with Apache License 2.0 5 votes vote down vote up
public Screencaster(Robot robot, TtyConnection conn) {
  this.robot = robot;
  this.conn = conn;
  conn.setEventHandler(new BiConsumer<TtyEvent, Integer>() {
    @Override
    public void accept(TtyEvent event, Integer key) {
      interrupted = true;
    }
  });
}
 
Example #17
Source File: TestTerm.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public void setEventHandler(BiConsumer<TtyEvent, Integer> handler) {
  eventHandler = handler;
}
 
Example #18
Source File: HttpTtyConnection.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public BiConsumer<TtyEvent, Integer> getEventHandler() {
  return eventDecoder.getEventHandler();
}
 
Example #19
Source File: HttpTtyConnection.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public void setEventHandler(BiConsumer<TtyEvent, Integer> handler) {
  eventDecoder.setEventHandler(handler);
}
 
Example #20
Source File: TelnetTtyConnection.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public BiConsumer<TtyEvent, Integer> getEventHandler() {
  return eventDecoder.getEventHandler();
}
 
Example #21
Source File: TelnetTtyConnection.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public void setEventHandler(BiConsumer<TtyEvent, Integer> handler) {
  eventDecoder.setEventHandler(handler);
}
 
Example #22
Source File: TtyCommand.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public BiConsumer<TtyEvent, Integer> getEventHandler() {
  return eventDecoder.getEventHandler();
}
 
Example #23
Source File: TtyCommand.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public void setEventHandler(BiConsumer<TtyEvent, Integer> handler) {
  eventDecoder.setEventHandler(handler);
}
 
Example #24
Source File: TestTerm.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public BiConsumer<TtyEvent, Integer> getEventHandler() {
  return eventHandler;
}
 
Example #25
Source File: ShellTest.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
@Test
public void testResumeProcessToBackground(TestContext context) throws Exception {
  TestTtyConnection conn = new TestTtyConnection(vertx);
  ShellImpl shell = createShell(conn);
  shell.init().readline();
  Async latch1 = context.async();
  Async latch2 = context.async();
  Async latch3 = context.async();
  commands.add(CommandBuilder.command("foo").processHandler(process -> {
    Job job = shell.jobController().getJob(1);
    context.assertTrue(process.isForeground());
    process.suspendHandler(v -> {
      context.assertFalse(process.isForeground());
      context.assertEquals(0, latch1.count());
      try {
        process.write("");
        context.fail();
      } catch (IllegalStateException ignore) {
      }
      latch2.countDown();
    });
    process.resumeHandler(v -> {
      context.assertFalse(process.isForeground());
      context.assertEquals(0, latch2.count());
      context.assertEquals(ExecStatus.RUNNING, job.status());
      process.write("");
      context.assertNull(shell.jobController().foregroundJob());
      latch3.awaitSuccess(2000);
      process.write("resumed");
    });
    process.stdinHandler(txt -> {
      context.fail();
    });
    latch1.countDown();
  }));
  conn.read("foo\r");
  latch1.awaitSuccess(10000);
  conn.sendEvent(TtyEvent.SUSP);
  latch2.awaitSuccess(10000);
  conn.out().setLength(0);
  conn.read("bg\r");
  conn.assertWritten("bg\n[1]+ Running foo\n% ");
  latch3.countDown();
  conn.assertWritten("resumed");
  conn.read("hello");
  conn.assertWritten("hello");
}
 
Example #26
Source File: ShellTest.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
@Test
public void testResumeProcessToForeground(TestContext context) throws Exception {
  TestTtyConnection conn = new TestTtyConnection(vertx);
  ShellImpl shell = createShell(conn);
  shell.init().readline();
  CountDownLatch latch1 = new CountDownLatch(1);
  CountDownLatch latch2 = new CountDownLatch(1);
  CountDownLatch latch3 = new CountDownLatch(1);
  CountDownLatch latch4 = new CountDownLatch(1);
  commands.add(CommandBuilder.command("foo").processHandler(process -> {
    Job job = shell.jobController().getJob(1);
    context.assertTrue(process.isForeground());
    process.suspendHandler(v -> {
      context.assertFalse(process.isForeground());
      context.assertEquals(0L, latch1.getCount());
      try {
        process.write("");
        context.fail();
      } catch (IllegalStateException ignore) {
      }
      latch2.countDown();
    });
    process.resumeHandler(v -> {
      context.assertTrue(process.isForeground());
      context.assertEquals(0L, latch2.getCount());
      context.assertEquals(ExecStatus.RUNNING, job.status());
      process.write("");
      context.assertEquals(job, shell.jobController().foregroundJob());
      conn.out().setLength(0);
      process.write("resumed");
      latch3.countDown();
    });
    process.stdinHandler(txt -> {
      context.assertEquals(0L, latch3.getCount());
      context.assertEquals("hello", txt);
      latch4.countDown();
    });
    latch1.countDown();
  }));
  conn.read("foo\r");
  latch1.await(10, TimeUnit.SECONDS);
  conn.sendEvent(TtyEvent.SUSP);
  latch2.await(10, TimeUnit.SECONDS);
  conn.read("fg\r");
  latch3.await(10, TimeUnit.SECONDS);
  conn.read("hello");
  latch4.await(10, TimeUnit.SECONDS);
  conn.assertWritten("resumed");
}
 
Example #27
Source File: ShellTest.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
@Test
public void testSuspendedProcessDisconnectedFromTty(TestContext context) throws Exception {
  TestTtyConnection conn = new TestTtyConnection(vertx);
  ShellImpl shell = createShell(conn);
  shell.init().readline();
  Async done = context.async();
  Async latch1 = context.async();
  Async latch2 = context.async();
  Async latch3 = context.async();
  commands.add(CommandBuilder.command("read").processHandler(process -> {
    process.stdinHandler(line -> {
      context.fail("Should not process line " + line);
    });
    process.suspendHandler(v -> {
      try {
        process.write("");
        context.fail();
      } catch (IllegalStateException ignore) {
      }
      latch2.countDown();
    });
    latch1.countDown();
  }));
  commands.add(CommandBuilder.command("wait").processHandler(process -> {
    // Do nothing, this command is used to escape from readline and make
    // sure that the read data is not sent to the stopped command
    latch3.countDown();
    process.suspendHandler(v -> {
      process.end(0);
    });
  }));
  commands.add(CommandBuilder.command("end").processHandler(process -> {
    done.complete();
  }));
  conn.read("read\r");
  latch1.awaitSuccess(10000);
  conn.sendEvent(TtyEvent.SUSP);
  latch2.awaitSuccess(10000);
  conn.read("wait\r");
  latch3.awaitSuccess(10000);
  conn.sendEvent(TtyEvent.SUSP);
  conn.read("end\r");
}
 
Example #28
Source File: TestTtyConnection.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
@Override
public void setEventHandler(BiConsumer<TtyEvent, Integer> handler) {
  eventHandler = handler;
}
 
Example #29
Source File: TestTtyConnection.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
@Override
public BiConsumer<TtyEvent, Integer> getEventHandler() {
  return eventHandler;
}
 
Example #30
Source File: TestTerm.java    From termd with Apache License 2.0 4 votes vote down vote up
@Override
public void setEventHandler(BiConsumer<TtyEvent, Integer> handler) {
  eventHandler = handler;
}