From 60c8b09b372e4d8e5f8f63246a2dbbe2a648839e Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 27 Oct 2019 17:25:58 +0100 Subject: [PATCH 1/7] Added ChatSynchronizer class to store chats locally --- .gitignore | 1 + src/main/java/envoy/client/Chat.java | 6 +- .../java/envoy/client/ChatSynchronizer.java | 47 +++++++++++++++ src/main/java/envoy/client/ui/ChatWindow.java | 59 ++++++++++++++----- src/main/java/envoy/client/ui/Startup.java | 3 +- 5 files changed, 98 insertions(+), 18 deletions(-) create mode 100644 src/main/java/envoy/client/ChatSynchronizer.java diff --git a/.gitignore b/.gitignore index b83d222..7836310 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target/ +/local_chats.db diff --git a/src/main/java/envoy/client/Chat.java b/src/main/java/envoy/client/Chat.java index 3f48c7c..6dcb35a 100644 --- a/src/main/java/envoy/client/Chat.java +++ b/src/main/java/envoy/client/Chat.java @@ -1,12 +1,16 @@ package envoy.client; +import java.io.Serializable; + import javax.swing.DefaultListModel; import envoy.schema.Message; import envoy.schema.User; -public class Chat { +public class Chat implements Serializable { + private static final long serialVersionUID = -7751248474547242056L; + private User recipient; private DefaultListModel model = new DefaultListModel<>(); diff --git a/src/main/java/envoy/client/ChatSynchronizer.java b/src/main/java/envoy/client/ChatSynchronizer.java new file mode 100644 index 0000000..0f7322f --- /dev/null +++ b/src/main/java/envoy/client/ChatSynchronizer.java @@ -0,0 +1,47 @@ +package envoy.client; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.List; + +/** + * Project: envoy-client
+ * File: ChatSynchronizer.java
+ * Created: 27.10.2019
+ * + * @author Kai S. K. Engelbart + * @since Envoy v0.1-alpha + */ +public class ChatSynchronizer { + + private File localDB; + private List chats = new ArrayList<>(); + + public ChatSynchronizer(String localDBPath) { + localDB = new File(localDBPath); + if (localDB.exists() && !localDB.isDirectory()) loadFromLocalDB(); + } + + public void saveToLocalDB() { + try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) { + out.writeObject(chats); + } catch(IOException ex) { + ex.printStackTrace(); + } + } + + @SuppressWarnings("unchecked") + private void loadFromLocalDB() { + try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) { + Object obj = in.readObject(); + if (obj instanceof ArrayList) chats = (ArrayList) obj; + } catch (ClassNotFoundException | IOException e) {} + } + + public List getChats() { return chats; } +} diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index dd7ff5e..4f5256e 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -6,8 +6,8 @@ import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; -import java.util.ArrayList; -import java.util.List; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; import javax.swing.DefaultListModel; import javax.swing.JButton; @@ -24,6 +24,7 @@ import javax.swing.Timer; import javax.swing.border.EmptyBorder; import envoy.client.Chat; +import envoy.client.ChatSynchronizer; import envoy.client.Client; import envoy.schema.Message; import envoy.schema.Messages; @@ -46,20 +47,30 @@ public class ChatWindow extends JFrame { private JPanel contentPane = new JPanel(); - private Client client; + private Client client; + private ChatSynchronizer chatSynchronizer; - private JList userList = new JList<>(); - private List partnerChatList = new ArrayList(); + private JList userList = new JList<>(); private Chat currentChat; - public ChatWindow(Client client) { - this.client = client; + public ChatWindow(Client client, ChatSynchronizer chatSynchronizer) { + this.client = client; + this.chatSynchronizer = chatSynchronizer; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); setTitle("Envoy"); setLocationRelativeTo(null); + // Save chats when window closes + addWindowListener(new WindowAdapter() { + + @Override + public void windowClosing(WindowEvent e) { + chatSynchronizer.saveToLocalDB(); + } + }); + contentPane.setBackground(new Color(0, 0, 0)); contentPane.setForeground(Color.white); contentPane.setBorder(new EmptyBorder(0, 5, 0, 0)); @@ -138,7 +149,8 @@ public class ChatWindow extends JFrame { postButton.addActionListener((evt) -> { if (!client.hasRecipient()) { - JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", + JOptionPane.INFORMATION_MESSAGE); return; } @@ -158,8 +170,7 @@ public class ChatWindow extends JFrame { } catch (Exception e) { JOptionPane.showMessageDialog(this, "An exception occured while sending a message. See the log for more details.", - "Exception occured", - JOptionPane.ERROR_MESSAGE); + "Exception occured", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } }); @@ -186,12 +197,18 @@ public class ChatWindow extends JFrame { userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); userList.addListSelectionListener((listSelectionEvent) -> { if (!listSelectionEvent.getValueIsAdjusting()) { - @SuppressWarnings("unchecked") + @SuppressWarnings( + "unchecked" + ) final JList selectedUserList = (JList) listSelectionEvent.getSource(); final User user = selectedUserList.getSelectedValue(); client.setRecipient(user); - currentChat = partnerChatList.stream().filter(chat -> chat.getRecipient().getID() == user.getID()).findFirst().get(); + currentChat = chatSynchronizer.getChats() + .stream() + .filter(chat -> chat.getRecipient().getID() == user.getID()) + .findFirst() + .get(); client.setRecipient(user); @@ -232,7 +249,15 @@ public class ChatWindow extends JFrame { new Thread(() -> { Users users = client.getUsersListXml(); DefaultListModel userListModel = new DefaultListModel<>(); - users.getUser().forEach(user -> { userListModel.addElement(user); partnerChatList.add(new Chat(user)); }); + users.getUser().forEach(user -> { + userListModel.addElement(user); + + // Check if user exists in local DB + if (chatSynchronizer.getChats() + .stream() + .filter(c -> c.getRecipient().getID() == user.getID()) + .count() == 0) chatSynchronizer.getChats().add(new Chat(user)); + }); SwingUtilities.invokeLater(() -> userList.setModel(userListModel)); }).start(); } @@ -247,9 +272,11 @@ public class ChatWindow extends JFrame { 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 < partnerChatList.size(); j++) - if (partnerChatList.get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender()) - partnerChatList.get(j).appendMessage(unreadMessages.getMessage().get(i)); + for (int j = 0; j < chatSynchronizer.getChats().size(); j++) + if (chatSynchronizer.getChats().get(j) + .getRecipient() + .getID() == unreadMessages.getMessage().get(i).getMetaData().getSender()) + chatSynchronizer.getChats().get(j).appendMessage(unreadMessages.getMessage().get(i)); }).start(); } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index b525861..e664aa8 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -6,6 +6,7 @@ import java.util.Properties; import javax.swing.JOptionPane; +import envoy.client.ChatSynchronizer; import envoy.client.Client; import envoy.client.Config; @@ -51,7 +52,7 @@ public class Startup { EventQueue.invokeLater(() -> { try { - ChatWindow frame = new ChatWindow(client); + ChatWindow frame = new ChatWindow(client, new ChatSynchronizer("local_chats.db")); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); From 6e4b4e4d4eb19926567a1d884d97f2e5a1cf0b72 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 27 Oct 2019 21:40:40 +0100 Subject: [PATCH 2/7] Renamed ChatSynchronizer to LocalDB, added user-specific files --- src/main/java/envoy/client/Config.java | 29 +++++++++++----- .../{ChatSynchronizer.java => LocalDB.java} | 34 ++++++++++++++----- src/main/java/envoy/client/ui/ChatWindow.java | 22 ++++++------ src/main/java/envoy/client/ui/Startup.java | 19 ++++++++--- src/main/resources/client.properties | 3 ++ src/main/resources/server.properties | 2 -- 6 files changed, 74 insertions(+), 35 deletions(-) rename src/main/java/envoy/client/{ChatSynchronizer.java => LocalDB.java} (50%) create mode 100644 src/main/resources/client.properties delete mode 100644 src/main/resources/server.properties diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index fda0f97..19a5843 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -1,5 +1,6 @@ package envoy.client; +import java.io.File; import java.util.Properties; /** @@ -14,25 +15,28 @@ public class Config { private String server; private int port; + private File localDB; /** - * Defaults to the {@code server.properties} file for information. + * Defaults to the {@code client.properties} file for information. * - * - * @param properties - The two options for internet connection server and port - * @since Envoy 0.1 + * @param properties a {@link Properties} object containing information about + * the server and port, as well as the path to the local + * database + * @since Envoy v0.1-alpha */ 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")); } /** - * Sets the server and the port via command line properties --server/ -s and --port/ -p. + * Sets the server, port and localDB path via command line properties --server / + * -s, --port / -p and --localDB / -db. * - * @param args {@code --server} or {@code -s} followed by the specified URL - * and {@code --port} or {@code -p} followed by a 4-digit Integer - * @since Envoy 0.1 + * @param args the command line arguments to parse + * @since Envoy v0.1-alpha */ public void load(String[] args) { for (int i = 0; i < args.length; i++) @@ -45,10 +49,13 @@ public class Config { case "-p": port = Integer.parseInt(args[++i]); break; + case "--localDB": + case "-db": + localDB = new File(args[++i]); } } - public boolean isInitialized() { return server != null && !server.isEmpty() && port > 0; } + public boolean isInitialized() { return server != null && !server.isEmpty() && port > 0 && port < 65566 && localDB != null; } public String getServer() { return server; } @@ -57,4 +64,8 @@ public class Config { public int getPort() { return port; } public void setPort(int port) { this.port = port; } + + public File getLocalDB() { return localDB; } + + public void setLocalDB(File localDB) { this.localDB = localDB; } } diff --git a/src/main/java/envoy/client/ChatSynchronizer.java b/src/main/java/envoy/client/LocalDB.java similarity index 50% rename from src/main/java/envoy/client/ChatSynchronizer.java rename to src/main/java/envoy/client/LocalDB.java index 0f7322f..5a06a02 100644 --- a/src/main/java/envoy/client/ChatSynchronizer.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -9,38 +9,54 @@ import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; +import envoy.exception.EnvoyException; +import envoy.schema.User; + /** * Project: envoy-client
- * File: ChatSynchronizer.java
+ * File: LocalDB.java
* Created: 27.10.2019
* * @author Kai S. K. Engelbart * @since Envoy v0.1-alpha */ -public class ChatSynchronizer { +public class LocalDB { private File localDB; + private User sender; private List chats = new ArrayList<>(); - public ChatSynchronizer(String localDBPath) { - localDB = new File(localDBPath); - if (localDB.exists() && !localDB.isDirectory()) loadFromLocalDB(); + public LocalDB(User sender) { this.sender = sender; } + + 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())); + localDB = new File(localDBDir, sender.getID() + ".db"); + if (localDB.exists()) loadFromLocalDB(); } public void saveToLocalDB() { + try { + localDB.getParentFile().mkdirs(); + localDB.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) { out.writeObject(chats); - } catch(IOException ex) { + } catch (IOException ex) { ex.printStackTrace(); } } - + @SuppressWarnings("unchecked") - private void loadFromLocalDB() { + private void loadFromLocalDB() throws EnvoyException { try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) { Object obj = in.readObject(); if (obj instanceof ArrayList) chats = (ArrayList) obj; - } catch (ClassNotFoundException | IOException e) {} + } catch (ClassNotFoundException | IOException e) { + throw new EnvoyException(e); + } } public List getChats() { return chats; } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 4f5256e..8feb3ac 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -24,8 +24,8 @@ import javax.swing.Timer; import javax.swing.border.EmptyBorder; import envoy.client.Chat; -import envoy.client.ChatSynchronizer; import envoy.client.Client; +import envoy.client.LocalDB; import envoy.schema.Message; import envoy.schema.Messages; import envoy.schema.User; @@ -48,14 +48,14 @@ public class ChatWindow extends JFrame { private JPanel contentPane = new JPanel(); private Client client; - private ChatSynchronizer chatSynchronizer; + private LocalDB localDB; private JList userList = new JList<>(); private Chat currentChat; - public ChatWindow(Client client, ChatSynchronizer chatSynchronizer) { + public ChatWindow(Client client, LocalDB localDB) { this.client = client; - this.chatSynchronizer = chatSynchronizer; + this.localDB = localDB; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); @@ -67,7 +67,7 @@ public class ChatWindow extends JFrame { @Override public void windowClosing(WindowEvent e) { - chatSynchronizer.saveToLocalDB(); + localDB.saveToLocalDB(); } }); @@ -204,7 +204,7 @@ public class ChatWindow extends JFrame { final User user = selectedUserList.getSelectedValue(); client.setRecipient(user); - currentChat = chatSynchronizer.getChats() + currentChat = localDB.getChats() .stream() .filter(chat -> chat.getRecipient().getID() == user.getID()) .findFirst() @@ -253,10 +253,10 @@ public class ChatWindow extends JFrame { userListModel.addElement(user); // Check if user exists in local DB - if (chatSynchronizer.getChats() + if (localDB.getChats() .stream() .filter(c -> c.getRecipient().getID() == user.getID()) - .count() == 0) chatSynchronizer.getChats().add(new Chat(user)); + .count() == 0) localDB.getChats().add(new Chat(user)); }); SwingUtilities.invokeLater(() -> userList.setModel(userListModel)); }).start(); @@ -272,11 +272,11 @@ public class ChatWindow extends JFrame { 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 < chatSynchronizer.getChats().size(); j++) - if (chatSynchronizer.getChats().get(j) + for (int j = 0; j < localDB.getChats().size(); j++) + if (localDB.getChats().get(j) .getRecipient() .getID() == unreadMessages.getMessage().get(i).getMetaData().getSender()) - chatSynchronizer.getChats().get(j).appendMessage(unreadMessages.getMessage().get(i)); + localDB.getChats().get(j).appendMessage(unreadMessages.getMessage().get(i)); }).start(); } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index e664aa8..d494536 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -6,9 +6,10 @@ import java.util.Properties; import javax.swing.JOptionPane; -import envoy.client.ChatSynchronizer; import envoy.client.Client; import envoy.client.Config; +import envoy.client.LocalDB; +import envoy.exception.EnvoyException; /** * Starts the Envoy client and prompts the user to enter their name. @@ -31,7 +32,7 @@ public class Startup { ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { Properties configProperties = new Properties(); - configProperties.load(loader.getResourceAsStream("server.properties")); + configProperties.load(loader.getResourceAsStream("client.properties")); config.load(configProperties); } catch (IOException e) { e.printStackTrace(); @@ -48,11 +49,21 @@ public class Startup { System.err.println("User name is not set or empty. Exiting..."); System.exit(1); } - Client client = new Client(config, userName); + Client client = new Client(config, userName); + LocalDB localDB = new LocalDB(client.getSender()); + try { + localDB.initializeDBFile(config.getLocalDB()); + } catch (EnvoyException e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(null, + "Error while loading local database: " + e.toString() + "\nChats will not be stored locally.", + "Local DB error", + JOptionPane.WARNING_MESSAGE); + } EventQueue.invokeLater(() -> { try { - ChatWindow frame = new ChatWindow(client, new ChatSynchronizer("local_chats.db")); + ChatWindow frame = new ChatWindow(client, localDB); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/resources/client.properties b/src/main/resources/client.properties new file mode 100644 index 0000000..2641927 --- /dev/null +++ b/src/main/resources/client.properties @@ -0,0 +1,3 @@ +server=http://kske.feste-ip.net +port=43315 +localDB=.\\localDB diff --git a/src/main/resources/server.properties b/src/main/resources/server.properties deleted file mode 100644 index 6b0ae8f..0000000 --- a/src/main/resources/server.properties +++ /dev/null @@ -1,2 +0,0 @@ -server=http://kske.feste-ip.net -port=43315 \ No newline at end of file From 57b3a57452d150d83daff5b7a03257761f79ba9d Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 30 Oct 2019 06:16:44 +0100 Subject: [PATCH 3/7] Updated .gitignore to include localDB --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7836310..e12b13a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ /target/ -/local_chats.db +/localDB/ \ No newline at end of file From 910896152268c7f968162042f974df63347f1c2e Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 30 Oct 2019 06:19:50 +0100 Subject: [PATCH 4/7] Fixed formatting --- src/main/java/envoy/client/ui/ChatWindow.java | 36 +++++++------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 8feb3ac..5f79b13 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -47,14 +47,14 @@ public class ChatWindow extends JFrame { private JPanel contentPane = new JPanel(); - private Client client; + private Client client; private LocalDB localDB; private JList userList = new JList<>(); private Chat currentChat; public ChatWindow(Client client, LocalDB localDB) { - this.client = client; + this.client = client; this.localDB = localDB; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -66,11 +66,9 @@ public class ChatWindow extends JFrame { addWindowListener(new WindowAdapter() { @Override - public void windowClosing(WindowEvent e) { - localDB.saveToLocalDB(); - } + public void windowClosing(WindowEvent e) { localDB.saveToLocalDB(); } }); - + contentPane.setBackground(new Color(0, 0, 0)); contentPane.setForeground(Color.white); contentPane.setBorder(new EmptyBorder(0, 5, 0, 0)); @@ -149,8 +147,7 @@ public class ChatWindow extends JFrame { postButton.addActionListener((evt) -> { if (!client.hasRecipient()) { - JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", - JOptionPane.INFORMATION_MESSAGE); + JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE); return; } @@ -170,7 +167,8 @@ public class ChatWindow extends JFrame { } catch (Exception e) { JOptionPane.showMessageDialog(this, "An exception occured while sending a message. See the log for more details.", - "Exception occured", JOptionPane.ERROR_MESSAGE); + "Exception occured", + JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } }); @@ -197,18 +195,12 @@ public class ChatWindow extends JFrame { userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); userList.addListSelectionListener((listSelectionEvent) -> { if (!listSelectionEvent.getValueIsAdjusting()) { - @SuppressWarnings( - "unchecked" - ) + @SuppressWarnings("unchecked") final JList selectedUserList = (JList) listSelectionEvent.getSource(); 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); @@ -253,10 +245,8 @@ public class ChatWindow extends JFrame { userListModel.addElement(user); // Check if user exists in local DB - if (localDB.getChats() - .stream() - .filter(c -> c.getRecipient().getID() == user.getID()) - .count() == 0) localDB.getChats().add(new Chat(user)); + if (localDB.getChats().stream().filter(c -> c.getRecipient().getID() == user.getID()).count() == 0) + localDB.getChats().add(new Chat(user)); }); SwingUtilities.invokeLater(() -> userList.setModel(userListModel)); }).start(); @@ -273,9 +263,7 @@ public class ChatWindow extends JFrame { 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()) + if (localDB.getChats().get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender()) localDB.getChats().get(j).appendMessage(unreadMessages.getMessage().get(i)); }).start(); } From 46521d8230100b783faea624465be2bd1dd66535 Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 30 Oct 2019 07:45:33 +0100 Subject: [PATCH 5/7] Updated Javadoc in Config File --- src/main/java/envoy/client/Config.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index 0ffcca8..d6bf106 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -52,6 +52,7 @@ public class Config { case "--localDB": case "-db": localDB = new File(args[++i]); + break; } } @@ -91,7 +92,18 @@ public class Config { */ public void setPort(int port) { this.port = port; } + /** + *@return the current local database that is held for that user + *@since Envoy v0.1-alpha + **/ public File getLocalDB() { return localDB; } + /** + *Changes the default local database. + *Exclusively meant for developing reasons. + * + *@param the local database object to set + *@since Envoy v0.1-alpha + **/ public void setLocalDB(File localDB) { this.localDB = localDB; } -} \ No newline at end of file +} From d739f7a4458eed515d0539793a4c533874b68fb5 Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 30 Oct 2019 08:10:40 +0100 Subject: [PATCH 6/7] Updated Javadoc for LocalDB File --- src/main/java/envoy/client/LocalDB.java | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 5a06a02..d6a257a 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -26,8 +26,22 @@ public class LocalDB { private User sender; private List chats = new ArrayList<>(); + /** + *Constructs an empty local database. + * + *@param sender the user who logs in + *@since Envoy v0.1-alpha + **/ public LocalDB(User sender) { this.sender = sender; } + /** + *Initialises the local database and fills it with values + *if the user already sent/ received a message.
+ * + *@param localDBDir the directory where we wish to save/load the database from. + *@throws EnvoyException if the directory selected is not an actual directory. + *@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())); @@ -35,20 +49,34 @@ public class LocalDB { if (localDB.exists()) loadFromLocalDB(); } + /** + *Saves the database into a file for future use. + * + *@throws IOException if something went wrong during saving + *@since Envoy v0.1-alpha + **/ public void saveToLocalDB() { try { localDB.getParentFile().mkdirs(); localDB.createNewFile(); } catch (IOException e) { e.printStackTrace(); + System.err.println("unable to save the messages"); } try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) { out.writeObject(chats); } catch (IOException ex) { ex.printStackTrace(); + System.err.println("unable to save the messages"); } } + /** + *Loads all chats saved by Envoy for the user.
+ * + *@throws EnvoyException if something fails while loading. + *@since Envoy v0.1-alpha + **/ @SuppressWarnings("unchecked") private void loadFromLocalDB() throws EnvoyException { try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) { @@ -59,5 +87,9 @@ public class LocalDB { } } + /** + *@return all chats that the user has saved + *@since Envoy v0.1-alpha + **/ public List getChats() { return chats; } } From 316936a1f85c91abf8ec5f64e1b8812de665740c Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 30 Oct 2019 17:01:55 +0100 Subject: [PATCH 7/7] Fixed Javadoc formatting and spelling --- src/main/java/envoy/client/Config.java | 37 +++++++++--------- src/main/java/envoy/client/LocalDB.java | 51 +++++++++++++------------ 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index d6bf106..1d107ca 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -52,58 +52,57 @@ public class Config { case "--localDB": case "-db": localDB = new File(args[++i]); - break; } } - /** - * @return {@code true} if server, port andd localDB directory are known. + /** + * @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; } /** - * @return the URL of our website + * @return the host name of the Envoy server * @since Envoy v0.1-alpha */ public String getServer() { return server; } /** - * Changes the default URL. - * Exclusively meant for developing reasons. (localhost) + * Changes the default server host name. + * Exclusively intended for development purposes. * - * @param server the URL where wish to host Envoy + * @param server the host name of the Envoy server * @since Envoy v0.1-alpha */ public void setServer(String server) { this.server = server; } /** - * @return the port at which Envoy is located in the Server + * @return the port at which the Envoy server is located on the host * @since Envoy v0.1-alpha */ public int getPort() { return port; } /** * Changes the default port. - * Exclusively meant for developing reasons. (localhost) + * Exclusively intended for development purposes. * - * @param port the port where we wish to connect to {@code Envoy}. + * @param port the port where an Envoy server is located * @since Envoy v0.1-alpha */ public void setPort(int port) { this.port = port; } /** - *@return the current local database that is held for that user - *@since Envoy v0.1-alpha - **/ + * @return the local database specific to the client user + * @since Envoy v0.1-alpha + **/ public File getLocalDB() { return localDB; } /** - *Changes the default local database. - *Exclusively meant for developing reasons. - * - *@param the local database object to set - *@since Envoy v0.1-alpha - **/ + * Changes the default local database. + * Exclusively intended for development purposes. + * + * @param the file containing the local database + * @since Envoy v0.1-alpha + **/ public void setLocalDB(File localDB) { this.localDB = localDB; } } diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index d6a257a..d7b3409 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -27,21 +27,21 @@ public class LocalDB { private List chats = new ArrayList<>(); /** - *Constructs an empty local database. - * - *@param sender the user who logs in - *@since Envoy v0.1-alpha - **/ + * Constructs an empty local database. + * + * @param sender the user that is logged in with this client + * @since Envoy v0.1-alpha + **/ public LocalDB(User sender) { this.sender = sender; } /** - *Initialises the local database and fills it with values - *if the user already sent/ received a message.
- * - *@param localDBDir the directory where we wish to save/load the database from. - *@throws EnvoyException if the directory selected is not an actual directory. - *@since Envoy v0.1-alpha - **/ + * Initializes the local database and fills it with values + * if the user has already sent or received messages. + * + * @param localDBDir the directory where we wish to save/load the database from. + * @throws EnvoyException if the directory selected is not an actual directory. + * @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())); @@ -50,11 +50,11 @@ public class LocalDB { } /** - *Saves the database into a file for future use. - * - *@throws IOException if something went wrong during saving - *@since Envoy v0.1-alpha - **/ + * Saves the database into a file for future use. + * + * @throws IOException if something went wrong during saving + * @since Envoy v0.1-alpha + **/ public void saveToLocalDB() { try { localDB.getParentFile().mkdirs(); @@ -72,11 +72,11 @@ public class LocalDB { } /** - *Loads all chats saved by Envoy for the user.
- * - *@throws EnvoyException if something fails while loading. - *@since Envoy v0.1-alpha - **/ + * Loads all chats saved by Envoy for the client user. + * + * @throws EnvoyException if something fails while loading. + * @since Envoy v0.1-alpha + **/ @SuppressWarnings("unchecked") private void loadFromLocalDB() throws EnvoyException { try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) { @@ -88,8 +88,9 @@ public class LocalDB { } /** - *@return all chats that the user has saved - *@since Envoy v0.1-alpha - **/ + * @return all saves {@link Chat} objects that list the client user as the + * sender + * @since Envoy v0.1-alpha + **/ public List getChats() { return chats; } }