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; package fr.insa.clavardator.chat;
import fr.insa.clavardator.users.User; import fr.insa.clavardator.users.User;
import fr.insa.clavardator.users.UserInformation;
import java.io.Serializable; import java.io.Serializable;
public class Message implements Serializable { public class Message implements Serializable {
private final String text; private final String text;
private final User sender; // Send only ids ? private final UserInformation sender;
private final User recipient; // Send only ids ? private final UserInformation recipient;
public Message(UserInformation sender, UserInformation recipient) {
this(sender, recipient, "");
}
public Message(User sender, User recipient) { public Message(User sender, User recipient) {
this(sender, recipient, ""); this(sender, recipient, "");
} }
public Message(User sender, User recipient, String text) { public Message(UserInformation sender, UserInformation recipient, String text) {
this.sender = sender; this.sender = sender;
this.recipient = recipient; this.recipient = recipient;
this.text = text; 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() { public String getText() {
return text; return text;
} }
public User getSender() { public UserInformation getSender() {
return sender; return sender;
} }
public User getRecipient() { public UserInformation getRecipient() {
return recipient; return recipient;
} }
} }

View file

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

View file

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

View file

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

View file

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