Skip to content

Commit

Permalink
change EmissaryServer to use an enum for mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jpdahlke committed Jan 7, 2025
1 parent 1e9b231 commit 6d80005
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 76 deletions.
32 changes: 10 additions & 22 deletions src/main/java/emissary/command/ServerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import emissary.client.EmissaryResponse;
import emissary.command.converter.ProjectBaseConverter;
import emissary.command.validator.ServerModeValidator;
import emissary.core.EmissaryException;
import emissary.core.EmissaryRuntimeException;
import emissary.command.converter.ServerModeConverter;
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 = ServerModeConverter.class,
defaultValue = "standalone")
private EmissaryNode.EmissaryMode 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.EmissaryMode 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
20 changes: 20 additions & 0 deletions src/main/java/emissary/command/converter/ServerModeConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package emissary.command.converter;

import emissary.directory.EmissaryNode;

import picocli.CommandLine.ITypeConverter;

public class ServerModeConverter implements ITypeConverter<EmissaryNode.EmissaryMode> {

@Override
public EmissaryNode.EmissaryMode convert(String s) throws Exception {
switch (s.toLowerCase()) {
case "cluster":
return EmissaryNode.EmissaryMode.CLUSTER;
case "standalone":
return EmissaryNode.EmissaryMode.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 EmissaryMode {
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 EmissaryMode nodeMode;
protected boolean nodeNameIsDefault = false;
@Nullable
protected String nodeServiceType = null;

protected boolean strictStartupMode = false;

public EmissaryNode() {
this(DEFAULT_NODE_MODE);
this(EmissaryMode.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(EmissaryMode 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 EmissaryMode 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(EmissaryMode.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.EmissaryMode.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.EmissaryMode 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

0 comments on commit 6d80005

Please sign in to comment.