Updated config mechanism and added config for the server
Additionally fixed a small bug in EnvoyLog and envoy.server.Startup, fixed Receiver not stopping when the server was stopped and added access token authorization for the server config
This commit is contained in:
@ -3,10 +3,8 @@ package envoy.client.data;
|
||||
import static java.util.function.Function.identity;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import envoy.data.Config;
|
||||
import envoy.data.ConfigItem;
|
||||
|
||||
/**
|
||||
* Implements a configuration specific to the Envoy Client with default values
|
||||
@ -33,15 +31,13 @@ public class ClientConfig extends Config {
|
||||
}
|
||||
|
||||
private ClientConfig() {
|
||||
items.put("server", new ConfigItem<>("server", "s", identity(), null, true));
|
||||
items.put("port", new ConfigItem<>("port", "p", Integer::parseInt, null, true));
|
||||
items.put("localDB", new ConfigItem<>("localDB", "db", File::new, new File("localDB"), true));
|
||||
items.put("ignoreLocalDB", new ConfigItem<>("ignoreLocalDB", "nodb", Boolean::parseBoolean, false, false));
|
||||
items.put("homeDirectory", new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy"), true));
|
||||
items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.OFF, true));
|
||||
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.OFF, true));
|
||||
items.put("user", new ConfigItem<>("user", "u", identity()));
|
||||
items.put("password", new ConfigItem<>("password", "pw", identity()));
|
||||
super(".envoy");
|
||||
put("server", "s", identity(), true);
|
||||
put("port", "p", Integer::parseInt, true);
|
||||
put("localDB", "db", File::new, true);
|
||||
put("ignoreLocalDB", "nodb", Boolean::parseBoolean);
|
||||
put("user", "u", identity());
|
||||
put("password", "pw", identity());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,25 +62,10 @@ public class ClientConfig extends Config {
|
||||
* @return {@code true} if the local database is to be ignored
|
||||
* @since Envoy Client v0.3-alpha
|
||||
*/
|
||||
public Boolean isIgnoreLocalDB() { return (Boolean) items.get("ignoreLocalDB").get(); }
|
||||
|
||||
/**
|
||||
* @return the directory in which all local files are saves
|
||||
* @since Envoy Client v0.2-alpha
|
||||
*/
|
||||
public File getHomeDirectory() { return (File) items.get("homeDirectory").get(); }
|
||||
|
||||
/**
|
||||
* @return the minimal {@link Level} to log inside the log file
|
||||
* @since Envoy Client v0.2-alpha
|
||||
*/
|
||||
public Level getFileLevelBarrier() { return (Level) items.get("fileLevelBarrier").get(); }
|
||||
|
||||
/**
|
||||
* @return the minimal {@link Level} to log inside the console
|
||||
* @since Envoy Client v0.2-alpha
|
||||
*/
|
||||
public Level getConsoleLevelBarrier() { return (Level) items.get("consoleLevelBarrier").get(); }
|
||||
public Boolean isIgnoreLocalDB() {
|
||||
final var ignoreLocalDB = items.get("ignoreLocalDB").get();
|
||||
return ignoreLocalDB != null && (Boolean) ignoreLocalDB;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the user name
|
||||
|
@ -24,6 +24,8 @@ import envoy.util.SerializationUtils;
|
||||
*/
|
||||
public class Receiver extends Thread {
|
||||
|
||||
private boolean isAlive = true;
|
||||
|
||||
private final InputStream in;
|
||||
private final Map<Class<?>, Consumer<?>> processors = new HashMap<>();
|
||||
|
||||
@ -49,7 +51,7 @@ public class Receiver extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
while (true) {
|
||||
while (isAlive)
|
||||
try {
|
||||
// Read object length
|
||||
final byte[] lenBytes = new byte[4];
|
||||
@ -64,6 +66,12 @@ public class Receiver extends Thread {
|
||||
|
||||
// Catch LV encoding errors
|
||||
if (len != bytesRead) {
|
||||
// Server has stopped sending, i.e. because he went offline
|
||||
if (len == 0 && bytesRead == -1) {
|
||||
isAlive = false;
|
||||
logger.log(Level.WARNING, "Lost connection to the server. Exiting receiver");
|
||||
continue;
|
||||
}
|
||||
logger.log(Level.WARNING,
|
||||
String.format("LV encoding violated: expected %d bytes, received %d bytes. Discarding object...", len, bytesRead));
|
||||
continue;
|
||||
@ -87,7 +95,6 @@ public class Receiver extends Thread {
|
||||
} catch (final Exception e) {
|
||||
logger.log(Level.SEVERE, "Error on receiver thread", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -102,7 +109,7 @@ public class Receiver extends Thread {
|
||||
|
||||
/**
|
||||
* Adds a map of object processors to this {@link Receiver}.
|
||||
*
|
||||
*
|
||||
* @param processors the processors to add the processors to add
|
||||
* @since Envoy Client v0.1-beta
|
||||
*/
|
||||
|
@ -2,7 +2,6 @@ package envoy.client.ui;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -19,7 +18,6 @@ import envoy.data.GroupMessage;
|
||||
import envoy.data.Message;
|
||||
import envoy.event.GroupMessageStatusChange;
|
||||
import envoy.event.MessageStatusChange;
|
||||
import envoy.exception.EnvoyException;
|
||||
import envoy.util.EnvoyLog;
|
||||
|
||||
/**
|
||||
@ -57,30 +55,13 @@ public final class Startup extends Application {
|
||||
@Override
|
||||
public void start(Stage stage) throws Exception {
|
||||
try {
|
||||
// Load the configuration from client.properties first
|
||||
final Properties properties = new Properties();
|
||||
properties.load(Startup.class.getClassLoader().getResourceAsStream("client.properties"));
|
||||
config.load(properties);
|
||||
|
||||
// Override configuration values with command line arguments
|
||||
final String[] args = getParameters().getRaw().toArray(new String[0]);
|
||||
if (args.length > 0) config.load(args);
|
||||
|
||||
// Check if all mandatory configuration values have been initialized
|
||||
if (!config.isInitialized()) throw new EnvoyException("Configuration is not fully initialized");
|
||||
} catch (final Exception e) {
|
||||
config.loadAll(Startup.class, "client.properties", getParameters().getRaw().toArray(new String[0]));
|
||||
EnvoyLog.initialize(config);
|
||||
} catch (final IllegalStateException e) {
|
||||
new Alert(AlertType.ERROR, "Error loading configuration values:\n" + e);
|
||||
logger.log(Level.SEVERE, "Error loading configuration values: ", e);
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
// Setup logger for the envoy package
|
||||
EnvoyLog.initialize(config);
|
||||
EnvoyLog.attach("envoy");
|
||||
EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
|
||||
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());
|
||||
|
||||
logger.log(Level.INFO, "Envoy starting...");
|
||||
|
||||
// Initialize the local database
|
||||
|
@ -1,4 +1,6 @@
|
||||
server=localhost
|
||||
port=8080
|
||||
localDB=localDB
|
||||
consoleLevelBarrier=FINER
|
||||
server=localhost
|
||||
port=8080
|
||||
localDB=localDB
|
||||
consoleLevelBarrier=FINER
|
||||
fileLevelBarrier=OFF
|
||||
ignoreLocalDB=false
|
||||
|
Reference in New Issue
Block a user