Compare commits

..

No commits in common. "main" and "bugfix/login-name-port" have entirely different histories.

4 changed files with 15 additions and 29 deletions

View File

@ -94,7 +94,7 @@ public class ChatWindow extends javax.swing.JDialog {
if (jTextArea1.getText().trim().length() == 0) {
return;
}
ChatMessage message = new ChatMessage(mainWindow.getUserName(), jTextArea1.getText());
ChatMessage message = new ChatMessage(mainWindow.getName(), jTextArea1.getText());
this.sendChatMessage(message);
jTextArea1.setText("");
}

View File

@ -1,3 +1,8 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pitiupi.GUI;
import java.awt.BorderLayout;
@ -6,7 +11,6 @@ import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.prefs.Preferences;
import javax.swing.JButton;
import javax.swing.JDialog;
@ -22,10 +26,6 @@ import javax.swing.JTextField;
*/
public final class Login extends JDialog {
private static final Preferences prefs = Preferences.userNodeForPackage(Login.class);
private static final String PREF_NOME = "lastUserName";
private static final String PREF_PORTA = "lastPort";
JLabel nameLabel = new JLabel("Name: ");
JLabel portLabel = new JLabel("Port: ");
@ -44,12 +44,8 @@ public final class Login extends JDialog {
public void initComponents() {
this.setTitle("Login");
String nomeSalvo = prefs.get(PREF_NOME, System.getProperty("user.name"));
int portaSalva = prefs.getInt(PREF_PORTA, 8000);
portField.setModel(new javax.swing.SpinnerNumberModel(portaSalva, 8000, 9000, 1));
nameField.setText(nomeSalvo);
portField.setModel(new javax.swing.SpinnerNumberModel(8000, 8000, 9000, 1));
nameField.setText(System.getProperty("user.name"));
JPanel topPanel = new JPanel(new GridBagLayout());
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
@ -87,19 +83,17 @@ public final class Login extends JDialog {
this.add(topPanel);
this.add(buttonPanel, BorderLayout.SOUTH);
okButton.addActionListener((ActionEvent e) -> {
try {
portField.commitEdit();
} catch (java.text.ParseException ex) {
return;
}
prefs.put(PREF_NOME, nameField.getText());
prefs.putInt(PREF_PORTA, (Integer) portField.getValue());
Login.this.setVisible(false);
});
cancelButton.addActionListener((ActionEvent e) -> {
System.exit(0);
});

View File

@ -14,7 +14,6 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import pitiupi.GUI.MainWindow;
import pitiupi.net.HeartbeatMessage;
import java.util.UUID;
/**
*
* @author tony
@ -28,7 +27,6 @@ public class HeartbeatManager {
private final Map<String, PeerInfo> peers = new ConcurrentHashMap<>();
private final List<PeerListener> listeners = new CopyOnWriteArrayList<>();
private ScheduledExecutorService scheduler;
private final String instanceId = UUID.randomUUID().toString();
public HeartbeatManager(MainWindow mainWindow) {
this.mainWindow = mainWindow;
@ -48,21 +46,21 @@ public class HeartbeatManager {
private void announce() {
try {
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName(), instanceId);
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName());
mainWindow.getSocket().send(msg.toByteArray());
} catch (IOException 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) {
// ignora o próprio heartbeat
if (msg.getInstanceId().equals(instanceId) ) {
if (msg.getUserName().equals(mainWindow.getUserName())) {
return;
}
String key = msg.getInstanceId();
String key = from.getHostAddress() + "#" + msg.getUserName();
boolean isNew = !peers.containsKey(key);
PeerInfo peer = peers.computeIfAbsent(key, k -> new PeerInfo(msg.getUserName(), from, msg.getTimestamp()));

View File

@ -7,11 +7,9 @@ public class HeartbeatMessage extends Message {
private final String userName;
private final long timestamp;
private final String instanceId;
public HeartbeatMessage(String userName, String instanceId) {
public HeartbeatMessage(String userName) {
this.userName = userName;
this.instanceId = instanceId;
this.timestamp = System.currentTimeMillis();
}
@ -22,8 +20,4 @@ public class HeartbeatMessage extends Message {
public long getTimestamp() {
return timestamp;
}
public String getInstanceId() {
return instanceId;
}
}