Compare commits
No commits in common. "main" and "bugfix/login-name-port" have entirely different histories.
main
...
bugfix/log
|
|
@ -94,7 +94,7 @@ public class ChatWindow extends javax.swing.JDialog {
|
||||||
if (jTextArea1.getText().trim().length() == 0) {
|
if (jTextArea1.getText().trim().length() == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ChatMessage message = new ChatMessage(mainWindow.getUserName(), jTextArea1.getText());
|
ChatMessage message = new ChatMessage(mainWindow.getName(), jTextArea1.getText());
|
||||||
this.sendChatMessage(message);
|
this.sendChatMessage(message);
|
||||||
jTextArea1.setText("");
|
jTextArea1.setText("");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
package pitiupi.GUI;
|
||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
|
|
@ -6,7 +11,6 @@ import java.awt.GridBagConstraints;
|
||||||
import java.awt.GridBagLayout;
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.util.prefs.Preferences;
|
|
||||||
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JDialog;
|
import javax.swing.JDialog;
|
||||||
|
|
@ -22,10 +26,6 @@ import javax.swing.JTextField;
|
||||||
*/
|
*/
|
||||||
public final class Login extends JDialog {
|
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 nameLabel = new JLabel("Name: ");
|
||||||
JLabel portLabel = new JLabel("Port: ");
|
JLabel portLabel = new JLabel("Port: ");
|
||||||
|
|
||||||
|
|
@ -44,12 +44,8 @@ public final class Login extends JDialog {
|
||||||
public void initComponents() {
|
public void initComponents() {
|
||||||
this.setTitle("Login");
|
this.setTitle("Login");
|
||||||
|
|
||||||
|
portField.setModel(new javax.swing.SpinnerNumberModel(8000, 8000, 9000, 1));
|
||||||
String nomeSalvo = prefs.get(PREF_NOME, System.getProperty("user.name"));
|
nameField.setText(System.getProperty("user.name"));
|
||||||
int portaSalva = prefs.getInt(PREF_PORTA, 8000);
|
|
||||||
|
|
||||||
portField.setModel(new javax.swing.SpinnerNumberModel(portaSalva, 8000, 9000, 1));
|
|
||||||
nameField.setText(nomeSalvo);
|
|
||||||
|
|
||||||
JPanel topPanel = new JPanel(new GridBagLayout());
|
JPanel topPanel = new JPanel(new GridBagLayout());
|
||||||
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
|
||||||
|
|
@ -87,19 +83,17 @@ public final class Login extends JDialog {
|
||||||
this.add(topPanel);
|
this.add(topPanel);
|
||||||
this.add(buttonPanel, BorderLayout.SOUTH);
|
this.add(buttonPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
|
||||||
okButton.addActionListener((ActionEvent e) -> {
|
okButton.addActionListener((ActionEvent e) -> {
|
||||||
try {
|
try {
|
||||||
portField.commitEdit();
|
portField.commitEdit();
|
||||||
} catch (java.text.ParseException ex) {
|
} catch (java.text.ParseException ex) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
prefs.put(PREF_NOME, nameField.getText());
|
|
||||||
prefs.putInt(PREF_PORTA, (Integer) portField.getValue());
|
|
||||||
|
|
||||||
Login.this.setVisible(false);
|
Login.this.setVisible(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
cancelButton.addActionListener((ActionEvent e) -> {
|
cancelButton.addActionListener((ActionEvent e) -> {
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ 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
|
||||||
|
|
@ -28,7 +27,6 @@ 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;
|
||||||
|
|
@ -48,21 +46,21 @@ public class HeartbeatManager {
|
||||||
|
|
||||||
private void announce() {
|
private void announce() {
|
||||||
try {
|
try {
|
||||||
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName(), instanceId);
|
HeartbeatMessage msg = new HeartbeatMessage(mainWindow.getUserName());
|
||||||
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.getInstanceId().equals(instanceId) ) {
|
if (msg.getUserName().equals(mainWindow.getUserName())) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String key = msg.getInstanceId();
|
String key = from.getHostAddress() + "#" + msg.getUserName();
|
||||||
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,11 +7,9 @@ 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, String instanceId) {
|
public HeartbeatMessage(String userName) {
|
||||||
this.userName = userName;
|
this.userName = userName;
|
||||||
this.instanceId = instanceId;
|
|
||||||
this.timestamp = System.currentTimeMillis();
|
this.timestamp = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -22,8 +20,4 @@ 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