2019-10-06 10:45:19 +02:00
|
|
|
package envoy.client;
|
|
|
|
|
2019-12-29 11:54:05 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.net.Socket;
|
2019-12-14 10:53:20 +01:00
|
|
|
import java.util.Map;
|
2019-12-21 18:29:59 +01:00
|
|
|
import java.util.logging.Logger;
|
2019-11-27 17:07:25 +01:00
|
|
|
|
2019-12-21 18:29:59 +01:00
|
|
|
import envoy.client.util.EnvoyLog;
|
2019-12-29 11:54:05 +01:00
|
|
|
import envoy.data.LoginCredentials;
|
2019-12-28 21:20:43 +01:00
|
|
|
import envoy.data.User;
|
2019-12-29 11:54:05 +01:00
|
|
|
import envoy.util.SerializationUtils;
|
2019-10-06 10:45:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
2019-10-12 11:19:29 +02:00
|
|
|
* File: <strong>Client.java</strong><br>
|
2019-10-06 10:45:19 +02:00
|
|
|
* Created: <strong>28 Sep 2019</strong><br>
|
2019-12-21 18:29:59 +01:00
|
|
|
*
|
2019-10-12 08:19:00 +02:00
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @author Maximilian Käfer
|
2019-10-12 14:45:58 +02:00
|
|
|
* @author Leon Hofmeister
|
2019-10-31 22:11:50 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
2019-10-06 10:45:19 +02:00
|
|
|
*/
|
2019-10-12 11:19:29 +02:00
|
|
|
public class Client {
|
2019-10-06 10:45:19 +02:00
|
|
|
|
2019-12-29 11:54:05 +01:00
|
|
|
private Socket socket;
|
|
|
|
private Config config = Config.getInstance();
|
|
|
|
private User sender, recipient;
|
|
|
|
private boolean online;
|
2019-10-06 10:45:19 +02:00
|
|
|
|
2019-12-21 18:29:59 +01:00
|
|
|
private static final Logger logger = EnvoyLog.getLogger(Client.class.getSimpleName());
|
|
|
|
|
2019-12-14 11:30:00 +01:00
|
|
|
/**
|
|
|
|
* Enters the online mode by acquiring a user ID from the server.
|
2019-12-21 18:29:59 +01:00
|
|
|
*
|
2019-12-29 11:54:05 +01:00
|
|
|
* @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
|
2019-12-14 11:30:00 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-29 11:54:05 +01:00
|
|
|
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
|
|
|
|
2019-12-29 11:54:05 +01:00
|
|
|
// Read response (user object)
|
|
|
|
InputStream in = socket.getInputStream();
|
2019-11-04 23:10:53 +01:00
|
|
|
|
2019-12-29 11:54:05 +01:00
|
|
|
// Read object
|
|
|
|
try {
|
|
|
|
sender = SerializationUtils.read(in, User.class);
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
throw new IOException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
online = true;
|
2019-11-04 23:10:53 +01:00
|
|
|
}
|
2019-11-09 13:25:18 +01:00
|
|
|
|
2019-11-04 23:10:53 +01:00
|
|
|
/**
|
2019-12-14 11:30:00 +01:00
|
|
|
* @return a {@code Map<String, User>} of all users on the server with their
|
|
|
|
* user names as keys
|
2019-12-14 10:53:20 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
2019-11-04 23:10:53 +01:00
|
|
|
*/
|
2019-12-14 10:53:20 +01:00
|
|
|
public Map<String, User> getUsers() {
|
2019-12-29 11:54:05 +01:00
|
|
|
// TODO
|
|
|
|
return null;
|
2019-10-19 15:09:32 +02:00
|
|
|
}
|
|
|
|
|
2019-10-12 17:35:58 +02:00
|
|
|
/**
|
2019-11-04 23:10:53 +01:00
|
|
|
* @return the sender object that represents this client.
|
2019-10-26 18:31:49 +02:00
|
|
|
* @since Envoy v0.1-alpha
|
2019-10-12 17:35:58 +02:00
|
|
|
*/
|
2019-10-19 12:10:52 +02:00
|
|
|
public User getSender() { return sender; }
|
|
|
|
|
2019-12-14 11:30:00 +01:00
|
|
|
/**
|
|
|
|
* Sets the client user which is used to send messages.
|
2019-12-21 18:29:59 +01:00
|
|
|
*
|
2019-12-14 11:30:00 +01:00
|
|
|
* @param sender the client user to set
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-14 10:53:20 +01:00
|
|
|
public void setSender(User sender) { this.sender = sender; }
|
|
|
|
|
2019-11-04 23:10:53 +01:00
|
|
|
/**
|
|
|
|
* @return the current recipient of the current chat.
|
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
2019-10-19 12:10:52 +02:00
|
|
|
public User getRecipient() { return recipient; }
|
|
|
|
|
2019-11-04 23:10:53 +01:00
|
|
|
/**
|
2019-12-07 10:44:25 +01:00
|
|
|
* Sets the recipient.
|
2019-12-21 18:29:59 +01:00
|
|
|
*
|
2019-12-14 11:30:00 +01:00
|
|
|
* @param recipient the recipient to set
|
2019-11-04 23:10:53 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
2019-10-19 12:10:52 +02:00
|
|
|
public void setRecipient(User recipient) { this.recipient = recipient; }
|
|
|
|
|
2019-12-07 10:44:25 +01:00
|
|
|
/**
|
2019-10-31 22:11:50 +01:00
|
|
|
* @return true, if a recipient is selected
|
2019-11-04 23:10:53 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
2019-10-19 12:10:52 +02:00
|
|
|
public boolean hasRecipient() { return recipient != null; }
|
2019-12-11 18:52:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {@code true} if a connection to the server could be established
|
2019-12-14 11:30:00 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
2019-12-11 18:52:30 +01:00
|
|
|
*/
|
|
|
|
public boolean isOnline() { return online; }
|
2019-12-21 21:23:19 +01:00
|
|
|
}
|