This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/Client.java

112 lines
2.9 KiB
Java
Raw Normal View History

2019-10-06 10:45:19 +02:00
package envoy.client;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Map;
import java.util.logging.Logger;
import envoy.client.util.EnvoyLog;
import envoy.data.LoginCredentials;
import envoy.data.User;
import envoy.util.SerializationUtils;
2019-10-06 10:45:19 +02:00
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>Client.java</strong><br>
2019-10-06 10:45:19 +02:00
* Created: <strong>28 Sep 2019</strong><br>
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @author Leon Hofmeister
* @since Envoy v0.1-alpha
2019-10-06 10:45:19 +02:00
*/
public class Client {
2019-10-06 10:45:19 +02:00
private Socket socket;
private Config config = Config.getInstance();
private User sender, recipient;
private boolean online;
2019-10-06 10:45:19 +02:00
private static final Logger logger = EnvoyLog.getLogger(Client.class.getSimpleName());
/**
* Enters the online mode by acquiring a user ID from the server.
*
* @param credentials the login credentials of the user
* @throws IOException if the online mode could not be entered or the request
* failed for some other reason
* @since Envoy v0.2-alpha
*/
public void onlineInit(LoginCredentials credentials) throws IOException {
logger.info(String.format("Attempting connection to server %s:%d...", config.getServer(), config.getPort()));
socket = new Socket(config.getServer(), config.getPort());
logger.info("Successfully connected to server.");
// Write login credentials
logger.finest("Sending login credentials...");
SerializationUtils.writeBytesWithLength(credentials, socket.getOutputStream());
2019-10-06 10:45:19 +02:00
// Read response (user object)
InputStream in = socket.getInputStream();
// Read object
try {
sender = SerializationUtils.read(in, User.class);
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
online = true;
}
2019-11-09 13:25:18 +01:00
/**
* @return a {@code Map<String, User>} of all users on the server with their
* user names as keys
* @since Envoy v0.2-alpha
*/
public Map<String, User> getUsers() {
// TODO
return null;
}
/**
* @return the sender object that represents this client.
* @since Envoy v0.1-alpha
*/
public User getSender() { return sender; }
/**
* Sets the client user which is used to send messages.
*
* @param sender the client user to set
* @since Envoy v0.2-alpha
*/
public void setSender(User sender) { this.sender = sender; }
/**
* @return the current recipient of the current chat.
* @since Envoy v0.1-alpha
*/
public User getRecipient() { return recipient; }
/**
2019-12-07 10:44:25 +01:00
* Sets the recipient.
*
* @param recipient the recipient to set
* @since Envoy v0.1-alpha
*/
public void setRecipient(User recipient) { this.recipient = recipient; }
2019-12-07 10:44:25 +01:00
/**
* @return true, if a recipient is selected
* @since Envoy v0.1-alpha
*/
public boolean hasRecipient() { return recipient != null; }
/**
* @return {@code true} if a connection to the server could be established
* @since Envoy v0.2-alpha
*/
public boolean isOnline() { return online; }
2019-12-21 21:23:19 +01:00
}