Compare commits
No commits in common. "1d3e56e9b7b73f2b0c4177116ce77a31e6679ccc" and "c50a2fb2516fbed930dcb13eddaff1d80b12bc5f" have entirely different histories.
1d3e56e9b7
...
c50a2fb251
5 changed files with 14 additions and 24 deletions
|
|
@ -54,7 +54,9 @@ public class ConnectionListener {
|
|||
try {
|
||||
server = new ServerSocket(TCP_PORT);
|
||||
while (!shouldStop) {
|
||||
System.out.println("Accepting...");
|
||||
Socket clientSocket = server.accept();
|
||||
System.out.println("New connection from " + clientSocket.getRemoteSocketAddress());
|
||||
callback.onNewConnection(clientSocket);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
|
|
|
|||
|
|
@ -41,13 +41,4 @@ public class NetUtil {
|
|||
}
|
||||
return localList;
|
||||
}
|
||||
|
||||
public static int getIdFromIp(InetAddress ipAddr) {
|
||||
byte[] addr = ipAddr.getAddress();
|
||||
int id = 0;
|
||||
for (byte b : addr) {
|
||||
id = (id << 8) + b;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,17 +100,9 @@ public class MainController implements Initializable {
|
|||
}
|
||||
}
|
||||
|
||||
private void startListening() {
|
||||
if (userList != null) {
|
||||
userList.startDiscoveryListening();
|
||||
userList.startUserListening((e) ->
|
||||
Log.e(this.getClass().getSimpleName(), "Error listening to users", e));
|
||||
}
|
||||
}
|
||||
|
||||
private void startChat() {
|
||||
discoverActiveUsers();
|
||||
startListening();
|
||||
userList.startDiscoveryListening();
|
||||
Platform.runLater(this::showChat);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,8 +9,6 @@ import java.util.List;
|
|||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import static fr.insa.clavardator.network.NetUtil.getIdFromIp;
|
||||
|
||||
public class CurrentUser extends User {
|
||||
private static CurrentUser instance = null;
|
||||
|
||||
|
|
@ -33,11 +31,10 @@ public class CurrentUser extends User {
|
|||
public void init() throws SocketException {
|
||||
final List<InetAddress> addresses = NetUtil.listAllLocalAddresses();
|
||||
if (addresses.size() > 0) {
|
||||
id = getIdFromIp(addresses.get(0));
|
||||
id = addresses.get(0).hashCode();
|
||||
} else {
|
||||
throw new SocketException();
|
||||
}
|
||||
setUsername("Moi2");
|
||||
// TODO place by db username fetching
|
||||
Timer t = new Timer();
|
||||
t.schedule(new TimerTask() {
|
||||
|
|
|
|||
|
|
@ -13,8 +13,6 @@ import java.util.HashMap;
|
|||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static fr.insa.clavardator.network.NetUtil.getIdFromIp;
|
||||
|
||||
public class UserList {
|
||||
|
||||
private final Map<Integer, PeerUser> inactiveUsers = new HashMap<>();
|
||||
|
|
@ -141,6 +139,16 @@ public class UserList {
|
|||
}
|
||||
}
|
||||
|
||||
private int getIdFromIp(InetAddress ipAddr) {
|
||||
byte[] addr = ipAddr.getAddress();
|
||||
int id = 0;
|
||||
for (byte b : addr) {
|
||||
id = (id << 8) + b;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
public void addActiveUsersObserver(UserConnectionCallback connectionCallback, UserDisconnectionCallback disconnectionCallback) {
|
||||
userConnectionObservers.add(connectionCallback);
|
||||
userDisconnectionObservers.add(disconnectionCallback);
|
||||
|
|
|
|||
Loading…
Reference in a new issue