Merge pull request #87 from informatik-ag-ngl/standalone_server_integration

Standalone server integration
This commit is contained in:
Kai S. K. Engelbart 2020-01-06 18:10:03 +01:00 committed by GitHub
commit 8406f46358
24 changed files with 420 additions and 632 deletions

View File

@ -28,11 +28,5 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

14
pom.xml
View File

@ -5,7 +5,7 @@
<groupId>informatik-ag-ngl</groupId> <groupId>informatik-ag-ngl</groupId>
<artifactId>envoy-client</artifactId> <artifactId>envoy-client</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.3-alpha</version>
<name>Envoy Client</name> <name>Envoy Client</name>
<url>https://github.com/informatik-ag-ngl/envoy-client</url> <url>https://github.com/informatik-ag-ngl/envoy-client</url>
@ -18,20 +18,10 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>4.1.1.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency> <dependency>
<groupId>informatik-ag-ngl</groupId> <groupId>informatik-ag-ngl</groupId>
<artifactId>envoy-common</artifactId> <artifactId>envoy-common</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.2-alpha</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -4,8 +4,8 @@ import java.io.Serializable;
import javax.swing.DefaultListModel; import javax.swing.DefaultListModel;
import envoy.schema.Message; import envoy.data.Message;
import envoy.schema.User; import envoy.data.User;
/** /**
* Represents a chat between two {@link User}s <br> * Represents a chat between two {@link User}s <br>

View File

@ -1,23 +1,17 @@
package envoy.client; package envoy.client;
import java.io.StringWriter; import java.io.Closeable;
import java.io.IOException;
import java.net.Socket;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.ws.rs.client.ClientBuilder; import javax.naming.TimeLimitExceededException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import envoy.client.util.EnvoyLog; import envoy.client.util.EnvoyLog;
import envoy.exception.EnvoyException; import envoy.data.*;
import envoy.schema.ObjectFactory; import envoy.util.SerializationUtils;
import envoy.schema.Sync;
import envoy.schema.User;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -29,46 +23,80 @@ import envoy.schema.User;
* @author Leon Hofmeister * @author Leon Hofmeister
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public class Client { public class Client implements Closeable {
private ObjectFactory objectFactory = new ObjectFactory(); private Socket socket;
private Config config; private Receiver receiver;
private User sender, recipient; private boolean online;
private boolean online = false;
private volatile User sender;
private User recipient;
private volatile Contacts contacts;
private Config config = Config.getInstance();
private static final Logger logger = EnvoyLog.getLogger(Client.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(Client.class.getSimpleName());
/** /**
* Initializes the client. At this state, the client user has yet to be * Enters the online mode by acquiring a user ID from the server. As a
* initialized, which can be done by calling {@link Client#onlineInit(String)}. * connection has to be established and a handshake has to be made, this method
* will block for up to 5 seconds. If the handshake does exceed this time limit,
* an exception is thrown.
* *
* @param config The {@link Config} instance to use in this client * @param credentials the login credentials of the user
* @since Envoy v0.2-alpha * @throws Exception if the online mode could not be entered or the request
*/
public Client(Config config) { this.config = config; }
/**
* Enters the online mode by acquiring a user ID from the server.
*
* @param userName the name of the client user
* @throws EnvoyException if the online mode could not be entered or the request
* failed for some other reason * failed for some other reason
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public void onlineInit(String userName) throws EnvoyException { public void onlineInit(LoginCredentials credentials) throws Exception {
sender = getUser(userName); // Establish TCP connection
online = true; logger.info(String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort()));
socket = new Socket(config.getServer(), config.getPort());
logger.info("Successfully connected to server.");
// Create message receiver
receiver = new Receiver(socket.getInputStream());
// Register user creation processor
receiver.registerProcessor(User.class, sender -> { logger.info("Acquired user object " + sender); this.sender = sender; });
receiver.registerProcessor(Contacts.class, contacts -> { logger.info("Acquired contacts object " + contacts); this.contacts = contacts; });
// Start receiver
new Thread(receiver).start();
// Write login credentials
logger.finest("Sending login credentials...");
SerializationUtils.writeBytesWithLength(credentials, socket.getOutputStream());
// Wait for a maximum of five seconds to acquire the sender object
long start = System.currentTimeMillis();
while (sender == null || contacts == null) {
if (System.currentTimeMillis() - start > 5000) throw new TimeLimitExceededException("Did not log in after 5 seconds");
Thread.sleep(500);
} }
private <T, R> R post(String uri, T body, Class<R> responseBodyClass) { logger.info("Handshake completed.");
javax.ws.rs.client.Client client = ClientBuilder.newClient(); online = true;
WebTarget target = client.target(uri);
Response response = target.request().post(Entity.entity(body, "application/xml"));
R responseBody = response.readEntity(responseBodyClass);
response.close();
client.close();
return responseBody; // Remove user creation processor
receiver.removeAllProcessors();
// Register processors for message and status handling
receiver.registerProcessor(Message.class, new ReceivedMessageProcessor());
}
/**
* Sends a message to the server.
*
* @param message the message to send
* @throws IOException if the message does not reach the server
* @since Envoy v0.3-alpha
*/
public void sendMessage(Message message) throws IOException {
checkOnline();
SerializationUtils.writeBytesWithLength(message, socket.getOutputStream());
message.nextStatus();
} }
/** /**
@ -77,107 +105,16 @@ public class Client {
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public Map<String, User> getUsers() { public Map<String, User> getUsers() {
Sync sendSync = objectFactory.createSync(); checkOnline();
User user = objectFactory.createUser();
user.setID(-1);
sendSync.getUsers().add(user);
Sync returnSync = post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
sendSync,
Sync.class);
Map<String, User> users = new HashMap<>(); Map<String, User> users = new HashMap<>();
returnSync.getUsers().forEach(u -> users.put(u.getName(), u)); contacts.getContacts().forEach(u -> users.put(u.getName(), u));
return users; return users;
} }
/** @Override
* Returns a {@link User} with a specific id by name. public void close() throws IOException { if (online) socket.close(); }
*
* @param name - the name of the {@link User}
* @return a {@link User} with the specified name
* @throws EnvoyException if the server does not return the requested ID
* @since Envoy v0.1-alpha
*/
private User getUser(String name) throws EnvoyException {
// Create a sync with only a user with the requested name
Sync senderSync = objectFactory.createSync();
User user = objectFactory.createUser();
user.setName(name);
senderSync.getUsers().add(user);
try { private void checkOnline() { if (!online) throw new IllegalStateException("Client is not online"); }
Sync sync = post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
senderSync,
Sync.class);
// Expecting a single user with an ID
if (sync.getUsers().size() == 1) {
online = true;
return sync.getUsers().get(0);
} else throw new EnvoyException("Unexpected response from Envoy Server");
} catch (Exception e) {
throw new EnvoyException("Could not connect to server", e);
}
}
/**
* Sends the "sync" Sync Object to the server and gets a "returnSync" Sync
* Object as response. <br>
* 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))<br>
* 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)) <br>
* This method also processes the response Sync Object. <br>
* It sorts its users and messages by specific variables and does certain things
* with them. <br>
* <br>
* Messages: <br>
* -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)<br>
* -State RECEIVED, SenderID != 0: Adds the unread Messages returned from the
* server in the latest sync to the "unreadMessagesSync" Sync Object. <br>
* -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).) <br>
* -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).) <br>
* <br>
* Users: <br>
* Updating UserStatus of all users in LocalDB. (Server sends all users with
* their updated UserStatus to the client.) <br>
*
* @param userId the id of the {@link Client} who sends the {@link Sync}
* @param sync the sync object (yet to be converted from java class to
* sync.xml)
* @return a returnSync.xml file
* @throws EnvoyException if the client is not in online mode
* @since Envoy v0.1-alpha
*/
public Sync sendSync(long userId, Sync sync) throws EnvoyException {
if(!isOnline())
throw new EnvoyException("Client is not in online mode");
// Print sync XML to console
JAXBContext jc;
try {
jc = JAXBContext.newInstance("envoy.schema");
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter stringWriter = new StringWriter();
m.marshal(sync, stringWriter);
logger.fine("Sending sync:\n" + stringWriter.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
// Send sync
return post(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), userId), sync, Sync.class);
}
/** /**
* @return the sender object that represents this client. * @return the sender object that represents this client.
@ -213,6 +150,11 @@ public class Client {
*/ */
public boolean hasRecipient() { return recipient != null; } public boolean hasRecipient() { return recipient != null; }
/**
* @return the {@link Receiver} used by this {@link Client}
*/
public Receiver getReceiver() { return receiver; }
/** /**
* @return {@code true} if a connection to the server could be established * @return {@code true} if a connection to the server could be established
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha

View File

@ -2,20 +2,10 @@ package envoy.client;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.logging.Logger;
import javax.xml.datatype.DatatypeConfigurationException; import envoy.data.User;
import javax.xml.datatype.DatatypeFactory; import envoy.util.SerializationUtils;
import envoy.client.event.EventBus;
import envoy.client.event.MessageCreationEvent;
import envoy.client.util.EnvoyLog;
import envoy.client.util.SerializationUtils;
import envoy.exception.EnvoyException;
import envoy.schema.*;
import envoy.schema.Message.Metadata.MessageState;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -33,15 +23,6 @@ public class LocalDB {
private Map<String, User> users = new HashMap<>(); private Map<String, User> users = new HashMap<>();
private List<Chat> chats = new ArrayList<>(); private List<Chat> chats = new ArrayList<>();
private ObjectFactory objectFactory = new ObjectFactory();
private DatatypeFactory datatypeFactory;
private Sync unreadMessagesSync = objectFactory.createSync();
private Sync sync = objectFactory.createSync();
private Sync readMessages = objectFactory.createSync();
private static final Logger logger = EnvoyLog.getLogger(LocalDB.class.getSimpleName());
/** /**
* Constructs an empty local database. To serialize any chats to the file * Constructs an empty local database. To serialize any chats to the file
* system, call {@link LocalDB#initializeDBFile()}. * system, call {@link LocalDB#initializeDBFile()}.
@ -53,12 +34,6 @@ public class LocalDB {
public LocalDB(File localDBDir) throws IOException { public LocalDB(File localDBDir) throws IOException {
this.localDBDir = localDBDir; this.localDBDir = localDBDir;
try {
datatypeFactory = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
e.printStackTrace();
}
// Initialize local database directory // Initialize local database directory
if (localDBDir.exists() && !localDBDir.isDirectory()) if (localDBDir.exists() && !localDBDir.isDirectory())
throw new IOException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath())); throw new IOException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
@ -73,7 +48,7 @@ public class LocalDB {
*/ */
public void initializeDBFile() { public void initializeDBFile() {
if (user == null) throw new NullPointerException("Client user is null"); if (user == null) throw new NullPointerException("Client user is null");
localDBFile = new File(localDBDir, user.getID() + ".db"); localDBFile = new File(localDBDir, user.getId() + ".db");
} }
/** /**
@ -94,187 +69,20 @@ public class LocalDB {
/** /**
* Loads all users that are stored in the local database. * Loads all users that are stored in the local database.
* *
* @throws EnvoyException if the loading process failed * @throws IOException if the loading process failed
* @throws ClassNotFoundException if the loading process failed
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public void loadUsers() throws EnvoyException { users = SerializationUtils.read(usersFile, HashMap.class); } public void loadUsers() throws ClassNotFoundException, IOException { users = SerializationUtils.read(usersFile, HashMap.class); }
/** /**
* Loads all chats saved by Envoy for the client user. * Loads all chats saved by Envoy for the client user.
* *
* @throws EnvoyException if the loading process failed * @throws IOException if the loading process failed
* @throws ClassNotFoundException if the loading process failed
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public void loadChats() throws EnvoyException { chats = SerializationUtils.read(localDBFile, ArrayList.class); } public void loadChats() throws ClassNotFoundException, IOException { chats = SerializationUtils.read(localDBFile, ArrayList.class); }
/**
* Creates a {@link Message} object serializable to XML.
*
* @param textContent The content (text) of the message
* @param recipientID The recipient of the message
* @return prepared {@link Message} object
* @since Envoy v0.1-alpha
*/
public Message createMessage(String textContent, long recipientID) {
Message.Metadata metaData = objectFactory.createMessageMetadata();
metaData.setSender(user.getID());
metaData.setRecipient(recipientID);
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;
}
/**
* Creates a {@link Sync} object filled with the changes that occurred to the
* local database since the last synchronization.
*
* @param userId the ID of the user that is synchronized by this client
* @return {@link Sync} object filled with the current changes
* @since Envoy v0.1-alpha
*/
public Sync fillSync(long userId) {
addWaitingMessagesToSync();
sync.getMessages().addAll(readMessages.getMessages());
readMessages.getMessages().clear();
logger.finest(String.format("Filled sync with %d messages.", sync.getMessages().size()));
return sync;
}
/**
* Applies the changes carried by a {@link Sync} object to the local database
*
* @param returnSync the {@link Sync} object to apply
* @since Envoy v0.1-alpha
*/
public void applySync(Sync returnSync) {
for (int i = 0; i < returnSync.getMessages().size(); i++) {
// The message has an ID
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0) {
// Messages are processes differently corresponding to their state
switch (returnSync.getMessages().get(i).getMetadata().getState()) {
case SENT:
// Update previously waiting and now sent messages that were assigned an ID by
// the server
sync.getMessages().get(i).getMetadata().setMessageId(returnSync.getMessages().get(i).getMetadata().getMessageId());
sync.getMessages().get(i).getMetadata().setState(returnSync.getMessages().get(i).getMetadata().getState());
break;
case RECEIVED:
if (returnSync.getMessages().get(i).getMetadata().getSender() != 0) {
// these are the unread Messages from the server
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
// Create and dispatch message creation event
EventBus.getInstance().dispatch(new MessageCreationEvent(returnSync.getMessages().get(i)));
} else {
// Update Messages in localDB to state RECEIVED
for (Chat chat : getChats())
if (chat.getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient())
for (int j = 0; j < chat.getModel().getSize(); j++)
if (chat.getModel().get(j).getMetadata().getMessageId() == returnSync.getMessages()
.get(i)
.getMetadata()
.getMessageId())
chat.getModel().get(j).getMetadata().setState(returnSync.getMessages().get(i).getMetadata().getState());
}
break;
case READ:
// Update local Messages to state READ
logger.info("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId()
+ "was initialized to be set to READ in localDB.");
for (Chat chat : getChats())
if (chat.getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
logger.info("Chat with: " + chat.getRecipient().getID() + "was selected.");
for (int k = 0; k < chat.getModel().getSize(); k++)
if (chat.getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
.get(i)
.getMetadata()
.getMessageId()) {
logger.info("Message with ID: " + chat.getModel().get(k).getMetadata().getMessageId() + "was selected.");
chat.getModel().get(k).getMetadata().setState(returnSync.getMessages().get(i).getMetadata().getState());
logger.info("Message State is now: " + chat.getModel().get(k).getMetadata().getState());
}
}
break;
}
}
}
// Updating UserStatus of all users in LocalDB
for (User user : returnSync.getUsers())
for (Chat chat : getChats())
if (user.getID() == chat.getRecipient().getID()) chat.getRecipient().setStatus(user.getStatus());
sync.getMessages().clear();
sync.getUsers().clear();
}
/**
* Adds the unread messages returned from the server in the latest sync to the
* right chats in the LocalDB.
*
* @since Envoy v0.1-alpha
*/
public void addUnreadMessagesToLocalDB() {
for (Message message : unreadMessagesSync.getMessages())
for (Chat chat : getChats())
if (message.getMetadata().getSender() == chat.getRecipient().getID()) {
chat.appendMessage(message);
break;
}
}
/**
* Changes all messages with state {@code RECEIVED} of a specific chat to state
* {@code READ}.
* <br>
* Adds these messages to the {@code readMessages} {@link Sync} object.
*
* @param currentChat the {@link Chat} that was just opened
* @since Envoy v0.1-alpha
*/
public void setMessagesToRead(Chat currentChat) {
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;
}
/**
* Adds all messages with state {@code WAITING} from the {@link LocalDB} to the
* {@link Sync} object.
*
* @since Envoy v0.1-alpha
*/
private void addWaitingMessagesToSync() {
for (Chat chat : getChats())
for (int i = 0; i < chat.getModel().size(); i++)
if (chat.getModel().get(i).getMetadata().getState() == MessageState.WAITING) {
logger.info("Got Waiting Message");
sync.getMessages().add(chat.getModel().get(i));
}
}
/**
* Clears the {@code unreadMessagesSync} {@link Sync} object.
*
* @since Envoy v0.1-alpha
*/
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
/** /**
* @return a {@code Map<String, User>} of all users stored locally with their * @return a {@code Map<String, User>} of all users stored locally with their

View File

@ -0,0 +1,32 @@
package envoy.client;
import java.util.function.Consumer;
import java.util.logging.Logger;
import envoy.client.event.MessageCreationEvent;
import envoy.client.util.EnvoyLog;
import envoy.data.Message;
import envoy.data.Message.MessageStatus;
import envoy.event.EventBus;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>ReceivedMessageProcessor.java</strong><br>
* Created: <strong>31.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.3-alpha
*/
public class ReceivedMessageProcessor implements Consumer<Message> {
private static final Logger logger = EnvoyLog.getLogger(ReceivedMessageProcessor.class.getSimpleName());
@Override
public void accept(Message message) {
logger.info("Received message object " + message);
if (message.getStatus() != MessageStatus.SENT) logger.warning("The message has the unexpected status " + message.getStatus());
else
// Dispatch event
EventBus.getInstance().dispatch(new MessageCreationEvent(message));
}
}

View File

@ -0,0 +1,86 @@
package envoy.client;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import envoy.client.util.EnvoyLog;
import envoy.util.SerializationUtils;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>Receiver.java</strong><br>
* Created: <strong>30.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.3-alpha
*/
public class Receiver implements Runnable {
private final InputStream in;
private final Map<Class<?>, Consumer<?>> processors = new HashMap<>();
private static final Logger logger = EnvoyLog.getLogger(Receiver.class.getSimpleName());
/**
* Creates an instance of {@link Receiver}.
*
* @param in the {@link InputStream} to parse objects from
*/
public Receiver(InputStream in) { this.in = in; }
@SuppressWarnings("unchecked")
@Override
public void run() {
try {
while (true) {
// Read object length
byte[] lenBytes = new byte[4];
in.read(lenBytes);
int len = SerializationUtils.bytesToInt(lenBytes, 0);
// Read object into byte array
byte[] objBytes = new byte[len];
in.read(objBytes);
try (ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(objBytes))) {
Object obj = oin.readObject();
logger.finest("Received object " + obj);
// Get appropriate processor
@SuppressWarnings("rawtypes")
Consumer processor = processors.get(obj.getClass());
if (processor == null)
logger.severe(String.format("The received object has the class %s for which no processor is defined.", obj.getClass()));
else processor.accept(obj);
}
}
} catch (SocketException e) {
logger.info("Connection probably closed by client. Exiting receiver thread...");
} catch (Exception e) {
logger.log(Level.SEVERE, "Error on receiver thread", e);
e.printStackTrace();
}
}
/**
* Adds an object processor to this {@link Receiver}. It will be called once an
* object of the accepted class has been received.
*
* @param processorClass the object class accepted by the processor
* @param processor the object processor
*/
public <T> void registerProcessor(Class<T> processorClass, Consumer<T> processor) { processors.put(processorClass, processor); }
/**
* Removes all object processors registered at this {@link Receiver}.
*/
public void removeAllProcessors() { processors.clear(); }
}

View File

@ -1,14 +1,14 @@
package envoy.client; package envoy.client;
import java.io.*; import java.io.File;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.prefs.Preferences; import java.util.prefs.Preferences;
import envoy.client.ui.Color; import envoy.client.ui.Color;
import envoy.client.ui.Theme; import envoy.client.ui.Theme;
import envoy.client.util.SerializationUtils; import envoy.util.SerializationUtils;
import envoy.exception.EnvoyException;
/** /**
* Manages all application settings, which are different objects that can be * Manages all application settings, which are different objects that can be
@ -55,7 +55,7 @@ public class Settings {
// Load settings from settings file // Load settings from settings file
try { try {
items = SerializationUtils.read(settingsFile, HashMap.class); items = SerializationUtils.read(settingsFile, HashMap.class);
} catch (EnvoyException e) { } catch (ClassNotFoundException | IOException e) {
items = new HashMap<>(); items = new HashMap<>();
} }
supplementDefaults(); supplementDefaults();
@ -63,7 +63,7 @@ public class Settings {
// Load themes from theme file // Load themes from theme file
try { try {
themes = SerializationUtils.read(themeFile, HashMap.class); themes = SerializationUtils.read(themeFile, HashMap.class);
} catch (EnvoyException e1) { } catch (ClassNotFoundException | IOException e1) {
themes = new HashMap<>(); themes = new HashMap<>();
setCurrentTheme("dark"); setCurrentTheme("dark");
} }

View File

@ -1,19 +0,0 @@
package envoy.client.event;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>Event.java</strong><br>
* Created: <strong>04.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @param <T> the type of the Event
* @since Envoy v0.2-alpha
*/
public interface Event<T> {
/**
* @return the data associated with this event
*/
T get();
}

View File

@ -1,81 +0,0 @@
package envoy.client.event;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This class handles events by allowing {@link EventHandler} object to register
* themselves and then be notified about certain events dispatched by the event
* bus.<br>
* <br>
* The event bus is a singleton and can be used across the entire application to
* guarantee the propagation of events.<br>
*
* Project: <strong>envoy-client</strong><br>
* File: <strong>EventBus.java</strong><br>
* Created: <strong>04.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.2-alpha
*/
public class EventBus {
/**
* Contains all {@link EventHandler} instances registered at this
* {@link EventBus} as values mapped to by their supported {@link Event}
* classes.
*/
private Map<Class<? extends Event<?>>, List<EventHandler>> handlers = new HashMap<>();
/**
* The singleton instance of this {@link EventBus} that is used across the
* entire application.
*/
private static EventBus eventBus = new EventBus();
/**
* This constructor is not accessible from outside this class because a
* singleton instance of it is provided by the {@link EventBus#getInstance()}
* method.
*/
private EventBus() {}
/**
* @return the singleton instance of the {@link EventBus}
* @since Envoy v0.2-alpha
*/
public static EventBus getInstance() { return eventBus; }
/**
* Registers an {@link EventHandler} to be notified when a
* {@link Event} of a certain type is dispatched.
*
* @param eventClass the class which the {@link EventHandler} is subscribed to
* @param handler the {@link EventHandler} to register
* @since Envoy v0.2-alpha
*/
public void register(Class<? extends Event<?>> eventClass, EventHandler handler) {
if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>());
handlers.get(eventClass).add(handler);
}
/**
* Dispatches a {@link Event} to every {@link EventHandler} subscribed to it.
*
* @param event the {@link Event} to dispatch
* @since Envoy v0.2-alpha
*/
public void dispatch(Event<?> event) {
handlers.keySet().stream().filter(event.getClass()::isAssignableFrom).map(handlers::get).flatMap(List::stream).forEach(h -> h.handle(event));
}
/**
* @return a map of all {@link EventHandler} instances currently registered at
* this {@link EventBus} with the {@link Event} classes they are
* subscribed to as keys
* @since Envoy v0.2-alpha
*/
public Map<Class<? extends Event<?>>, List<EventHandler>> getHandlers() { return handlers; }
}

View File

@ -1,18 +0,0 @@
package envoy.client.event;
/**
* Project: <strong>envoy-clientChess</strong><br>
* File: <strong>EventHandler.javaEvent.java</strong><br>
* Created: <strong>04.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
*/
public interface EventHandler {
/**
* Consumes an event dispatched by the event bus.
*
* @param event The event dispatched by the event bus, only of supported type
*/
void handle(Event<?> event);
}

View File

@ -1,6 +1,7 @@
package envoy.client.event; package envoy.client.event;
import envoy.schema.Message; import envoy.data.Message;
import envoy.event.MessageEvent;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>

View File

@ -1,27 +0,0 @@
package envoy.client.event;
import envoy.schema.Message;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>MessageCreationEvent.java</strong><br>
* Created: <strong>4 Dec 2019</strong><br>
*
* @author Kai S. K. Engelbart
*/
public class MessageEvent implements Event<Message> {
protected final Message message;
/**
* Initializes a {@link MessageEvent} conveying information about a
* {@link Message} object.
*
* @param message the {@link Message} object to attach to this event
* @since Envoy v0.2-alpha
*/
public MessageEvent(Message message) { this.message = message; }
@Override
public Message get() { return message; }
}

View File

@ -1,6 +1,7 @@
package envoy.client.event; package envoy.client.event;
import envoy.schema.Message; import envoy.data.Message;
import envoy.event.MessageEvent;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>

View File

@ -1,6 +1,7 @@
package envoy.client.event; package envoy.client.event;
import envoy.client.ui.Theme; import envoy.client.ui.Theme;
import envoy.event.Event;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>

View File

@ -10,12 +10,13 @@ import javax.swing.*;
import javax.swing.border.EmptyBorder; import javax.swing.border.EmptyBorder;
import envoy.client.*; import envoy.client.*;
import envoy.client.event.EventBus;
import envoy.client.event.ThemeChangeEvent; import envoy.client.event.ThemeChangeEvent;
import envoy.client.ui.settings.SettingsScreen; import envoy.client.ui.settings.SettingsScreen;
import envoy.client.util.EnvoyLog; import envoy.client.util.EnvoyLog;
import envoy.schema.Message; import envoy.data.Message;
import envoy.schema.User; import envoy.data.MessageBuilder;
import envoy.data.User;
import envoy.event.EventBus;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -164,7 +165,7 @@ public class ChatWindow extends JFrame {
final JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource(); final JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
final User user = selectedUserList.getSelectedValue(); final User user = selectedUserList.getSelectedValue();
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();
// Set all unread messages in the chat to read // Set all unread messages in the chat to read
readCurrentChat(); readCurrentChat();
@ -245,8 +246,15 @@ public class ChatWindow extends JFrame {
if (!messageEnterTextArea.getText().isEmpty()) try { if (!messageEnterTextArea.getText().isEmpty()) try {
// Create and send message object // Create message
final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient().getID()); final Message message = new MessageBuilder(localDB.getUser().getId(), currentChat.getRecipient().getId())
.setText(messageEnterTextArea.getText())
.build();
// Send message
client.sendMessage(message);
// Add message to LocalDB and update UI
currentChat.appendMessage(message); currentChat.appendMessage(message);
messageList.setModel(currentChat.getModel()); messageList.setModel(currentChat.getModel());
@ -275,7 +283,7 @@ public class ChatWindow extends JFrame {
userListModel.addElement(user); userListModel.addElement(user);
// Check if user exists in local DB // Check if user exists in local DB
if (localDB.getChats().stream().filter(c -> c.getRecipient().getID() == user.getID()).count() == 0) if (localDB.getChats().stream().filter(c -> c.getRecipient().getId() == user.getId()).count() == 0)
localDB.getChats().add(new Chat(user)); localDB.getChats().add(new Chat(user));
}); });
SwingUtilities.invokeLater(() -> userList.setModel(userListModel)); SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
@ -296,14 +304,15 @@ public class ChatWindow extends JFrame {
// Synchronize // Synchronize
try { try {
localDB.applySync(client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID()))); // localDB.applySync(client.sendSync(client.getSender().getId(),
// localDB.fillSync(client.getSender().getId())));
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, "Could not perform sync", e); logger.log(Level.SEVERE, "Could not perform sync", e);
} }
// Process unread messages // TODO: Process unread messages
localDB.addUnreadMessagesToLocalDB(); // localDB.addUnreadMessagesToLocalDB();
localDB.clearUnreadMessagesSync(); // localDB.clearUnreadMessagesSync();
// Mark unread messages as read when they are in the current chat // Mark unread messages as read when they are in the current chat
readCurrentChat(); readCurrentChat();
@ -317,14 +326,18 @@ public class ChatWindow extends JFrame {
private void updateUserStates() { private void updateUserStates() {
for (int i = 0; i < userList.getModel().getSize(); i++) for (int i = 0; i < userList.getModel().getSize(); i++)
for (int j = 0; j < localDB.getChats().size(); j++) for (int j = 0; j < localDB.getChats().size(); j++)
if (userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID()) if (userList.getModel().getElementAt(i).getId() == localDB.getChats().get(j).getRecipient().getId())
userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus()); userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus());
} }
/** /**
* Marks messages in the current chat as {@code READ}. * Marks messages in the current chat as {@code READ}.
*/ */
private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } } private void readCurrentChat() {
if (currentChat != null) {
// TODO: localDB.setMessagesToRead(currentChat);
}
}
/** /**
* Sets the {@link Client} used by this {@link ChatWindow}. If the client is * Sets the {@link Client} used by this {@link ChatWindow}. If the client is

View File

@ -0,0 +1,118 @@
package envoy.client.ui;
import java.awt.*;
import java.security.NoSuchAlgorithmException;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import envoy.data.LoginCredentials;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>LoginDialog.java</strong><br>
* Created: <strong>01.01.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.3-alpha
*/
public class LoginDialog extends JDialog {
private final JPanel contentPanel = new JPanel();
private static final long serialVersionUID = 352021600833907468L;
private JTextField textField;
private JPasswordField passwordField;
private LoginCredentials credentials;
/**
* Displays a dialog enabling the user to enter their user name and password.
*
* @since Envoy v0.3-alpha
*/
public LoginDialog() {
setSize(338, 123);
setLocationRelativeTo(null);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
GridBagLayout gbl_contentPanel = new GridBagLayout();
gbl_contentPanel.columnWidths = new int[] { 0, 0, 0 };
gbl_contentPanel.rowHeights = new int[] { 0, 0, 0 };
gbl_contentPanel.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
gbl_contentPanel.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
contentPanel.setLayout(gbl_contentPanel);
{
JLabel lblUserName = new JLabel("User name:");
GridBagConstraints gbc_lblUserName = new GridBagConstraints();
gbc_lblUserName.anchor = GridBagConstraints.EAST;
gbc_lblUserName.insets = new Insets(0, 0, 5, 5);
gbc_lblUserName.gridx = 0;
gbc_lblUserName.gridy = 0;
contentPanel.add(lblUserName, gbc_lblUserName);
}
{
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.insets = new Insets(0, 0, 5, 0);
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
contentPanel.add(textField, gbc_textField);
textField.setColumns(10);
}
{
JLabel lblPassword = new JLabel("Password:");
GridBagConstraints gbc_lblPassword = new GridBagConstraints();
gbc_lblPassword.anchor = GridBagConstraints.EAST;
gbc_lblPassword.insets = new Insets(0, 0, 0, 5);
gbc_lblPassword.gridx = 0;
gbc_lblPassword.gridy = 1;
contentPanel.add(lblPassword, gbc_lblPassword);
}
{
passwordField = new JPasswordField();
GridBagConstraints gbc_passwordField = new GridBagConstraints();
gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
gbc_passwordField.gridx = 1;
gbc_passwordField.gridy = 1;
contentPanel.add(passwordField, gbc_passwordField);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.addActionListener((evt) -> {
try {
credentials = new LoginCredentials(textField.getText(), passwordField.getPassword());
dispose();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener((evt) -> dispose());
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
setModal(true);
setVisible(true);
}
/**
* @return the {@link LoginCredentials} entered by the user, or {@code null} if
* the dialog has been cancelled
* @since Envoy v0.3-alpha
*/
public LoginCredentials getCredentials() { return credentials; }
}

View File

@ -8,7 +8,7 @@ import javax.swing.JList;
import javax.swing.ListCellRenderer; import javax.swing.ListCellRenderer;
import envoy.client.Settings; import envoy.client.Settings;
import envoy.schema.Message; import envoy.data.Message;
/** /**
* Defines how a message is displayed.<br> * Defines how a message is displayed.<br>
@ -37,19 +37,17 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer<Mess
setOpaque(true); setOpaque(true);
final String text = value.getContent().get(0).getText(); // TODO: Handle message attachments
final String state = value.getMetadata().getState().toString();
final String date = value.getMetadata().getDate() == null ? "" final String text = value.getText();
: new SimpleDateFormat("dd.MM.yyyy HH:mm ").format(value.getMetadata().getDate().toGregorianCalendar().getTime()); final String state = value.getStatus().toString();
final String date = new SimpleDateFormat("dd.MM.yyyy HH.mm").format(value.getDate());
// Getting the MessageColor in the Chat of the current theme // Getting the MessageColor in the Chat of the current theme
String textColor = null; String textColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat().toHex();
textColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat().toHex();
// Getting the DateColor in the Chat of the current theme // Getting the DateColor in the Chat of the current theme
String dateColor = null; String dateColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat().toHex();
dateColor = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat().toHex();
setText(String.format("<html><p style=\"color:%s\"><b><small>%s</b></small><br><p style=\"color:%s\">%s :%s</html>", setText(String.format("<html><p style=\"color:%s\"><b><small>%s</b></small><br><p style=\"color:%s\">%s :%s</html>",
dateColor, dateColor,

View File

@ -2,6 +2,7 @@ package envoy.client.ui;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -12,8 +13,9 @@ import javax.swing.SwingUtilities;
import envoy.client.*; import envoy.client.*;
import envoy.client.util.EnvoyLog; import envoy.client.util.EnvoyLog;
import envoy.data.LoginCredentials;
import envoy.data.User;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.schema.User;
/** /**
* Starts the Envoy client and prompts the user to enter their name.<br> * Starts the Envoy client and prompts the user to enter their name.<br>
@ -69,11 +71,12 @@ public class Startup {
EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier()); EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier()); EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());
// Ask the user for their user name // Ask the user for their user name and password
String userName = JOptionPane.showInputDialog("Please enter your username"); LoginCredentials credentials = new LoginDialog().getCredentials();
if (userName == null || userName.isEmpty()) {
logger.severe("User name is not set or empty. Exiting..."); if (credentials == null) {
System.exit(1); logger.info("The login process has been aborted by the user. Exiting...");
System.exit(0);
} }
// Initialize the local database // Initialize the local database
@ -91,20 +94,21 @@ public class Startup {
// Acquire the client user (with ID) either from the server or from the local // Acquire the client user (with ID) either from the server or from the local
// database, which triggers offline mode // database, which triggers offline mode
Client client = new Client(config); Client client = new Client();
try { try {
// Try entering online mode first // Try entering online mode first
client.onlineInit(userName); client.onlineInit(credentials);
} catch (Exception e1) { } catch (Exception e1) {
logger.warning("Could not connect to server. Trying offline mode..."); logger.warning("Could not connect to server. Trying offline mode...");
e1.printStackTrace();
try { try {
// Try entering offline mode // Try entering offline mode
localDB.loadUsers(); localDB.loadUsers();
User clientUser = localDB.getUsers().get(userName); User clientUser = localDB.getUsers().get(credentials.getName());
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown"); if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
client.setSender(clientUser); client.setSender(clientUser);
JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null,
"A connection to the server could not be established. Starting in offline mode.", "A connection to the server could not be established. Starting in offline mode.\n" + e1,
"Connection error", "Connection error",
JOptionPane.WARNING_MESSAGE); JOptionPane.WARNING_MESSAGE);
} catch (Exception e2) { } catch (Exception e2) {
@ -121,16 +125,16 @@ public class Startup {
try { try {
localDB.initializeDBFile(); localDB.initializeDBFile();
localDB.loadChats(); localDB.loadChats();
} catch (EnvoyException e) { } catch (FileNotFoundException e) {
// The local database file has not yet been created, probably first login
} catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null,
"Error while loading local database: " + e.toString() + "\nChats will not be stored locally.", "Error while loading local database: " + e + "\nChats might not be stored locally.",
"Local DB error", "Local DB error",
JOptionPane.WARNING_MESSAGE); JOptionPane.WARNING_MESSAGE);
} }
logger.info("Client user ID: " + client.getSender().getID());
// Save all users to the local database // Save all users to the local database
if (client.isOnline()) localDB.setUsers(client.getUsers()); if (client.isOnline()) localDB.setUsers(client.getUsers());
@ -142,8 +146,13 @@ public class Startup {
try { try {
new StatusTrayIcon(chatWindow).show(); new StatusTrayIcon(chatWindow).show();
// If the tray icon is supported and corresponding settings is set, hide the chat window on close // If the tray icon is supported and corresponding settings is set, hide the
Settings.getInstance().getItems().get("onCloseMode").setChangeHandler((onCloseMode) -> chatWindow.setDefaultCloseOperation((boolean) onCloseMode ? JFrame.HIDE_ON_CLOSE : JFrame.EXIT_ON_CLOSE)); // chat window on close
Settings.getInstance()
.getItems()
.get("onCloseMode")
.setChangeHandler((onCloseMode) -> chatWindow
.setDefaultCloseOperation((boolean) onCloseMode ? JFrame.HIDE_ON_CLOSE : JFrame.EXIT_ON_CLOSE));
} catch (EnvoyException e) { } catch (EnvoyException e) {
logger.warning("The StatusTrayIcon is not supported on this platform!"); logger.warning("The StatusTrayIcon is not supported on this platform!");
} }
@ -155,13 +164,14 @@ public class Startup {
// Save Settings and LocalDB on shutdown // Save Settings and LocalDB on shutdown
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try { try {
logger.info("Saving local database..."); logger.info("Closing connection...");
client.close();
logger.info("Saving local database and settings...");
localDB.save(); localDB.save();
logger.info("Saving settings...");
Settings.getInstance().save(); Settings.getInstance().save();
} catch (IOException e1) { } catch (IOException e) {
e1.printStackTrace(); logger.log(Level.SEVERE, "Unable to save local files", e);
logger.log(Level.WARNING, "Unable to save the messages", e1);
} }
})); }));
} }

View File

@ -1,21 +1,14 @@
package envoy.client.ui; package envoy.client.ui;
import java.awt.AWTException; import java.awt.*;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType; import java.awt.TrayIcon.MessageType;
import java.awt.Window;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import envoy.client.event.EventBus;
import envoy.client.event.MessageCreationEvent; import envoy.client.event.MessageCreationEvent;
import envoy.data.Message;
import envoy.event.EventBus;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.schema.Message;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -81,9 +74,12 @@ public class StatusTrayIcon {
trayIcon.addActionListener((evt) -> { focusTarget.setVisible(true); focusTarget.requestFocus(); }); trayIcon.addActionListener((evt) -> { focusTarget.setVisible(true); focusTarget.requestFocus(); });
// Start processing message events // Start processing message events
EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> { EventBus.getInstance()
.register(MessageCreationEvent.class,
(evt) -> {
// TODO: Handle other message types
if (displayMessages) if (displayMessages)
trayIcon.displayMessage("New message received", ((MessageCreationEvent) evt).get().getContent().get(0).getText(), MessageType.INFO); trayIcon.displayMessage("New message received", ((MessageCreationEvent) evt).get().getText(), MessageType.INFO);
}); });
} }

View File

@ -7,8 +7,8 @@ import javax.swing.JList;
import javax.swing.ListCellRenderer; import javax.swing.ListCellRenderer;
import envoy.client.Settings; import envoy.client.Settings;
import envoy.schema.User; import envoy.data.User;
import envoy.schema.User.UserStatus; import envoy.data.User.UserStatus;
/** /**
* Defines how the {@code UserList} is displayed.<br> * Defines how the {@code UserList} is displayed.<br>

View File

@ -10,11 +10,11 @@ import java.util.logging.Logger;
import javax.swing.*; import javax.swing.*;
import envoy.client.Settings; import envoy.client.Settings;
import envoy.client.event.EventBus;
import envoy.client.event.ThemeChangeEvent; import envoy.client.event.ThemeChangeEvent;
import envoy.client.ui.PrimaryButton; import envoy.client.ui.PrimaryButton;
import envoy.client.ui.Theme; import envoy.client.ui.Theme;
import envoy.client.util.EnvoyLog; import envoy.client.util.EnvoyLog;
import envoy.event.EventBus;
/** /**
* This class provides the GUI to change the user specific settings.<br> * This class provides the GUI to change the user specific settings.<br>

View File

@ -10,11 +10,11 @@ import java.util.logging.Logger;
import javax.swing.*; import javax.swing.*;
import envoy.client.Settings; import envoy.client.Settings;
import envoy.client.event.EventBus;
import envoy.client.event.ThemeChangeEvent; import envoy.client.event.ThemeChangeEvent;
import envoy.client.ui.Color; import envoy.client.ui.Color;
import envoy.client.ui.Theme; import envoy.client.ui.Theme;
import envoy.client.util.EnvoyLog; import envoy.client.util.EnvoyLog;
import envoy.event.EventBus;
/** /**
* Displays GUI components that allow changing the current {@Theme} and creating * Displays GUI components that allow changing the current {@Theme} and creating

View File

@ -1,57 +0,0 @@
package envoy.client.util;
import java.io.*;
import envoy.exception.EnvoyException;
/**
* Project: <strong>envoy-clientChess</strong><br>
* File: <strong>SerializationUtils.javaEvent.java</strong><br>
* Created: <strong>23.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.3-alpha
*/
public class SerializationUtils {
private SerializationUtils() {}
/**
* Deserializes an arbitrary {@link Serializable} object from a file.
*
* @param <T> the type of the object to deserialize
* @param file the file deserialize from
* @param serializedClass the class of the object to deserialize
* @return the deserialized object
* @throws EnvoyException if an error occurred during deserialization
* @since Envoy v0.3-alpha
*/
public static <T extends Serializable> T read(File file, Class<T> serializedClass) throws EnvoyException {
if (file == null) throw new NullPointerException("File is null");
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
return serializedClass.cast(in.readObject());
} catch (ClassNotFoundException | IOException e) {
throw new EnvoyException("Could not load serialized object", e);
}
}
/**
* Serializes an arbitrary object to a file.
*
* @param file the file to serialize to
* @param obj the object to serialize
* @throws IOException if an error occurred during serialization
* @since Envoy v0.3-alpha
*/
public static void write(File file, Object obj) throws IOException {
if (file == null) throw new NullPointerException("File is null");
if (obj == null) throw new NullPointerException("Object to serialize is null");
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
out.writeObject(obj);
}
}
}