Revised structure
This commit is contained in:
parent
c58a4a73d3
commit
6c212df309
@ -31,51 +31,30 @@ import envoy.schema.User;
|
||||
public class Client {
|
||||
|
||||
private ObjectFactory objectFactory = new ObjectFactory();
|
||||
private DatatypeFactory datatypeFactory;
|
||||
private Config config;
|
||||
private User sender, recipient;
|
||||
|
||||
private Sync unreadMessagesSync = objectFactory.createSync();
|
||||
public Sync sync = objectFactory.createSync();
|
||||
public Sync readMessages = objectFactory.createSync();
|
||||
|
||||
public Client(Config config, String username) {
|
||||
this.config = config;
|
||||
try {
|
||||
datatypeFactory = DatatypeFactory.newInstance();
|
||||
} catch (DatatypeConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
sender = getUser(username);
|
||||
System.out.println("ID: " + sender.getID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link Message} object serializable to XML.
|
||||
*
|
||||
* @param textContent The content (text) of the message
|
||||
* @return prepared {@link Message} object
|
||||
*/
|
||||
public Message createMessage(String textContent) {
|
||||
Message.Metadata metaData = objectFactory.createMessageMetadata();
|
||||
metaData.setSender(sender.getID());
|
||||
metaData.setRecipient(recipient.getID());
|
||||
metaData.setState(MessageState.WAITING);
|
||||
metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString()));
|
||||
private <T, R> R post(String uri, T body, Class<R> responseBodyClass) {
|
||||
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(uri);
|
||||
|
||||
Message.Content content = objectFactory.createMessageContent();
|
||||
content.setType("text");
|
||||
content.setText(textContent);
|
||||
Response response = target.request().post(Entity.entity(body, "application/xml"));
|
||||
R responseBody = response.readEntity(responseBodyClass);
|
||||
response.close();
|
||||
client.close();
|
||||
|
||||
Message message = objectFactory.createMessage();
|
||||
message.setMetadata(metaData);
|
||||
message.getContent().add(content);
|
||||
|
||||
return message;
|
||||
return responseBody;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Sync} with all users on the server.
|
||||
*
|
||||
* @return Sync - List of all users on the server.
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
@ -85,14 +64,11 @@ public class Client {
|
||||
user.setID(-1);
|
||||
sendSync.getUsers().add(user);
|
||||
|
||||
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(String
|
||||
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0));
|
||||
|
||||
Response response = target.request().post(Entity.entity(sendSync, "application/xml"));
|
||||
Sync returnSendSync = response.readEntity(Sync.class);
|
||||
response.close();
|
||||
client.close();
|
||||
Sync returnSendSync = post(
|
||||
String
|
||||
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
|
||||
sendSync,
|
||||
Sync.class);
|
||||
return returnSendSync;
|
||||
|
||||
}
|
||||
@ -110,20 +86,20 @@ public class Client {
|
||||
user.setName(name);
|
||||
senderSync.getUsers().add(user);
|
||||
|
||||
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(String
|
||||
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0));
|
||||
Sync returnSenderSync = post(
|
||||
String
|
||||
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0),
|
||||
senderSync,
|
||||
Sync.class);
|
||||
|
||||
Response response = target.request().post(Entity.entity(senderSync, "application/xml"));
|
||||
User returnSender = objectFactory.createUser();
|
||||
Sync returnSenderSync = response.readEntity(Sync.class);
|
||||
|
||||
if (returnSenderSync.getUsers().size() == 1) {
|
||||
returnSender = returnSenderSync.getUsers().get(0);
|
||||
} else {
|
||||
System.out.println("ERROR exiting...");
|
||||
}
|
||||
response.close();
|
||||
client.close();
|
||||
|
||||
return returnSender;
|
||||
}
|
||||
|
||||
@ -131,9 +107,11 @@ public class Client {
|
||||
* 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>
|
||||
* (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>
|
||||
* (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>
|
||||
@ -144,21 +122,22 @@ public class Client {
|
||||
* 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.
|
||||
* -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
|
||||
* -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>
|
||||
* Updating UserStatus of all users in LocalDB. (Server sends all users with
|
||||
* their updated UserStatus to the client.) <br>
|
||||
*
|
||||
* @param userId
|
||||
* @param localDB
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void sendSync(long userId, LocalDB localDB) {
|
||||
new Thread(() -> {
|
||||
public Sync sendSync(long userId, Sync sync) {
|
||||
// Print sync XML to console
|
||||
JAXBContext jc;
|
||||
try {
|
||||
@ -169,262 +148,12 @@ public class Client {
|
||||
} catch (JAXBException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
addWaitingMessagesToSync(localDB);
|
||||
|
||||
getSentStateMessagesFromLocalDB(localDB);
|
||||
for (int i = 0; i < readMessages.getMessages().size(); i++) {
|
||||
sync.getMessages().add(readMessages.getMessages().get(i));
|
||||
}
|
||||
readMessages.getMessages().clear();
|
||||
|
||||
// Send sync
|
||||
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client
|
||||
.target(String.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d",
|
||||
config.getServer(),
|
||||
config.getPort(),
|
||||
userId));
|
||||
|
||||
Response response = target.request().post(Entity.entity(sync, "application/xml"));
|
||||
|
||||
Sync returnSync = response.readEntity(Sync.class);
|
||||
if (returnSync.getMessages().size() != 0) {
|
||||
System.out.println("Message ID: " + returnSync.getMessages().get(0).getMetadata().getMessageId());
|
||||
}
|
||||
|
||||
for (int i = 0; i < returnSync.getMessages().size(); i++) {
|
||||
|
||||
// Print sync XML to console
|
||||
JAXBContext jc2;
|
||||
try {
|
||||
jc2 = JAXBContext.newInstance("envoy.schema");
|
||||
Marshaller m = jc2.createMarshaller();
|
||||
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||
m.marshal(returnSync, System.out);
|
||||
} catch (JAXBException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) {
|
||||
// Update Local Messages with State WAITING (add Message ID and change State to
|
||||
// SENT)
|
||||
for (int j = 0; j < sync.getMessages().size(); j++) {
|
||||
if (j == i) {
|
||||
sync.getMessages()
|
||||
.get(j)
|
||||
.getMetadata()
|
||||
.setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId());
|
||||
sync.getMessages()
|
||||
.get(j)
|
||||
.getMetadata()
|
||||
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getSender() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
||||
// these are the unread Messages from the server
|
||||
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
|
||||
|
||||
}
|
||||
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getSender() == 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
||||
// Update Messages in localDB to state RECEIVED
|
||||
for (int j = 0; j < localDB.getChats().size(); j++) {
|
||||
if (localDB.getChats().get(j).getRecipient().getID() == returnSync.getMessages()
|
||||
.get(i)
|
||||
.getMetadata()
|
||||
.getRecipient()) {
|
||||
for (int k = 0; k < localDB.getChats().get(j).getModel().getSize(); k++) {
|
||||
if (localDB.getChats()
|
||||
.get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) {
|
||||
// Update Message in LocalDB
|
||||
localDB.getChats()
|
||||
.get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) {
|
||||
// Update local Messages to state READ
|
||||
System.out
|
||||
.println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId()
|
||||
+ "was initialized to be set to READ in localDB.");
|
||||
for (int j = 0; j < localDB.getChats().size(); j++) {
|
||||
if (localDB.getChats().get(j).getRecipient().getID() == returnSync.getMessages()
|
||||
.get(i)
|
||||
.getMetadata()
|
||||
.getRecipient()) {
|
||||
System.out.println(
|
||||
"Chat with: " + localDB.getChats().get(j).getRecipient().getID() + "was selected.");
|
||||
for (int k = 0; k < localDB.getChats().get(j).getModel().getSize(); k++) {
|
||||
if (localDB.getChats()
|
||||
.get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) {
|
||||
System.out.println("Message with ID: "
|
||||
+ localDB.getChats().get(j).getModel().get(k).getMetadata().getMessageId()
|
||||
+ "was selected.");
|
||||
localDB.getChats()
|
||||
.get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.setState(returnSync.getMessages().get(i).getMetadata().getState());
|
||||
System.out.println("Message State is now: " + localDB.getChats()
|
||||
.get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.getState()
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Updating UserStatus of all Users in LocalDB
|
||||
for (int j = 0; j < returnSync.getUsers().size(); j++) {
|
||||
for (int k = 0; k < localDB.getChats().size(); k++) {
|
||||
if (localDB.getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) {
|
||||
|
||||
localDB.getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus());
|
||||
System.out.println(localDB.getChats().get(k).getRecipient().getStatus().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Response code: " + response.getStatus());
|
||||
|
||||
response.close();
|
||||
client.close();
|
||||
sync.getMessages().clear();
|
||||
sync.getUsers().clear();
|
||||
|
||||
}).start();
|
||||
System.out.println(sync.getMessages().size());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a message to the "sync" Sync object.
|
||||
*
|
||||
* @param message
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addMessageToSync(Message message) { sync.getMessages().add(message); }
|
||||
|
||||
/**
|
||||
* Adds a user to the "sync" Sync object.
|
||||
*
|
||||
* @param user
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addUserToSync(User user) { sync.getUsers().add(user); }
|
||||
|
||||
/**
|
||||
* Adds the unread Messages returned from the server in the latest sync to the
|
||||
* right chats in the LocalDB.
|
||||
*
|
||||
* @param localDB
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addUnreadMessagesToLocalDB(LocalDB localDB) {
|
||||
Sync unreadMessages = unreadMessagesSync;
|
||||
for (int i = 0; i < unreadMessages.getMessages().size(); i++)
|
||||
for (int j = 0; j < localDB.getChats().size(); j++)
|
||||
if (localDB.getChats().get(j).getRecipient().getID() == unreadMessages.getMessages()
|
||||
.get(i)
|
||||
.getMetadata()
|
||||
.getSender()) {
|
||||
localDB.getChats().get(j).appendMessage(unreadMessages.getMessages().get(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all Messages with state SENT from the LocalDB and adds them to the
|
||||
* "sync" Sync object.
|
||||
*
|
||||
* @param localDB
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void getSentStateMessagesFromLocalDB(LocalDB localDB) {
|
||||
for (int i = 0; i < localDB.getChats().size(); i++) {
|
||||
for (int j = 0; j < localDB.getChats().get(i).getModel().getSize(); j++) {
|
||||
if (localDB.getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.SENT) {
|
||||
addMessageToSync(localDB.getChats().get(i).getModel().get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes all Messages with State RECEIVED of a specific chat to State READ.
|
||||
* <br>
|
||||
* Adds these Messages to the "readMessages" Sync object.
|
||||
*
|
||||
* @param currentChat
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void setMessagesToRead(Chat currentChat) {
|
||||
for (int j = 0; j < currentChat.getModel().getSize(); j++) {
|
||||
if (currentChat.getModel().get(j).getMetadata().getRecipient() != currentChat.getRecipient().getID()) {
|
||||
if (currentChat.getModel().get(j).getMetadata().getState() == MessageState.RECEIVED) {
|
||||
currentChat.getModel().get(j).getMetadata().setState(MessageState.READ);
|
||||
readMessages.getMessages().add(currentChat.getModel().get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a Message with State WAITING to a specific chat in the LocalDB.
|
||||
*
|
||||
* @param message
|
||||
* @param currentChat
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); }
|
||||
|
||||
/**
|
||||
* Adds all Messages with State WAITING from the LocalDB to the Sync.
|
||||
*
|
||||
* @param localDB
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addWaitingMessagesToSync(LocalDB localDB) {
|
||||
for (int i = 0; i < localDB.getChats().size(); i++) {
|
||||
for (int j = 0; j < localDB.getChats().get(i).getModel().getSize(); j++) {
|
||||
if (localDB.getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) {
|
||||
// addMessageToSync(localDB.getChats().get(i).getModel().get(j));
|
||||
System.out.println("Got Waiting Message");
|
||||
sync.getMessages().add(0, localDB.getChats().get(i).getModel().get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
return post(String
|
||||
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), userId),
|
||||
sync,
|
||||
Sync.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -453,10 +182,4 @@ public class Client {
|
||||
*/
|
||||
public boolean hasRecipient() { return recipient != null; }
|
||||
|
||||
/**
|
||||
* Clears the "unreadMessagesSync" Sync object.
|
||||
*
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
|
||||
}
|
@ -6,11 +6,22 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
|
||||
import envoy.exception.EnvoyException;
|
||||
import envoy.schema.Message;
|
||||
import envoy.schema.User;
|
||||
import envoy.schema.Message.Metadata.MessageState;
|
||||
import envoy.schema.ObjectFactory;
|
||||
import envoy.schema.Sync;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-client</strong><br>
|
||||
@ -25,6 +36,8 @@ public class LocalDB {
|
||||
private File localDB;
|
||||
private User sender;
|
||||
private List<Chat> chats = new ArrayList<>();
|
||||
private ObjectFactory objectFactory = new ObjectFactory();
|
||||
private DatatypeFactory datatypeFactory;
|
||||
|
||||
/**
|
||||
* Constructs an empty local database.
|
||||
@ -32,7 +45,14 @@ public class LocalDB {
|
||||
* @param sender the user that is logged in with this client
|
||||
* @since Envoy v0.1-alpha
|
||||
**/
|
||||
public LocalDB(User sender) { this.sender = sender; }
|
||||
public LocalDB(User sender) {
|
||||
this.sender = sender;
|
||||
try {
|
||||
datatypeFactory = DatatypeFactory.newInstance();
|
||||
} catch (DatatypeConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the local database and fills it with values
|
||||
@ -43,8 +63,8 @@ public class LocalDB {
|
||||
* @since Envoy v0.1-alpha
|
||||
**/
|
||||
public void initializeDBFile(File localDBDir) throws EnvoyException {
|
||||
if (localDBDir.exists() && !localDBDir.isDirectory())
|
||||
throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
|
||||
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();
|
||||
}
|
||||
@ -87,6 +107,276 @@ public class LocalDB {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
/**
|
||||
* Creates a {@link Message} object serializable to XML.
|
||||
*
|
||||
* @param textContent The content (text) of the message
|
||||
* @return prepared {@link Message} object
|
||||
*/
|
||||
public Message createMessage(String textContent, User recipient) {
|
||||
Message.Metadata metaData = objectFactory.createMessageMetadata();
|
||||
metaData.setSender(sender.getID());
|
||||
metaData.setRecipient(recipient.getID());
|
||||
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;
|
||||
}
|
||||
|
||||
private Sync unreadMessagesSync = objectFactory.createSync();
|
||||
public Sync sync = objectFactory.createSync();
|
||||
public Sync readMessages = objectFactory.createSync();
|
||||
|
||||
public Sync fillSync(long userId) {
|
||||
|
||||
addWaitingMessagesToSync();
|
||||
|
||||
getSentStateMessagesFromLocalDB();
|
||||
for (int i = 0; i < readMessages.getMessages().size(); i++) {
|
||||
sync.getMessages().add(readMessages.getMessages().get(i));
|
||||
}
|
||||
readMessages.getMessages().clear();
|
||||
|
||||
|
||||
System.out.println(sync.getMessages().size());
|
||||
return sync;
|
||||
}
|
||||
|
||||
public void applySync (Sync returnSync) {
|
||||
for (int i = 0; i < returnSync.getMessages().size(); i++) {
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) {
|
||||
// Update Local Messages with State WAITING (add Message ID and change State to
|
||||
// SENT)
|
||||
for (int j = 0; j < sync.getMessages().size(); j++) {
|
||||
if (j == i) {
|
||||
sync.getMessages()
|
||||
.get(j)
|
||||
.getMetadata()
|
||||
.setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId());
|
||||
sync.getMessages()
|
||||
.get(j)
|
||||
.getMetadata()
|
||||
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getSender() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
||||
// these are the unread Messages from the server
|
||||
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
|
||||
|
||||
}
|
||||
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getSender() == 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
|
||||
// Update Messages in localDB to state RECEIVED
|
||||
for (int j = 0; j < getChats().size(); j++) {
|
||||
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages()
|
||||
.get(i)
|
||||
.getMetadata()
|
||||
.getRecipient()) {
|
||||
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
||||
if (getChats()
|
||||
.get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) {
|
||||
// Update Message in LocalDB
|
||||
getChats()
|
||||
.get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.setState(returnSync.getMessages().get(j).getMetadata().getState());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
|
||||
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) {
|
||||
// Update local Messages to state READ
|
||||
System.out
|
||||
.println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId()
|
||||
+ "was initialized to be set to READ in localDB.");
|
||||
for (int j = 0; j < getChats().size(); j++) {
|
||||
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages()
|
||||
.get(i)
|
||||
.getMetadata()
|
||||
.getRecipient()) {
|
||||
System.out.println(
|
||||
"Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
|
||||
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
|
||||
if (getChats()
|
||||
.get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) {
|
||||
System.out.println("Message with ID: "
|
||||
+ getChats().get(j).getModel().get(k).getMetadata().getMessageId()
|
||||
+ "was selected.");
|
||||
getChats()
|
||||
.get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.setState(returnSync.getMessages().get(i).getMetadata().getState());
|
||||
System.out.println("Message State is now: " + getChats()
|
||||
.get(j)
|
||||
.getModel()
|
||||
.get(k)
|
||||
.getMetadata()
|
||||
.getState()
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Updating UserStatus of all Users in LocalDB
|
||||
for (int j = 0; j < returnSync.getUsers().size(); j++) {
|
||||
for (int k = 0; k < getChats().size(); k++) {
|
||||
if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) {
|
||||
|
||||
getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus());
|
||||
System.out.println(getChats().get(k).getRecipient().getStatus().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sync.getMessages().clear();
|
||||
sync.getUsers().clear();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a message to the "sync" Sync object.
|
||||
*
|
||||
* @param message
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addMessageToSync(Message message) { sync.getMessages().add(message); }
|
||||
|
||||
/**
|
||||
* Adds a user to the "sync" Sync object.
|
||||
*
|
||||
* @param user
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addUserToSync(User user) { sync.getUsers().add(user); }
|
||||
|
||||
/**
|
||||
* Adds the unread Messages returned from the server in the latest sync to the
|
||||
* right chats in the LocalDB.
|
||||
*
|
||||
* @param localDB
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addUnreadMessagesToLocalDB() {
|
||||
Sync unreadMessages = unreadMessagesSync;
|
||||
for (int i = 0; i < unreadMessages.getMessages().size(); i++)
|
||||
for (int j = 0; j < getChats().size(); j++)
|
||||
if (getChats().get(j).getRecipient().getID() == unreadMessages.getMessages()
|
||||
.get(i)
|
||||
.getMetadata()
|
||||
.getSender()) {
|
||||
getChats().get(j).appendMessage(unreadMessages.getMessages().get(i));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all Messages with state SENT from the LocalDB and adds them to the
|
||||
* "sync" Sync object.
|
||||
*
|
||||
* @param localDB
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void getSentStateMessagesFromLocalDB() {
|
||||
for (int i = 0; i < getChats().size(); i++) {
|
||||
for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) {
|
||||
if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.SENT) {
|
||||
addMessageToSync(getChats().get(i).getModel().get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes all Messages with State RECEIVED of a specific chat to State READ.
|
||||
* <br>
|
||||
* Adds these Messages to the "readMessages" Sync object.
|
||||
*
|
||||
* @param currentChat
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void setMessagesToRead(Chat currentChat) {
|
||||
for (int j = 0; j < currentChat.getModel().getSize(); j++) {
|
||||
if (currentChat.getModel().get(j).getMetadata().getRecipient() != currentChat.getRecipient().getID()) {
|
||||
if (currentChat.getModel().get(j).getMetadata().getState() == MessageState.RECEIVED) {
|
||||
currentChat.getModel().get(j).getMetadata().setState(MessageState.READ);
|
||||
readMessages.getMessages().add(currentChat.getModel().get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a Message with State WAITING to a specific chat in the LocalDB.
|
||||
*
|
||||
* @param message
|
||||
* @param currentChat
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); }
|
||||
|
||||
/**
|
||||
* Adds all Messages with State WAITING from the LocalDB to the Sync.
|
||||
*
|
||||
* @param localDB
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void addWaitingMessagesToSync() {
|
||||
for (int i = 0; i < getChats().size(); i++) {
|
||||
for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) {
|
||||
if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) {
|
||||
// addMessageToSync(localDB.getChats().get(i).getModel().get(j));
|
||||
System.out.println("Got Waiting Message");
|
||||
sync.getMessages().add(0, getChats().get(i).getModel().get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears the "unreadMessagesSync" Sync object.
|
||||
*
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
|
||||
|
||||
|
||||
/**
|
||||
* @return all saves {@link Chat} objects that list the client user as the
|
||||
* sender
|
||||
|
@ -30,7 +30,6 @@ import envoy.schema.Message;
|
||||
import envoy.schema.Sync;
|
||||
import envoy.schema.User;
|
||||
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-client</strong><br>
|
||||
* File: <strong>ChatWindow.java</strong><br>
|
||||
@ -156,8 +155,9 @@ public class ChatWindow extends JFrame {
|
||||
if (!messageEnterTextfield.getText().isEmpty()) try {
|
||||
|
||||
// Create and send message object
|
||||
final Message message = client.createMessage(messageEnterTextfield.getText());
|
||||
client.addWaitingMessageToLocalDB(message, currentChat);
|
||||
final Message message = localDB.createMessage(messageEnterTextfield.getText(),
|
||||
currentChat.getRecipient());
|
||||
localDB.addWaitingMessageToLocalDB(message, currentChat);
|
||||
messageList.setModel(currentChat.getModel());
|
||||
|
||||
// Clear text field
|
||||
@ -214,8 +214,6 @@ public class ChatWindow extends JFrame {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
userList.setSelectionForeground(new Color(255, 255, 255));
|
||||
userList.setSelectionBackground(new Color(102, 0, 153));
|
||||
userList.setForeground(new Color(255, 255, 255));
|
||||
@ -233,9 +231,8 @@ public class ChatWindow extends JFrame {
|
||||
contentPane.add(userList, gbc_userList);
|
||||
contentPane.revalidate();
|
||||
|
||||
|
||||
loadUsersAndChats();
|
||||
startReceiverThread(5000);
|
||||
startSyncThread(5000);
|
||||
|
||||
contentPane.revalidate();
|
||||
}
|
||||
@ -266,19 +263,22 @@ public class ChatWindow extends JFrame {
|
||||
* the server
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
private void startReceiverThread(int timeout) {
|
||||
private void startSyncThread(int timeout) {
|
||||
new Timer(timeout, (evt) -> {
|
||||
if(currentChat != null) {
|
||||
client.setMessagesToRead(currentChat);
|
||||
}
|
||||
client.sendSync(client.getSender().getID(), localDB);
|
||||
client.addUnreadMessagesToLocalDB(localDB);
|
||||
client.clearUnreadMessagesSync();
|
||||
if (currentChat != null) { localDB.setMessagesToRead(currentChat); }
|
||||
|
||||
updateUserStates();
|
||||
new Thread(() -> {
|
||||
|
||||
contentPane.revalidate();
|
||||
contentPane.repaint();
|
||||
// Synchronize
|
||||
localDB.applySync(
|
||||
client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
|
||||
localDB.addUnreadMessagesToLocalDB();
|
||||
localDB.clearUnreadMessagesSync();
|
||||
|
||||
// Update UI
|
||||
SwingUtilities
|
||||
.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
|
||||
}).start();
|
||||
}).start();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user