Merge branch 'develop' into f/logging
This commit is contained in:
commit
9e83cc1c66
@ -31,6 +31,7 @@ import envoy.schema.User;
|
|||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public class LocalDB {
|
public class LocalDB {
|
||||||
|
|
||||||
private File localDB;
|
private File localDB;
|
||||||
private User sender;
|
private User sender;
|
||||||
private List<Chat> chats = new ArrayList<>();
|
private List<Chat> chats = new ArrayList<>();
|
||||||
@ -39,12 +40,16 @@ public class LocalDB {
|
|||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName());
|
private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName());
|
||||||
|
|
||||||
|
private Sync unreadMessagesSync = objectFactory.createSync();
|
||||||
|
private Sync sync = objectFactory.createSync();
|
||||||
|
private Sync readMessages = objectFactory.createSync();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an empty local database.
|
* Constructs an empty local database.
|
||||||
*
|
*
|
||||||
* @param client the user that is logged in with this client
|
* @param client the user that is logged in with this client
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
**/
|
*/
|
||||||
|
|
||||||
public LocalDB(User sender) {
|
public LocalDB(User sender) {
|
||||||
this.sender = sender;
|
this.sender = sender;
|
||||||
@ -55,7 +60,6 @@ public class LocalDB {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the local database and fills it with values
|
* Initializes the local database and fills it with values
|
||||||
* if the user has already sent or received messages.
|
* if the user has already sent or received messages.
|
||||||
@ -63,10 +67,10 @@ public class LocalDB {
|
|||||||
* @param localDBDir the directory where we wish to save/load the database from.
|
* @param localDBDir the directory where we wish to save/load the database from.
|
||||||
* @throws EnvoyException if the directory selected is not an actual directory.
|
* @throws EnvoyException if the directory selected is not an actual directory.
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
**/
|
*/
|
||||||
public void initializeDBFile(File localDBDir) throws EnvoyException {
|
public void initializeDBFile(File localDBDir) throws EnvoyException {
|
||||||
if (localDBDir.exists() && !localDBDir.isDirectory()) throw new EnvoyException(
|
if (localDBDir.exists() && !localDBDir.isDirectory())
|
||||||
String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
|
throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
|
||||||
localDB = new File(localDBDir, sender.getID() + ".db");
|
localDB = new File(localDBDir, sender.getID() + ".db");
|
||||||
if (localDB.exists()) loadFromLocalDB();
|
if (localDB.exists()) loadFromLocalDB();
|
||||||
}
|
}
|
||||||
@ -76,7 +80,7 @@ public class LocalDB {
|
|||||||
*
|
*
|
||||||
* @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
|
||||||
**/
|
*/
|
||||||
public void saveToLocalDB() {
|
public void saveToLocalDB() {
|
||||||
try {
|
try {
|
||||||
localDB.getParentFile().mkdirs();
|
localDB.getParentFile().mkdirs();
|
||||||
@ -98,7 +102,7 @@ public class LocalDB {
|
|||||||
*
|
*
|
||||||
* @throws EnvoyException if something fails while loading.
|
* @throws EnvoyException if something fails while loading.
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
**/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private void loadFromLocalDB() throws EnvoyException {
|
private void loadFromLocalDB() throws EnvoyException {
|
||||||
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) {
|
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) {
|
||||||
@ -133,18 +137,10 @@ public class LocalDB {
|
|||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Sync unreadMessagesSync = objectFactory.createSync();
|
|
||||||
public Sync sync = objectFactory.createSync();
|
|
||||||
public Sync readMessages = objectFactory.createSync();
|
|
||||||
|
|
||||||
public Sync fillSync(long userId) {
|
public Sync fillSync(long userId) {
|
||||||
|
|
||||||
addWaitingMessagesToSync();
|
addWaitingMessagesToSync();
|
||||||
|
|
||||||
getSentStateMessagesFromLocalDB();
|
sync.getMessages().addAll(readMessages.getMessages());
|
||||||
for (int i = 0; i < readMessages.getMessages().size(); i++) {
|
|
||||||
sync.getMessages().add(readMessages.getMessages().get(i));
|
|
||||||
}
|
|
||||||
readMessages.getMessages().clear();
|
readMessages.getMessages().clear();
|
||||||
|
|
||||||
logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size()));
|
logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size()));
|
||||||
@ -159,47 +155,29 @@ public class LocalDB {
|
|||||||
// SENT)
|
// SENT)
|
||||||
for (int j = 0; j < sync.getMessages().size(); j++) {
|
for (int j = 0; j < sync.getMessages().size(); j++) {
|
||||||
if (j == i) {
|
if (j == i) {
|
||||||
sync.getMessages()
|
sync.getMessages().get(j).getMetadata().setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId());
|
||||||
.get(j)
|
sync.getMessages().get(j).getMetadata().setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||||
.getMetadata()
|
|
||||||
.setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId());
|
|
||||||
sync.getMessages()
|
|
||||||
.get(j)
|
|
||||||
.getMetadata()
|
|
||||||
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() != 0
|
||||||
&& returnSync.getMessages().get(i).getMetadata().getSender() != 0
|
|
||||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
||||||
// these are the unread Messages from the server
|
// these are the unread Messages from the server
|
||||||
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
|
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() == 0
|
||||||
&& returnSync.getMessages().get(i).getMetadata().getSender() == 0
|
|
||||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
||||||
// Update Messages in localDB to state RECEIVED
|
// Update Messages in localDB to state RECEIVED
|
||||||
for (int j = 0; j < getChats().size(); j++) {
|
for (int j = 0; j < getChats().size(); j++) {
|
||||||
if (getChats().get(j)
|
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
||||||
.getRecipient()
|
|
||||||
.getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
|
||||||
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
||||||
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync
|
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
|
||||||
.getMessages()
|
|
||||||
.get(i)
|
.get(i)
|
||||||
.getMetadata()
|
.getMetadata()
|
||||||
.getMessageId()) {
|
.getMessageId()) {
|
||||||
// Update Message in LocalDB
|
// Update Message in LocalDB
|
||||||
getChats().get(j)
|
getChats().get(j).getModel().get(k).getMetadata().setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||||
.getModel()
|
|
||||||
.get(k)
|
|
||||||
.getMetadata()
|
|
||||||
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -218,8 +196,7 @@ public class LocalDB {
|
|||||||
.getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
.getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
||||||
logger.info("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
|
logger.info("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
|
||||||
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
||||||
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync
|
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
|
||||||
.getMessages()
|
|
||||||
.get(i)
|
.get(i)
|
||||||
.getMetadata()
|
.getMetadata()
|
||||||
.getMessageId()) {
|
.getMessageId()) {
|
||||||
@ -237,7 +214,6 @@ public class LocalDB {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -254,25 +230,8 @@ public class LocalDB {
|
|||||||
|
|
||||||
sync.getMessages().clear();
|
sync.getMessages().clear();
|
||||||
sync.getUsers().clear();
|
sync.getUsers().clear();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a message to the "sync" Sync object.
|
|
||||||
*
|
|
||||||
* @param message
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
*/
|
|
||||||
public void addMessageToSync(Message message) { sync.getMessages().add(message); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a user to the {@code sync} {@link Sync} object.
|
|
||||||
*
|
|
||||||
* @param user
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
*/
|
|
||||||
public void addUserToSync(User user) { sync.getUsers().add(user); }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the unread messages returned from the server in the latest sync to the
|
* Adds the unread messages returned from the server in the latest sync to the
|
||||||
* right chats in the LocalDB.
|
* right chats in the LocalDB.
|
||||||
@ -284,34 +243,16 @@ public class LocalDB {
|
|||||||
Sync unreadMessages = unreadMessagesSync;
|
Sync unreadMessages = unreadMessagesSync;
|
||||||
for (int i = 0; i < unreadMessages.getMessages().size(); i++)
|
for (int i = 0; i < unreadMessages.getMessages().size(); i++)
|
||||||
for (int j = 0; j < getChats().size(); j++)
|
for (int j = 0; j < getChats().size(); j++)
|
||||||
if (getChats().get(j)
|
if (getChats().get(j).getRecipient().getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) {
|
||||||
.getRecipient()
|
|
||||||
.getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) {
|
|
||||||
getChats().get(j).appendMessage(unreadMessages.getMessages().get(i));
|
getChats().get(j).appendMessage(unreadMessages.getMessages().get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all messages with state SENT from the LocalDB and adds them to the
|
* Changes all messages with state {@code RECEIVED} of a specific chat to state
|
||||||
* "sync" Sync object.
|
* {@code READ}.
|
||||||
*
|
|
||||||
* @param localDB
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
*/
|
|
||||||
public void getSentStateMessagesFromLocalDB() {
|
|
||||||
for (int i = 0; i < getChats().size(); i++) {
|
|
||||||
for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) {
|
|
||||||
if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.SENT) {
|
|
||||||
addMessageToSync(getChats().get(i).getModel().get(j));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Changes all messages with State RECEIVED of a specific chat to State READ.
|
|
||||||
* <br>
|
* <br>
|
||||||
* Adds these Messages to the {@code readMessages} {@link Sync} object.
|
* Adds these messages to the {@code readMessages} {@link Sync} object.
|
||||||
*
|
*
|
||||||
* @param currentChat
|
* @param currentChat
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
@ -326,29 +267,18 @@ public class LocalDB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a message with State WAITING to a specific chat in the LocalDB.
|
* Adds all messages with state {@code WAITING} from the {@link LocalDB} to the
|
||||||
|
* {@link Sync} object.
|
||||||
*
|
*
|
||||||
* @param message
|
|
||||||
* @param currentChat
|
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); }
|
private void addWaitingMessagesToSync() {
|
||||||
|
for (Chat chat : getChats())
|
||||||
/**
|
for (int i = 0; i < chat.getModel().size(); i++)
|
||||||
* Adds all messages with State WAITING from the {@link LocalDB} to the Sync.
|
if (chat.getModel().get(i).getMetadata().getState() == MessageState.WAITING) {
|
||||||
*
|
|
||||||
* @param localDB
|
|
||||||
* @since Envoy v0.1-alpha
|
|
||||||
*/
|
|
||||||
public void addWaitingMessagesToSync() {
|
|
||||||
for (int i = 0; i < getChats().size(); i++) {
|
|
||||||
for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) {
|
|
||||||
if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) {
|
|
||||||
// addMessageToSync(localDB.getChats().get(i).getModel().get(j));
|
// addMessageToSync(localDB.getChats().get(i).getModel().get(j));
|
||||||
logger.info("Got Waiting Message");
|
logger.info("Got Waiting Message");
|
||||||
sync.getMessages().add(0, getChats().get(i).getModel().get(j));
|
sync.getMessages().add(chat.getModel().get(i));
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -367,7 +297,7 @@ public class LocalDB {
|
|||||||
public List<Chat> getChats() { return chats; }
|
public List<Chat> getChats() { return chats; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the User who initialised the local Database
|
* @return the {@link User} who initialized the local database
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public User getUser() { return sender; }
|
public User getUser() { return sender; }
|
||||||
|
@ -226,7 +226,7 @@ public class ChatWindow extends JFrame {
|
|||||||
.get();
|
.get();
|
||||||
|
|
||||||
// Set all unread messages in the chat to read
|
// Set all unread messages in the chat to read
|
||||||
if (currentChat != null) { localDB.setMessagesToRead(currentChat); }
|
readCurrentChat();
|
||||||
|
|
||||||
client.setRecipient(user);
|
client.setRecipient(user);
|
||||||
|
|
||||||
@ -272,7 +272,7 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
// Create and send message object
|
// Create and send message object
|
||||||
final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient());
|
final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient());
|
||||||
localDB.addWaitingMessageToLocalDB(message, currentChat);
|
currentChat.appendMessage(message);
|
||||||
messageList.setModel(currentChat.getModel());
|
messageList.setModel(currentChat.getModel());
|
||||||
|
|
||||||
// Clear text field
|
// Clear text field
|
||||||
@ -309,7 +309,8 @@ public class ChatWindow extends JFrame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the data model and the ui every x seconds.
|
* Updates the data model and the UI repeatedly after a certain amount of
|
||||||
|
* time.
|
||||||
*
|
*
|
||||||
* @param timeout the amount of time that passes between two requests sent to
|
* @param timeout the amount of time that passes between two requests sent to
|
||||||
* the server
|
* the server
|
||||||
@ -322,9 +323,14 @@ public class ChatWindow extends JFrame {
|
|||||||
// Synchronize
|
// Synchronize
|
||||||
localDB.applySync(
|
localDB.applySync(
|
||||||
client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
|
client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
|
||||||
|
|
||||||
|
// Process unread messages
|
||||||
localDB.addUnreadMessagesToLocalDB();
|
localDB.addUnreadMessagesToLocalDB();
|
||||||
localDB.clearUnreadMessagesSync();
|
localDB.clearUnreadMessagesSync();
|
||||||
|
|
||||||
|
// Mark unread messages as read when they are in the current chat
|
||||||
|
readCurrentChat();
|
||||||
|
|
||||||
// Update UI
|
// Update UI
|
||||||
SwingUtilities
|
SwingUtilities
|
||||||
.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
|
.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
|
||||||
@ -338,4 +344,9 @@ public class ChatWindow extends JFrame {
|
|||||||
if (userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID())
|
if (userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID())
|
||||||
userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus());
|
userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Marks messages in the current chat as {@code READ}.
|
||||||
|
*/
|
||||||
|
private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } }
|
||||||
}
|
}
|
Reference in New Issue
Block a user