Skip to content

Commit

Permalink
Implement requested changes in PR bisq-network#4097
Browse files Browse the repository at this point in the history
Change member name OptionParser cmdParser -> parser.
Change server listening port to 9998, client port to 9998.
Change constructor argument from String[] param -> args.
Print the result only, w/out exec time.
Handle help command & print that to stdout;  print help
triggered by user error to stderr.
Use explicit system SUCCESS/FAIL codes in System.exit(0 || 1).
  • Loading branch information
ghubstan authored and cbeams committed Apr 2, 2020
1 parent 2c90b50 commit f8d902c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
37 changes: 18 additions & 19 deletions cli/src/main/java/bisq/cli/app/BisqCliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

import static bisq.cli.app.CommandParser.GETBALANCE;
import static bisq.cli.app.CommandParser.GETVERSION;
import static bisq.cli.app.CommandParser.HELP;
import static bisq.cli.app.CommandParser.STOPSERVER;
import static java.lang.String.format;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.out;

Expand All @@ -43,27 +43,23 @@
@Slf4j
public class BisqCliMain {

private static final int EXIT_SUCCESS = 0;
private static final int EXIT_FAILURE = 1;

private final ManagedChannel channel;
private final CliCommand cmd;
private final OptionParser cmdParser;
private final OptionParser parser;

public static void main(String[] args) {
new BisqCliMain("localhost", 8888, args);
new BisqCliMain("localhost", 9998, args);
}

private BisqCliMain(String host, int port, String[] params) {
private BisqCliMain(String host, int port, String[] args) {
// Channels are secure by default (via SSL/TLS); for the example disable TLS to avoid needing certificates.
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext().build());

long startTs = currentTimeMillis();

String command = parseCommand(params);
String command = parseCommand(args);
String result = runCommand(command);

// First response is rather slow (300 ms) but following responses are fast (3-5 ms).
// log.info("{}\t{}", result, cmd.responseTime.apply(startTs));
out.println(result + "\t" + cmd.responseTime.apply(startTs));

out.println(result);
try {
shutdown(); // Orderly channel shutdown
} catch (InterruptedException ignored) {
Expand All @@ -76,12 +72,15 @@ private BisqCliMain(String host, int port, String[] params) {
private BisqCliMain(ManagedChannel channel) {
this.channel = channel;
this.cmd = new CliCommand(channel);
this.cmdParser = new CommandParser().configure();
this.parser = new CommandParser().configure();
}

private String runCommand(String command) {
final String result;
switch (command) {
case HELP:
CommandParser.printHelp();
exit(EXIT_SUCCESS);
case GETBALANCE:
long satoshis = cmd.getBalance();
result = satoshis == -1 ? "Server initializing..." : cmd.prettyBalance.apply(satoshis);
Expand All @@ -100,18 +99,18 @@ private String runCommand(String command) {
}

private String parseCommand(String[] params) {
OptionSpec<String> nonOptions = cmdParser.nonOptions().ofType(String.class);
OptionSet options = cmdParser.parse(params);
OptionSpec<String> nonOptions = parser.nonOptions().ofType(String.class);
OptionSet options = parser.parse(params);
List<String> detectedOptions = nonOptions.values(options);
if (detectedOptions.isEmpty()) {
CommandParser.printUsage();
exit(0);
CommandParser.printHelp();
exit(EXIT_FAILURE);
}
return detectedOptions.get(0);
}

private void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(1, TimeUnit.SECONDS);
exit(0);
exit(EXIT_SUCCESS);
}
}
4 changes: 0 additions & 4 deletions cli/src/main/java/bisq/cli/app/CliCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import lombok.extern.slf4j.Slf4j;

import static java.lang.System.currentTimeMillis;

@Slf4j
final class CliCommand {

Expand All @@ -32,8 +30,6 @@ final class CliCommand {
@SuppressWarnings("BigDecimalMethodWithoutRoundingCalled")
final Function<Long, String> prettyBalance = (sats) -> btcFormat.format(BigDecimal.valueOf(sats).divide(satoshiDivisor));

final Function<Long, String> responseTime = (t0) -> "(response time: " + (currentTimeMillis() - t0) + " ms)";

CliCommand(ManagedChannel channel) {
getBalanceStub = GetBalanceGrpc.newBlockingStub(channel);
getVersionStub = GetVersionGrpc.newBlockingStub(channel);
Expand Down
6 changes: 3 additions & 3 deletions cli/src/main/java/bisq/cli/app/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import joptsimple.OptionParser;

import static java.lang.System.err;
import static java.lang.System.out;

final class CommandParser {

Expand All @@ -20,8 +20,8 @@ OptionParser configure() {
return parser;
}

static void printUsage() {
err.println("Usage: bisq-cli getbalance | getversion");
static void printHelp() {
out.println("Usage: bisq-cli getbalance | getversion");
}

}
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/grpc/BisqGrpcServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public void stop() {

private void start() throws IOException {
// TODO add to options
int port = 8888;
int port = 9998;

// Config services
server = ServerBuilder.forPort(port)
Expand Down

0 comments on commit f8d902c

Please sign in to comment.