use of io.vertx.ext.shell.command.Command in project vertx-examples by vert-x3.
the class HelloWorldCommand method start.
@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();
}
});
}
use of io.vertx.ext.shell.command.Command in project vertx-examples by vert-x3.
the class StarwarsCommand method start.
@Override
public void start() throws Exception {
Command starwars = CommandBuilder.command("starwars").processHandler(process -> {
// Connect the client
NetClient client = process.vertx().createNetClient();
client.connect(23, "towel.blinkenlights.nl", ar -> {
if (ar.succeeded()) {
NetSocket socket = ar.result();
// Ctrl-C closes the socket
process.interruptHandler(v -> {
socket.close();
});
//
socket.handler(buff -> {
// Push the data to the Shell
process.write(buff.toString("UTF-8"));
}).exceptionHandler(err -> {
err.printStackTrace();
socket.close();
});
// When socket closes, end the command
socket.closeHandler(v -> {
process.end();
});
} else {
process.write("Could not connect to remote Starwars server\n").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();
}
});
}
use of io.vertx.ext.shell.command.Command in project vertx-examples by vert-x3.
the class TopCommand method start.
@Override
public void start() throws Exception {
Command starwars = CommandBuilder.command("top").processHandler(process -> {
long id = process.vertx().setPeriodic(500, id_ -> {
StringBuilder buf = new StringBuilder();
Formatter formatter = new Formatter(buf);
List<Thread> threads = new ArrayList<>(Thread.getAllStackTraces().keySet());
for (int i = 1; i <= process.height(); 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$-20s %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());
}
}
}
process.write(buf.toString());
});
// Terminate when user hits Ctrl-C
process.interruptHandler(v -> {
vertx.cancelTimer(id);
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();
}
});
}
use of io.vertx.ext.shell.command.Command in project vertx-examples by vert-x3.
the class EchoKeyboardCommand method start.
@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();
}
});
}
use of io.vertx.ext.shell.command.Command in project vertx-examples by vert-x3.
the class WgetCommand method start.
@Override
public void start() throws Exception {
// Create the wget CLI
CLI cli = CLI.create("wget").setSummary("Wget implemented with Vert.x HTTP client").addArgument(new Argument().setIndex(0).setArgName("http-url").setDescription("the HTTP uri to get"));
// Create the command
Command helloWorld = CommandBuilder.command(cli).processHandler(process -> {
URL url;
try {
url = new URL(process.commandLine().getArgumentValue(0));
} catch (MalformedURLException e) {
process.write("Bad url\n").end();
return;
}
HttpClient client = process.vertx().createHttpClient();
process.write("Connecting to " + url + "\n");
int port = url.getPort();
if (port == -1) {
port = 80;
}
HttpClientRequest req = client.get(port, url.getHost(), url.getPath());
req.exceptionHandler(err -> {
process.write("wget: error " + err.getMessage() + "\n");
process.end();
});
req.handler(resp -> {
process.write(resp.statusCode() + " " + resp.statusMessage() + "\n");
String contentType = resp.getHeader("Content-Type");
String contentLength = resp.getHeader("Content-Length");
process.write("Length: " + (contentLength != null ? contentLength : "unspecified"));
if (contentType != null) {
process.write("[" + contentType + "]");
}
process.write("\n");
process.end();
});
req.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();
}
});
}
Aggregations