Merge branch 'develop' into f/message_notification
This commit is contained in:
commit
bfa6455e16
@ -1,5 +1,7 @@
|
|||||||
package envoy.client;
|
package envoy.client;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.ws.rs.client.ClientBuilder;
|
import javax.ws.rs.client.ClientBuilder;
|
||||||
import javax.ws.rs.client.Entity;
|
import javax.ws.rs.client.Entity;
|
||||||
import javax.ws.rs.client.WebTarget;
|
import javax.ws.rs.client.WebTarget;
|
||||||
@ -28,16 +30,18 @@ public class Client {
|
|||||||
private Config config;
|
private Config config;
|
||||||
private User sender, recipient;
|
private User sender, recipient;
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
|
||||||
|
|
||||||
public Client(Config config, String username) {
|
public Client(Config config, String username) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
sender = getUser(username);
|
sender = getUser(username);
|
||||||
System.out.println("ID: " + sender.getID());
|
|
||||||
|
logger.info("ID: " + sender.getID());
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T, R> R post(String uri, T body, Class<R> responseBodyClass) {
|
private <T, R> R post(String uri, T body, Class<R> responseBodyClass) {
|
||||||
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
||||||
WebTarget target = client.target(uri);
|
WebTarget target = client.target(uri);
|
||||||
|
|
||||||
Response response = target.request().post(Entity.entity(body, "application/xml"));
|
Response response = target.request().post(Entity.entity(body, "application/xml"));
|
||||||
R responseBody = response.readEntity(responseBodyClass);
|
R responseBody = response.readEntity(responseBodyClass);
|
||||||
response.close();
|
response.close();
|
||||||
@ -92,7 +96,7 @@ public class Client {
|
|||||||
if (returnSenderSync.getUsers().size() == 1) {
|
if (returnSenderSync.getUsers().size() == 1) {
|
||||||
returnSender = returnSenderSync.getUsers().get(0);
|
returnSender = returnSenderSync.getUsers().get(0);
|
||||||
} else {
|
} else {
|
||||||
System.out.println("ERROR exiting...");
|
logger.warning("ERROR exiting...");
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnSender;
|
return returnSender;
|
||||||
|
@ -32,7 +32,7 @@ public class Config {
|
|||||||
*
|
*
|
||||||
* @param properties a {@link Properties} object containing information about
|
* @param properties a {@link Properties} object containing information about
|
||||||
* the server and port, as well as the path to the local
|
* the server and port, as well as the path to the local
|
||||||
* database
|
* database and the synchronization timeout
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public void load(Properties properties) {
|
public void load(Properties properties) {
|
||||||
@ -64,8 +64,6 @@ public class Config {
|
|||||||
case "-db":
|
case "-db":
|
||||||
localDB = new File(args[++i]);
|
localDB = new File(args[++i]);
|
||||||
}
|
}
|
||||||
if (localDB == null) localDB = new File(".\\localDB");
|
|
||||||
if (syncTimeout == 0) syncTimeout = 1000;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,303 +1,303 @@
|
|||||||
package envoy.client;
|
package envoy.client;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.xml.datatype.DatatypeConfigurationException;
|
import javax.xml.datatype.DatatypeConfigurationException;
|
||||||
import javax.xml.datatype.DatatypeFactory;
|
import javax.xml.datatype.DatatypeFactory;
|
||||||
|
|
||||||
import envoy.client.event.EventBus;
|
import envoy.client.event.EventBus;
|
||||||
import envoy.client.event.MessageCreationEvent;
|
import envoy.client.event.MessageCreationEvent;
|
||||||
import envoy.exception.EnvoyException;
|
import envoy.exception.EnvoyException;
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.Message.Metadata.MessageState;
|
import envoy.schema.Message.Metadata.MessageState;
|
||||||
import envoy.schema.ObjectFactory;
|
import envoy.schema.ObjectFactory;
|
||||||
import envoy.schema.Sync;
|
import envoy.schema.Sync;
|
||||||
import envoy.schema.User;
|
import envoy.schema.User;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
* File: <strong>LocalDB.java</strong><br>
|
* File: <strong>LocalDB.java</strong><br>
|
||||||
* Created: <strong>27.10.2019</strong><br>
|
* Created: <strong>27.10.2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
* @author Maximilian Käfer
|
* @author Maximilian Käfer
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public class LocalDB {
|
public class LocalDB {
|
||||||
|
|
||||||
private File localDB;
|
private File localDB;
|
||||||
private User sender;
|
private User sender;
|
||||||
private List<Chat> chats = new ArrayList<>();
|
private List<Chat> chats = new ArrayList<>();
|
||||||
private ObjectFactory objectFactory = new ObjectFactory();
|
private ObjectFactory objectFactory = new ObjectFactory();
|
||||||
private DatatypeFactory datatypeFactory;
|
private DatatypeFactory datatypeFactory;
|
||||||
|
|
||||||
private Sync unreadMessagesSync = objectFactory.createSync();
|
private Sync unreadMessagesSync = objectFactory.createSync();
|
||||||
private Sync sync = objectFactory.createSync();
|
private Sync sync = objectFactory.createSync();
|
||||||
private Sync readMessages = objectFactory.createSync();
|
private Sync readMessages = objectFactory.createSync();
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName());
|
private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an empty local database.
|
* Constructs an empty local database.
|
||||||
*
|
*
|
||||||
* @param client the user that is logged in with this client
|
* @param client the user that is logged in with this client
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public LocalDB(User sender) {
|
public LocalDB(User sender) {
|
||||||
this.sender = sender;
|
this.sender = sender;
|
||||||
try {
|
try {
|
||||||
datatypeFactory = DatatypeFactory.newInstance();
|
datatypeFactory = DatatypeFactory.newInstance();
|
||||||
} catch (DatatypeConfigurationException e) {
|
} catch (DatatypeConfigurationException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the local database and fills it with values
|
* Initializes the local database and fills it with values
|
||||||
* if the user has already sent or received messages.
|
* if the user has already sent or received messages.
|
||||||
*
|
*
|
||||||
* @param localDBDir the directory where we wish to save/load the database from.
|
* @param localDBDir the directory where we wish to save/load the database from.
|
||||||
* @throws EnvoyException if the directory selected is not an actual directory.
|
* @throws EnvoyException if the directory selected is not an actual directory.
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public void initializeDBFile(File localDBDir) throws EnvoyException {
|
public void initializeDBFile(File localDBDir) throws EnvoyException {
|
||||||
if (localDBDir.exists() && !localDBDir.isDirectory())
|
if (localDBDir.exists() && !localDBDir.isDirectory())
|
||||||
throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
|
throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
|
||||||
localDB = new File(localDBDir, sender.getID() + ".db");
|
localDB = new File(localDBDir, sender.getID() + ".db");
|
||||||
if (localDB.exists()) loadFromLocalDB();
|
if (localDB.exists()) loadFromLocalDB();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the database into a file for future use.
|
* Saves the database into a file for future use.
|
||||||
*
|
*
|
||||||
* @throws IOException if something went wrong during saving
|
* @throws IOException if something went wrong during saving
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public void saveToLocalDB() {
|
public void saveToLocalDB() {
|
||||||
try {
|
try {
|
||||||
localDB.getParentFile().mkdirs();
|
localDB.getParentFile().mkdirs();
|
||||||
localDB.createNewFile();
|
localDB.createNewFile();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
logger.warning("unable to save the messages");
|
logger.warning("unable to save the messages");
|
||||||
}
|
}
|
||||||
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) {
|
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) {
|
||||||
out.writeObject(chats);
|
out.writeObject(chats);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
logger.warning("unable to save the messages");
|
logger.warning("unable to save the messages");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads all chats saved by Envoy for the client user.
|
* Loads all chats saved by Envoy for the client user.
|
||||||
*
|
*
|
||||||
* @throws EnvoyException if something fails while loading.
|
* @throws EnvoyException if something fails while loading.
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private void loadFromLocalDB() throws EnvoyException {
|
private void loadFromLocalDB() throws EnvoyException {
|
||||||
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) {
|
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) {
|
||||||
Object obj = in.readObject();
|
Object obj = in.readObject();
|
||||||
if (obj instanceof ArrayList<?>) chats = (ArrayList<Chat>) obj;
|
if (obj instanceof ArrayList<?>) chats = (ArrayList<Chat>) obj;
|
||||||
} catch (ClassNotFoundException | IOException e) {
|
} catch (ClassNotFoundException | IOException e) {
|
||||||
throw new EnvoyException(e);
|
throw new EnvoyException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@link Message} object serializable to XML.
|
* Creates a {@link Message} object serializable to XML.
|
||||||
*
|
*
|
||||||
* @param textContent The content (text) of the message
|
* @param textContent The content (text) of the message
|
||||||
* @return prepared {@link Message} object
|
* @return prepared {@link Message} object
|
||||||
*/
|
*/
|
||||||
public Message createMessage(String textContent, User recipient) {
|
public Message createMessage(String textContent, User recipient) {
|
||||||
Message.Metadata metaData = objectFactory.createMessageMetadata();
|
Message.Metadata metaData = objectFactory.createMessageMetadata();
|
||||||
metaData.setSender(sender.getID());
|
metaData.setSender(sender.getID());
|
||||||
metaData.setRecipient(recipient.getID());
|
metaData.setRecipient(recipient.getID());
|
||||||
metaData.setState(MessageState.WAITING);
|
metaData.setState(MessageState.WAITING);
|
||||||
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
||||||
|
|
||||||
Message.Content content = objectFactory.createMessageContent();
|
Message.Content content = objectFactory.createMessageContent();
|
||||||
content.setType("text");
|
content.setType("text");
|
||||||
content.setText(textContent);
|
content.setText(textContent);
|
||||||
|
|
||||||
Message message = objectFactory.createMessage();
|
Message message = objectFactory.createMessage();
|
||||||
message.setMetadata(metaData);
|
message.setMetadata(metaData);
|
||||||
message.getContent().add(content);
|
message.getContent().add(content);
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Sync fillSync(long userId) {
|
public Sync fillSync(long userId) {
|
||||||
addWaitingMessagesToSync();
|
addWaitingMessagesToSync();
|
||||||
|
|
||||||
sync.getMessages().addAll(readMessages.getMessages());
|
sync.getMessages().addAll(readMessages.getMessages());
|
||||||
readMessages.getMessages().clear();
|
readMessages.getMessages().clear();
|
||||||
|
|
||||||
logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size()));
|
logger.info(String.format("Filled sync with %d messages.", sync.getMessages().size()));
|
||||||
return sync;
|
return sync;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void applySync(Sync returnSync) {
|
public void applySync(Sync returnSync) {
|
||||||
for (int i = 0; i < returnSync.getMessages().size(); i++) {
|
for (int i = 0; i < returnSync.getMessages().size(); i++) {
|
||||||
|
|
||||||
// The message has an ID
|
// The message has an ID
|
||||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0) {
|
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0) {
|
||||||
|
|
||||||
// Messages are processes differently corresponding to their state
|
// Messages are processes differently corresponding to their state
|
||||||
switch (returnSync.getMessages().get(i).getMetadata().getState()) {
|
switch (returnSync.getMessages().get(i).getMetadata().getState()) {
|
||||||
case SENT:
|
case SENT:
|
||||||
// Update previously waiting and now sent messages that were assigned an ID by
|
// Update previously waiting and now sent messages that were assigned an ID by
|
||||||
// the server
|
// the server
|
||||||
sync.getMessages().get(i).getMetadata().setMessageId(returnSync.getMessages().get(i).getMetadata().getMessageId());
|
sync.getMessages().get(i).getMetadata().setMessageId(returnSync.getMessages().get(i).getMetadata().getMessageId());
|
||||||
sync.getMessages().get(i).getMetadata().setState(returnSync.getMessages().get(i).getMetadata().getState());
|
sync.getMessages().get(i).getMetadata().setState(returnSync.getMessages().get(i).getMetadata().getState());
|
||||||
break;
|
break;
|
||||||
case RECEIVED:
|
case RECEIVED:
|
||||||
if (returnSync.getMessages().get(i).getMetadata().getSender() != 0) {
|
if (returnSync.getMessages().get(i).getMetadata().getSender() != 0) {
|
||||||
// these are the unread Messages from the server
|
// these are the unread Messages from the server
|
||||||
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
|
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
|
||||||
|
|
||||||
// Create and dispatch message creation event
|
// Create and dispatch message creation event
|
||||||
EventBus.getInstance().dispatch(new MessageCreationEvent(returnSync.getMessages().get(i)));
|
EventBus.getInstance().dispatch(new MessageCreationEvent(returnSync.getMessages().get(i)));
|
||||||
} else {
|
} else {
|
||||||
// Update Messages in localDB to state RECEIVED
|
// Update Messages in localDB to state RECEIVED
|
||||||
for (int j = 0; j < getChats().size(); j++) {
|
for (int j = 0; j < getChats().size(); j++) {
|
||||||
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
||||||
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
||||||
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
|
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
|
||||||
.get(i)
|
.get(i)
|
||||||
.getMetadata()
|
.getMetadata()
|
||||||
.getMessageId()) {
|
.getMessageId()) {
|
||||||
// Update Message in LocalDB
|
// Update Message in LocalDB
|
||||||
getChats().get(j)
|
getChats().get(j)
|
||||||
.getModel()
|
.getModel()
|
||||||
.get(k)
|
.get(k)
|
||||||
.getMetadata()
|
.getMetadata()
|
||||||
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case READ:
|
case READ:
|
||||||
// Update local Messages to state READ
|
// Update local Messages to state READ
|
||||||
logger.info("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId()
|
logger.info("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId()
|
||||||
+ "was initialized to be set to READ in localDB.");
|
+ "was initialized to be set to READ in localDB.");
|
||||||
for (int j = 0; j < getChats().size(); j++) {
|
for (int j = 0; j < getChats().size(); j++) {
|
||||||
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
|
||||||
logger.info("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
|
logger.info("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
|
||||||
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
||||||
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
|
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
|
||||||
.get(i)
|
.get(i)
|
||||||
.getMetadata()
|
.getMetadata()
|
||||||
.getMessageId()) {
|
.getMessageId()) {
|
||||||
logger.info("Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId()
|
logger.info("Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId()
|
||||||
+ "was selected.");
|
+ "was selected.");
|
||||||
getChats().get(j)
|
getChats().get(j)
|
||||||
.getModel()
|
.getModel()
|
||||||
.get(k)
|
.get(k)
|
||||||
.getMetadata()
|
.getMetadata()
|
||||||
.setState(returnSync.getMessages().get(i).getMetadata().getState());
|
.setState(returnSync.getMessages().get(i).getMetadata().getState());
|
||||||
logger.info(
|
logger.info(
|
||||||
"Message State is now: " + getChats().get(j).getModel().get(k).getMetadata().getState().toString());
|
"Message State is now: " + getChats().get(j).getModel().get(k).getMetadata().getState().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updating UserStatus of all users in LocalDB
|
// Updating UserStatus of all users in LocalDB
|
||||||
for (User user : returnSync.getUsers())
|
for (User user : returnSync.getUsers())
|
||||||
for (Chat chat : getChats())
|
for (Chat chat : getChats())
|
||||||
if (user.getID() == chat.getRecipient().getID()) {
|
if (user.getID() == chat.getRecipient().getID()) {
|
||||||
chat.getRecipient().setStatus(user.getStatus());
|
chat.getRecipient().setStatus(user.getStatus());
|
||||||
logger.info(chat.getRecipient().getStatus().toString());
|
logger.info(chat.getRecipient().getStatus().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
sync.getMessages().clear();
|
sync.getMessages().clear();
|
||||||
sync.getUsers().clear();
|
sync.getUsers().clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the unread messages returned from the server in the latest sync to the
|
* Adds the unread messages returned from the server in the latest sync to the
|
||||||
* right chats in the LocalDB.
|
* right chats in the LocalDB.
|
||||||
*
|
*
|
||||||
* @param localDB
|
* @param localDB
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public void addUnreadMessagesToLocalDB() {
|
public void addUnreadMessagesToLocalDB() {
|
||||||
for(Message message : unreadMessagesSync.getMessages())
|
for(Message message : unreadMessagesSync.getMessages())
|
||||||
for(Chat chat : getChats())
|
for(Chat chat : getChats())
|
||||||
if(message.getMetadata().getSender() == chat.getRecipient().getID()) {
|
if(message.getMetadata().getSender() == chat.getRecipient().getID()) {
|
||||||
chat.appendMessage(message);
|
chat.appendMessage(message);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Changes all messages with state {@code RECEIVED} of a specific chat to state
|
* Changes all messages with state {@code RECEIVED} of a specific chat to state
|
||||||
* {@code READ}.
|
* {@code READ}.
|
||||||
* <br>
|
* <br>
|
||||||
* Adds these messages to the {@code readMessages} {@link Sync} object.
|
* Adds these messages to the {@code readMessages} {@link Sync} object.
|
||||||
*
|
*
|
||||||
* @param currentChat
|
* @param currentChat
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public void setMessagesToRead(Chat currentChat) {
|
public void setMessagesToRead(Chat currentChat) {
|
||||||
for (int i = currentChat.getModel().size() - 1; i >= 0; --i)
|
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().getRecipient() != currentChat.getRecipient().getID())
|
||||||
if (currentChat.getModel().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
if (currentChat.getModel().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
||||||
currentChat.getModel().get(i).getMetadata().setState(MessageState.READ);
|
currentChat.getModel().get(i).getMetadata().setState(MessageState.READ);
|
||||||
readMessages.getMessages().add(currentChat.getModel().get(i));
|
readMessages.getMessages().add(currentChat.getModel().get(i));
|
||||||
} else break;
|
} else break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds all messages with state {@code WAITING} from the {@link LocalDB} to the
|
* Adds all messages with state {@code WAITING} from the {@link LocalDB} to the
|
||||||
* {@link Sync} object.
|
* {@link Sync} object.
|
||||||
*
|
*
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
private void addWaitingMessagesToSync() {
|
private void addWaitingMessagesToSync() {
|
||||||
for (Chat chat : getChats())
|
for (Chat chat : getChats())
|
||||||
for (int i = 0; i < chat.getModel().size(); i++)
|
for (int i = 0; i < chat.getModel().size(); i++)
|
||||||
if (chat.getModel().get(i).getMetadata().getState() == MessageState.WAITING) {
|
if (chat.getModel().get(i).getMetadata().getState() == MessageState.WAITING) {
|
||||||
logger.info("Got Waiting Message");
|
logger.info("Got Waiting Message");
|
||||||
sync.getMessages().add(chat.getModel().get(i));
|
sync.getMessages().add(chat.getModel().get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears the {@code unreadMessagesSync} {@link Sync} object.
|
* Clears the {@code unreadMessagesSync} {@link Sync} object.
|
||||||
*
|
*
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
|
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return all saved {@link Chat} objects that list the client user as the
|
* @return all saved {@link Chat} objects that list the client user as the
|
||||||
* sender
|
* sender
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
**/
|
**/
|
||||||
public List<Chat> getChats() { return chats; }
|
public List<Chat> getChats() { return chats; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the {@link User} who initialized the local database
|
* @return the {@link User} who initialized the local database
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public User getUser() { return sender; }
|
public User getUser() { return sender; }
|
||||||
}
|
}
|
@ -10,6 +10,7 @@ import java.awt.event.KeyAdapter;
|
|||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.swing.DefaultListModel;
|
import javax.swing.DefaultListModel;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
@ -56,6 +57,8 @@ public class ChatWindow extends JFrame {
|
|||||||
private Chat currentChat;
|
private Chat currentChat;
|
||||||
|
|
||||||
private JTextArea messageEnterTextArea;
|
private JTextArea messageEnterTextArea;
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(ChatWindow.class.getSimpleName());
|
||||||
|
|
||||||
public ChatWindow(Client client, LocalDB localDB) {
|
public ChatWindow(Client client, LocalDB localDB) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
@ -184,7 +187,7 @@ public class ChatWindow extends JFrame {
|
|||||||
SettingsScreen.open(localDB.getUser().getName());
|
SettingsScreen.open(localDB.getUser().getName());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
SettingsScreen.open();
|
SettingsScreen.open();
|
||||||
System.err.println("An error occured while opening the settings screen: " + e);
|
logger.warning("An error occured while opening the settings screen: " + e);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -3,6 +3,8 @@ package envoy.client.ui;
|
|||||||
import java.awt.EventQueue;
|
import java.awt.EventQueue;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
@ -24,41 +26,48 @@ import envoy.exception.EnvoyException;
|
|||||||
*/
|
*/
|
||||||
public class Startup {
|
public class Startup {
|
||||||
|
|
||||||
|
private static final Logger logger = Logger.getLogger(Client.class.getSimpleName());
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
logger.setLevel(Level.ALL);
|
||||||
|
|
||||||
Config config = Config.getInstance();
|
Config config = Config.getInstance();
|
||||||
if (args.length > 0) {
|
|
||||||
config.load(args);
|
// Load the configuration from client.properties first
|
||||||
} else {
|
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
||||||
ClassLoader loader = Thread.currentThread().getContextClassLoader();
|
try {
|
||||||
try {
|
Properties configProperties = new Properties();
|
||||||
Properties configProperties = new Properties();
|
configProperties.load(loader.getResourceAsStream("client.properties"));
|
||||||
configProperties.load(loader.getResourceAsStream("client.properties"));
|
config.load(configProperties);
|
||||||
config.load(configProperties);
|
} catch (IOException e) {
|
||||||
} catch (IOException e) {
|
e.printStackTrace();
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Override configuration values with command line arguments
|
||||||
|
if (args.length > 0)
|
||||||
|
config.load(args);
|
||||||
|
|
||||||
if (!config.isInitialized()) {
|
if (!config.isInitialized()) {
|
||||||
System.err.println("Server or port are not defined. Exiting...");
|
logger.warning("Server or port are not defined. Exiting...");
|
||||||
|
JOptionPane.showMessageDialog(null, "Error loading configuration values.", "Configuration error",
|
||||||
|
JOptionPane.ERROR_MESSAGE);
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
String userName = JOptionPane.showInputDialog("Please enter your username");
|
String userName = JOptionPane.showInputDialog("Please enter your username");
|
||||||
if (userName == null || userName.isEmpty()) {
|
if (userName == null || userName.isEmpty()) {
|
||||||
System.err.println("User name is not set or empty. Exiting...");
|
logger.warning("User name is not set or empty. Exiting...");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
Client client = new Client(config, userName);
|
Client client = new Client(config, userName);
|
||||||
LocalDB localDB = new LocalDB(client.getSender());
|
LocalDB localDB = new LocalDB(client.getSender());
|
||||||
try {
|
try {
|
||||||
localDB.initializeDBFile(config.getLocalDB());
|
localDB.initializeDBFile(config.getLocalDB());
|
||||||
} catch (EnvoyException e) {
|
} catch (EnvoyException 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.toString() + "\nChats will not be stored locally.",
|
||||||
"Local DB error",
|
"Local DB error", JOptionPane.WARNING_MESSAGE);
|
||||||
JOptionPane.WARNING_MESSAGE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EventQueue.invokeLater(() -> {
|
EventQueue.invokeLater(() -> {
|
||||||
|
Reference in New Issue
Block a user