From 07dc11eebd3851b3507ecc74a23ed27e8d40fd06 Mon Sep 17 00:00:00 2001 From: kske Date: Sun, 27 Oct 2019 21:40:40 +0100 Subject: [PATCH] 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