Added documentation, refined exception handling
This commit is contained in:
parent
0123935cea
commit
ac7b7fa3d6
@ -33,8 +33,23 @@ public class Client {
|
|||||||
private User sender, recipient;
|
private User sender, recipient;
|
||||||
private boolean online = false;
|
private boolean online = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the client. At this state, the client user has yet to be
|
||||||
|
* initialized, which can be done by calling {@link Client#onlineInit(String).
|
||||||
|
*
|
||||||
|
* @param config The {@link Config} instance to use in this client
|
||||||
|
* @since Envoy v0.2-alpha
|
||||||
|
*/
|
||||||
public Client(Config config) { this.config = config; }
|
public Client(Config config) { this.config = config; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enters the online mode by acquiring a user ID from the server.
|
||||||
|
*
|
||||||
|
* @param userName the name of the client user
|
||||||
|
* @throws EnvoyException if the online mode could not be entered or the request
|
||||||
|
* failed for some other reason
|
||||||
|
* @since Envoy v0.2-alpha
|
||||||
|
*/
|
||||||
public void onlineInit(String userName) throws EnvoyException {
|
public void onlineInit(String userName) throws EnvoyException {
|
||||||
sender = getUser(userName);
|
sender = getUser(userName);
|
||||||
online = true;
|
online = true;
|
||||||
@ -52,10 +67,8 @@ public class Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a {@code Map<String, User>} of all users on the server with their
|
* @return a {@code Map<String, User>} of all users on the server with their
|
||||||
* user names as keys.
|
* user names as keys
|
||||||
*
|
|
||||||
* @return Sync - List of all users on the server.
|
|
||||||
* @since Envoy v0.2-alpha
|
* @since Envoy v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public Map<String, User> getUsers() {
|
public Map<String, User> getUsers() {
|
||||||
@ -138,9 +151,12 @@ public class Client {
|
|||||||
* @param sync the sync object (yet to be converted from java class to
|
* @param sync the sync object (yet to be converted from java class to
|
||||||
* sync.xml)
|
* sync.xml)
|
||||||
* @return a returnSync.xml file
|
* @return a returnSync.xml file
|
||||||
|
* @throws EnvoyException if the client is not in online mode
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public Sync sendSync(long userId, Sync sync) {
|
public Sync sendSync(long userId, Sync sync) throws EnvoyException {
|
||||||
|
if(!isOnline())
|
||||||
|
throw new EnvoyException("Client is not in online mode");
|
||||||
// Print sync XML to console
|
// Print sync XML to console
|
||||||
JAXBContext jc;
|
JAXBContext jc;
|
||||||
try {
|
try {
|
||||||
@ -162,6 +178,12 @@ public class Client {
|
|||||||
*/
|
*/
|
||||||
public User getSender() { return sender; }
|
public User getSender() { return sender; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the client user which is used to send messages.
|
||||||
|
*
|
||||||
|
* @param sender the client user to set
|
||||||
|
* @since Envoy v0.2-alpha
|
||||||
|
*/
|
||||||
public void setSender(User sender) { this.sender = sender; }
|
public void setSender(User sender) { this.sender = sender; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -173,7 +195,7 @@ public class Client {
|
|||||||
/**
|
/**
|
||||||
* Sets the recipient.
|
* Sets the recipient.
|
||||||
*
|
*
|
||||||
* @param recipient - the recipient to set
|
* @param recipient the recipient to set
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public void setRecipient(User recipient) { this.recipient = recipient; }
|
public void setRecipient(User recipient) { this.recipient = recipient; }
|
||||||
@ -186,6 +208,7 @@ public class Client {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {@code true} if a connection to the server could be established
|
* @return {@code true} if a connection to the server could be established
|
||||||
|
* @since Envoy v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public boolean isOnline() { return online; }
|
public boolean isOnline() { return online; }
|
||||||
}
|
}
|
||||||
|
@ -54,6 +54,7 @@ public class LocalDB {
|
|||||||
* Constructs an empty local database. To serialize any chats to the file
|
* Constructs an empty local database. To serialize any chats to the file
|
||||||
* system, call {@link LocalDB#initializeDBFile(File)}.
|
* system, call {@link LocalDB#initializeDBFile(File)}.
|
||||||
*
|
*
|
||||||
|
* @param localDBDir the directory in which to store users and chats
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public LocalDB(File localDBDir) throws IOException {
|
public LocalDB(File localDBDir) throws IOException {
|
||||||
@ -72,20 +73,19 @@ public class LocalDB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the local database and fills it with values
|
* Creates a database file for a user-specific list of chats.
|
||||||
* if the user has already sent or received messages.
|
|
||||||
*
|
*
|
||||||
* @param localDBDir the directory where we wish to save/load the database from.
|
* @throws NullPointerException if the client user is not yet specified
|
||||||
* @throws EnvoyException if the directory selected is not an actual directory.
|
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public void initializeDBFile() throws EnvoyException {
|
public void initializeDBFile() {
|
||||||
if (user == null) throw new NullPointerException("Client user is null");
|
if (user == null) throw new NullPointerException("Client user is null");
|
||||||
localDBFile = new File(localDBDir, user.getID() + ".db");
|
localDBFile = new File(localDBDir, user.getID() + ".db");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the database into a file for future use.
|
* Stores all users to the local database. If the client user is specified, the
|
||||||
|
* chats related to this user are stored as well.
|
||||||
*
|
*
|
||||||
* @throws IOException if something went wrong during saving
|
* @throws IOException if something went wrong during saving
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
@ -98,30 +98,36 @@ public class LocalDB {
|
|||||||
write(localDBFile, chats);
|
write(localDBFile, chats);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads all users that are stored in the local database.
|
||||||
|
*
|
||||||
|
* @throws EnvoyException if the loading process failed
|
||||||
|
* @since Envoy v0.2-alpha
|
||||||
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void loadUsers() throws ClassNotFoundException, IOException { users = read(usersFile, HashMap.class); }
|
public void loadUsers() throws EnvoyException { users = read(usersFile, HashMap.class); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads all chats saved by Envoy for the client user.
|
* Loads all chats saved by Envoy for the client user.
|
||||||
*
|
*
|
||||||
* @throws EnvoyException if something fails while loading.
|
* @throws EnvoyException if the loading process failed
|
||||||
* @throws IOException
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void loadChats() throws ClassNotFoundException, IOException { chats = read(localDBFile, ArrayList.class); }
|
public void loadChats() throws EnvoyException { chats = read(localDBFile, ArrayList.class); }
|
||||||
|
|
||||||
private <T> T read(File file, Class<T> serializedClass) throws ClassNotFoundException, IOException {
|
private <T> T read(File file, Class<T> serializedClass) throws EnvoyException {
|
||||||
|
if (file == null) throw new NullPointerException("File is null");
|
||||||
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
|
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
|
||||||
return serializedClass.cast(in.readObject());
|
return serializedClass.cast(in.readObject());
|
||||||
} catch (ClassNotFoundException | IOException e) {
|
} catch (ClassNotFoundException | IOException e) {
|
||||||
throw e;
|
throw new EnvoyException("Could not load serialized object", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void write(File file, T obj) throws IOException {
|
private <T> void write(File file, T obj) throws IOException {
|
||||||
if (file == null) throw new NullPointerException("File is null");
|
if (file == null) throw new NullPointerException("File is null");
|
||||||
|
if (obj == null) throw new NullPointerException("Object to serialize is null");
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
file.getParentFile().mkdirs();
|
file.getParentFile().mkdirs();
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
@ -306,7 +312,8 @@ public class LocalDB {
|
|||||||
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
|
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the users
|
* @return a {@code Map<String, User>} of all users stored locally with their user names as keys
|
||||||
|
* @since Envoy v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public Map<String, User> getUsers() { return users; }
|
public Map<String, User> getUsers() { return users; }
|
||||||
|
|
||||||
|
@ -239,8 +239,7 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
loadUsersAndChats();
|
loadUsersAndChats();
|
||||||
|
|
||||||
if(client.isOnline())
|
if (client.isOnline()) startSyncThread(Config.getInstance().getSyncTimeout());
|
||||||
startSyncThread(Config.getInstance().getSyncTimeout());
|
|
||||||
|
|
||||||
contentPane.revalidate();
|
contentPane.revalidate();
|
||||||
}
|
}
|
||||||
@ -342,7 +341,11 @@ public class ChatWindow extends JFrame {
|
|||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
|
|
||||||
// Synchronize
|
// Synchronize
|
||||||
|
try {
|
||||||
localDB.applySync(client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
|
localDB.applySync(client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.log(Level.SEVERE, "Could not perform sync", e);
|
||||||
|
}
|
||||||
|
|
||||||
// Process unread messages
|
// Process unread messages
|
||||||
localDB.addUnreadMessagesToLocalDB();
|
localDB.addUnreadMessagesToLocalDB();
|
||||||
|
@ -97,7 +97,7 @@ public class Startup {
|
|||||||
try {
|
try {
|
||||||
localDB.initializeDBFile();
|
localDB.initializeDBFile();
|
||||||
localDB.loadChats();
|
localDB.loadChats();
|
||||||
} catch (EnvoyException | ClassNotFoundException | IOException e) {
|
} catch (EnvoyException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
JOptionPane.showMessageDialog(null,
|
JOptionPane.showMessageDialog(null,
|
||||||
"Error while loading local database: " + e.toString() + "\nChats will not be stored locally.",
|
"Error while loading local database: " + e.toString() + "\nChats will not be stored locally.",
|
||||||
|
Reference in New Issue
Block a user