Sync
* Completely revised communication between client and server. * Added synchronization functionality. * Added Message State updates * Added UserStatus updates
This commit is contained in:
parent
239d2bb029
commit
a0dc25ba61
@ -18,12 +18,6 @@
|
|||||||
<attribute name="test" value="true"/>
|
<attribute name="test" 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="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="maven.pomderived" value="true"/>
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package envoy.client;
|
package envoy.client;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
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;
|
||||||
@ -14,11 +12,10 @@ import javax.xml.datatype.DatatypeConfigurationException;
|
|||||||
import javax.xml.datatype.DatatypeFactory;
|
import javax.xml.datatype.DatatypeFactory;
|
||||||
|
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.Message.MetaData.MessageState;
|
import envoy.schema.Message.Metadata.MessageState;
|
||||||
import envoy.schema.Messages;
|
|
||||||
import envoy.schema.ObjectFactory;
|
import envoy.schema.ObjectFactory;
|
||||||
|
import envoy.schema.Sync;
|
||||||
import envoy.schema.User;
|
import envoy.schema.User;
|
||||||
import envoy.schema.Users;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
@ -38,6 +35,10 @@ public class Client {
|
|||||||
private Config config;
|
private Config config;
|
||||||
private User sender, recipient;
|
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) {
|
public Client(Config config, String username) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
try {
|
try {
|
||||||
@ -46,43 +47,7 @@ public class Client {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
sender = getUser(username);
|
sender = getUser(username);
|
||||||
}
|
System.out.println("ID: " + sender.getID());
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a message with text content to the server.<br>
|
|
||||||
* Because sending a request is a blocking operation, it is executed
|
|
||||||
* asynchronously.
|
|
||||||
*
|
|
||||||
* @param sender name of the sender
|
|
||||||
* @param recipient name of the recipient
|
|
||||||
* @param textContent content (text) of the message
|
|
||||||
*/
|
|
||||||
public void sendMessage(Message message) {
|
|
||||||
new Thread(() -> {
|
|
||||||
// Wrap single message into messages list
|
|
||||||
Messages messages = wrapMessages(message);
|
|
||||||
|
|
||||||
// Print message XML to console
|
|
||||||
JAXBContext jc;
|
|
||||||
try {
|
|
||||||
jc = JAXBContext.newInstance("envoy.schema");
|
|
||||||
Marshaller m = jc.createMarshaller();
|
|
||||||
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
|
||||||
m.marshal(messages, System.out);
|
|
||||||
} catch (JAXBException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send message
|
|
||||||
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
|
||||||
WebTarget target = client
|
|
||||||
.target(String.format("%s:%d/envoy-server/rest/message/send", config.getServer(), config.getPort()));
|
|
||||||
|
|
||||||
Response response = target.request().post(Entity.entity(messages, "application/xml"));
|
|
||||||
System.out.println("Response code: " + response.getStatus());
|
|
||||||
response.close();
|
|
||||||
client.close();
|
|
||||||
}).start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -92,7 +57,7 @@ public class Client {
|
|||||||
* @return prepared {@link Message} object
|
* @return prepared {@link Message} object
|
||||||
*/
|
*/
|
||||||
public Message createMessage(String textContent) {
|
public Message createMessage(String textContent) {
|
||||||
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);
|
||||||
@ -103,16 +68,33 @@ public class Client {
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a {@link Sync} with all users on the server.
|
||||||
|
* @return Sync - List of all users on the server.
|
||||||
|
* @since Envoy v0.1-alpha
|
||||||
|
*/
|
||||||
|
public Sync getUsersListXml() {
|
||||||
|
Sync sendSync = objectFactory.createSync();
|
||||||
|
User user = objectFactory.createUser();
|
||||||
|
user.setID(-1);
|
||||||
|
sendSync.getUsers().add(user);
|
||||||
|
|
||||||
public Users getUsersListXml() { return get(String.format("%s:%d/envoy-server/rest/user", config.getServer(), config.getPort()), Users.class); }
|
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();
|
||||||
|
return returnSendSync;
|
||||||
|
|
||||||
public Messages getUnreadMessages(long userId) {
|
|
||||||
return get(String.format("%s:%d/envoy-server/rest/message/receive?userId=%d", config.getServer(), config.getPort(), userId), Messages.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -123,52 +105,358 @@ public class Client {
|
|||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
private User getUser(String name) {
|
private User getUser(String name) {
|
||||||
return get(String.format("%s:%d/envoy-server/rest/user/sender?name=%s", config.getServer(), config.getPort(), name), Users.class).getUser()
|
Sync senderSync = objectFactory.createSync();
|
||||||
.get(0);
|
User user = objectFactory.createUser();
|
||||||
}
|
user.setName(name);
|
||||||
|
senderSync.getUsers().add(user);
|
||||||
|
|
||||||
/**
|
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
||||||
* Invokes the GET method of a web service.
|
WebTarget target = client.target(String
|
||||||
*
|
.format("%s:%d/envoy-server/rest/sync/syncData?userId=%d", config.getServer(), config.getPort(), 0));
|
||||||
* @param <T> the type of the object returned by the web service
|
|
||||||
* @param uri the URI of the web service
|
Response response = target.request().post(Entity.entity(senderSync, "application/xml"));
|
||||||
* @param responseClass the class of the object returned by the web service
|
User returnSender = objectFactory.createUser();
|
||||||
* @return the object returned by the web service
|
Sync returnSenderSync = response.readEntity(Sync.class);
|
||||||
* @since Envoy v0.1-alpha
|
if (returnSenderSync.getUsers().size() == 1) {
|
||||||
*/
|
returnSender = returnSenderSync.getUsers().get(0);
|
||||||
private <T> T get(String uri, Class<T> responseClass) {
|
} else {
|
||||||
javax.ws.rs.client.Client client = ClientBuilder.newClient();
|
System.out.println("ERROR exiting...");
|
||||||
WebTarget target = client.target(uri);
|
}
|
||||||
Response response = target.request("application/xml").get();
|
|
||||||
T responseObject = response.readEntity(responseClass);
|
|
||||||
System.out.println("Response code: " + response.getStatus());
|
|
||||||
response.close();
|
response.close();
|
||||||
client.close();
|
client.close();
|
||||||
return responseObject;
|
return returnSender;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps one or more {@link Message} objects into a {@link Messages} object.
|
* 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 messages the {@link Message} objects to wrap
|
* @param userId
|
||||||
* @return {@link Messages} object with all messages as its children
|
* @param localDB
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
private Messages wrapMessages(Message... messages) {
|
public void sendSync(long userId, LocalDB localDB) {
|
||||||
Messages wrapper = objectFactory.createMessages();
|
new Thread(() -> {
|
||||||
wrapper.getMessage().addAll(Arrays.asList(messages));
|
// Print sync XML to console
|
||||||
return wrapper;
|
JAXBContext jc;
|
||||||
|
try {
|
||||||
|
jc = JAXBContext.newInstance("envoy.schema");
|
||||||
|
Marshaller m = jc.createMarshaller();
|
||||||
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||||
|
m.marshal(sync, System.out);
|
||||||
|
} 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());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the sender object that represents this client
|
* 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 the sender object that represents this client.
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public User getSender() { return sender; }
|
public User getSender() { return sender; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the current recipient of the current chat.
|
||||||
|
* @since Envoy v0.1-alpha
|
||||||
|
*/
|
||||||
public User getRecipient() { return recipient; }
|
public User getRecipient() { return recipient; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the recipient.
|
||||||
|
*
|
||||||
|
* @param recipient
|
||||||
|
* @since Envoy v0.1-alpha
|
||||||
|
*/
|
||||||
public void setRecipient(User recipient) { this.recipient = recipient; }
|
public void setRecipient(User recipient) { this.recipient = recipient; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return boolean weather the recipient is currently set.
|
||||||
|
* @since Envoy v0.1-alpha
|
||||||
|
*/
|
||||||
public boolean hasRecipient() { return recipient != null; }
|
public boolean hasRecipient() { return recipient != null; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the "unreadMessagesSync" Sync object.
|
||||||
|
*
|
||||||
|
* @since Envoy v0.1-alpha
|
||||||
|
*/
|
||||||
|
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
|
||||||
}
|
}
|
@ -27,9 +27,9 @@ import envoy.client.Chat;
|
|||||||
import envoy.client.Client;
|
import envoy.client.Client;
|
||||||
import envoy.client.LocalDB;
|
import envoy.client.LocalDB;
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.Messages;
|
import envoy.schema.Sync;
|
||||||
import envoy.schema.User;
|
import envoy.schema.User;
|
||||||
import envoy.schema.Users;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-client</strong><br>
|
* Project: <strong>envoy-client</strong><br>
|
||||||
@ -147,18 +147,17 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
postButton.addActionListener((evt) -> {
|
postButton.addActionListener((evt) -> {
|
||||||
if (!client.hasRecipient()) {
|
if (!client.hasRecipient()) {
|
||||||
JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE);
|
JOptionPane.showMessageDialog(this,
|
||||||
return;
|
"Please select a recipient!",
|
||||||
|
"Cannot send message",
|
||||||
|
JOptionPane.INFORMATION_MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!messageEnterTextfield.getText().isEmpty()) try {
|
if (!messageEnterTextfield.getText().isEmpty()) try {
|
||||||
|
|
||||||
// Create and send message object
|
// Create and send message object
|
||||||
final Message message = client.createMessage(messageEnterTextfield.getText());
|
final Message message = client.createMessage(messageEnterTextfield.getText());
|
||||||
client.sendMessage(message);
|
client.addWaitingMessageToLocalDB(message, currentChat);
|
||||||
|
|
||||||
// Append message object to chat
|
|
||||||
currentChat.appendMessage(message);
|
|
||||||
messageList.setModel(currentChat.getModel());
|
messageList.setModel(currentChat.getModel());
|
||||||
|
|
||||||
// Clear text field
|
// Clear text field
|
||||||
@ -200,16 +199,23 @@ public class ChatWindow extends JFrame {
|
|||||||
final User user = selectedUserList.getSelectedValue();
|
final User user = selectedUserList.getSelectedValue();
|
||||||
client.setRecipient(user);
|
client.setRecipient(user);
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
client.setRecipient(user);
|
client.setRecipient(user);
|
||||||
|
|
||||||
textPane.setText(currentChat.getRecipient().getName());
|
textPane.setText(currentChat.getRecipient().getName());
|
||||||
|
|
||||||
messageList.setModel(currentChat.getModel());
|
messageList.setModel(currentChat.getModel());
|
||||||
contentPane.revalidate();
|
contentPane.revalidate();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
userList.setSelectionForeground(new Color(255, 255, 255));
|
userList.setSelectionForeground(new Color(255, 255, 255));
|
||||||
userList.setSelectionBackground(new Color(102, 0, 153));
|
userList.setSelectionBackground(new Color(102, 0, 153));
|
||||||
userList.setForeground(new Color(255, 255, 255));
|
userList.setForeground(new Color(255, 255, 255));
|
||||||
@ -227,6 +233,7 @@ public class ChatWindow extends JFrame {
|
|||||||
contentPane.add(userList, gbc_userList);
|
contentPane.add(userList, gbc_userList);
|
||||||
contentPane.revalidate();
|
contentPane.revalidate();
|
||||||
|
|
||||||
|
|
||||||
loadUsersAndChats();
|
loadUsersAndChats();
|
||||||
startReceiverThread(5000);
|
startReceiverThread(5000);
|
||||||
|
|
||||||
@ -239,9 +246,9 @@ public class ChatWindow extends JFrame {
|
|||||||
*/
|
*/
|
||||||
private void loadUsersAndChats() {
|
private void loadUsersAndChats() {
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
Users users = client.getUsersListXml();
|
Sync users = client.getUsersListXml();
|
||||||
DefaultListModel<User> userListModel = new DefaultListModel<>();
|
DefaultListModel<User> userListModel = new DefaultListModel<>();
|
||||||
users.getUser().forEach(user -> {
|
users.getUsers().forEach(user -> {
|
||||||
userListModel.addElement(user);
|
userListModel.addElement(user);
|
||||||
|
|
||||||
// Check if user exists in local DB
|
// Check if user exists in local DB
|
||||||
@ -253,18 +260,34 @@ public class ChatWindow extends JFrame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks for new messages and adds them to the chat list.
|
* For detailed information see Javadoc of corresponding methods.
|
||||||
*
|
*
|
||||||
* @param timeout the amount of time that passes between two requests sent to
|
* @param timeout the amount of time that passes between two requests sent to
|
||||||
* the server
|
* the server
|
||||||
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
private void startReceiverThread(int timeout) {
|
private void startReceiverThread(int timeout) {
|
||||||
new Timer(timeout, (evt) -> {
|
new Timer(timeout, (evt) -> {
|
||||||
Messages unreadMessages = client.getUnreadMessages(client.getSender().getID());
|
if(currentChat != null) {
|
||||||
for (int i = 0; i < unreadMessages.getMessage().size(); i++)
|
client.setMessagesToRead(currentChat);
|
||||||
for (int j = 0; j < localDB.getChats().size(); j++)
|
}
|
||||||
if (localDB.getChats().get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender())
|
client.sendSync(client.getSender().getID(), localDB);
|
||||||
localDB.getChats().get(j).appendMessage(unreadMessages.getMessage().get(i));
|
client.addUnreadMessagesToLocalDB(localDB);
|
||||||
}).start();
|
client.clearUnreadMessagesSync();
|
||||||
|
|
||||||
|
for (int i = 0; i < userList.getModel().getSize(); i++) {
|
||||||
|
for (int j = 0; j < localDB.getChats().size(); j++) {
|
||||||
|
if(userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID()) {
|
||||||
|
userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
contentPane.revalidate();
|
||||||
|
contentPane.repaint();
|
||||||
|
userList.revalidate();
|
||||||
|
userList.repaint();
|
||||||
|
}).start();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -18,6 +18,7 @@ import envoy.schema.Message;
|
|||||||
* Created: <strong>19 Oct 2019</strong><br>
|
* Created: <strong>19 Oct 2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
|
* @author Maximilian Käfer
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public class MessageListRenderer extends JLabel implements ListCellRenderer<Message> {
|
public class MessageListRenderer extends JLabel implements ListCellRenderer<Message> {
|
||||||
@ -38,14 +39,16 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer<Mess
|
|||||||
setOpaque(true);
|
setOpaque(true);
|
||||||
|
|
||||||
final String text = value.getContent().get(0).getText();
|
final String text = value.getContent().get(0).getText();
|
||||||
final String date = value.getMetaData().getDate() == null ? ""
|
final String state = value.getMetadata().getState().toString();
|
||||||
|
final String date = value.getMetadata().getDate() == null ? ""
|
||||||
: new SimpleDateFormat("dd.MM.yyyy hh:mm ")
|
: new SimpleDateFormat("dd.MM.yyyy hh:mm ")
|
||||||
.format(value.getMetaData().getDate().toGregorianCalendar().getTime());
|
.format(value.getMetadata().getDate().toGregorianCalendar().getTime());
|
||||||
|
|
||||||
setText(String.format(
|
setText(String.format(
|
||||||
"<html><p style=\"color:#d2d235\"><b><small>%s</b></small><br><p style=\"color:white\">%s</html>",
|
"<html><p style=\"color:#d2d235\"><b><small>%s</b></small><br><p style=\"color:white\">%s :%s</html>",
|
||||||
date,
|
date,
|
||||||
text));
|
text,
|
||||||
|
state));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,6 +7,7 @@ import javax.swing.JList;
|
|||||||
import javax.swing.ListCellRenderer;
|
import javax.swing.ListCellRenderer;
|
||||||
|
|
||||||
import envoy.schema.User;
|
import envoy.schema.User;
|
||||||
|
import envoy.schema.User.UserStatus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines how the {@code UserList} is displayed.
|
* Defines how the {@code UserList} is displayed.
|
||||||
@ -16,6 +17,7 @@ import envoy.schema.User;
|
|||||||
* Created: <strong>12 Oct 2019</strong><br>
|
* Created: <strong>12 Oct 2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
|
* @author Maximilian Käfer
|
||||||
* @since Envoy v0.1-alpha
|
* @since Envoy v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public class UserListRenderer extends JLabel implements ListCellRenderer<User> {
|
public class UserListRenderer extends JLabel implements ListCellRenderer<User> {
|
||||||
@ -36,8 +38,27 @@ public class UserListRenderer extends JLabel implements ListCellRenderer<User> {
|
|||||||
// Enable background rendering
|
// Enable background rendering
|
||||||
setOpaque(true);
|
setOpaque(true);
|
||||||
|
|
||||||
setText(value.getName());
|
|
||||||
setFont(list.getFont());
|
final String name = value.getName();
|
||||||
|
final UserStatus status = value.getStatus();
|
||||||
|
|
||||||
|
switch (status) {
|
||||||
|
case ONLINE:
|
||||||
|
setText(String.format(
|
||||||
|
"<html><p style=\"color:#03fc20\"><b><small>%s</b></small><br><p style=\"color:white\">%s</html>",
|
||||||
|
status,
|
||||||
|
name));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case OFFLINE:
|
||||||
|
setText(String.format(
|
||||||
|
"<html><p style=\"color:#fc0303\"><b><small>%s</b></small><br><p style=\"color:white\">%s</html>",
|
||||||
|
status,
|
||||||
|
name));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user