Compare commits

..

No commits in common. "3e08f1a786b2da0f77ffd74170b126ea006d17f2" and "ecf921f91e8899983df34493487701898ee9ebcf" have entirely different histories.

6 changed files with 14 additions and 110 deletions

View file

@ -1,44 +1,33 @@
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 UserInformation sender; private final User sender; // Send only ids ?
private final UserInformation recipient; private final User recipient; // Send only ids ?
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(UserInformation sender, UserInformation recipient, String text) { public Message(User sender, User 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 UserInformation getSender() { public User getSender() {
return sender; return sender;
} }
public UserInformation getRecipient() { public User getRecipient() {
return recipient; return recipient;
} }
} }

View file

@ -3,6 +3,7 @@ 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;
@ -28,7 +29,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().id == CurrentUser.getInstance().getId()) { if (message.getSender() instanceof CurrentUser) {
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,11 +21,6 @@ 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)
* *
@ -34,10 +29,6 @@ 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) -> {
@ -56,9 +47,6 @@ 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);
@ -80,7 +68,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.id; assert id == userInfo.getId();
setUsername(userInfo.getUsername()); setUsername(userInfo.getUsername());
callback.onUserConnected(); callback.onUserConnected();
@ -102,11 +90,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).id == getId(); assert ((UserInformation) msg).getId() == 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().id != id; assert ((Message) msg).getRecipient() != this;
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 {
public final int id; private 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) {
this.id = user.getId(); public int getId() {
this.username = user.getUsername(); return id;
} }
public String getUsername() { public String getUsername() {

View file

@ -33,12 +33,6 @@ 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, "");
@ -59,7 +53,6 @@ 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);

View file

@ -1,67 +0,0 @@
package fr.insa.clavardator.util;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Log {
/**
* Verbose level: 0 = nothing,
* 1: errors,
* 2: errors & warnings,
* 3: errors & warnings & debug,
* 4: all
*/
public static int verboseLevel = 0;
public static void v(String prefix, String message) {
v(prefix, message, null);
}
public static void v(String prefix, String message, Exception e) {
if (verboseLevel >= 4) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
System.out.println(sdf.format(date) + " | V | "+ prefix + ": " + message);
if (e != null)
e.printStackTrace();
}
}
public static void d(String prefix, String message) {
d(prefix, message, null);
}
public static void d(String prefix, String message, Exception e) {
if (verboseLevel >= 3) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
System.out.println(sdf.format(date) + " | D | "+ prefix + ": " + message);
if (e != null)
e.printStackTrace();
}
}
public static void w(String prefix, String message) {
w(prefix, message, null);
}
public static void w(String prefix, String message, Exception e) {
if (verboseLevel >= 2) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
System.out.println(sdf.format(date) + " | W | "+ prefix + ": " + message);
if (e != null)
e.printStackTrace();
}
}
public static void e(String prefix, String message) {
e(prefix, message, null);
}
public static void e(String prefix, String message, Exception e) {
if (verboseLevel >= 1) {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
System.out.println(sdf.format(date) + " | E | "+ prefix + " " + message);
if (e != null)
e.printStackTrace();
}
}
}