Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli port input #736

Merged
merged 8 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/cli/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public class Arguments {
description = "execute kernel with selected database directory")
private String directory = null;

@Option(
names = {"-p", "--port"},
description = "execute kernel with selected port")
private String port = null;

// offline database query and update
@Option(
names = {"ps", "--state"},
Expand Down Expand Up @@ -240,6 +245,10 @@ public String getDirectory() {
return directory;
}

public String getPort() {
return port;
}

public String getPruneStateOption() {
return pruntStateOption;
}
Expand Down
45 changes: 39 additions & 6 deletions modAionImpl/src/org/aion/zero/impl/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,37 @@ public ReturnType call(final String[] args, Cfg cfg) {
// true means the UUID must be set
boolean overwrite = cfg.fromXML(configFile);

// 4. can be influenced by the -d argument above
// determine the port configuration, can be combined with the -n, -d, -c, -i arguments
if (options.getPort() != null) {

int currentPort = cfg.getNet().getP2p().getPort();
int portNumber = currentPort;
boolean validPort = true;

try {
portNumber = Integer.parseInt(options.getPort());
} catch (NumberFormatException e) {
validPort = false;
System.out.println("Port must be an integer value");
}

if (portNumber < 0 || portNumber > 0xFFFF) {
validPort = false;
System.out.println("Port out of range: " + portNumber);
}

if (validPort && portNumber != currentPort) {
// update port in config
cfg.getNet().getP2p().setPort(portNumber);
overwrite = true;
System.out.println("Port set to: " + portNumber);
} else {
System.out.println("Using the current port configuration: " + currentPort);
}
// no return, allow for other parameters combined with -p
}

// 4. can be influenced by the -n, -d, -p arguments above

if (options.getConfig() != null) {
// network was already set above
Expand Down Expand Up @@ -229,15 +259,15 @@ public ReturnType call(final String[] args, Cfg cfg) {
return ReturnType.EXIT;
}

// 5. options that can be influenced by the -d and -n arguments
// 5. options that can be influenced by the -d, -n and -p arguments

if (options.isInfo()) {
System.out.println(
"Reading config file from: "
+ getRelativePath(configFile.getAbsolutePath()));
if (overwrite) {
// updating the file in case the user id was not set
cfg.toXML(new String[] {"--id=" + cfg.getId()}, configFile);
// updating the file in case the user id was not set; overwrite port
cfg.toXML(null, configFile);
}
printInfo(cfg);
return ReturnType.EXIT;
Expand All @@ -247,8 +277,8 @@ public ReturnType call(final String[] args, Cfg cfg) {
makeDirs(configFile, forkFile, cfg);

if (overwrite) {
// only updating the file in case the user id was not set
cfg.toXML(new String[] {"--id=" + cfg.getId()}, cfg.getExecConfigFile());
// updating the file in case the user id was not set; overwrite port
cfg.toXML(null, cfg.getExecConfigFile());
}

// set correct keystore directory
Expand Down Expand Up @@ -861,6 +891,9 @@ Set<String> getSkippedTasks(Arguments options, TaskPriority breakingTaskPriority
if (options.getDirectory() != null) {
skippedTasks.add("--datadir");
}
if (options.getPort() != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some tests that use the port option in the CliTest.testCheckArguments as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for adding some tests for the input validation

skippedTasks.add("--port");
}
if (options.getConfig() != null) {
skippedTasks.add("--config");
}
Expand Down
8 changes: 6 additions & 2 deletions modAionImpl/src/org/aion/zero/impl/config/CfgAion.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ public void toXML(final String[] args, File file) {
if (subArgsArr.length > 0) {
List<String> _nodes = new ArrayList<>();
for (String subArg : subArgsArr) {
if (!subArg.equals("")) _nodes.add(subArg);
if (!subArg.equals("")) {
_nodes.add(subArg);
}
}
this.getNet().setNodes(_nodes.toArray(new String[0]));
}
Expand All @@ -354,7 +356,9 @@ public void toXML(final String[] args, File file) {
override = true;
String[] subArgsArr = arg.replace("--p2p=", "").split(",");
if (subArgsArr.length == 2) {
this.getNet().getP2p().setIp(subArgsArr[0]);
if (!subArgsArr[0].equals("")) {
this.getNet().getP2p().setIp(subArgsArr[0]);
}
this.getNet().getP2p().setPort(Integer.parseInt(subArgsArr[1]));
}
}
Expand Down
Loading