fix: usar instanceId único em vez de userName para identificar heartbeats
This commit is contained in:
parent
668b2d417b
commit
a7ba307daf
|
|
@ -14,6 +14,7 @@ import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import pitiupi.GUI.MainWindow;
|
import pitiupi.GUI.MainWindow;
|
||||||
import pitiupi.net.HeartbeatMessage;
|
import pitiupi.net.HeartbeatMessage;
|
||||||
|
import java.util.UUID;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author tony
|
* @author tony
|
||||||
|
|
@ -27,6 +28,7 @@ public class HeartbeatManager {
|
||||||
private final Map<String, PeerInfo> peers = new ConcurrentHashMap<>();
|
private final Map<String, PeerInfo> peers = new ConcurrentHashMap<>();
|
||||||
private final List<PeerListener> listeners = new CopyOnWriteArrayList<>();
|
private final List<PeerListener> listeners = new CopyOnWriteArrayList<>();
|
||||||
private ScheduledExecutorService scheduler;
|
private ScheduledExecutorService scheduler;
|
||||||
|
private final String instanceId = UUID.randomUUID().toString();
|
||||||
|
|
||||||
public HeartbeatManager(MainWindow mainWindow) {
|
public HeartbeatManager(MainWindow mainWindow) {
|
||||||
this.mainWindow = mainWindow;
|
this.mainWindow = mainWindow;
|
||||||
|
|
@ -46,21 +48,21 @@ public class HeartbeatManager {
|
||||||
|
|
||||||
private void announce() {
|
private void announce() {
|
||||||
try {
|
try {
|
||||||
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName());
|
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName(), instanceId);
|
||||||
mainWindow.getSocket().send(msg.toByteArray());
|
mainWindow.getSocket().send(msg.toByteArray());
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
Logger.getLogger(HeartbeatManager.class.getName()).log(Level.WARNING, "Falha ao enviar heartbeat", ex);
|
Logger.getLogger(HeartbeatManager.class.getName()).log(Level.WARNING, "Falha ao enviar heartbeat", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chamado pelo Socket quando um pacote é identificado como heartbeat
|
|
||||||
public void receiveHeartbeat(HeartbeatMessage msg, InetAddress from) {
|
public void receiveHeartbeat(HeartbeatMessage msg, InetAddress from) {
|
||||||
// ignora o próprio heartbeat
|
// ignora o próprio heartbeat
|
||||||
if (msg.getUserName().equals(mainWindow.getUserName())) {
|
if (msg.getInstanceId().equals(instanceId) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String key = from.getHostAddress() + "#" + msg.getUserName();
|
String key = msg.getInstanceId();
|
||||||
boolean isNew = !peers.containsKey(key);
|
boolean isNew = !peers.containsKey(key);
|
||||||
|
|
||||||
PeerInfo peer = peers.computeIfAbsent(key, k -> new PeerInfo(msg.getUserName(), from, msg.getTimestamp()));
|
PeerInfo peer = peers.computeIfAbsent(key, k -> new PeerInfo(msg.getUserName(), from, msg.getTimestamp()));
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,11 @@ public class HeartbeatMessage extends Message {
|
||||||
|
|
||||||
private final String userName;
|
private final String userName;
|
||||||
private final long timestamp;
|
private final long timestamp;
|
||||||
|
private final String instanceId;
|
||||||
|
|
||||||
public HeartbeatMessage(String userName) {
|
public HeartbeatMessage(String userName, String instanceId) {
|
||||||
this.userName = userName;
|
this.userName = userName;
|
||||||
|
this.instanceId = instanceId;
|
||||||
this.timestamp = System.currentTimeMillis();
|
this.timestamp = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -20,4 +22,8 @@ public class HeartbeatMessage extends Message {
|
||||||
public long getTimestamp() {
|
public long getTimestamp() {
|
||||||
return timestamp;
|
return timestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getInstanceId() {
|
||||||
|
return instanceId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue