Added offline mode to Client
+ Saving the user ID in Settings + Loading the user ID from Settings in Client if the server can't be reached
This commit is contained in:
parent
30dc9838ea
commit
117f9043ce
@ -10,6 +10,7 @@ import javax.xml.bind.JAXBContext;
|
|||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
import javax.xml.bind.Marshaller;
|
import javax.xml.bind.Marshaller;
|
||||||
|
|
||||||
|
import envoy.exception.EnvoyException;
|
||||||
import envoy.schema.ObjectFactory;
|
import envoy.schema.ObjectFactory;
|
||||||
import envoy.schema.Sync;
|
import envoy.schema.Sync;
|
||||||
import envoy.schema.User;
|
import envoy.schema.User;
|
||||||
@ -29,14 +30,18 @@ public class Client {
|
|||||||
private ObjectFactory objectFactory = new ObjectFactory();
|
private ObjectFactory objectFactory = new ObjectFactory();
|
||||||
private Config config;
|
private Config config;
|
||||||
private User sender, recipient;
|
private User sender, recipient;
|
||||||
|
private boolean online = false;
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
|
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
|
||||||
|
|
||||||
public Client(Config config, String username) {
|
public Client(Config config, String userName) throws EnvoyException {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
sender = getUser(username);
|
sender = getUser(userName);
|
||||||
|
|
||||||
logger.info("ID: " + sender.getID());
|
// Update the user ID in the cache
|
||||||
|
Settings.getInstance().setUserID(sender.getID());
|
||||||
|
|
||||||
|
logger.info("Client user ID: " + sender.getID());
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T, R> R post(String uri, T body, Class<R> responseBodyClass) {
|
private <T, R> R post(String uri, T body, Class<R> responseBodyClass) {
|
||||||
@ -48,7 +53,6 @@ public class Client {
|
|||||||
client.close();
|
client.close();
|
||||||
|
|
||||||
return responseBody;
|
return responseBody;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -63,9 +67,7 @@ public class Client {
|
|||||||
user.setID(-1);
|
user.setID(-1);
|
||||||
sendSync.getUsers().add(user);
|
sendSync.getUsers().add(user);
|
||||||
|
|
||||||
Sync returnSendSync = post(
|
Sync returnSendSync = post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
|
||||||
String
|
|
||||||
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
|
|
||||||
sendSync,
|
sendSync,
|
||||||
Sync.class);
|
Sync.class);
|
||||||
return returnSendSync;
|
return returnSendSync;
|
||||||
@ -77,29 +79,33 @@ public class Client {
|
|||||||
*
|
*
|
||||||
* @param name - the name of the {@link User}
|
* @param name - the name of the {@link User}
|
||||||
* @return a {@link User} with the specified name
|
* @return a {@link User} with the specified name
|
||||||
|
* @throws EnvoyException if the server does not return the requested ID
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
private User getUser(String name) {
|
private User getUser(String name) throws EnvoyException {
|
||||||
|
// Create a sync with only a user with the requested name
|
||||||
Sync senderSync = objectFactory.createSync();
|
Sync senderSync = objectFactory.createSync();
|
||||||
User user = objectFactory.createUser();
|
User user = objectFactory.createUser();
|
||||||
user.setName(name);
|
user.setName(name);
|
||||||
senderSync.getUsers().add(user);
|
senderSync.getUsers().add(user);
|
||||||
|
|
||||||
Sync returnSenderSync = post(
|
try {
|
||||||
String
|
Sync sync = post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
|
||||||
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
|
|
||||||
senderSync,
|
senderSync,
|
||||||
Sync.class);
|
Sync.class);
|
||||||
|
|
||||||
User returnSender = objectFactory.createUser();
|
// Expecting a single user with an ID
|
||||||
|
if (sync.getUsers().size() == 1) {
|
||||||
if (returnSenderSync.getUsers().size() == 1) {
|
online = true;
|
||||||
returnSender = returnSenderSync.getUsers().get(0);
|
return sync.getUsers().get(0);
|
||||||
} else {
|
} else throw new EnvoyException("Unexpected response from Envoy Server");
|
||||||
logger.warning("ERROR exiting...");
|
} catch (Exception e) {
|
||||||
|
logger.warning("Could not connect to server, trying offline mode.");
|
||||||
|
if (Settings.getInstance().getUserID() != -1) {
|
||||||
|
user.setID(Settings.getInstance().getUserID());
|
||||||
|
return user;
|
||||||
|
} else throw new EnvoyException("Could not enter offline mode.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnSender;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -134,7 +140,8 @@ public class Client {
|
|||||||
* their updated UserStatus to the client.) <br>
|
* their updated UserStatus to the client.) <br>
|
||||||
*
|
*
|
||||||
* @param userId the id of the {@link Client} who sends the {@link Sync}
|
* @param userId the id of the {@link Client} who sends the {@link Sync}
|
||||||
* @param sync the sync object (yet to be converted from java class to sync.xml)
|
* @param sync the sync object (yet to be converted from java class to
|
||||||
|
* sync.xml)
|
||||||
* @return a returnSync.xml file
|
* @return a returnSync.xml file
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
@ -151,10 +158,7 @@ public class Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send sync
|
// Send sync
|
||||||
return post(String
|
return post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), userId), sync, Sync.class);
|
||||||
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), userId),
|
|
||||||
sync,
|
|
||||||
Sync.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -182,4 +186,9 @@ public class Client {
|
|||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public boolean hasRecipient() { return recipient != null; }
|
public boolean hasRecipient() { return recipient != null; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {@code true} if a connection to the server could be established
|
||||||
|
*/
|
||||||
|
public boolean isOnline() { return online; }
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@ public class Settings {
|
|||||||
|
|
||||||
// Actual settings accessible by the rest of the application
|
// Actual settings accessible by the rest of the application
|
||||||
private String username;
|
private String username;
|
||||||
|
private long userID;
|
||||||
private String email;
|
private String email;
|
||||||
private boolean enterToSend = true;
|
private boolean enterToSend = true;
|
||||||
private Map<String, Theme> themes;
|
private Map<String, Theme> themes;
|
||||||
@ -66,6 +67,7 @@ public class Settings {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private void load() {
|
private void load() {
|
||||||
setUsername(prefs.get("username", ""));
|
setUsername(prefs.get("username", ""));
|
||||||
|
setUserID(prefs.getLong("userID", -1));
|
||||||
setEmail(prefs.get("email", ""));
|
setEmail(prefs.get("email", ""));
|
||||||
setEnterToSend(prefs.getBoolean("enterToSend", true));
|
setEnterToSend(prefs.getBoolean("enterToSend", true));
|
||||||
setCurrentTheme(prefs.get("theme", "dark"));
|
setCurrentTheme(prefs.get("theme", "dark"));
|
||||||
@ -90,13 +92,14 @@ public class Settings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* updates prefs when save button is clicked
|
* Updates the preferences when the save button is clicked.
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* @since Envoy v0.2-alpha
|
* @since Envoy v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public void save() throws IOException {
|
public void save() throws IOException {
|
||||||
prefs.put("username", getUsername());
|
prefs.put("username", getUsername());
|
||||||
|
prefs.putLong("userID", getUserID());
|
||||||
prefs.put("email", getEmail());
|
prefs.put("email", getEmail());
|
||||||
prefs.put("theme", currentTheme);
|
prefs.put("theme", currentTheme);
|
||||||
prefs.putBoolean("enterToSend", isEnterToSend());
|
prefs.putBoolean("enterToSend", isEnterToSend());
|
||||||
@ -145,6 +148,16 @@ public class Settings {
|
|||||||
*/
|
*/
|
||||||
public void setUsername(String username) { this.username = username; }
|
public void setUsername(String username) { this.username = username; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the userID
|
||||||
|
*/
|
||||||
|
public long getUserID() { return userID; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param userID the userID to set
|
||||||
|
*/
|
||||||
|
public void setUserID(long userID) { this.userID = userID; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the email associated with that user.
|
* @return the email associated with that user.
|
||||||
* @since Envoy v0.2-alpha
|
* @since Envoy v0.2-alpha
|
||||||
|
@ -16,7 +16,7 @@ import envoy.exception.EnvoyException;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the Envoy client and prompts the user to enter their name.
|
* Starts the Envoy client and prompts the user to enter their name.
|
||||||
*
|
* <br>
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
* File: <strong>Startup.java</strong><br>
|
* File: <strong>Startup.java</strong><br>
|
||||||
* Created: <strong>12 Oct 2019</strong><br>
|
* Created: <strong>12 Oct 2019</strong><br>
|
||||||
@ -48,18 +48,33 @@ public class Startup {
|
|||||||
// Override configuration values with command line arguments
|
// Override configuration values with command line arguments
|
||||||
if (args.length > 0) config.load(args);
|
if (args.length > 0) config.load(args);
|
||||||
|
|
||||||
|
// Check if all configuration values have been initialized
|
||||||
if (!config.isInitialized()) {
|
if (!config.isInitialized()) {
|
||||||
logger.severe("Server or port are not defined. Exiting...");
|
logger.severe("Server or port are not defined. Exiting...");
|
||||||
JOptionPane.showMessageDialog(null, "Error loading configuration values.", "Configuration error", JOptionPane.ERROR_MESSAGE);
|
JOptionPane.showMessageDialog(null, "Error loading configuration values.", "Configuration error", JOptionPane.ERROR_MESSAGE);
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ask the user for their user name
|
||||||
String userName = JOptionPane.showInputDialog("Please enter your username");
|
String userName = JOptionPane.showInputDialog("Please enter your username");
|
||||||
if (userName == null || userName.isEmpty()) {
|
if (userName == null || userName.isEmpty()) {
|
||||||
logger.severe("User name is not set or empty. Exiting...");
|
logger.severe("User name is not set or empty. Exiting...");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
Client client = new Client(config, userName);
|
|
||||||
|
// Acquire the client user (with ID) either from the server or from the local
|
||||||
|
// cache (Preferences), which triggers offline mode
|
||||||
|
Client client;
|
||||||
|
try {
|
||||||
|
client = new Client(config, userName);
|
||||||
|
} catch (Exception e1) {
|
||||||
|
logger.log(Level.SEVERE, "Failed to acquire client user ID", e1);
|
||||||
|
JOptionPane.showMessageDialog(null, e1.toString(), "Client error", JOptionPane.ERROR_MESSAGE);
|
||||||
|
System.exit(1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load the local database
|
||||||
LocalDB localDB = new LocalDB(client.getSender());
|
LocalDB localDB = new LocalDB(client.getSender());
|
||||||
try {
|
try {
|
||||||
localDB.initializeDBFile(config.getLocalDB());
|
localDB.initializeDBFile(config.getLocalDB());
|
||||||
|
Reference in New Issue
Block a user