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

change EmissaryServer to use an enum for mode #1042

Merged
merged 3 commits into from
Jan 17, 2025
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
32 changes: 10 additions & 22 deletions src/main/java/emissary/command/ServerCommand.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package emissary.command;

import emissary.client.EmissaryResponse;
import emissary.command.converter.ModeConverter;
import emissary.command.converter.ProjectBaseConverter;
import emissary.command.validator.ServerModeValidator;
import emissary.core.EmissaryException;
import emissary.core.EmissaryRuntimeException;
import emissary.directory.EmissaryNode;
import emissary.server.EmissaryServer;
import emissary.server.api.Pause;

Expand All @@ -28,15 +27,9 @@ public class ServerCommand extends ServiceCommand {

public static final int DEFAULT_PORT = 8001;

private String mode = "standalone";

@Option(names = {"-m", "--mode"}, description = "mode: standalone or cluster\nDefault: ${DEFAULT-VALUE}", defaultValue = "standalone")
@SuppressWarnings("unused")
private void setMode(String value) {
ServerModeValidator smv = new ServerModeValidator();
smv.validate("mode", value);
mode = value;
}
@Option(names = {"-m", "--mode"}, description = "mode: standalone or cluster\nDefault: ${DEFAULT-VALUE}", converter = ModeConverter.class,
defaultValue = "standalone")
private EmissaryNode.Mode mode;

@Option(names = "--staticDir", description = "path to static assets, loaded from classpath otherwise", converter = ProjectBaseConverter.class)
private Path staticDir;
Expand All @@ -60,7 +53,7 @@ public int getDefaultPort() {
return DEFAULT_PORT;
}

public String getMode() {
public EmissaryNode.Mode getMode() {
return mode;
}

Expand Down Expand Up @@ -89,20 +82,15 @@ public boolean shouldStrictMode() {
public void setupCommand() {
setupHttp();
reinitLogback();
try {
setupServer();
} catch (EmissaryException e) {
LOG.error("Got an exception", e);
throw new EmissaryRuntimeException(e);
}
setupServer();
}

public void setupServer() throws EmissaryException {
public void setupServer() {
String flavorMode;
if (getFlavor() == null) {
flavorMode = getMode().toUpperCase(Locale.getDefault());
flavorMode = getMode().toString();
} else {
flavorMode = getMode().toUpperCase(Locale.getDefault()) + "," + getFlavor();
flavorMode = getMode().toString() + "," + getFlavor();
}

if (shouldStrictMode()) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/emissary/command/converter/ModeConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package emissary.command.converter;

import emissary.directory.EmissaryNode;

import picocli.CommandLine.ITypeConverter;

import java.util.Locale;

public class ModeConverter implements ITypeConverter<EmissaryNode.Mode> {

@Override
public EmissaryNode.Mode convert(String s) throws Exception {
switch (s.toLowerCase(Locale.getDefault())) {
case "cluster":
return EmissaryNode.Mode.CLUSTER;
case "standalone":
return EmissaryNode.Mode.STANDALONE;
default:
throw new IllegalArgumentException("Unknown mode: " + s);
}
}
}
21 changes: 0 additions & 21 deletions src/main/java/emissary/command/validator/ServerModeValidator.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/emissary/command/validator/package-info.java

This file was deleted.

23 changes: 11 additions & 12 deletions src/main/java/emissary/directory/EmissaryNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public class EmissaryNode {
/** Property that determines if server will shut down in the event a place fails to start */
public static final String STRICT_STARTUP_MODE = "strict.mode";

// types are feeder, worker, standalone
// TODO: make an enum for these
private static final String DEFAULT_NODE_MODE = "standalone";
public enum Mode {
STANDALONE, CLUSTER;
}

@Nullable
protected String nodeName = null;
Expand All @@ -76,23 +76,22 @@ public class EmissaryNode {
// this is the OS for all practical purposes
@Nullable
protected String nodeType = null;
@Nullable
protected String nodeMode = null; // probably better as nodeType, but that requires a refactor
protected Mode nodeMode;
protected boolean nodeNameIsDefault = false;
@Nullable
protected String nodeServiceType = null;

protected boolean strictStartupMode = false;

public EmissaryNode() {
this(DEFAULT_NODE_MODE);
this(Mode.STANDALONE);
}

/**
* Construct the node. The node name and port are from system properties. The node type is based on the os.name in this
* implementation
*/
public EmissaryNode(String nodeMode) {
public EmissaryNode(Mode nodeMode) {
this.nodeMode = nodeMode;
this.nodeName = System.getProperty(NODE_NAME_PROPERTY);
if (this.nodeName == null) {
Expand All @@ -113,6 +112,10 @@ public EmissaryNode(String nodeMode) {
this.strictStartupMode = Boolean.parseBoolean(System.getProperty(STRICT_STARTUP_MODE, String.valueOf(false)));
}

public Mode getNodeMode() {
return nodeMode;
}

/**
* The node name
*/
Expand Down Expand Up @@ -173,11 +176,7 @@ public boolean isValidStandalone() {
* True if this node appears to be a stand-alone (non P2P) node
*/
public boolean isStandalone() {
return isValidStandalone() && getNodeMode().equals("standalone");
}

private Object getNodeMode() {
return nodeMode;
return isValidStandalone() && getNodeMode().equals(Mode.STANDALONE);
}

public boolean isStrictStartupMode() {
Expand Down
20 changes: 3 additions & 17 deletions src/test/java/emissary/server/api/PeersIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void setup() throws Exception {
String projectBase = System.getenv(ConfigUtil.PROJECT_BASE_ENV);
ServerCommand cmd = ServerCommand.parse(ServerCommand.class, "-b ", projectBase, "-m", "cluster", "-p", "123456");
cmd.setupServer();
EmissaryServer server = EmissaryServer.init(cmd, new TestEmissaryNode());
EmissaryServer server = EmissaryServer.init(cmd, new TestEmissaryNode(EmissaryNode.Mode.CLUSTER));
Namespace.bind("EmissaryServer", server);

DirectoryPlace directoryPlace = new DirectoryPlace(DIRNAME, server.getNode());
Expand Down Expand Up @@ -108,25 +108,11 @@ void peersNoDirectoryPlace() throws NamespaceException {

static class TestEmissaryNode extends EmissaryNode {

public TestEmissaryNode() {
public TestEmissaryNode(EmissaryNode.Mode mode) {
super(mode);
nodeNameIsDefault = true;
}

@Override
public int getNodePort() {
return TEST_PORT;
}

@Override
public String getNodeName() {
return TEST_NODE;
}

@Override
public boolean isStandalone() {
return false;
}

@Override
public Configurator getPeerConfigurator() throws IOException {
// just go get this from the src/test/resources directory
Expand Down
Loading