Move presence init server to user list

This commit is contained in:
Arnaud Vergnet 2021-01-07 15:02:03 +01:00
parent 2929211c85
commit 7a36ef2c60
4 changed files with 45 additions and 35 deletions

View file

@ -2,7 +2,6 @@ package fr.insa.clavardator.server;
import fr.insa.clavardator.network.PeerConnection;
import java.net.InetAddress;
import java.util.ArrayList;
public interface Presence {

View file

@ -249,38 +249,14 @@ public class MainController implements Initializable {
* then init current user, and finally load user list.
*/
private void initBackend() {
ConfigLoader.load((Config config) -> {
initPresenceServer(config);
new DatabaseController().initTables(
() -> userList.retrievedPreviousUsers(
() -> currentUser.init(this::onInitError),
this::onInitError
), this::onInitError);
});
}
/**
* Initializes the presence server based on user config
*
* @param config The user config
*/
private void initPresenceServer(Config config) {
final Config.ServerConfig serverConfig = config.getServerConfig();
if (serverConfig.isEnabled()) {
try {
final PresenceType type = serverConfig.getType();
final String uri = serverConfig.getUri();
final int presencePort = serverConfig.getPresencePort();
final int proxyPort = serverConfig.getProxyPort();
presenceServer = PresenceFactory.create(type, uri, presencePort, proxyPort);
Log.v("INIT", "Presence server support enabled: " + type + "@" + uri + ':' + presencePort + " / proxy:" + proxyPort);
} catch (UnknownPresenceException e) {
Log.e("INIT", "Presence server type not found", e);
presenceServer = null;
}
} else {
Log.v("INIT", "Presence server support disabled.");
}
ConfigLoader.load(
(Config config) -> new DatabaseController().initTables(
() -> userList.retrievedPreviousUsers(
() -> currentUser.init(
() -> userList.initPresenceServer(config),
this::onInitError),
this::onInitError
), this::onInitError));
}
/**

View file

@ -29,7 +29,7 @@ public class CurrentUser extends User {
return UUID.randomUUID().toString();
}
public void init(ErrorCallback errorCallback) {
public void init(InitCallback callback, ErrorCallback errorCallback) {
final DatabaseController db = new DatabaseController();
db.getCurrentUser((user) -> {
if (user == null) {
@ -37,7 +37,10 @@ public class CurrentUser extends User {
Log.v(getClass().getSimpleName(), "No previous user found, generating id: " + id);
db.addCurrentUser(
new UserInformation(this),
() -> setState(State.NONE),
() -> {
setState(State.NONE);
callback.onFinish();
},
errorCallback);
} else {
id = user.id;
@ -48,6 +51,7 @@ public class CurrentUser extends User {
Log.v(getClass().getSimpleName(), "No username found, asking user");
setState(State.NONE);
}
callback.onFinish();
}
}, errorCallback);
}

View file

@ -1,9 +1,14 @@
package fr.insa.clavardator.users;
import fr.insa.clavardator.config.Config;
import fr.insa.clavardator.db.DatabaseController;
import fr.insa.clavardator.network.ConnectionListener;
import fr.insa.clavardator.network.NetDiscoverer;
import fr.insa.clavardator.network.PeerHandshake;
import fr.insa.clavardator.server.Presence;
import fr.insa.clavardator.server.PresenceFactory;
import fr.insa.clavardator.server.PresenceType;
import fr.insa.clavardator.server.UnknownPresenceException;
import fr.insa.clavardator.util.ErrorCallback;
import fr.insa.clavardator.util.Log;
import javafx.application.Platform;
@ -24,6 +29,8 @@ public class UserList {
private final NetDiscoverer netDiscoverer = new NetDiscoverer();
private final ConnectionListener connectionListener = new ConnectionListener();
private Presence presenceServer;
public UserList() {
}
@ -125,6 +132,30 @@ public class UserList {
userHashmap.forEach((id, user) -> user.sendCurrentUser(Throwable::printStackTrace));
}
/**
* Initializes the presence server based on user config
*
* @param config The user config
*/
public void initPresenceServer(Config config) {
final Config.ServerConfig serverConfig = config.getServerConfig();
if (serverConfig.isEnabled()) {
try {
final PresenceType type = serverConfig.getType();
final String uri = serverConfig.getUri();
final int presencePort = serverConfig.getPresencePort();
final int proxyPort = serverConfig.getProxyPort();
presenceServer = PresenceFactory.create(type, uri, presencePort, proxyPort);
Log.v(getClass().getSimpleName(), "Presence server support enabled: " + type + "@" + uri + ':' + presencePort + " / proxy:" + proxyPort);
} catch (UnknownPresenceException e) {
Log.e(getClass().getSimpleName(), "Presence server type not found", e);
presenceServer = null;
}
} else {
Log.v(getClass().getSimpleName(), "Presence server support disabled.");
}
}
/**
* Closes all running threads, sockets and db connection.
* Must be called before exiting the app.