Renamed ChatSynchronizer to LocalDB, added user-specific files

This commit is contained in:
Kai S. K. Engelbart 2019-10-27 21:40:40 +01:00
parent d2dbf91b8a
commit 07dc11eebd
6 changed files with 74 additions and 35 deletions

View File

@ -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 <strong> server</strong> and <strong> port</strong>
* @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; }
}

View File

@ -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: <strong>envoy-client</strong><br>
* File: <strong>ChatSynchronizer.java</strong><br>
* File: <strong>LocalDB.java</strong><br>
* Created: <strong>27.10.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.1-alpha
*/
public class ChatSynchronizer {
public class LocalDB {
private File localDB;
private User sender;
private List<Chat> 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<Chat>) obj;
} catch (ClassNotFoundException | IOException e) {}
} catch (ClassNotFoundException | IOException e) {
throw new EnvoyException(e);
}
}
public List<Chat> getChats() { return chats; }

View File

@ -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<User> 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();
}
}

View File

@ -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();

View File

@ -0,0 +1,3 @@
server=http://kske.feste-ip.net
port=43315
localDB=.\\localDB

View File

@ -1,2 +0,0 @@
server=http://kske.feste-ip.net
port=43315