Make ip to id function static
This commit is contained in:
parent
46565951e8
commit
5d85a6061a
3 changed files with 15 additions and 11 deletions
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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