Change User to UserInformation in Message

This commit is contained in:
Yohan Simard 2020-12-14 15:26:20 +01:00
parent ecf921f91e
commit 0ff7d63a4b
5 changed files with 43 additions and 14 deletions

View file

@ -1,33 +1,44 @@
package fr.insa.clavardator.chat;
import fr.insa.clavardator.users.User;
import fr.insa.clavardator.users.UserInformation;
import java.io.Serializable;
public class Message implements Serializable {
private final String text;
private final User sender; // Send only ids ?
private final User recipient; // Send only ids ?
private final UserInformation sender;
private final UserInformation recipient;
public Message(UserInformation sender, UserInformation recipient) {
this(sender, recipient, "");
}
public Message(User sender, User recipient) {
this(sender, recipient, "");
}
public Message(User sender, User recipient, String text) {
public Message(UserInformation sender, UserInformation recipient, String text) {
this.sender = sender;
this.recipient = recipient;
this.text = text;
}
public Message(User sender, User recipient, String text) {
this.sender = new UserInformation(sender);
this.recipient = new UserInformation(recipient);
this.text = text;
}
public String getText() {
return text;
}
public User getSender() {
public UserInformation getSender() {
return sender;
}
public User getRecipient() {
public UserInformation getRecipient() {
return recipient;
}
}

View file

@ -3,7 +3,6 @@ package fr.insa.clavardator.ui.chat;
import com.jfoenix.controls.JFXButton;
import fr.insa.clavardator.chat.Message;
import fr.insa.clavardator.users.CurrentUser;
import fr.insa.clavardator.users.User;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
@ -29,7 +28,7 @@ public class MessageListItemController implements Initializable {
public void setMessage(Message message) {
button.setText(message.getText());
if (message.getSender() instanceof CurrentUser) {
if (message.getSender().id == CurrentUser.getInstance().getId()) {
container.setAlignment(Pos.CENTER_RIGHT);
button.getStyleClass().add("message-self");
} else {

View file

@ -21,6 +21,11 @@ public class PeerUser extends User implements Comparable<PeerUser> {
history = new ChatHistory(this);
}
public PeerUser(int id) {
super(id);
history = new ChatHistory(this);
}
/**
* Asynchronously connects to the user and receives its information (id, username)
*
@ -29,6 +34,10 @@ public class PeerUser extends User implements Comparable<PeerUser> {
* @param errorCallback The function to call on socket error
*/
public void connect(InetAddress ipAddr, UserConnectedCallback callback, ErrorCallback errorCallback) {
if (connection != null && connection.isOpen()) {
connection.close();
}
// Connect to the peer
setState(State.CONNECTING);
connection = new PeerConnection(ipAddr, (thisConnection) -> {
@ -47,6 +56,9 @@ public class PeerUser extends User implements Comparable<PeerUser> {
* @param errorCallback The function to call on socket error
*/
public void connect(Socket socket, UserConnectedCallback callback, ErrorCallback errorCallback) {
if (connection != null && connection.isOpen()) {
connection.close();
}
setState(State.CONNECTING);
connection = new PeerConnection(socket);
@ -68,7 +80,7 @@ public class PeerUser extends User implements Comparable<PeerUser> {
assert msg instanceof UserInformation;
UserInformation userInfo = (UserInformation) msg;
// TODO : Check username unique
assert id == userInfo.getId();
assert id == userInfo.id;
setUsername(userInfo.getUsername());
callback.onUserConnected();
@ -90,11 +102,11 @@ public class PeerUser extends User implements Comparable<PeerUser> {
connection.receive(
msg -> {
if (msg.getClass().isInstance(UserInformation.class)) {
assert ((UserInformation) msg).getId() == getId();
assert ((UserInformation) msg).id == getId();
setUsername(((UserInformation) msg).getUsername());
} else if (msg.getClass().isInstance(Message.class)) {
assert ((Message) msg).getRecipient() != this;
assert ((Message) msg).getRecipient().id != id;
history.addMessage((Message) msg);
}
},

View file

@ -3,16 +3,16 @@ package fr.insa.clavardator.users;
import java.io.Serializable;
public class UserInformation implements Serializable {
private final int id;
public final int id;
private final String username;
public UserInformation(int id, String username) {
this.id = id;
this.username = username;
}
public int getId() {
return id;
public UserInformation(User user) {
this.id = user.getId();
this.username = user.getUsername();
}
public String getUsername() {

View file

@ -33,6 +33,12 @@ public class UserList {
public void discoverActiveUsers(ErrorCallback errorCallback) {
netDiscoverer.discoverActiveUsers("CLAVARDATOR_BROADCAST", (ipAddr, data) -> {
int id = getIdFromIp(ipAddr);
// If already connected, do not modify
if (activeUsers.containsKey(id)) {
return;
}
PeerUser user = inactiveUsers.get(id);
if (user == null) {
user = new PeerUser(id, "");
@ -53,6 +59,7 @@ public class UserList {
return ipAddr.hashCode();
}
public void addActiveUsersObserver(UserConnectionCallback connectionCallback, UserDisconnectionCallback disconnectionCallback) {
userConnectionObservers.add(connectionCallback);
userDisconnectionObservers.add(disconnectionCallback);