From a0dc25ba61020eb823946c54986b65288a026d83 Mon Sep 17 00:00:00 2001 From: Maxi Date: Mon, 4 Nov 2019 23:10:53 +0100 Subject: [PATCH 1/9] Sync * Completely revised communication between client and server. * Added synchronization functionality. * Added Message State updates * Added UserStatus updates --- .classpath | 6 - src/main/java/envoy/client/Client.java | 436 +++++++++++++++--- src/main/java/envoy/client/ui/ChatWindow.java | 61 ++- .../envoy/client/ui/MessageListRenderer.java | 11 +- .../envoy/client/ui/UserListRenderer.java | 25 +- 5 files changed, 434 insertions(+), 105 deletions(-) diff --git a/.classpath b/.classpath index d2bc4e0..906bfce 100644 --- a/.classpath +++ b/.classpath @@ -18,12 +18,6 @@ - - - - - - diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index 0ebce3c..b755c4a 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -1,8 +1,6 @@ package envoy.client; import java.time.Instant; -import java.util.Arrays; - import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; @@ -14,11 +12,10 @@ import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import envoy.schema.Message; -import envoy.schema.Message.MetaData.MessageState; -import envoy.schema.Messages; +import envoy.schema.Message.Metadata.MessageState; import envoy.schema.ObjectFactory; +import envoy.schema.Sync; import envoy.schema.User; -import envoy.schema.Users; /** * Project: envoy-client
@@ -38,6 +35,10 @@ public class Client { private Config config; private User sender, recipient; + private Sync unreadMessagesSync = objectFactory.createSync(); + public Sync sync = objectFactory.createSync(); + public Sync readMessages = objectFactory.createSync(); + public Client(Config config, String username) { this.config = config; try { @@ -46,43 +47,7 @@ public class Client { e.printStackTrace(); } sender = getUser(username); - } - - /** - * Sends a message with text content to the server.
- * Because sending a request is a blocking operation, it is executed - * asynchronously. - * - * @param sender name of the sender - * @param recipient name of the recipient - * @param textContent content (text) of the message - */ - public void sendMessage(Message message) { - new Thread(() -> { - // Wrap single message into messages list - Messages messages = wrapMessages(message); - - // Print message XML to console - JAXBContext jc; - try { - jc = JAXBContext.newInstance("envoy.schema"); - Marshaller m = jc.createMarshaller(); - m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - m.marshal(messages, System.out); - } catch (JAXBException e) { - e.printStackTrace(); - } - - // Send message - javax.ws.rs.client.Client client = ClientBuilder.newClient(); - WebTarget target = client - .target(String.format("%s:%d/envoy-server/rest/message/send", config.getServer(), config.getPort())); - - Response response = target.request().post(Entity.entity(messages, "application/xml")); - System.out.println("Response code: " + response.getStatus()); - response.close(); - client.close(); - }).start(); + System.out.println("ID: " + sender.getID()); } /** @@ -92,7 +57,7 @@ public class Client { * @return prepared {@link Message} object */ public Message createMessage(String textContent) { - Message.MetaData metaData = objectFactory.createMessageMetaData(); + Message.Metadata metaData = objectFactory.createMessageMetadata(); metaData.setSender(sender.getID()); metaData.setRecipient(recipient.getID()); metaData.setState(MessageState.WAITING); @@ -103,16 +68,33 @@ public class Client { content.setText(textContent); Message message = objectFactory.createMessage(); - message.setMetaData(metaData); + message.setMetadata(metaData); message.getContent().add(content); return message; } + + /** + * Returns a {@link Sync} with all users on the server. + * @return Sync - List of all users on the server. + * @since Envoy v0.1-alpha + */ + public Sync getUsersListXml() { + Sync sendSync = objectFactory.createSync(); + User user = objectFactory.createUser(); + user.setID(-1); + sendSync.getUsers().add(user); - public Users getUsersListXml() { return get(String.format("%s:%d/envoy-server/rest/user", config.getServer(), config.getPort()), Users.class); } + javax.ws.rs.client.Client client = ClientBuilder.newClient(); + WebTarget target = client.target(String + .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0)); + + Response response = target.request().post(Entity.entity(sendSync, "application/xml")); + Sync returnSendSync = response.readEntity(Sync.class); + response.close(); + client.close(); + return returnSendSync; - public Messages getUnreadMessages(long userId) { - return get(String.format("%s:%d/envoy-server/rest/message/receive?userId=%d", config.getServer(), config.getPort(), userId), Messages.class); } /** @@ -123,52 +105,358 @@ public class Client { * @since Envoy v0.1-alpha */ private User getUser(String name) { - return get(String.format("%s:%d/envoy-server/rest/user/sender?name=%s", config.getServer(), config.getPort(), name), Users.class).getUser() - .get(0); - } + Sync senderSync = objectFactory.createSync(); + User user = objectFactory.createUser(); + user.setName(name); + senderSync.getUsers().add(user); - /** - * Invokes the GET method of a web service. - * - * @param the type of the object returned by the web service - * @param uri the URI of the web service - * @param responseClass the class of the object returned by the web service - * @return the object returned by the web service - * @since Envoy v0.1-alpha - */ - private T get(String uri, Class responseClass) { - javax.ws.rs.client.Client client = ClientBuilder.newClient(); - WebTarget target = client.target(uri); - Response response = target.request("application/xml").get(); - T responseObject = response.readEntity(responseClass); - System.out.println("Response code: " + response.getStatus()); + javax.ws.rs.client.Client client = ClientBuilder.newClient(); + WebTarget target = client.target(String + .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0)); + + Response response = target.request().post(Entity.entity(senderSync, "application/xml")); + User returnSender = objectFactory.createUser(); + Sync returnSenderSync = response.readEntity(Sync.class); + if (returnSenderSync.getUsers().size() == 1) { + returnSender = returnSenderSync.getUsers().get(0); + } else { + System.out.println("ERROR exiting..."); + } response.close(); client.close(); - return responseObject; + return returnSender; } /** - * Wraps one or more {@link Message} objects into a {@link Messages} object. + * Sends the "sync" Sync Object to the server and gets a "returnSync" Sync + * Object as response.
+ * It is also used to get the own sender at the start of the client + * (Client sends "sync" Sync Object with single user in it(name: the name entered at login, id: 0, UserStatus:null))
+ * and to get a complete list of all users saved on the server. + * (Client sends "sync" Sync Object with single user in it(name: "" (empty), id: -1, UserStatus:null))
+ * This method also processes the response Sync Object.
+ * It sorts its users and messages by specific variables and does certain things + * with them.
+ *
+ * Messages:
+ * -State SENT: Update Local message(s) with State WAITING (add Message ID and + * change State to SENT). (server sends these informations to the client if + * message(s) with State WAITING were successfully sent to the server)
+ * -State RECEIVED, SenderID != 0: Adds the unread Messages returned from the + * server in the latest sync to the "unreadMessagesSync" Sync Object.
+ * -State RECEIVED, SenderID == 0: Update message(s) in localDB to state RECEIVED. + * (server sends these informations to the client if the other client received + * the message(s).)
+ * -State READ: Update message(s) in the LocalDB to state READ. (server sends these informations to the client if the other client read + * the message(s).)
+ *
+ * Users:
+ * Updating UserStatus of all users in LocalDB. (Server sends all users with their updated UserStatus to the client.)
* - * @param messages the {@link Message} objects to wrap - * @return {@link Messages} object with all messages as its children + * @param userId + * @param localDB * @since Envoy v0.1-alpha */ - private Messages wrapMessages(Message... messages) { - Messages wrapper = objectFactory.createMessages(); - wrapper.getMessage().addAll(Arrays.asList(messages)); - return wrapper; + public void sendSync(long userId, LocalDB localDB) { + new Thread(() -> { + // Print sync XML to console + JAXBContext jc; + try { + jc = JAXBContext.newInstance("envoy.schema"); + Marshaller m = jc.createMarshaller(); + m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + m.marshal(sync, System.out); + } catch (JAXBException e) { + e.printStackTrace(); + } + addWaitingMessagesToSync(localDB); + + getSentStateMessagesFromLocalDB(localDB); + for (int i = 0; i < readMessages.getMessages().size(); i++) { + sync.getMessages().add(readMessages.getMessages().get(i)); + } + readMessages.getMessages().clear(); + + // Send sync + javax.ws.rs.client.Client client = ClientBuilder.newClient(); + WebTarget target = client + .target(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", + config.getServer(), + config.getPort(), + userId)); + + Response response = target.request().post(Entity.entity(sync, "application/xml")); + + Sync returnSync = response.readEntity(Sync.class); + if (returnSync.getMessages().size() != 0) { + System.out.println("Message ID: " + returnSync.getMessages().get(0).getMetadata().getMessageId()); + } + + for (int i = 0; i < returnSync.getMessages().size(); i++) { + + // Print sync XML to console + JAXBContext jc2; + try { + jc2 = JAXBContext.newInstance("envoy.schema"); + Marshaller m = jc2.createMarshaller(); + m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + m.marshal(returnSync, System.out); + } catch (JAXBException e) { + e.printStackTrace(); + } + + if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 + && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) { + // Update Local Messages with State WAITING (add Message ID and change State to + // SENT) + for (int j = 0; j < sync.getMessages().size(); j++) { + if (j == i) { + sync.getMessages() + .get(j) + .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 + && returnSync.getMessages().get(i).getMetadata().getSender() != 0 + && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { + // these are the unread Messages from the server + unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i)); + + } + + if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 + && returnSync.getMessages().get(i).getMetadata().getSender() == 0 + && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { + // Update Messages in localDB to state RECEIVED + for (int j = 0; j < localDB.getChats().size(); j++) { + if (localDB.getChats().get(j).getRecipient().getID() == returnSync.getMessages() + .get(i) + .getMetadata() + .getRecipient()) { + for (int k = 0; k < localDB.getChats().get(j).getModel().getSize(); k++) { + if (localDB.getChats() + .get(j) + .getModel() + .get(k) + .getMetadata() + .getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { + // Update Message in LocalDB + localDB.getChats() + .get(j) + .getModel() + .get(k) + .getMetadata() + .setState(returnSync.getMessages().get(j).getMetadata().getState()); + } + } + } + } + + } + + if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 + && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) { + // Update local Messages to state READ + System.out + .println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() + + "was initialized to be set to READ in localDB."); + for (int j = 0; j < localDB.getChats().size(); j++) { + if (localDB.getChats().get(j).getRecipient().getID() == returnSync.getMessages() + .get(i) + .getMetadata() + .getRecipient()) { + System.out.println( + "Chat with: " + localDB.getChats().get(j).getRecipient().getID() + "was selected."); + for (int k = 0; k < localDB.getChats().get(j).getModel().getSize(); k++) { + if (localDB.getChats() + .get(j) + .getModel() + .get(k) + .getMetadata() + .getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { + System.out.println("Message with ID: " + + localDB.getChats().get(j).getModel().get(k).getMetadata().getMessageId() + + "was selected."); + localDB.getChats() + .get(j) + .getModel() + .get(k) + .getMetadata() + .setState(returnSync.getMessages().get(i).getMetadata().getState()); + System.out.println("Message State is now: " + localDB.getChats() + .get(j) + .getModel() + .get(k) + .getMetadata() + .getState() + .toString()); + } + } + } + } + + } + } + + // Updating UserStatus of all Users in LocalDB + for (int j = 0; j < returnSync.getUsers().size(); j++) { + for (int k = 0; k < localDB.getChats().size(); k++) { + if (localDB.getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) { + + localDB.getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus()); + System.out.println(localDB.getChats().get(k).getRecipient().getStatus().toString()); + } + } + } + + System.out.println("Response code: " + response.getStatus()); + + response.close(); + client.close(); + sync.getMessages().clear(); + sync.getUsers().clear(); + + }).start(); + System.out.println(sync.getMessages().size()); + } /** - * @return the sender object that represents this client + * 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 "sync" 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 + * right chats in the LocalDB. + * + * @param localDB + * @since Envoy v0.1-alpha + */ + public void addUnreadMessagesToLocalDB(LocalDB localDB) { + Sync unreadMessages = unreadMessagesSync; + for (int i = 0; i < unreadMessages.getMessages().size(); i++) + for (int j = 0; j < localDB.getChats().size(); j++) + if (localDB.getChats().get(j).getRecipient().getID() == unreadMessages.getMessages() + .get(i) + .getMetadata() + .getSender()) { + localDB.getChats().get(j).appendMessage(unreadMessages.getMessages().get(i)); + } + } + + /** + * Gets all Messages with state SENT from the LocalDB and adds them to the + * "sync" Sync object. + * + * @param localDB + * @since Envoy v0.1-alpha + */ + public void getSentStateMessagesFromLocalDB(LocalDB localDB) { + for (int i = 0; i < localDB.getChats().size(); i++) { + for (int j = 0; j < localDB.getChats().get(i).getModel().getSize(); j++) { + if (localDB.getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.SENT) { + addMessageToSync(localDB.getChats().get(i).getModel().get(j)); + } + } + } + } + + /** + * Changes all Messages with State RECEIVED of a specific chat to State READ. + *
+ * Adds these Messages to the "readMessages" Sync object. + * + * @param currentChat + * @since Envoy v0.1-alpha + */ + public void setMessagesToRead(Chat currentChat) { + for (int j = 0; j < currentChat.getModel().getSize(); j++) { + if (currentChat.getModel().get(j).getMetadata().getRecipient() != currentChat.getRecipient().getID()) { + if (currentChat.getModel().get(j).getMetadata().getState() == MessageState.RECEIVED) { + currentChat.getModel().get(j).getMetadata().setState(MessageState.READ); + readMessages.getMessages().add(currentChat.getModel().get(j)); + } + } + } + } + + /** + * Adds a Message with State WAITING to a specific chat in the LocalDB. + * + * @param message + * @param currentChat + * @since Envoy v0.1-alpha + */ + public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); } + + /** + * Adds all Messages with State WAITING from the LocalDB to the Sync. + * + * @param localDB + * @since Envoy v0.1-alpha + */ + public void addWaitingMessagesToSync(LocalDB localDB) { + for (int i = 0; i < localDB.getChats().size(); i++) { + for (int j = 0; j < localDB.getChats().get(i).getModel().getSize(); j++) { + if (localDB.getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) { + // addMessageToSync(localDB.getChats().get(i).getModel().get(j)); + System.out.println("Got Waiting Message"); + sync.getMessages().add(0, localDB.getChats().get(i).getModel().get(j)); + } + } + } + } + + /** + * @return the sender object that represents this client. * @since Envoy v0.1-alpha */ public User getSender() { return sender; } + /** + * @return the current recipient of the current chat. + * @since Envoy v0.1-alpha + */ public User getRecipient() { return recipient; } + /** + * Sets the recipient. + * + * @param recipient + * @since Envoy v0.1-alpha + */ public void setRecipient(User recipient) { this.recipient = recipient; } + /** + * @return boolean weather the recipient is currently set. + * @since Envoy v0.1-alpha + */ public boolean hasRecipient() { return recipient != null; } + + /** + * Clears the "unreadMessagesSync" Sync object. + * + * @since Envoy v0.1-alpha + */ + public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 5f79b13..4ac1a6c 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -27,9 +27,9 @@ import envoy.client.Chat; import envoy.client.Client; import envoy.client.LocalDB; import envoy.schema.Message; -import envoy.schema.Messages; +import envoy.schema.Sync; import envoy.schema.User; -import envoy.schema.Users; + /** * Project: envoy-client
@@ -147,18 +147,17 @@ public class ChatWindow extends JFrame { postButton.addActionListener((evt) -> { if (!client.hasRecipient()) { - JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE); - return; + JOptionPane.showMessageDialog(this, + "Please select a recipient!", + "Cannot send message", + JOptionPane.INFORMATION_MESSAGE); } if (!messageEnterTextfield.getText().isEmpty()) try { // Create and send message object final Message message = client.createMessage(messageEnterTextfield.getText()); - client.sendMessage(message); - - // Append message object to chat - currentChat.appendMessage(message); + client.addWaitingMessageToLocalDB(message, currentChat); messageList.setModel(currentChat.getModel()); // Clear text field @@ -200,16 +199,23 @@ public class ChatWindow extends JFrame { final User user = selectedUserList.getSelectedValue(); client.setRecipient(user); - currentChat = localDB.getChats().stream().filter(chat -> chat.getRecipient().getID() == user.getID()).findFirst().get(); + currentChat = localDB.getChats() + .stream() + .filter(chat -> chat.getRecipient().getID() == user.getID()) + .findFirst() + .get(); client.setRecipient(user); textPane.setText(currentChat.getRecipient().getName()); + messageList.setModel(currentChat.getModel()); contentPane.revalidate(); } }); - + + + userList.setSelectionForeground(new Color(255, 255, 255)); userList.setSelectionBackground(new Color(102, 0, 153)); userList.setForeground(new Color(255, 255, 255)); @@ -227,6 +233,7 @@ public class ChatWindow extends JFrame { contentPane.add(userList, gbc_userList); contentPane.revalidate(); + loadUsersAndChats(); startReceiverThread(5000); @@ -239,9 +246,9 @@ public class ChatWindow extends JFrame { */ private void loadUsersAndChats() { new Thread(() -> { - Users users = client.getUsersListXml(); + Sync users = client.getUsersListXml(); DefaultListModel userListModel = new DefaultListModel<>(); - users.getUser().forEach(user -> { + users.getUsers().forEach(user -> { userListModel.addElement(user); // Check if user exists in local DB @@ -253,18 +260,34 @@ public class ChatWindow extends JFrame { } /** - * Checks for new messages and adds them to the chat list. + * For detailed information see Javadoc of corresponding methods. * * @param timeout the amount of time that passes between two requests sent to * the server + * @since Envoy v0.1-alpha */ private void startReceiverThread(int timeout) { new Timer(timeout, (evt) -> { - Messages unreadMessages = client.getUnreadMessages(client.getSender().getID()); - for (int i = 0; i < unreadMessages.getMessage().size(); i++) - for (int j = 0; j < localDB.getChats().size(); j++) - if (localDB.getChats().get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender()) - localDB.getChats().get(j).appendMessage(unreadMessages.getMessage().get(i)); - }).start(); + if(currentChat != null) { + client.setMessagesToRead(currentChat); + } + client.sendSync(client.getSender().getID(), localDB); + client.addUnreadMessagesToLocalDB(localDB); + client.clearUnreadMessagesSync(); + + for (int i = 0; i < userList.getModel().getSize(); i++) { + for (int j = 0; j < localDB.getChats().size(); j++) { + if(userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID()) { + userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus()); + } + } + + } + + contentPane.revalidate(); + contentPane.repaint(); + userList.revalidate(); + userList.repaint(); + }).start(); } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java index 4490b69..7afdec3 100644 --- a/src/main/java/envoy/client/ui/MessageListRenderer.java +++ b/src/main/java/envoy/client/ui/MessageListRenderer.java @@ -18,6 +18,7 @@ import envoy.schema.Message; * Created: 19 Oct 2019
* * @author Kai S. K. Engelbart + * @author Maximilian Käfer * @since Envoy v0.1-alpha */ public class MessageListRenderer extends JLabel implements ListCellRenderer { @@ -38,14 +39,16 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer

%s

%s", + "

%s

%s :%s", date, - text)); + text, + state)); return this; } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/UserListRenderer.java b/src/main/java/envoy/client/ui/UserListRenderer.java index c0c13ba..40bb2ad 100644 --- a/src/main/java/envoy/client/ui/UserListRenderer.java +++ b/src/main/java/envoy/client/ui/UserListRenderer.java @@ -7,6 +7,7 @@ import javax.swing.JList; import javax.swing.ListCellRenderer; import envoy.schema.User; +import envoy.schema.User.UserStatus; /** * Defines how the {@code UserList} is displayed. @@ -16,6 +17,7 @@ import envoy.schema.User; * Created: 12 Oct 2019
* * @author Kai S. K. Engelbart + * @author Maximilian Käfer * @since Envoy v0.1-alpha */ public class UserListRenderer extends JLabel implements ListCellRenderer { @@ -36,8 +38,27 @@ public class UserListRenderer extends JLabel implements ListCellRenderer { // Enable background rendering setOpaque(true); - setText(value.getName()); - setFont(list.getFont()); + + final String name = value.getName(); + final UserStatus status = value.getStatus(); + + switch (status) { + case ONLINE: + setText(String.format( + "

%s

%s", + status, + name)); + break; + + case OFFLINE: + setText(String.format( + "

%s

%s", + status, + name)); + break; + } + + return this; } From c58a4a73d3fc01ffd27bdcb2455e6708b611e719 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 9 Nov 2019 09:38:10 +0100 Subject: [PATCH 2/9] Extracted updateUserStates method, fixed localDB initialization --- src/main/java/envoy/client/Config.java | 1 + src/main/java/envoy/client/ui/ChatWindow.java | 18 ++++++++---------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index 1d107ca..0da3cf1 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -53,6 +53,7 @@ public class Config { case "-db": localDB = new File(args[++i]); } + if (localDB == null) localDB = new File(".\\localDB"); } /** diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 4ac1a6c..490308d 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -275,19 +275,17 @@ public class ChatWindow extends JFrame { client.addUnreadMessagesToLocalDB(localDB); client.clearUnreadMessagesSync(); - for (int i = 0; i < userList.getModel().getSize(); i++) { - for (int j = 0; j < localDB.getChats().size(); j++) { - if(userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID()) { - userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus()); - } - } - - } + updateUserStates(); contentPane.revalidate(); contentPane.repaint(); - userList.revalidate(); - userList.repaint(); }).start(); } + + private void updateUserStates() { + for (int i = 0; i < userList.getModel().getSize(); i++) + for (int j = 0; j < localDB.getChats().size(); j++) + if (userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID()) + userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus()); + } } \ No newline at end of file From 6c212df30913fb4c9ce7b8bb1ae3d85368ed8010 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 9 Nov 2019 13:25:18 +0100 Subject: [PATCH 3/9] Revised structure --- src/main/java/envoy/client/Client.java | 397 +++--------------- src/main/java/envoy/client/LocalDB.java | 302 ++++++++++++- src/main/java/envoy/client/ui/ChatWindow.java | 42 +- 3 files changed, 377 insertions(+), 364 deletions(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index b755c4a..ed2ff22 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -31,51 +31,30 @@ import envoy.schema.User; public class Client { private ObjectFactory objectFactory = new ObjectFactory(); - private DatatypeFactory datatypeFactory; private Config config; private User sender, recipient; - private Sync unreadMessagesSync = objectFactory.createSync(); - public Sync sync = objectFactory.createSync(); - public Sync readMessages = objectFactory.createSync(); - public Client(Config config, String username) { - this.config = config; - try { - datatypeFactory = DatatypeFactory.newInstance(); - } catch (DatatypeConfigurationException e) { - e.printStackTrace(); - } - sender = getUser(username); + this.config = config; + sender = getUser(username); System.out.println("ID: " + sender.getID()); } - /** - * Creates a {@link Message} object serializable to XML. - * - * @param textContent The content (text) of the message - * @return prepared {@link Message} object - */ - public Message createMessage(String textContent) { - Message.Metadata metaData = objectFactory.createMessageMetadata(); - metaData.setSender(sender.getID()); - metaData.setRecipient(recipient.getID()); - metaData.setState(MessageState.WAITING); - metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString())); + private R post(String uri, T body, Class responseBodyClass) { + javax.ws.rs.client.Client client = ClientBuilder.newClient(); + WebTarget target = client.target(uri); - Message.Content content = objectFactory.createMessageContent(); - content.setType("text"); - content.setText(textContent); + Response response = target.request().post(Entity.entity(body, "application/xml")); + R responseBody = response.readEntity(responseBodyClass); + response.close(); + client.close(); - Message message = objectFactory.createMessage(); - message.setMetadata(metaData); - message.getContent().add(content); - - return message; + return responseBody; } - + /** * Returns a {@link Sync} with all users on the server. + * * @return Sync - List of all users on the server. * @since Envoy v0.1-alpha */ @@ -85,14 +64,11 @@ public class Client { user.setID(-1); sendSync.getUsers().add(user); - javax.ws.rs.client.Client client = ClientBuilder.newClient(); - WebTarget target = client.target(String - .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0)); - - Response response = target.request().post(Entity.entity(sendSync, "application/xml")); - Sync returnSendSync = response.readEntity(Sync.class); - response.close(); - client.close(); + Sync returnSendSync = post( + String + .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0), + sendSync, + Sync.class); return returnSendSync; } @@ -110,321 +86,74 @@ public class Client { user.setName(name); senderSync.getUsers().add(user); - javax.ws.rs.client.Client client = ClientBuilder.newClient(); - WebTarget target = client.target(String - .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0)); + Sync returnSenderSync = post( + String + .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0), + senderSync, + Sync.class); + + User returnSender = objectFactory.createUser(); - Response response = target.request().post(Entity.entity(senderSync, "application/xml")); - User returnSender = objectFactory.createUser(); - Sync returnSenderSync = response.readEntity(Sync.class); if (returnSenderSync.getUsers().size() == 1) { returnSender = returnSenderSync.getUsers().get(0); } else { System.out.println("ERROR exiting..."); } - response.close(); - client.close(); + return returnSender; } /** * Sends the "sync" Sync Object to the server and gets a "returnSync" Sync * Object as response.
- * It is also used to get the own sender at the start of the client - * (Client sends "sync" Sync Object with single user in it(name: the name entered at login, id: 0, UserStatus:null))
- * and to get a complete list of all users saved on the server. - * (Client sends "sync" Sync Object with single user in it(name: "" (empty), id: -1, UserStatus:null))
+ * It is also used to get the own sender at the start of the client + * (Client sends "sync" Sync Object with single user in it(name: the name + * entered at login, id: 0, UserStatus:null))
+ * and to get a complete list of all users saved on the server. + * (Client sends "sync" Sync Object with single user in it(name: "" (empty), id: + * -1, UserStatus:null))
* This method also processes the response Sync Object.
* It sorts its users and messages by specific variables and does certain things * with them.
*
* Messages:
- * -State SENT: Update Local message(s) with State WAITING (add Message ID and - * change State to SENT). (server sends these informations to the client if - * message(s) with State WAITING were successfully sent to the server)
- * -State RECEIVED, SenderID != 0: Adds the unread Messages returned from the - * server in the latest sync to the "unreadMessagesSync" Sync Object.
- * -State RECEIVED, SenderID == 0: Update message(s) in localDB to state RECEIVED. - * (server sends these informations to the client if the other client received - * the message(s).)
- * -State READ: Update message(s) in the LocalDB to state READ. (server sends these informations to the client if the other client read - * the message(s).)
+ * -State SENT: Update Local message(s) with State WAITING (add Message ID and + * change State to SENT). (server sends these informations to the client if + * message(s) with State WAITING were successfully sent to the server)
+ * -State RECEIVED, SenderID != 0: Adds the unread Messages returned from the + * server in the latest sync to the "unreadMessagesSync" Sync Object.
+ * -State RECEIVED, SenderID == 0: Update message(s) in localDB to state + * RECEIVED. + * (server sends these informations to the client if the other client received + * the message(s).)
+ * -State READ: Update message(s) in the LocalDB to state READ. (server sends + * these informations to the client if the other client read + * the message(s).)
*
* Users:
- * Updating UserStatus of all users in LocalDB. (Server sends all users with their updated UserStatus to the client.)
+ * Updating UserStatus of all users in LocalDB. (Server sends all users with + * their updated UserStatus to the client.)
* * @param userId - * @param localDB * @since Envoy v0.1-alpha */ - public void sendSync(long userId, LocalDB localDB) { - new Thread(() -> { - // Print sync XML to console - JAXBContext jc; - try { - jc = JAXBContext.newInstance("envoy.schema"); - Marshaller m = jc.createMarshaller(); - m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - m.marshal(sync, System.out); - } catch (JAXBException e) { - e.printStackTrace(); - } - addWaitingMessagesToSync(localDB); - - getSentStateMessagesFromLocalDB(localDB); - for (int i = 0; i < readMessages.getMessages().size(); i++) { - sync.getMessages().add(readMessages.getMessages().get(i)); - } - readMessages.getMessages().clear(); - - // Send sync - javax.ws.rs.client.Client client = ClientBuilder.newClient(); - WebTarget target = client - .target(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", - config.getServer(), - config.getPort(), - userId)); - - Response response = target.request().post(Entity.entity(sync, "application/xml")); - - Sync returnSync = response.readEntity(Sync.class); - if (returnSync.getMessages().size() != 0) { - System.out.println("Message ID: " + returnSync.getMessages().get(0).getMetadata().getMessageId()); - } - - for (int i = 0; i < returnSync.getMessages().size(); i++) { - - // Print sync XML to console - JAXBContext jc2; - try { - jc2 = JAXBContext.newInstance("envoy.schema"); - Marshaller m = jc2.createMarshaller(); - m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - m.marshal(returnSync, System.out); - } catch (JAXBException e) { - e.printStackTrace(); - } - - if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 - && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) { - // Update Local Messages with State WAITING (add Message ID and change State to - // SENT) - for (int j = 0; j < sync.getMessages().size(); j++) { - if (j == i) { - sync.getMessages() - .get(j) - .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 - && returnSync.getMessages().get(i).getMetadata().getSender() != 0 - && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { - // these are the unread Messages from the server - unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i)); - - } - - if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 - && returnSync.getMessages().get(i).getMetadata().getSender() == 0 - && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { - // Update Messages in localDB to state RECEIVED - for (int j = 0; j < localDB.getChats().size(); j++) { - if (localDB.getChats().get(j).getRecipient().getID() == returnSync.getMessages() - .get(i) - .getMetadata() - .getRecipient()) { - for (int k = 0; k < localDB.getChats().get(j).getModel().getSize(); k++) { - if (localDB.getChats() - .get(j) - .getModel() - .get(k) - .getMetadata() - .getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { - // Update Message in LocalDB - localDB.getChats() - .get(j) - .getModel() - .get(k) - .getMetadata() - .setState(returnSync.getMessages().get(j).getMetadata().getState()); - } - } - } - } - - } - - if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 - && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) { - // Update local Messages to state READ - System.out - .println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() - + "was initialized to be set to READ in localDB."); - for (int j = 0; j < localDB.getChats().size(); j++) { - if (localDB.getChats().get(j).getRecipient().getID() == returnSync.getMessages() - .get(i) - .getMetadata() - .getRecipient()) { - System.out.println( - "Chat with: " + localDB.getChats().get(j).getRecipient().getID() + "was selected."); - for (int k = 0; k < localDB.getChats().get(j).getModel().getSize(); k++) { - if (localDB.getChats() - .get(j) - .getModel() - .get(k) - .getMetadata() - .getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { - System.out.println("Message with ID: " - + localDB.getChats().get(j).getModel().get(k).getMetadata().getMessageId() - + "was selected."); - localDB.getChats() - .get(j) - .getModel() - .get(k) - .getMetadata() - .setState(returnSync.getMessages().get(i).getMetadata().getState()); - System.out.println("Message State is now: " + localDB.getChats() - .get(j) - .getModel() - .get(k) - .getMetadata() - .getState() - .toString()); - } - } - } - } - - } - } - - // Updating UserStatus of all Users in LocalDB - for (int j = 0; j < returnSync.getUsers().size(); j++) { - for (int k = 0; k < localDB.getChats().size(); k++) { - if (localDB.getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) { - - localDB.getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus()); - System.out.println(localDB.getChats().get(k).getRecipient().getStatus().toString()); - } - } - } - - System.out.println("Response code: " + response.getStatus()); - - response.close(); - client.close(); - sync.getMessages().clear(); - sync.getUsers().clear(); - - }).start(); - System.out.println(sync.getMessages().size()); - - } - - /** - * 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 "sync" 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 - * right chats in the LocalDB. - * - * @param localDB - * @since Envoy v0.1-alpha - */ - public void addUnreadMessagesToLocalDB(LocalDB localDB) { - Sync unreadMessages = unreadMessagesSync; - for (int i = 0; i < unreadMessages.getMessages().size(); i++) - for (int j = 0; j < localDB.getChats().size(); j++) - if (localDB.getChats().get(j).getRecipient().getID() == unreadMessages.getMessages() - .get(i) - .getMetadata() - .getSender()) { - localDB.getChats().get(j).appendMessage(unreadMessages.getMessages().get(i)); - } - } - - /** - * Gets all Messages with state SENT from the LocalDB and adds them to the - * "sync" Sync object. - * - * @param localDB - * @since Envoy v0.1-alpha - */ - public void getSentStateMessagesFromLocalDB(LocalDB localDB) { - for (int i = 0; i < localDB.getChats().size(); i++) { - for (int j = 0; j < localDB.getChats().get(i).getModel().getSize(); j++) { - if (localDB.getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.SENT) { - addMessageToSync(localDB.getChats().get(i).getModel().get(j)); - } - } + public Sync sendSync(long userId, Sync sync) { + // Print sync XML to console + JAXBContext jc; + try { + jc = JAXBContext.newInstance("envoy.schema"); + Marshaller m = jc.createMarshaller(); + m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + m.marshal(sync, System.out); + } catch (JAXBException e) { + e.printStackTrace(); } - } - /** - * Changes all Messages with State RECEIVED of a specific chat to State READ. - *
- * Adds these Messages to the "readMessages" Sync object. - * - * @param currentChat - * @since Envoy v0.1-alpha - */ - public void setMessagesToRead(Chat currentChat) { - for (int j = 0; j < currentChat.getModel().getSize(); j++) { - if (currentChat.getModel().get(j).getMetadata().getRecipient() != currentChat.getRecipient().getID()) { - if (currentChat.getModel().get(j).getMetadata().getState() == MessageState.RECEIVED) { - currentChat.getModel().get(j).getMetadata().setState(MessageState.READ); - readMessages.getMessages().add(currentChat.getModel().get(j)); - } - } - } - } - - /** - * Adds a Message with State WAITING to a specific chat in the LocalDB. - * - * @param message - * @param currentChat - * @since Envoy v0.1-alpha - */ - public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); } - - /** - * Adds all Messages with State WAITING from the LocalDB to the Sync. - * - * @param localDB - * @since Envoy v0.1-alpha - */ - public void addWaitingMessagesToSync(LocalDB localDB) { - for (int i = 0; i < localDB.getChats().size(); i++) { - for (int j = 0; j < localDB.getChats().get(i).getModel().getSize(); j++) { - if (localDB.getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) { - // addMessageToSync(localDB.getChats().get(i).getModel().get(j)); - System.out.println("Got Waiting Message"); - sync.getMessages().add(0, localDB.getChats().get(i).getModel().get(j)); - } - } - } + // Send sync + return post(String + .format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), userId), + sync, + Sync.class); } /** @@ -453,10 +182,4 @@ public class Client { */ public boolean hasRecipient() { return recipient != null; } - /** - * Clears the "unreadMessagesSync" Sync object. - * - * @since Envoy v0.1-alpha - */ - public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); } } \ No newline at end of file diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index d7b3409..e63fc72 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -6,11 +6,22 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.time.Instant; import java.util.ArrayList; import java.util.List; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; + import envoy.exception.EnvoyException; +import envoy.schema.Message; import envoy.schema.User; +import envoy.schema.Message.Metadata.MessageState; +import envoy.schema.ObjectFactory; +import envoy.schema.Sync; /** * Project: envoy-client
@@ -22,9 +33,11 @@ import envoy.schema.User; */ public class LocalDB { - private File localDB; - private User sender; - private List chats = new ArrayList<>(); + private File localDB; + private User sender; + private List chats = new ArrayList<>(); + private ObjectFactory objectFactory = new ObjectFactory(); + private DatatypeFactory datatypeFactory; /** * Constructs an empty local database. @@ -32,7 +45,14 @@ public class LocalDB { * @param sender the user that is logged in with this client * @since Envoy v0.1-alpha **/ - public LocalDB(User sender) { this.sender = sender; } + public LocalDB(User sender) { + this.sender = sender; + try { + datatypeFactory = DatatypeFactory.newInstance(); + } catch (DatatypeConfigurationException e) { + e.printStackTrace(); + } + } /** * Initializes the local database and fills it with values @@ -43,8 +63,8 @@ public class LocalDB { * @since Envoy v0.1-alpha **/ public void initializeDBFile(File localDBDir) throws EnvoyException { - if (localDBDir.exists() && !localDBDir.isDirectory()) - throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath())); + if (localDBDir.exists() && !localDBDir.isDirectory()) throw new EnvoyException( + String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath())); localDB = new File(localDBDir, sender.getID() + ".db"); if (localDB.exists()) loadFromLocalDB(); } @@ -87,6 +107,276 @@ public class LocalDB { } } + // TODO + /** + * Creates a {@link Message} object serializable to XML. + * + * @param textContent The content (text) of the message + * @return prepared {@link Message} object + */ + public Message createMessage(String textContent, User recipient) { + Message.Metadata metaData = objectFactory.createMessageMetadata(); + metaData.setSender(sender.getID()); + metaData.setRecipient(recipient.getID()); + metaData.setState(MessageState.WAITING); + metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString())); + + Message.Content content = objectFactory.createMessageContent(); + content.setType("text"); + content.setText(textContent); + + Message message = objectFactory.createMessage(); + message.setMetadata(metaData); + message.getContent().add(content); + + return message; + } + + private Sync unreadMessagesSync = objectFactory.createSync(); + public Sync sync = objectFactory.createSync(); + public Sync readMessages = objectFactory.createSync(); + + public Sync fillSync(long userId) { + + addWaitingMessagesToSync(); + + getSentStateMessagesFromLocalDB(); + for (int i = 0; i < readMessages.getMessages().size(); i++) { + sync.getMessages().add(readMessages.getMessages().get(i)); + } + readMessages.getMessages().clear(); + + + System.out.println(sync.getMessages().size()); + return sync; + } + + public void applySync (Sync returnSync) { + for (int i = 0; i < returnSync.getMessages().size(); i++) { + if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 + && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) { + // Update Local Messages with State WAITING (add Message ID and change State to + // SENT) + for (int j = 0; j < sync.getMessages().size(); j++) { + if (j == i) { + sync.getMessages() + .get(j) + .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 + && returnSync.getMessages().get(i).getMetadata().getSender() != 0 + && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { + // these are the unread Messages from the server + unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i)); + + } + + if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 + && returnSync.getMessages().get(i).getMetadata().getSender() == 0 + && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { + // Update Messages in localDB to state RECEIVED + for (int j = 0; j < getChats().size(); j++) { + if (getChats().get(j).getRecipient().getID() == returnSync.getMessages() + .get(i) + .getMetadata() + .getRecipient()) { + for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { + if (getChats() + .get(j) + .getModel() + .get(k) + .getMetadata() + .getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { + // Update Message in LocalDB + getChats() + .get(j) + .getModel() + .get(k) + .getMetadata() + .setState(returnSync.getMessages().get(j).getMetadata().getState()); + } + } + } + } + + } + + if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 + && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) { + // Update local Messages to state READ + System.out + .println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() + + "was initialized to be set to READ in localDB."); + for (int j = 0; j < getChats().size(); j++) { + if (getChats().get(j).getRecipient().getID() == returnSync.getMessages() + .get(i) + .getMetadata() + .getRecipient()) { + System.out.println( + "Chat with: " + getChats().get(j).getRecipient().getID() + "was selected."); + for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { + if (getChats() + .get(j) + .getModel() + .get(k) + .getMetadata() + .getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { + System.out.println("Message with ID: " + + getChats().get(j).getModel().get(k).getMetadata().getMessageId() + + "was selected."); + getChats() + .get(j) + .getModel() + .get(k) + .getMetadata() + .setState(returnSync.getMessages().get(i).getMetadata().getState()); + System.out.println("Message State is now: " + getChats() + .get(j) + .getModel() + .get(k) + .getMetadata() + .getState() + .toString()); + } + } + } + } + + } + } + + // Updating UserStatus of all Users in LocalDB + for (int j = 0; j < returnSync.getUsers().size(); j++) { + for (int k = 0; k < getChats().size(); k++) { + if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) { + + getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus()); + System.out.println(getChats().get(k).getRecipient().getStatus().toString()); + } + } + } + + sync.getMessages().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 "sync" 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 + * right chats in the LocalDB. + * + * @param localDB + * @since Envoy v0.1-alpha + */ + public void addUnreadMessagesToLocalDB() { + Sync unreadMessages = unreadMessagesSync; + for (int i = 0; i < unreadMessages.getMessages().size(); i++) + for (int j = 0; j < getChats().size(); j++) + if (getChats().get(j).getRecipient().getID() == unreadMessages.getMessages() + .get(i) + .getMetadata() + .getSender()) { + getChats().get(j).appendMessage(unreadMessages.getMessages().get(i)); + } + } + + /** + * Gets all Messages with state SENT from the LocalDB and adds them to the + * "sync" Sync object. + * + * @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. + *
+ * Adds these Messages to the "readMessages" Sync object. + * + * @param currentChat + * @since Envoy v0.1-alpha + */ + public void setMessagesToRead(Chat currentChat) { + for (int j = 0; j < currentChat.getModel().getSize(); j++) { + if (currentChat.getModel().get(j).getMetadata().getRecipient() != currentChat.getRecipient().getID()) { + if (currentChat.getModel().get(j).getMetadata().getState() == MessageState.RECEIVED) { + currentChat.getModel().get(j).getMetadata().setState(MessageState.READ); + readMessages.getMessages().add(currentChat.getModel().get(j)); + } + } + } + } + + /** + * Adds a Message with State WAITING to a specific chat in the LocalDB. + * + * @param message + * @param currentChat + * @since Envoy v0.1-alpha + */ + public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); } + + /** + * Adds all Messages with State WAITING from the LocalDB to the Sync. + * + * @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)); + System.out.println("Got Waiting Message"); + sync.getMessages().add(0, getChats().get(i).getModel().get(j)); + } + } + } + } + + + /** + * Clears the "unreadMessagesSync" Sync object. + * + * @since Envoy v0.1-alpha + */ + public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); } + + /** * @return all saves {@link Chat} objects that list the client user as the * sender diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 490308d..545715e 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -30,7 +30,6 @@ import envoy.schema.Message; import envoy.schema.Sync; import envoy.schema.User; - /** * Project: envoy-client
* File: ChatWindow.java
@@ -156,8 +155,9 @@ public class ChatWindow extends JFrame { if (!messageEnterTextfield.getText().isEmpty()) try { // Create and send message object - final Message message = client.createMessage(messageEnterTextfield.getText()); - client.addWaitingMessageToLocalDB(message, currentChat); + final Message message = localDB.createMessage(messageEnterTextfield.getText(), + currentChat.getRecipient()); + localDB.addWaitingMessageToLocalDB(message, currentChat); messageList.setModel(currentChat.getModel()); // Clear text field @@ -208,14 +208,12 @@ public class ChatWindow extends JFrame { client.setRecipient(user); textPane.setText(currentChat.getRecipient().getName()); - + messageList.setModel(currentChat.getModel()); contentPane.revalidate(); } }); - - - + userList.setSelectionForeground(new Color(255, 255, 255)); userList.setSelectionBackground(new Color(102, 0, 153)); userList.setForeground(new Color(255, 255, 255)); @@ -233,9 +231,8 @@ public class ChatWindow extends JFrame { contentPane.add(userList, gbc_userList); contentPane.revalidate(); - loadUsersAndChats(); - startReceiverThread(5000); + startSyncThread(5000); contentPane.revalidate(); } @@ -266,20 +263,23 @@ public class ChatWindow extends JFrame { * the server * @since Envoy v0.1-alpha */ - private void startReceiverThread(int timeout) { + private void startSyncThread(int timeout) { new Timer(timeout, (evt) -> { - if(currentChat != null) { - client.setMessagesToRead(currentChat); - } - client.sendSync(client.getSender().getID(), localDB); - client.addUnreadMessagesToLocalDB(localDB); - client.clearUnreadMessagesSync(); - - updateUserStates(); - - contentPane.revalidate(); - contentPane.repaint(); + if (currentChat != null) { localDB.setMessagesToRead(currentChat); } + + new Thread(() -> { + + // Synchronize + localDB.applySync( + client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID()))); + localDB.addUnreadMessagesToLocalDB(); + localDB.clearUnreadMessagesSync(); + + // Update UI + SwingUtilities + .invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); }); }).start(); + }).start(); } private void updateUserStates() { From f69754d476a0aab480b4ec129fa611f39dea8964 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 9 Nov 2019 13:29:47 +0100 Subject: [PATCH 4/9] Removed unnecessary TODO item --- src/main/java/envoy/client/LocalDB.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index e63fc72..53cd1eb 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -107,7 +107,6 @@ public class LocalDB { } } - // TODO /** * Creates a {@link Message} object serializable to XML. * From 4ff67c69a26d4eb7cc04284a5a3dc85b406def33 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 9 Nov 2019 13:35:17 +0100 Subject: [PATCH 5/9] Fixed grammar --- src/main/java/envoy/client/LocalDB.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 53cd1eb..11b5526 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -253,7 +253,7 @@ public class LocalDB { } } - // Updating UserStatus of all Users in LocalDB + // Updating UserStatus of all users in LocalDB for (int j = 0; j < returnSync.getUsers().size(); j++) { for (int k = 0; k < getChats().size(); k++) { if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) { @@ -278,7 +278,7 @@ public class LocalDB { public void addMessageToSync(Message message) { sync.getMessages().add(message); } /** - * Adds a user to the "sync" Sync object. + * Adds a user to the {@code sync} {@link Sync} object. * * @param user * @since Envoy v0.1-alpha @@ -286,7 +286,7 @@ public class LocalDB { 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. * * @param localDB @@ -305,7 +305,7 @@ public class LocalDB { } /** - * Gets all Messages with state SENT from the LocalDB and adds them to the + * Gets all messages with state SENT from the LocalDB and adds them to the * "sync" Sync object. * * @param localDB @@ -322,9 +322,9 @@ public class LocalDB { } /** - * Changes all Messages with State RECEIVED of a specific chat to State READ. + * Changes all messages with State RECEIVED of a specific chat to State READ. *
- * Adds these Messages to the "readMessages" Sync object. + * Adds these Messages to the {@code readMessages} {@link Sync} object. * * @param currentChat * @since Envoy v0.1-alpha @@ -341,7 +341,7 @@ public class LocalDB { } /** - * Adds a Message with State WAITING to a specific chat in the LocalDB. + * Adds a message with State WAITING to a specific chat in the LocalDB. * * @param message * @param currentChat @@ -350,7 +350,7 @@ public class LocalDB { public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); } /** - * Adds all Messages with State WAITING from the LocalDB to the Sync. + * Adds all messages with State WAITING from the {@link LocalDB} to the Sync. * * @param localDB * @since Envoy v0.1-alpha @@ -369,7 +369,7 @@ public class LocalDB { /** - * Clears the "unreadMessagesSync" Sync object. + * Clears the {@code unreadMessagesSync} {@link Sync} object. * * @since Envoy v0.1-alpha */ From 42f3a97e8a6b3da3cf9cc829ddd308552e284b13 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 9 Nov 2019 13:37:51 +0100 Subject: [PATCH 6/9] Removed empty lines --- src/main/java/envoy/client/LocalDB.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 11b5526..fb69a94 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -366,8 +366,7 @@ public class LocalDB { } } } - - + /** * Clears the {@code unreadMessagesSync} {@link Sync} object. * @@ -375,7 +374,6 @@ public class LocalDB { */ public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); } - /** * @return all saves {@link Chat} objects that list the client user as the * sender From cd8a92c619ffbaa6a755c0c7db95506a79e3d9c0 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 9 Nov 2019 14:06:09 +0100 Subject: [PATCH 7/9] Fixed reading messages Fixes #23 --- src/main/java/envoy/client/Client.java | 5 - src/main/java/envoy/client/LocalDB.java | 127 ++++++++---------- src/main/java/envoy/client/ui/ChatWindow.java | 5 +- 3 files changed, 57 insertions(+), 80 deletions(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index ed2ff22..f56cfe4 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -1,6 +1,5 @@ package envoy.client; -import java.time.Instant; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; @@ -8,11 +7,7 @@ import javax.ws.rs.core.Response; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; -import javax.xml.datatype.DatatypeConfigurationException; -import javax.xml.datatype.DatatypeFactory; -import envoy.schema.Message; -import envoy.schema.Message.Metadata.MessageState; import envoy.schema.ObjectFactory; import envoy.schema.Sync; import envoy.schema.User; diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index fb69a94..e0ff7c1 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -10,18 +10,15 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import envoy.exception.EnvoyException; import envoy.schema.Message; -import envoy.schema.User; import envoy.schema.Message.Metadata.MessageState; import envoy.schema.ObjectFactory; import envoy.schema.Sync; +import envoy.schema.User; /** * Project: envoy-client
@@ -29,6 +26,7 @@ import envoy.schema.Sync; * Created: 27.10.2019
* * @author Kai S. K. Engelbart + * @author Maximilian Käfer * @since Envoy v0.1-alpha */ public class LocalDB { @@ -130,27 +128,26 @@ public class LocalDB { return message; } - + private Sync unreadMessagesSync = objectFactory.createSync(); public Sync sync = objectFactory.createSync(); public Sync readMessages = objectFactory.createSync(); - - public Sync fillSync(long userId) { - - addWaitingMessagesToSync(); - getSentStateMessagesFromLocalDB(); - for (int i = 0; i < readMessages.getMessages().size(); i++) { - sync.getMessages().add(readMessages.getMessages().get(i)); - } - readMessages.getMessages().clear(); - + public Sync fillSync(long userId) { + + addWaitingMessagesToSync(); + + getSentStateMessagesFromLocalDB(); + for (int i = 0; i < readMessages.getMessages().size(); i++) { + sync.getMessages().add(readMessages.getMessages().get(i)); + } + readMessages.getMessages().clear(); System.out.println(sync.getMessages().size()); return sync; } - - public void applySync (Sync returnSync) { + + public void applySync(Sync returnSync) { for (int i = 0; i < returnSync.getMessages().size(); i++) { if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) { @@ -167,37 +164,34 @@ public class LocalDB { .getMetadata() .setState(returnSync.getMessages().get(j).getMetadata().getState()); } - + } - + } if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() != 0 && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { // these are the unread Messages from the server unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i)); - + } - + if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() == 0 && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { // Update Messages in localDB to state RECEIVED for (int j = 0; j < getChats().size(); j++) { - if (getChats().get(j).getRecipient().getID() == returnSync.getMessages() - .get(i) - .getMetadata() - .getRecipient()) { + if (getChats().get(j) + .getRecipient() + .getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) { for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { - if (getChats() - .get(j) - .getModel() - .get(k) + if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync + .getMessages() + .get(i) .getMetadata() - .getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { + .getMessageId()) { // Update Message in LocalDB - getChats() - .get(j) + getChats().get(j) .getModel() .get(k) .getMetadata() @@ -206,50 +200,40 @@ public class LocalDB { } } } - + } - + if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) { // Update local Messages to state READ - System.out - .println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() - + "was initialized to be set to READ in localDB."); + System.out.println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() + + "was initialized to be set to READ in localDB."); for (int j = 0; j < getChats().size(); j++) { - if (getChats().get(j).getRecipient().getID() == returnSync.getMessages() - .get(i) - .getMetadata() - .getRecipient()) { - System.out.println( - "Chat with: " + getChats().get(j).getRecipient().getID() + "was selected."); + if (getChats().get(j) + .getRecipient() + .getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) { + System.out.println("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected."); for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { - if (getChats() - .get(j) - .getModel() - .get(k) + if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync + .getMessages() + .get(i) .getMetadata() - .getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { + .getMessageId()) { System.out.println("Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId() + "was selected."); - getChats() - .get(j) + getChats().get(j) .getModel() .get(k) .getMetadata() .setState(returnSync.getMessages().get(i).getMetadata().getState()); - System.out.println("Message State is now: " + getChats() - .get(j) - .getModel() - .get(k) - .getMetadata() - .getState() - .toString()); + System.out.println("Message State is now: " + + getChats().get(j).getModel().get(k).getMetadata().getState().toString()); } } } } - + } } @@ -257,13 +241,13 @@ public class LocalDB { for (int j = 0; j < returnSync.getUsers().size(); j++) { for (int k = 0; k < getChats().size(); k++) { if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) { - + getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus()); System.out.println(getChats().get(k).getRecipient().getStatus().toString()); } } } - + sync.getMessages().clear(); sync.getUsers().clear(); @@ -296,10 +280,9 @@ public class LocalDB { Sync unreadMessages = unreadMessagesSync; for (int i = 0; i < unreadMessages.getMessages().size(); i++) for (int j = 0; j < getChats().size(); j++) - if (getChats().get(j).getRecipient().getID() == unreadMessages.getMessages() - .get(i) - .getMetadata() - .getSender()) { + if (getChats().get(j) + .getRecipient() + .getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) { getChats().get(j).appendMessage(unreadMessages.getMessages().get(i)); } } @@ -330,14 +313,12 @@ public class LocalDB { * @since Envoy v0.1-alpha */ public void setMessagesToRead(Chat currentChat) { - for (int j = 0; j < currentChat.getModel().getSize(); j++) { - if (currentChat.getModel().get(j).getMetadata().getRecipient() != currentChat.getRecipient().getID()) { - if (currentChat.getModel().get(j).getMetadata().getState() == MessageState.RECEIVED) { - currentChat.getModel().get(j).getMetadata().setState(MessageState.READ); - readMessages.getMessages().add(currentChat.getModel().get(j)); - } - } - } + for (int i = currentChat.getModel().size() - 1; i >= 0; --i) + if (currentChat.getModel().get(i).getMetadata().getRecipient() != currentChat.getRecipient().getID()) + if (currentChat.getModel().get(i).getMetadata().getState() == MessageState.RECEIVED) { + currentChat.getModel().get(i).getMetadata().setState(MessageState.READ); + readMessages.getMessages().add(currentChat.getModel().get(i)); + } else break; } /** @@ -373,7 +354,7 @@ public class LocalDB { * @since Envoy v0.1-alpha */ public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); } - + /** * @return all saves {@link Chat} objects that list the client user as the * sender diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 545715e..c6e8c37 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -205,6 +205,9 @@ public class ChatWindow extends JFrame { .findFirst() .get(); + // Set all unread messages in the chat to read + if (currentChat != null) { localDB.setMessagesToRead(currentChat); } + client.setRecipient(user); textPane.setText(currentChat.getRecipient().getName()); @@ -265,8 +268,6 @@ public class ChatWindow extends JFrame { */ private void startSyncThread(int timeout) { new Timer(timeout, (evt) -> { - if (currentChat != null) { localDB.setMessagesToRead(currentChat); } - new Thread(() -> { // Synchronize From e1ef85d70258333d6bc006d0bb0664032a50d850 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 9 Nov 2019 14:23:26 +0100 Subject: [PATCH 8/9] Edit syncTimeout property, made Config a singleton --- src/main/java/envoy/client/Config.java | 22 +++++++++++++++++-- src/main/java/envoy/client/ui/ChatWindow.java | 5 +++-- .../envoy/client/ui/MessageListRenderer.java | 2 +- src/main/java/envoy/client/ui/Startup.java | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index 0da3cf1..c31c972 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -16,6 +16,16 @@ public class Config { private String server; private int port; private File localDB; + private int syncTimeout; + + private static Config config; + + private Config() {} + + public static Config getInstance() { + if (config == null) config = new Config(); + return config; + } /** * Defaults to the {@code client.properties} file for information. @@ -28,7 +38,8 @@ public class Config { public void load(Properties properties) { if (properties.containsKey("server")) server = properties.getProperty("server"); if (properties.containsKey("port")) port = Integer.parseInt(properties.getProperty("port")); - localDB = new File(properties.getProperty("localDB", ".\\localDB")); + localDB = new File(properties.getProperty("localDB", ".\\localDB")); + syncTimeout = Integer.parseInt(properties.getProperty("syncTimeout", "1000")); } /** @@ -54,13 +65,16 @@ public class Config { localDB = new File(args[++i]); } if (localDB == null) localDB = new File(".\\localDB"); + if (syncTimeout == 0) syncTimeout = 1000; } /** * @return {@code true} if server, port and localDB directory are known. * @since Envoy v0.1-alpha */ - public boolean isInitialized() { return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null; } + public boolean isInitialized() { + return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null; + } /** * @return the host name of the Envoy server @@ -106,4 +120,8 @@ public class Config { * @since Envoy v0.1-alpha **/ public void setLocalDB(File localDB) { this.localDB = localDB; } + + public int getSyncTimeout() { return syncTimeout; } + + public void setSyncTimeout(int syncTimeout) { this.syncTimeout = syncTimeout; } } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index c6e8c37..4a36c57 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -25,6 +25,7 @@ import javax.swing.border.EmptyBorder; import envoy.client.Chat; import envoy.client.Client; +import envoy.client.Config; import envoy.client.LocalDB; import envoy.schema.Message; import envoy.schema.Sync; @@ -235,7 +236,7 @@ public class ChatWindow extends JFrame { contentPane.revalidate(); loadUsersAndChats(); - startSyncThread(5000); + startSyncThread(Config.getInstance().getSyncTimeout()); contentPane.revalidate(); } @@ -260,7 +261,7 @@ public class ChatWindow extends JFrame { } /** - * For detailed information see Javadoc of corresponding methods. + * Updates the data model and the ui every x seconds. * * @param timeout the amount of time that passes between two requests sent to * the server diff --git a/src/main/java/envoy/client/ui/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java index 7afdec3..9053c6c 100644 --- a/src/main/java/envoy/client/ui/MessageListRenderer.java +++ b/src/main/java/envoy/client/ui/MessageListRenderer.java @@ -41,7 +41,7 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer 0) { config.load(args); } else { From acb744a34d955fcdd16549db8dbbce28afd52f66 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 9 Nov 2019 17:47:18 +0100 Subject: [PATCH 9/9] Revised merge conflict changes --- src/main/java/envoy/client/LocalDB.java | 2 +- src/main/java/envoy/client/ui/ChatWindow.java | 68 ++++++++----------- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 84383b3..142b687 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -367,5 +367,5 @@ public class LocalDB { * @return the User who initialised the local Database * @since Envoy v0.1-alpha */ - public User getUser() { return client; } + public User getUser() { return sender; } } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index e98a476..2a7ae11 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -125,8 +125,8 @@ public class ChatWindow extends JFrame { if (e.getKeyCode() == KeyEvent.VK_ENTER && ((SettingsScreen.enterToSend && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { - postMessage(client, messageList); + postMessage(messageList); } } @@ -164,42 +164,9 @@ public class ChatWindow extends JFrame { gbc_moveSelectionPostButton.insets = new Insets(10, 10, 10, 10); + postButton.addActionListener((evt) -> { postMessage(messageList); }); + contentPane.add(postButton, gbc_moveSelectionPostButton); - postButton.addActionListener((evt) -> { - if (!client.hasRecipient()) { - JOptionPane.showMessageDialog(this, - "Please select a recipient!", - "Cannot send message", - JOptionPane.INFORMATION_MESSAGE); - } - - // Create and send message object - final Message message = localDB.createMessage(messageEnterTextArea.getText(), - currentChat.getRecipient()); - localDB.addWaitingMessageToLocalDB(message, currentChat); - messageList.setModel(currentChat.getModel()); - - contentPane.add(postButton, gbc_moveSelectionPostButton); - - if (!messageEnterTextArea.getText().isEmpty()) try { - - // Create and send message object - final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient()); - localDB.addWaitingMessageToLocalDB(message, currentChat); - messageList.setModel(currentChat.getModel()); - - // Clear text field - messageEnterTextArea.setText(""); - contentPane.revalidate(); - } catch (Exception e) { - JOptionPane.showMessageDialog(this, - "An exception occured while sending a message. See the log for more details.", - "Exception occured", - JOptionPane.ERROR_MESSAGE); - e.printStackTrace(); - }); - - // Settings Button JButton settingsButton = new JButton("Settings"); settingsButton.setForeground(new Color(255, 255, 255)); @@ -208,7 +175,6 @@ public class ChatWindow extends JFrame { GridBagConstraints gbc_moveSelectionSettingsButton = new GridBagConstraints(); - gbc_moveSelectionSettingsButton.fill = GridBagConstraints.BOTH; gbc_moveSelectionSettingsButton.gridx = 2; gbc_moveSelectionSettingsButton.gridy = 0; @@ -291,7 +257,33 @@ public class ChatWindow extends JFrame { contentPane.revalidate(); } - + private void postMessage(JList messageList) { + if (!client.hasRecipient()) { + JOptionPane.showMessageDialog(this, + "Please select a recipient!", + "Cannot send message", + JOptionPane.INFORMATION_MESSAGE); + } + + if (!messageEnterTextArea.getText().isEmpty()) try { + + // Create and send message object + final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient()); + localDB.addWaitingMessageToLocalDB(message, currentChat); + messageList.setModel(currentChat.getModel()); + + // Clear text field + messageEnterTextArea.setText(""); + contentPane.revalidate(); + } catch (Exception e) { + JOptionPane.showMessageDialog(this, + "An exception occured while sending a message. See the log for more details.", + "Exception occured", + JOptionPane.ERROR_MESSAGE); + e.printStackTrace(); + } + } + /** * Initializes the elements of the user list by downloading them from the * server.