From fde3ae2859cef9f86c14112d41f55a492d6030ed Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Tue, 31 Dec 2019 16:38:52 +0200 Subject: [PATCH] Acquiring user list through the sender object --- src/main/java/envoy/client/Client.java | 17 +++++++++++------ src/main/java/envoy/client/LocalDB.java | 4 ---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index b87f31d..37fc456 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -3,6 +3,7 @@ package envoy.client; import java.io.Closeable; import java.io.IOException; import java.net.Socket; +import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; @@ -56,13 +57,13 @@ public class Client implements Closeable { // Register user creation processor receiver.registerProcessor(User.class, sender -> { logger.info("Acquired user object " + sender); this.sender = sender; }); + // Start receiver + new Thread(receiver).start(); + // Write login credentials logger.finest("Sending login credentials..."); SerializationUtils.writeBytesWithLength(credentials, socket.getOutputStream()); - // Start receiver - new Thread(receiver).start(); - // Wait for a maximum of five seconds to acquire the sender object long start = System.currentTimeMillis(); while (sender == null) { @@ -81,7 +82,7 @@ public class Client implements Closeable { * @since Envoy v0.3-alpha */ public void sendMessage(Message message) throws IOException { - if (!online) throw new IllegalStateException("Client is not online"); + checkOnline(); SerializationUtils.writeBytesWithLength(message, socket.getOutputStream()); } @@ -91,13 +92,17 @@ public class Client implements Closeable { * @since Envoy v0.2-alpha */ public Map getUsers() { - // TODO - return null; + checkOnline(); + Map users = new HashMap<>(); + sender.getContacts().forEach(u -> users.put(u.getName(), u)); + return users; } @Override public void close() throws IOException { if (online) socket.close(); } + private void checkOnline() { if (!online) throw new IllegalStateException("Client is not online"); } + /** * @return the sender object that represents this client. * @since Envoy v0.1-alpha diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index d550c3a..5bc5421 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -3,9 +3,7 @@ package envoy.client; import java.io.File; import java.io.IOException; import java.util.*; -import java.util.logging.Logger; -import envoy.client.util.EnvoyLog; import envoy.data.User; import envoy.util.SerializationUtils; @@ -25,8 +23,6 @@ public class LocalDB { private Map users = new HashMap<>(); private List chats = new ArrayList<>(); - private static final Logger logger = EnvoyLog.getLogger(LocalDB.class.getSimpleName()); - /** * Constructs an empty local database. To serialize any chats to the file * system, call {@link LocalDB#initializeDBFile()}.