basic project structure + some argument handling

This commit is contained in:
overflowerror 2018-09-12 21:52:13 +02:00
parent 0a9877b2a6
commit 1282abd4d7
27 changed files with 580 additions and 0 deletions

View file

@ -0,0 +1,5 @@
package net.persei.oracle;
public interface Application {
void execute();
}

View file

@ -0,0 +1,27 @@
package net.persei.oracle;
import net.persei.oracle.exceptions.PropertyAlreadySetException;
import net.persei.oracle.exceptions.SetupException;
import net.persei.oracle.setup.Settings;
public class ApplicationContext {
private ApplicationContext() {
}
private static ApplicationContext instance;
public static ApplicationContext getInstance() {
if (instance == null)
instance = new ApplicationContext();
return instance;
}
private Settings settings;
public void setSettings(Settings settings) throws SetupException {
if (this.settings != null)
throw new PropertyAlreadySetException("settings");
this.settings = settings;
}
}

View file

@ -0,0 +1,57 @@
package net.persei.oracle;
import net.persei.oracle.client.OracleClient;
import net.persei.oracle.exceptions.SetupException;
import net.persei.oracle.exceptions.ShutdownException;
import net.persei.oracle.network.objects.Version;
import net.persei.oracle.network.objects.VersionMode;
import net.persei.oracle.server.OracleServer;
import net.persei.oracle.setup.ArgumentParser;
import net.persei.oracle.setup.ConfigParser;
import net.persei.oracle.setup.ExecutionType;
import net.persei.oracle.setup.Settings;
public class Oracle {
public static final String APPLICATION_NAME = "ServiceOracle";
public static final Version CURRENT_VERSION = new Version(0, 1, VersionMode.PRE_ALPHA);
public static final String COPYRIGHT = "overflow";
public static final String LICENCE = "Released under the MIT licence.";
public static void main(String[] args) throws SetupException {
try {
Settings settings = getSettings(args);
ApplicationContext context = ApplicationContext.getInstance();
context.setSettings(settings);
Application application;
if (settings.getType() == ExecutionType.SERVER) {
application = new OracleServer();
} else {
application = new OracleClient();
}
application.execute();
} catch (ShutdownException e) {
System.exit(e.getExitCode());
}
}
private static Settings getSettings(String[] args) throws SetupException {
Settings settings = new Settings();
ArgumentParser argumentParser = new ArgumentParser(settings);
argumentParser.parse(args);
ConfigParser configParser = new ConfigParser(settings);
configParser.parse();
settings.checkValidity();
return settings;
}
}

View file

@ -0,0 +1,13 @@
package net.persei.oracle.client;
import net.persei.oracle.Application;
public class OracleClient implements Application {
@Override
public void execute() {
// TODO Auto-generated method stub
}
}

View file

@ -0,0 +1,10 @@
package net.persei.oracle.exceptions;
public class ArgumentParsingException extends SetupException {
public ArgumentParsingException(String message) {
super(message);
}
private static final long serialVersionUID = -433854290723809252L;
}

View file

@ -0,0 +1,11 @@
package net.persei.oracle.exceptions;
public class ConfigParsingException extends SetupException {
public ConfigParsingException(String message) {
super(message);
}
private static final long serialVersionUID = -7011858104070362180L;
}

View file

@ -0,0 +1,10 @@
package net.persei.oracle.exceptions;
public class PropertyAlreadySetException extends SetupException {
public PropertyAlreadySetException(String string) {
super(string);
}
private static final long serialVersionUID = -4052346277556698491L;
}

View file

@ -0,0 +1,10 @@
package net.persei.oracle.exceptions;
public class SettingsNotValidException extends SetupException {
public SettingsNotValidException(String message) {
super(message);
}
private static final long serialVersionUID = 1418784120827764515L;
}

View file

@ -0,0 +1,13 @@
package net.persei.oracle.exceptions;
public class SetupException extends Exception {
public SetupException(String message) {
super(message);
}
public SetupException(Throwable cause) {
super(cause);
}
private static final long serialVersionUID = 5791250500083364858L;
}

View file

@ -0,0 +1,21 @@
package net.persei.oracle.exceptions;
public class ShutdownException extends SetupException {
private static final long serialVersionUID = -3829264217427478192L;
public static final int ERROR = 1;
public static final int SUCCESS = 0;
private int exitCode;
public ShutdownException(int exitCode) {
super("Exitcode: " + exitCode);
this.exitCode = exitCode;
}
public int getExitCode() {
return exitCode;
}
}

View file

@ -0,0 +1,10 @@
package net.persei.oracle.network.objects;
import java.io.Serializable;
public class Contact implements Serializable {
private static final long serialVersionUID = 2506758055297999502L;
private String name;
private String email;
}

View file

@ -0,0 +1,18 @@
package net.persei.oracle.network.objects;
import java.io.Serializable;
import java.time.Instant;
public class HeartBeat implements Serializable {
private static final long serialVersionUID = -9072594881334560762L;
private Instant time;
public HeartBeat() {
time = Instant.now();
}
public Instant getTime() {
return time;
}
}

View file

@ -0,0 +1,9 @@
package net.persei.oracle.network.objects;
import java.io.Serializable;
public class Hello implements Serializable {
private static final long serialVersionUID = 5393259647355889964L;
private Version version;
}

View file

@ -0,0 +1,16 @@
package net.persei.oracle.network.objects;
import java.io.Serializable;
import java.net.InetAddress;
import java.time.Instant;
import java.util.List;
public class Host implements Serializable {
private static final long serialVersionUID = 5533783857823198187L;
private HostAuthKey key;
private Instant started;
private String hostname;
private List<InetAddress> addresses;
private List<Service> services;
}

View file

@ -0,0 +1,44 @@
package net.persei.oracle.network.objects;
import java.io.Serializable;
import java.util.Arrays;
import net.persei.tools.Base62;
public class HostAuthKey implements Serializable {
private static final long serialVersionUID = 5365701218557758110L;
private byte key[];
public HostAuthKey(String key) {
this.key = Base62.decode(key);
}
@Override
public String toString() {
return Base62.encode(key);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(key);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
HostAuthKey other = (HostAuthKey) obj;
if (!Arrays.equals(key, other.key))
return false;
return true;
}
}

View file

@ -0,0 +1,10 @@
package net.persei.oracle.network.objects;
import java.io.Serializable;
public class Port implements Serializable {
private static final long serialVersionUID = 8058028964271809291L;
private int port;
private PortProtocol protocol;
}

View file

@ -0,0 +1,5 @@
package net.persei.oracle.network.objects;
public enum PortProtocol {
TCP, UDP
}

View file

@ -0,0 +1,14 @@
package net.persei.oracle.network.objects;
import java.io.Serializable;
import java.util.List;
public class Service implements Serializable {
private static final long serialVersionUID = -5125680499936971528L;
private String name;
private String description;
private boolean enabled;
private List<Port> ports;
private Contact contact;
}

View file

@ -0,0 +1,10 @@
package net.persei.oracle.network.objects;
import java.io.Serializable;
import java.util.List;
public class Update implements Serializable {
long serialVersionUID = 1966048120690745996L;
private List<Service> services;
}

View file

@ -0,0 +1,94 @@
package net.persei.oracle.network.objects;
import java.io.Serializable;
public class Version implements Serializable, Comparable<Version> {
private static final long serialVersionUID = -287445485984060288L;
private int major;
private int minor;
private int patch;
private VersionMode mode;
public Version(int major, int minor, int patch, VersionMode mode) {
this.major = major;
this.minor = minor;
this.patch = patch;
this.mode = mode;
}
public Version(int major, int minor, int patch) {
this(major, minor, patch, VersionMode.RELEASE);
}
public Version(int major, int minor) {
this(major, minor, 0);
}
public Version(int major) {
this(major, 0);
}
public Version(int major, int minor, VersionMode mode) {
this(major, minor, 0, mode);
}
public Version(int major, VersionMode mode) {
this(major, 0, 0, mode);
}
@Override
public int compareTo(Version o) {
if (this.major > o.major)
return 1;
if (this.major < o.major)
return -1;
if (this.minor > o.minor)
return 1;
if (this.minor < o.minor)
return -1;
if (this.patch > o.patch)
return 1;
if (this.patch < o.patch)
return -1;
return this.mode.compareTo(o.mode);
}
@Override
public String toString() {
return "V " + major + "." + minor + "." + patch + "." + mode;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + major;
result = prime * result + minor;
result = prime * result + ((mode == null) ? 0 : mode.hashCode());
result = prime * result + patch;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Version other = (Version) obj;
if (major != other.major)
return false;
if (minor != other.minor)
return false;
if (mode != other.mode)
return false;
if (patch != other.patch)
return false;
return true;
}
}

View file

@ -0,0 +1,15 @@
package net.persei.oracle.network.objects;
public enum VersionMode {
PRE_ALPHA, ALPHA, BETA, PRERELEASE, RELEASE;
@Override
public String toString() {
if (this == PRE_ALPHA)
return "PRE ALPHA";
if (this == VersionMode.RELEASE)
return "";
return this.name();
}
}

View file

@ -0,0 +1,13 @@
package net.persei.oracle.server;
import net.persei.oracle.Application;
public class OracleServer implements Application {
@Override
public void execute() {
// TODO Auto-generated method stub
}
}

View file

@ -0,0 +1,83 @@
package net.persei.oracle.setup;
import java.io.File;
import java.io.FileNotFoundException;
import net.persei.oracle.Oracle;
import net.persei.oracle.exceptions.SetupException;
import net.persei.oracle.exceptions.ShutdownException;
import net.persei.tools.getopt.ArgumentType;
import net.persei.tools.getopt.Option;
import net.persei.tools.getopt.OptionParser;
import net.persei.tools.getopt.OptionType;
import net.persei.tools.getopt.exceptions.OptionAlreadyGivenException;
import net.persei.tools.getopt.exceptions.OptionDoesNotHaveArgumentException;
import net.persei.tools.getopt.exceptions.OptionException;
import net.persei.tools.getopt.exceptions.OptionNeedsArgumentException;
import net.persei.tools.getopt.exceptions.SilentShutdownException;
import net.persei.tools.getopt.exceptions.UnknownOptionException;
public class ArgumentParser {
private static final String HELP_TEXT = "[help text]";
private OptionParser optionParser;
public ArgumentParser(Settings settings) {
optionParser = new OptionParser();
try {
optionParser.addOption(new Option().setShortOption('c').setLongOption("config")
.setOptionType(OptionType.NECESSARY).setArgumentType(ArgumentType.NECESSARY).setSingleton(true)
.setSynopsis("set config file").setHandler((argument) -> {
File file = new File(argument);
if (!file.exists())
throw new OptionException(new FileNotFoundException(argument));
settings.setConfigFile(file);
}));
optionParser.addOption(new Option().setLongOption("client").setArgumentType(ArgumentType.NONE)
.setOptionType(OptionType.OPTIONAL).setSingleton(true).setSynopsis("client mode")
.setHandler((argument) -> {
settings.setType(ExecutionType.CLIENT);
}));
optionParser.addOption(new Option().setLongOption("server").setArgumentType(ArgumentType.NONE)
.setOptionType(OptionType.OPTIONAL).setSingleton(true).setSynopsis("client mode")
.setHandler((argument) -> {
settings.setType(ExecutionType.SERVER);
}));
optionParser.setHelp(
new Option().setShortOption('h').setLongOption("help").setSynopsis("display this help"),
Oracle.APPLICATION_NAME, Oracle.CURRENT_VERSION.toString(), Oracle.COPYRIGHT, Oracle.LICENCE,
HELP_TEXT, "");
} catch (OptionException e) {
e.printStackTrace();
}
}
public void parse(String[] args) throws SetupException {
try {
args = optionParser.parse(args);
} catch (SilentShutdownException e) {
throw new ShutdownException(e.getErrorCode());
} catch (OptionNeedsArgumentException e) {
System.err.println("The option " + e.getMessage() + " needs an argument.");
throw new ShutdownException(ShutdownException.ERROR);
} catch (OptionAlreadyGivenException e) {
System.err.println("The option " + e.getMessage() + " was already given.");
throw new ShutdownException(ShutdownException.ERROR);
} catch (UnknownOptionException e) {
System.err.println("The option " + e.getMessage() + " is unknown.");
throw new ShutdownException(ShutdownException.ERROR);
} catch (OptionDoesNotHaveArgumentException e) {
System.err.println("The option " + e.getMessage() + " does not have an argument.");
throw new ShutdownException(ShutdownException.ERROR);
} catch (OptionException e) {
throw new SetupException(e);
}
}
}

View file

@ -0,0 +1,14 @@
package net.persei.oracle.setup;
public class ConfigParser {
public ConfigParser(Settings settings) {
// TODO Auto-generated constructor stub
}
public void parse() {
// TODO Auto-generated method stub
}
}

View file

@ -0,0 +1,6 @@
package net.persei.oracle.setup;
public class DefaultSettings {
public static final ExecutionType DEFAULT_EXECUTION_TYPE = ExecutionType.CLIENT;
}

View file

@ -0,0 +1,5 @@
package net.persei.oracle.setup;
public enum ExecutionType {
SERVER, CLIENT
}

View file

@ -0,0 +1,37 @@
package net.persei.oracle.setup;
import java.io.File;
import net.persei.oracle.exceptions.SettingsNotValidException;
public class Settings {
private ExecutionType type = DefaultSettings.DEFAULT_EXECUTION_TYPE;
private File configFile = null;
public void checkValidity() throws SettingsNotValidException {
if (type == null)
throw new SettingsNotValidException("No execution type set.");
if (configFile == null)
throw new SettingsNotValidException("No config file set.");
}
public ExecutionType getType() {
return type;
}
public void setType(ExecutionType type) {
this.type = type;
}
public File getConfigFile() {
return configFile;
}
public void setConfigFile(File configFile) {
this.configFile = configFile;
}
}