io.termd.core.tty.TtyConnection Java Examples
The following examples show how to use
io.termd.core.tty.TtyConnection.
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: NettyWebsocketTtyBootstrap.java From termd with Apache License 2.0 | 6 votes |
public void start(Consumer<TtyConnection> handler, Consumer<Throwable> doneHandler) { group = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(group) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new TtyServerInitializer(channelGroup, handler)); ChannelFuture f = b.bind(host, port); f.addListener(abc -> { if (abc.isSuccess()) { channel = f.channel(); doneHandler.accept(null); } else { doneHandler.accept(abc.cause()); } }); }
Example #2
Source File: NettyWebsocketTtyBootstrap.java From termd with Apache License 2.0 | 6 votes |
public void start(Consumer<TtyConnection> handler, final Consumer<Throwable> doneHandler) { group = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(group) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new TtyServerInitializer(channelGroup, handler, httpResourcePath)); final ChannelFuture f = b.bind(host, port); f.addListener(new GenericFutureListener<Future<? super Void>>() { @Override public void operationComplete(Future<? super Void> future) throws Exception { if (future.isSuccess()) { channel = f.channel(); doneHandler.accept(null); } else { doneHandler.accept(future.cause()); } } }); }
Example #3
Source File: Readline.java From termd with Apache License 2.0 | 6 votes |
/** * Schedule delivery of pending events in the event queue. */ public void schedulePendingEvent() { TtyConnection conn; synchronized (this) { if (interaction == null) { throw new IllegalStateException("No interaction!"); } if (decoder.hasNext()) { conn = interaction.conn; } else { return; } } conn.execute(new Runnable() { @Override public void run() { deliver(); } }); }
Example #4
Source File: NettySshTtyBootstrap.java From termd with Apache License 2.0 | 6 votes |
public void start(final Consumer<TtyConnection> factory, Consumer<Throwable> doneHandler) { server = SshServer.setUpDefaultServer(); server.setIoServiceFactoryFactory(new NettyIoServiceFactoryFactory(childGroup)); server.setPort(port); server.setHost(host); server.setKeyPairProvider(keyPairProvider); server.setPasswordAuthenticator(passwordAuthenticator); server.setShellFactory(new Factory<Command>() { @Override public Command create() { return new TtyCommand(charset, factory); } }); try { server.start(); } catch (Exception e) { doneHandler.accept(e); return; } doneHandler.accept(null); }
Example #5
Source File: TermImpl.java From arthas with Apache License 2.0 | 6 votes |
public TermImpl(Keymap keymap, TtyConnection conn) { this.conn = conn; readline = new Readline(keymap); readline.setHistory(FileUtils.loadCommandHistory(new File(Constants.CMD_HISTORY_FILE))); for (Function function : readlineFunctions) { readline.addFunction(function); } echoHandler = new DefaultTermStdinHandler(this); conn.setStdinHandler(echoHandler); conn.setEventHandler(new EventHandler(this)); }
Example #6
Source File: HttpTelnetTermServer.java From arthas with Apache License 2.0 | 6 votes |
@Override public TermServer listen(Handler<Future<TermServer>> listenHandler) { // TODO: charset and inputrc from options bootstrap = new NettyHttpTelnetTtyBootstrap(workerGroup).setHost(hostIp).setPort(port); try { bootstrap.start(new Consumer<TtyConnection>() { @Override public void accept(final TtyConnection conn) { termHandler.handle(new TermImpl(Helper.loadKeymap(), conn)); } }).get(connectionTimeout, TimeUnit.MILLISECONDS); listenHandler.handle(Future.<TermServer>succeededFuture()); } catch (Throwable t) { logger.error("Error listening to port " + port, t); listenHandler.handle(Future.<TermServer>failedFuture(t)); } return this; }
Example #7
Source File: TtyBridge.java From termd with Apache License 2.0 | 6 votes |
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 #8
Source File: NettyHttpTelnetBootstrap.java From arthas with Apache License 2.0 | 6 votes |
public void start(final Supplier<TelnetHandler> handlerFactory, final Consumer<TtyConnection> factory, final Consumer<Throwable> doneHandler) { ServerBootstrap boostrap = new ServerBootstrap(); boostrap.group(group).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ProtocolDetectHandler(channelGroup, handlerFactory, factory, workerGroup)); } }); boostrap.bind(getHost(), getPort()).addListener(new GenericFutureListener<Future<? super Void>>() { @Override public void operationComplete(Future<? super Void> future) throws Exception { if (future.isSuccess()) { doneHandler.accept(null); } else { doneHandler.accept(future.cause()); } } }); }
Example #9
Source File: NettyWebsocketTtyBootstrap.java From arthas with Apache License 2.0 | 6 votes |
public void start(Consumer<TtyConnection> handler, final Consumer<Throwable> doneHandler) { group = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new TtyServerInitializer(channelGroup, handler, workerGroup)); final ChannelFuture f = b.bind(host, port); f.addListener(new GenericFutureListener<Future<? super Void>>() { @Override public void operationComplete(Future<? super Void> future) throws Exception { if (future.isSuccess()) { channel = f.channel(); doneHandler.accept(null); } else { doneHandler.accept(future.cause()); } } }); }
Example #10
Source File: HttpTermServer.java From arthas with Apache License 2.0 | 6 votes |
@Override public TermServer listen(Handler<Future<TermServer>> listenHandler) { // TODO: charset and inputrc from options bootstrap = new NettyWebsocketTtyBootstrap(workerGroup).setHost(hostIp).setPort(port); try { bootstrap.start(new Consumer<TtyConnection>() { @Override public void accept(final TtyConnection conn) { termHandler.handle(new TermImpl(Helper.loadKeymap(), conn)); } }).get(connectionTimeout, TimeUnit.MILLISECONDS); listenHandler.handle(Future.<TermServer>succeededFuture()); } catch (Throwable t) { logger.error("Error listening to port " + port, t); listenHandler.handle(Future.<TermServer>failedFuture(t)); } return this; }
Example #11
Source File: Shell.java From termd with Apache License 2.0 | 6 votes |
@Override public void execute(TtyConnection conn, List<String> args) throws Exception { if (args.isEmpty()) { conn.write("usage: sleep seconds\n"); return; } int time = -1; try { time = Integer.parseInt(args.get(0)); } catch (NumberFormatException ignore) { } if (time > 0) { // Sleep until timeout or Ctrl-C interrupted Thread.sleep(time * 1000); } }
Example #12
Source File: TelnetTermServer.java From arthas with Apache License 2.0 | 6 votes |
@Override public TermServer listen(Handler<Future<TermServer>> listenHandler) { // TODO: charset and inputrc from options bootstrap = new NettyTelnetTtyBootstrap().setHost(hostIp).setPort(port); try { bootstrap.start(new Consumer<TtyConnection>() { @Override public void accept(final TtyConnection conn) { termHandler.handle(new TermImpl(Helper.loadKeymap(), conn)); } }).get(connectionTimeout, TimeUnit.MILLISECONDS); listenHandler.handle(Future.<TermServer>succeededFuture()); } catch (Throwable t) { logger.error("Error listening to port " + port, t); listenHandler.handle(Future.<TermServer>failedFuture(t)); } return this; }
Example #13
Source File: Shell.java From termd with Apache License 2.0 | 6 votes |
@Override public void execute(TtyConnection conn, List<String> args) throws Exception { if (args.isEmpty()) { conn.write("usage: sleep seconds\n"); return; } int time = -1; try { time = Integer.parseInt(args.get(0)); } catch (NumberFormatException ignore) { } if (time > 0) { // Sleep until timeout or Ctrl-C interrupted Thread.sleep(time * 1000); } }
Example #14
Source File: Shell.java From termd with Apache License 2.0 | 6 votes |
@Override public void execute(TtyConnection conn, List<String> args) throws Exception { conn.write("Current window size " + conn.size() + ", try resize it\n"); // Refresh the screen with the new size conn.setSizeHandler(size -> { conn.write("Window resized " + size + "\n"); }); try { // Wait until interrupted new CountDownLatch(1).await(); } finally { conn.setSizeHandler(null); } }
Example #15
Source File: NettySshTtyBootstrap.java From termd with Apache License 2.0 | 6 votes |
public void start(Consumer<TtyConnection> factory, Consumer<Throwable> doneHandler) { server = SshServer.setUpDefaultServer(); server.setIoServiceFactoryFactory(new NettyIoServiceFactoryFactory(childGroup)); server.setPort(port); server.setHost(host); server.setKeyPairProvider(keyPairProvider); server.setPasswordAuthenticator(passwordAuthenticator); server.setShellFactory(() -> new TtyCommand(charset, factory)); try { server.start(); } catch (Exception e) { doneHandler.accept(e); return; } doneHandler.accept(null); }
Example #16
Source File: Shell.java From termd with Apache License 2.0 | 6 votes |
@Override public void execute(TtyConnection conn, List<String> args) throws Exception { // Subscribe to key events and print them conn.setStdinHandler(keys -> { for (int key : keys) { conn.write(key + " pressed\n"); } }); try { // Wait until interrupted new CountDownLatch(1).await(); } finally { conn.setStdinHandler(null); } }
Example #17
Source File: ProtocolDetectHandler.java From arthas with Apache License 2.0 | 5 votes |
public ProtocolDetectHandler(ChannelGroup channelGroup, final Supplier<TelnetHandler> handlerFactory, Consumer<TtyConnection> ttyConnectionFactory, EventExecutorGroup workerGroup) { this.channelGroup = channelGroup; this.handlerFactory = handlerFactory; this.ttyConnectionFactory = ttyConnectionFactory; this.workerGroup = workerGroup; }
Example #18
Source File: TelnetReadlineExample.java From termd with Apache License 2.0 | 5 votes |
public synchronized static void main(String[] args) throws Throwable { NettyTelnetTtyBootstrap bootstrap = new NettyTelnetTtyBootstrap().setOutBinary(true).setHost("localhost").setPort(4000); bootstrap.start(new Consumer<TtyConnection>() { @Override public void accept(TtyConnection conn) { ReadlineExample.handle(conn); } }).get(10, TimeUnit.SECONDS); System.out.println("Telnet server started on localhost:4000"); TelnetReadlineExample.class.wait(); }
Example #19
Source File: TtyBridge.java From termd with Apache License 2.0 | 5 votes |
private void onStdOut(TtyConnection conn, int[] buffer) { conn.execute(() -> { conn.stdoutHandler().accept(buffer); }); if (processStdoutListener != null) { processStdoutListener.accept(buffer); } }
Example #20
Source File: Readline.java From termd with Apache License 2.0 | 5 votes |
/** * Read a line until a request can be processed. * * @param requestHandler the requestHandler */ public void readline(TtyConnection conn, String prompt, Consumer<String> requestHandler, Consumer<Completion> completionHandler) { synchronized (this) { if (interaction != null) { throw new IllegalStateException("Already reading a line"); } interaction = new Interaction(conn, prompt, requestHandler, completionHandler); } interaction.install(); conn.write(prompt); schedulePendingEvent(); }
Example #21
Source File: Readline.java From termd with Apache License 2.0 | 5 votes |
/** * Read a line until a request can be processed. * * @param requestHandler the requestHandler */ public void readline(TtyConnection conn, String prompt, Consumer<String> requestHandler, Consumer<Completion> completionHandler) { synchronized (this) { if (interaction != null) { throw new IllegalStateException("Already reading a line"); } interaction = new Interaction(conn, prompt, requestHandler, completionHandler); } interaction.install(); conn.write(prompt); schedulePendingEvent(); }
Example #22
Source File: Shell.java From termd with Apache License 2.0 | 5 votes |
@Override public void execute(TtyConnection conn, List<String> args) throws Exception { while (true) { StringBuilder buf = new StringBuilder(); Formatter formatter = new Formatter(buf); List<Thread> threads = new ArrayList<>(Thread.getAllStackTraces().keySet()); for (int i = 1;i <= conn.size().y();i++) { // Change cursor position and erase line with ANSI escape code magic buf.append("\033[").append(i).append(";1H\033[K"); // String format = " %1$-5s %2$-10s %3$-50s %4$s"; if (i == 1) { formatter.format(format, "ID", "STATE", "NAME", "GROUP"); } else { int index = i - 2; if (index < threads.size()) { Thread thread = threads.get(index); formatter.format(format, thread.getId(), thread.getState().name(), thread.getName(), thread.getThreadGroup().getName()); } } } conn.write(buf.toString()); // Sleep until we refresh the list of interrupted Thread.sleep(1000); } }
Example #23
Source File: Readline.java From termd with Apache License 2.0 | 5 votes |
private Interaction( TtyConnection conn, String prompt, Consumer<String> requestHandler, Consumer<Completion> completionHandler) { this.conn = conn; this.prompt = prompt; this.data = new HashMap<String, Object>(); this.currentPrompt = prompt; this.requestHandler = requestHandler; this.completionHandler = completionHandler; }
Example #24
Source File: Shell.java From termd with Apache License 2.0 | 5 votes |
@Override public void execute(TtyConnection conn, List<String> args) throws Exception { StringBuilder msg = new StringBuilder("Demo term, try commands: "); Command[] commands = Command.values(); for (int i = 0;i < commands.length;i++) { if (i > 0) { msg.append(","); } msg.append(" ").append(commands[i].name()); } msg.append("...\n"); conn.write(msg.toString()); }
Example #25
Source File: TermImpl.java From vertx-shell with Apache License 2.0 | 5 votes |
public TermImpl(Vertx vertx, Keymap keymap, TtyConnection conn) { this.vertx = vertx; this.conn = conn; readline = new Readline(keymap); readlineFunctions.forEach(readline::addFunction); echoHandler = codePoints -> { // Echo echo(codePoints); readline.queueEvent(codePoints); }; conn.setStdinHandler(echoHandler); conn.setEventHandler((event, key) -> { switch (event) { case INTR: if (interruptHandler == null || !interruptHandler.deliver(key)) { echo(key, '\n'); } break; case EOF: // Pseudo signal if (stdinHandler != null) { stdinHandler.handle(Helper.fromCodePoints(new int[]{key})); } else { echo(key); readline.queueEvent(new int[]{key}); } break; case SUSP: if (suspendHandler == null || !suspendHandler.deliver(key)) { echo(key, 'Z' - 64); } break; } }); }
Example #26
Source File: NettyHttpTelnetTtyBootstrap.java From arthas with Apache License 2.0 | 5 votes |
public void start(final Consumer<TtyConnection> factory, Consumer<Throwable> doneHandler) { httpTelnetTtyBootstrap.start(new Supplier<TelnetHandler>() { @Override public TelnetHandler get() { return new TelnetTtyConnection(inBinary, outBinary, charset, factory); } }, factory, doneHandler); }
Example #27
Source File: Shell.java From termd with Apache License 2.0 | 5 votes |
@Override public void execute(TtyConnection conn, List<String> args) throws Exception { for (int i = 0;i < args.size();i++) { if (i > 0) { conn.write(" "); } conn.write(args.get(i)); } conn.write("\n"); }
Example #28
Source File: Shell.java From termd with Apache License 2.0 | 5 votes |
/** * Use {@link Readline} to read a user input and then process it * * @param conn the tty connection * @param readline the readline object */ public void read(final TtyConnection conn, final Readline readline) { // Just call readline and get a callback when line is read readline.readline(conn, "% ", line -> { // Ctrl-D if (line == null) { conn.write("logout\n").close(); return; } Matcher matcher = splitter.matcher(line); if (matcher.find()) { String cmd = matcher.group(); // Gather args List<String> args = new ArrayList<>(); while (matcher.find()) { args.add(matcher.group()); } try { new Task(conn, readline, Command.valueOf(cmd), args).start(); return; } catch (IllegalArgumentException e) { conn.write(cmd + ": command not found\n"); } } read(conn, readline); }); }
Example #29
Source File: Shell.java From termd with Apache License 2.0 | 5 votes |
public void accept(final TtyConnection conn) { InputStream inputrc = Keymap.class.getResourceAsStream("inputrc"); Keymap keymap = new Keymap(inputrc); Readline readline = new Readline(keymap); for (io.termd.core.readline.Function function : Helper.loadServices(Thread.currentThread().getContextClassLoader(), io.termd.core.readline.Function.class)) { readline.addFunction(function); } conn.write("Welcome to Term.d shell example\n\n"); read(conn, readline); }
Example #30
Source File: ReadlineFunctionExample.java From termd with Apache License 2.0 | 5 votes |
public static void handle(TtyConnection conn) { // The reverse function simply reverse the edit buffer Function reverseFunction = new ReverseFunction(); ReadlineExample.readline( // Bind reverse to Ctrl-g to the reverse function new Readline(Keymap.getDefault().bindFunction("\\C-g", "reverse")). addFunctions(Function.loadDefaults()).addFunction(reverseFunction), conn); }