Merge pull request #17 from informatik-ag-ngl/b/user_load

Reduced the number of user list requests to one
This commit is contained in:
Kai S. K. Engelbart 2019-10-26 21:15:24 +02:00 committed by GitHub
commit e492933dcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 64 deletions

View File

@ -60,7 +60,7 @@ public class Client {
public void sendMessage(Message message) { public void sendMessage(Message message) {
new Thread(() -> { new Thread(() -> {
// Wrap single message into messages list // Wrap single message into messages list
Messages messages = wrapMessage(message); Messages messages = wrapMessages(message);
// Print message XML to console // Print message XML to console
JAXBContext jc; JAXBContext jc;
@ -109,22 +109,10 @@ public class Client {
return message; return message;
} }
public Messages wrapMessage(Message... messages) { public Users getUsersListXml() { return get(String.format("%s:%d/envoy-server/rest/user", config.getServer(), config.getPort()), Users.class); }
Messages wrapper = objectFactory.createMessages();
wrapper.getMessage().addAll(Arrays.asList(messages));
return wrapper;
}
public Users getUsersListXml() { public Messages getUnreadMessages(long userId) {
javax.ws.rs.client.Client client = ClientBuilder.newClient(); return get(String.format("%s:%d/envoy-server/rest/message/receive?userId=%d", config.getServer(), config.getPort(), userId), Messages.class);
WebTarget target = client
.target(String.format("%s:%d/envoy-server/rest/user", config.getServer(), config.getPort()));
Response response = target.request("application/xml").get();
Users users = response.readEntity(Users.class);
System.out.println("Response code: " + response.getStatus());
response.close();
client.close();
return users;
} }
/** /**
@ -132,35 +120,49 @@ public class Client {
* *
* @param name - the name of the {@link User} * @param name - the name of the {@link User}
* @return a {@link User} with the specified name * @return a {@link User} with the specified name
* @since Envoy 0.1 * @since Envoy v0.1-alpha
*/ */
private User getUser(String name) { private User getUser(String name) {
javax.ws.rs.client.Client client = ClientBuilder.newClient(); return get(String.format("%s:%d/envoy-server/rest/user/sender?name=%s", config.getServer(), config.getPort(), name), Users.class).getUser()
WebTarget target = client.target(String .get(0);
.format("%s:%d/envoy-server/rest/user/sender?name=%s", config.getServer(), config.getPort(), name));
Response response = target.request("application/xml").get();
Users users = response.readEntity(Users.class);
System.out.println("Response code: " + response.getStatus());
response.close();
client.close();
return users.getUser().get(0);
} }
public Messages getUnreadMessages(long userId) { /**
* Invokes the GET method of a web service.
*
* @param <T> the type of the object returned by the web service
* @param uri the URI of the web service
* @param responseClass the class of the object returned by the web service
* @return the object returned by the web service
* @since Envoy v0.1-alpha
*/
private <T> T get(String uri, Class<T> responseClass) {
javax.ws.rs.client.Client client = ClientBuilder.newClient(); javax.ws.rs.client.Client client = ClientBuilder.newClient();
WebTarget target = client.target(String WebTarget target = client.target(uri);
.format("%s:%d/envoy-server/rest/message/receive?userId=%d", config.getServer(), config.getPort(), userId));
Response response = target.request("application/xml").get(); Response response = target.request("application/xml").get();
Messages unreadMessages = response.readEntity(Messages.class); T responseObject = response.readEntity(responseClass);
System.out.println("Response code: " + response.getStatus()); System.out.println("Response code: " + response.getStatus());
response.close(); response.close();
client.close(); client.close();
return unreadMessages; return responseObject;
}
/**
* Wraps one or more {@link Message} objects into a {@link Messages} object.
*
* @param messages the {@link Message} objects to wrap
* @return {@link Messages} object with all messages as its children
* @since Envoy v0.1-alpha
*/
private Messages wrapMessages(Message... messages) {
Messages wrapper = objectFactory.createMessages();
wrapper.getMessage().addAll(Arrays.asList(messages));
return wrapper;
} }
/** /**
* @return the sender object that represents this client * @return the sender object that represents this client
* @since Envoy 0.1 * @since Envoy v0.1-alpha
*/ */
public User getSender() { return sender; } public User getSender() { return sender; }

View File

@ -48,18 +48,13 @@ public class ChatWindow extends JFrame {
private Client client; private Client client;
private DefaultListModel<Message> messageListModel = new DefaultListModel<>(); private JList<User> userList = new JList<>();
private List<Chat> partnerChatList = new ArrayList<Chat>(); private List<Chat> partnerChatList = new ArrayList<Chat>();
private Chat currentChat; private Chat currentChat;
public ChatWindow(Client client) { public ChatWindow(Client client) {
this.client = client; this.client = client;
// Initialize chat list and current chat
Users chatUsers = client.getUsersListXml();
chatUsers.getUser().forEach(user -> partnerChatList.add(new Chat(user)));
if (partnerChatList.size() > 0) currentChat = partnerChatList.get(0);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800); setBounds(100, 100, 600, 800);
setTitle("Envoy"); setTitle("Envoy");
@ -86,6 +81,7 @@ public class ChatWindow extends JFrame {
messageList.setForeground(new Color(255, 255, 255)); messageList.setForeground(new Color(255, 255, 255));
messageList.setBackground(new Color(51, 51, 51)); messageList.setBackground(new Color(51, 51, 51));
DefaultListModel<Message> messageListModel = new DefaultListModel<>();
messageList.setModel(messageListModel); messageList.setModel(messageListModel);
messageList.setFont(new Font("Arial", Font.PLAIN, 17)); messageList.setFont(new Font("Arial", Font.PLAIN, 17));
messageList.setFixedCellHeight(60); messageList.setFixedCellHeight(60);
@ -142,10 +138,7 @@ public class ChatWindow extends JFrame {
postButton.addActionListener((evt) -> { postButton.addActionListener((evt) -> {
if (!client.hasRecipient()) { if (!client.hasRecipient()) {
JOptionPane.showMessageDialog(this, JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE);
"Please select a recipient!",
"Cannot send message",
JOptionPane.INFORMATION_MESSAGE);
return; return;
} }
@ -189,7 +182,6 @@ public class ChatWindow extends JFrame {
gbc_partnerName.insets = new Insets(0, 10, 0, 10); gbc_partnerName.insets = new Insets(0, 10, 0, 10);
contentPane.add(textPane, gbc_partnerName); contentPane.add(textPane, gbc_partnerName);
JList<User> userList = new JList<>();
userList.setCellRenderer(new UserListRenderer()); userList.setCellRenderer(new UserListRenderer());
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
userList.addListSelectionListener((listSelectionEvent) -> { userList.addListSelectionListener((listSelectionEvent) -> {
@ -199,10 +191,7 @@ public class ChatWindow extends JFrame {
final User user = selectedUserList.getSelectedValue(); final User user = selectedUserList.getSelectedValue();
client.setRecipient(user); client.setRecipient(user);
currentChat = partnerChatList.stream() currentChat = partnerChatList.stream().filter(chat -> chat.getRecipient().getID() == user.getID()).findFirst().get();
.filter(chat -> chat.getRecipient().getID() == user.getID())
.findFirst()
.get();
client.setRecipient(user); client.setRecipient(user);
@ -229,32 +218,38 @@ public class ChatWindow extends JFrame {
contentPane.add(userList, gbc_userList); contentPane.add(userList, gbc_userList);
contentPane.revalidate(); contentPane.revalidate();
loadUserList(userList); loadUsersAndChats();
startReceiverThread(5000);
new Timer(5000, (evt) -> {
Messages unreadMessages = client.getUnreadMessages(client.getSender().getID());
for (int i = 0; i < unreadMessages.getMessage().size(); i++)
for (int j = 0; j < partnerChatList.size(); j++)
if (partnerChatList.get(j)
.getRecipient()
.getID() == unreadMessages.getMessage().get(i).getMetaData().getSender())
partnerChatList.get(j).appendMessage(unreadMessages.getMessage().get(i));
}).start();
contentPane.revalidate(); contentPane.revalidate();
} }
/** /**
* Initializes the elements of the user list by downloading them from the * Initializes the elements of the user list by downloading them from the
* server. * server.
*
* @param userList The {@link JList} to put the elements in
*/ */
private void loadUserList(JList<User> userList) { private void loadUsersAndChats() {
new Thread(() -> { new Thread(() -> {
Users users = client.getUsersListXml(); Users users = client.getUsersListXml();
DefaultListModel<User> userListModel = new DefaultListModel<>(); DefaultListModel<User> userListModel = new DefaultListModel<>();
users.getUser().forEach(user -> userListModel.addElement(user)); users.getUser().forEach(user -> { userListModel.addElement(user); partnerChatList.add(new Chat(user)); });
SwingUtilities.invokeLater(() -> userList.setModel(userListModel)); SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
}).start(); }).start();
} }
/**
* Checks for new messages and adds them to the chat list.
*
* @param timeout the amount of time that passes between two requests sent to
* the server
*/
private void startReceiverThread(int timeout) {
new Timer(timeout, (evt) -> {
Messages unreadMessages = client.getUnreadMessages(client.getSender().getID());
for (int i = 0; i < unreadMessages.getMessage().size(); i++)
for (int j = 0; j < partnerChatList.size(); j++)
if (partnerChatList.get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender())
partnerChatList.get(j).appendMessage(unreadMessages.getMessage().get(i));
}).start();
}
} }