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