|
|
|
|
@ -19,8 +19,7 @@ public class UserList {
|
|
|
|
|
private final Map<Integer, PeerUser> inactiveUsers = new HashMap<>();
|
|
|
|
|
private final Map<Integer, PeerUser> activeUsers = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
private final ArrayList<UserConnectionCallback> userConnectionObservers = new ArrayList<>();
|
|
|
|
|
private final ArrayList<UserDisconnectionCallback> userDisconnectionObservers = new ArrayList<>();
|
|
|
|
|
private final ArrayList<UserConnectionCallback> newUsersObservers = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
private final DatabaseController db = new DatabaseController();
|
|
|
|
|
private final NetDiscoverer netDiscoverer = new NetDiscoverer();
|
|
|
|
|
@ -33,30 +32,19 @@ public class UserList {
|
|
|
|
|
* Discover all active users over the network. Observers are notified for each user discovered.
|
|
|
|
|
*
|
|
|
|
|
* @param errorCallback The function to call on error
|
|
|
|
|
* @see UserList#addActiveUsersObserver(UserConnectionCallback, UserDisconnectionCallback)
|
|
|
|
|
* @see UserList#addNewUserObserver(UserConnectionCallback)
|
|
|
|
|
*/
|
|
|
|
|
public void discoverActiveUsers(ErrorCallback errorCallback) {
|
|
|
|
|
netDiscoverer.discoverActiveUsers("CLAVARDATOR_BROADCAST", (ipAddr, data) -> {
|
|
|
|
|
int id = getIdFromIp(ipAddr);
|
|
|
|
|
Log.v(this.getClass().getSimpleName(), "Discovered new user: " + id);
|
|
|
|
|
|
|
|
|
|
// If already connected, do not modify
|
|
|
|
|
if (activeUsers.containsKey(id)) {
|
|
|
|
|
return;
|
|
|
|
|
final PeerUser finalUser = createNewUser(id);
|
|
|
|
|
if (finalUser != null) {
|
|
|
|
|
finalUser.connect(ipAddr, () -> {
|
|
|
|
|
notifyNewUserObservers(finalUser);
|
|
|
|
|
finalUser.addObserver(evt -> userChangeObserver(finalUser, evt));
|
|
|
|
|
}, errorCallback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PeerUser user = inactiveUsers.get(id);
|
|
|
|
|
if (user == null) {
|
|
|
|
|
// Username is set on TCP connection start
|
|
|
|
|
user = new PeerUser(id, "");
|
|
|
|
|
inactiveUsers.put(id, user);
|
|
|
|
|
}
|
|
|
|
|
PeerUser finalUser = user;
|
|
|
|
|
user.connect(ipAddr, () -> {
|
|
|
|
|
notifyConnectionObservers(finalUser);
|
|
|
|
|
finalUser.addObserver(evt -> userChangeObserver(finalUser, evt));
|
|
|
|
|
}, errorCallback);
|
|
|
|
|
|
|
|
|
|
}, errorCallback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -68,32 +56,39 @@ public class UserList {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void startUserListening(ErrorCallback errorCallback) {
|
|
|
|
|
connectionListener.acceptConnection(clientSocket -> {
|
|
|
|
|
int id = getIdFromIp(clientSocket.getInetAddress());
|
|
|
|
|
Log.v(this.getClass().getSimpleName(), "new connection from user: " + id);
|
|
|
|
|
connectionListener.acceptConnection(
|
|
|
|
|
(clientSocket) -> {
|
|
|
|
|
int id = getIdFromIp(clientSocket.getInetAddress());
|
|
|
|
|
Log.v(this.getClass().getSimpleName(), "new connection from user: " + id);
|
|
|
|
|
final PeerUser finalUser = createNewUser(id);
|
|
|
|
|
if (finalUser != null) {
|
|
|
|
|
finalUser.connect(clientSocket, () -> {
|
|
|
|
|
notifyNewUserObservers(finalUser);
|
|
|
|
|
finalUser.addObserver(evt -> userChangeObserver(finalUser, evt));
|
|
|
|
|
}, errorCallback);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
errorCallback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If already connected, warn and return
|
|
|
|
|
if (activeUsers.containsKey(id)) {
|
|
|
|
|
Log.w(getClass().getSimpleName(), "An already connected user tried to initiate a new connection: user id " + id);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
private PeerUser createNewUser(int id) {
|
|
|
|
|
// If already connected, warn and return
|
|
|
|
|
if (activeUsers.containsKey(id)) {
|
|
|
|
|
Log.w(getClass().getSimpleName(),
|
|
|
|
|
"An already connected user tried to initiate a new connection: user id " + id);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the user if already existing
|
|
|
|
|
PeerUser user = inactiveUsers.get(id);
|
|
|
|
|
// else create it
|
|
|
|
|
if (user == null) {
|
|
|
|
|
// Username is set on TCP connection start
|
|
|
|
|
user = new PeerUser(id, "");
|
|
|
|
|
inactiveUsers.put(id, user);
|
|
|
|
|
}
|
|
|
|
|
// Get the user if already existing
|
|
|
|
|
PeerUser user = inactiveUsers.get(id);
|
|
|
|
|
// else create it
|
|
|
|
|
if (user == null) {
|
|
|
|
|
// Username is set on TCP connection start
|
|
|
|
|
user = new PeerUser(id, "");
|
|
|
|
|
inactiveUsers.put(id, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PeerUser finalUser = user;
|
|
|
|
|
user.connect(clientSocket, () -> {
|
|
|
|
|
notifyConnectionObservers(finalUser);
|
|
|
|
|
finalUser.addObserver(evt -> userChangeObserver(finalUser, evt));
|
|
|
|
|
}, errorCallback);
|
|
|
|
|
|
|
|
|
|
}, errorCallback);
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void userChangeObserver(PeerUser user, PropertyChangeEvent evt) {
|
|
|
|
|
@ -117,11 +112,8 @@ public class UserList {
|
|
|
|
|
|
|
|
|
|
inactiveUsers.remove(id);
|
|
|
|
|
activeUsers.put(id, user);
|
|
|
|
|
notifyConnectionObservers(user);
|
|
|
|
|
Log.v(getClass().getSimpleName(), "State of user " + id + " updated from " +
|
|
|
|
|
oldState.toString() + " to " + newState.toString());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (oldState == PeerUser.State.CONNECTED &&
|
|
|
|
|
(newState == PeerUser.State.DISCONNECTED || newState == PeerUser.State.CONNECTING)) {
|
|
|
|
|
// Disconnection
|
|
|
|
|
@ -136,25 +128,18 @@ public class UserList {
|
|
|
|
|
|
|
|
|
|
activeUsers.remove(id);
|
|
|
|
|
inactiveUsers.put(id, user);
|
|
|
|
|
notifyDisconnectionObservers(user);
|
|
|
|
|
|
|
|
|
|
Log.v(getClass().getSimpleName(), "State of user " + id + " updated from " +
|
|
|
|
|
oldState.toString() + " to " + newState.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void addActiveUsersObserver(UserConnectionCallback connectionCallback, UserDisconnectionCallback disconnectionCallback) {
|
|
|
|
|
userConnectionObservers.add(connectionCallback);
|
|
|
|
|
userDisconnectionObservers.add(disconnectionCallback);
|
|
|
|
|
public void addNewUserObserver(UserConnectionCallback connectionCallback) {
|
|
|
|
|
newUsersObservers.add(connectionCallback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void notifyConnectionObservers(PeerUser user) {
|
|
|
|
|
userConnectionObservers.forEach(callback -> callback.onUserConnected(user));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void notifyDisconnectionObservers(PeerUser user) {
|
|
|
|
|
userDisconnectionObservers.forEach(callback -> callback.onUserDisconnected(user));
|
|
|
|
|
private void notifyNewUserObservers(PeerUser user) {
|
|
|
|
|
newUsersObservers.forEach(callback -> callback.onUserConnected(user));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|