|
@@ -19,8 +19,7 @@ public class UserList {
|
19
|
19
|
private final Map<Integer, PeerUser> inactiveUsers = new HashMap<>();
|
20
|
20
|
private final Map<Integer, PeerUser> activeUsers = new HashMap<>();
|
21
|
21
|
|
22
|
|
- private final ArrayList<UserConnectionCallback> userConnectionObservers = new ArrayList<>();
|
23
|
|
- private final ArrayList<UserDisconnectionCallback> userDisconnectionObservers = new ArrayList<>();
|
|
22
|
+ private final ArrayList<UserConnectionCallback> newUsersObservers = new ArrayList<>();
|
24
|
23
|
|
25
|
24
|
private final DatabaseController db = new DatabaseController();
|
26
|
25
|
private final NetDiscoverer netDiscoverer = new NetDiscoverer();
|
|
@@ -33,30 +32,19 @@ public class UserList {
|
33
|
32
|
* Discover all active users over the network. Observers are notified for each user discovered.
|
34
|
33
|
*
|
35
|
34
|
* @param errorCallback The function to call on error
|
36
|
|
- * @see UserList#addActiveUsersObserver(UserConnectionCallback, UserDisconnectionCallback)
|
|
35
|
+ * @see UserList#addNewUserObserver(UserConnectionCallback)
|
37
|
36
|
*/
|
38
|
37
|
public void discoverActiveUsers(ErrorCallback errorCallback) {
|
39
|
38
|
netDiscoverer.discoverActiveUsers("CLAVARDATOR_BROADCAST", (ipAddr, data) -> {
|
40
|
39
|
int id = getIdFromIp(ipAddr);
|
41
|
40
|
Log.v(this.getClass().getSimpleName(), "Discovered new user: " + id);
|
42
|
|
-
|
43
|
|
- // If already connected, do not modify
|
44
|
|
- if (activeUsers.containsKey(id)) {
|
45
|
|
- return;
|
|
41
|
+ final PeerUser finalUser = createNewUser(id);
|
|
42
|
+ if (finalUser != null) {
|
|
43
|
+ finalUser.connect(ipAddr, () -> {
|
|
44
|
+ notifyNewUserObservers(finalUser);
|
|
45
|
+ finalUser.addObserver(evt -> userChangeObserver(finalUser, evt));
|
|
46
|
+ }, errorCallback);
|
46
|
47
|
}
|
47
|
|
-
|
48
|
|
- PeerUser user = inactiveUsers.get(id);
|
49
|
|
- if (user == null) {
|
50
|
|
- // Username is set on TCP connection start
|
51
|
|
- user = new PeerUser(id, "");
|
52
|
|
- inactiveUsers.put(id, user);
|
53
|
|
- }
|
54
|
|
- PeerUser finalUser = user;
|
55
|
|
- user.connect(ipAddr, () -> {
|
56
|
|
- notifyConnectionObservers(finalUser);
|
57
|
|
- finalUser.addObserver(evt -> userChangeObserver(finalUser, evt));
|
58
|
|
- }, errorCallback);
|
59
|
|
-
|
60
|
48
|
}, errorCallback);
|
61
|
49
|
}
|
62
|
50
|
|
|
@@ -68,32 +56,39 @@ public class UserList {
|
68
|
56
|
}
|
69
|
57
|
|
70
|
58
|
public void startUserListening(ErrorCallback errorCallback) {
|
71
|
|
- connectionListener.acceptConnection(clientSocket -> {
|
72
|
|
- int id = getIdFromIp(clientSocket.getInetAddress());
|
73
|
|
- Log.v(this.getClass().getSimpleName(), "new connection from user: " + id);
|
74
|
|
-
|
75
|
|
- // If already connected, warn and return
|
76
|
|
- if (activeUsers.containsKey(id)) {
|
77
|
|
- Log.w(getClass().getSimpleName(), "An already connected user tried to initiate a new connection: user id " + id);
|
78
|
|
- return;
|
79
|
|
- }
|
|
59
|
+ connectionListener.acceptConnection(
|
|
60
|
+ (clientSocket) -> {
|
|
61
|
+ int id = getIdFromIp(clientSocket.getInetAddress());
|
|
62
|
+ Log.v(this.getClass().getSimpleName(), "new connection from user: " + id);
|
|
63
|
+ final PeerUser finalUser = createNewUser(id);
|
|
64
|
+ if (finalUser != null) {
|
|
65
|
+ finalUser.connect(clientSocket, () -> {
|
|
66
|
+ notifyNewUserObservers(finalUser);
|
|
67
|
+ finalUser.addObserver(evt -> userChangeObserver(finalUser, evt));
|
|
68
|
+ }, errorCallback);
|
|
69
|
+ }
|
|
70
|
+ },
|
|
71
|
+ errorCallback);
|
|
72
|
+ }
|
80
|
73
|
|
81
|
|
- // Get the user if already existing
|
82
|
|
- PeerUser user = inactiveUsers.get(id);
|
83
|
|
- // else create it
|
84
|
|
- if (user == null) {
|
85
|
|
- // Username is set on TCP connection start
|
86
|
|
- user = new PeerUser(id, "");
|
87
|
|
- inactiveUsers.put(id, user);
|
88
|
|
- }
|
|
74
|
+ private PeerUser createNewUser(int id) {
|
|
75
|
+ // If already connected, warn and return
|
|
76
|
+ if (activeUsers.containsKey(id)) {
|
|
77
|
+ Log.w(getClass().getSimpleName(),
|
|
78
|
+ "An already connected user tried to initiate a new connection: user id " + id);
|
|
79
|
+ return null;
|
|
80
|
+ }
|
89
|
81
|
|
90
|
|
- PeerUser finalUser = user;
|
91
|
|
- user.connect(clientSocket, () -> {
|
92
|
|
- notifyConnectionObservers(finalUser);
|
93
|
|
- finalUser.addObserver(evt -> userChangeObserver(finalUser, evt));
|
94
|
|
- }, errorCallback);
|
|
82
|
+ // Get the user if already existing
|
|
83
|
+ PeerUser user = inactiveUsers.get(id);
|
|
84
|
+ // else create it
|
|
85
|
+ if (user == null) {
|
|
86
|
+ // Username is set on TCP connection start
|
|
87
|
+ user = new PeerUser(id, "");
|
|
88
|
+ inactiveUsers.put(id, user);
|
|
89
|
+ }
|
95
|
90
|
|
96
|
|
- }, errorCallback);
|
|
91
|
+ return user;
|
97
|
92
|
}
|
98
|
93
|
|
99
|
94
|
private void userChangeObserver(PeerUser user, PropertyChangeEvent evt) {
|
|
@@ -117,11 +112,8 @@ public class UserList {
|
117
|
112
|
|
118
|
113
|
inactiveUsers.remove(id);
|
119
|
114
|
activeUsers.put(id, user);
|
120
|
|
- notifyConnectionObservers(user);
|
121
|
115
|
Log.v(getClass().getSimpleName(), "State of user " + id + " updated from " +
|
122
|
116
|
oldState.toString() + " to " + newState.toString());
|
123
|
|
-
|
124
|
|
-
|
125
|
117
|
} else if (oldState == PeerUser.State.CONNECTED &&
|
126
|
118
|
(newState == PeerUser.State.DISCONNECTED || newState == PeerUser.State.CONNECTING)) {
|
127
|
119
|
// Disconnection
|
|
@@ -136,25 +128,18 @@ public class UserList {
|
136
|
128
|
|
137
|
129
|
activeUsers.remove(id);
|
138
|
130
|
inactiveUsers.put(id, user);
|
139
|
|
- notifyDisconnectionObservers(user);
|
140
|
|
-
|
141
|
131
|
Log.v(getClass().getSimpleName(), "State of user " + id + " updated from " +
|
142
|
132
|
oldState.toString() + " to " + newState.toString());
|
143
|
133
|
}
|
144
|
134
|
}
|
145
|
135
|
}
|
146
|
136
|
|
147
|
|
- public void addActiveUsersObserver(UserConnectionCallback connectionCallback, UserDisconnectionCallback disconnectionCallback) {
|
148
|
|
- userConnectionObservers.add(connectionCallback);
|
149
|
|
- userDisconnectionObservers.add(disconnectionCallback);
|
150
|
|
- }
|
151
|
|
-
|
152
|
|
- private void notifyConnectionObservers(PeerUser user) {
|
153
|
|
- userConnectionObservers.forEach(callback -> callback.onUserConnected(user));
|
|
137
|
+ public void addNewUserObserver(UserConnectionCallback connectionCallback) {
|
|
138
|
+ newUsersObservers.add(connectionCallback);
|
154
|
139
|
}
|
155
|
140
|
|
156
|
|
- private void notifyDisconnectionObservers(PeerUser user) {
|
157
|
|
- userDisconnectionObservers.forEach(callback -> callback.onUserDisconnected(user));
|
|
141
|
+ private void notifyNewUserObservers(PeerUser user) {
|
|
142
|
+ newUsersObservers.forEach(callback -> callback.onUserConnected(user));
|
158
|
143
|
}
|
159
|
144
|
|
160
|
145
|
/**
|