Acquiring user list through the sender object

This commit is contained in:
Kai S. K. Engelbart 2019-12-31 16:38:52 +02:00
parent 1fec53f35a
commit fde3ae2859
2 changed files with 11 additions and 10 deletions

View File

@ -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<String, User> getUsers() {
// TODO
return null;
checkOnline();
Map<String, User> 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

View File

@ -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<String, User> users = new HashMap<>();
private List<Chat> 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()}.