Compare commits
No commits in common. "main" and "feature/heartbeat-online-users" have entirely different histories.
main
...
feature/he
|
|
@ -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;
|
||||||
|
|
@ -18,14 +22,9 @@ import javax.swing.JTextField;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author flavio
|
* @author flavio
|
||||||
* @author tony
|
|
||||||
*/
|
*/
|
||||||
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 +43,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));
|
||||||
|
|
@ -88,15 +83,6 @@ public final class Login extends JDialog {
|
||||||
this.add(buttonPanel, BorderLayout.SOUTH);
|
this.add(buttonPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
okButton.addActionListener((ActionEvent e) -> {
|
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);
|
Login.this.setVisible(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,8 @@ public class MainWindow extends javax.swing.JFrame {
|
||||||
|
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
|
|
||||||
private String userName;
|
private final String userName;
|
||||||
private int port;
|
private final int port;
|
||||||
|
|
||||||
private PluginLoader pluginManager;
|
private PluginLoader pluginManager;
|
||||||
private final List<Plugin> plugins;
|
private final List<Plugin> plugins;
|
||||||
|
|
@ -111,28 +111,4 @@ public class MainWindow extends javax.swing.JFrame {
|
||||||
public List<Plugin> getPlugins() {
|
public List<Plugin> getPlugins() {
|
||||||
return plugins;
|
return plugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUserName(String userName) {
|
|
||||||
this.userName = userName;
|
|
||||||
updateStatus();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPort(int port) {
|
|
||||||
this.port = port;
|
|
||||||
reconnect();
|
|
||||||
updateStatus();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateStatus() {
|
|
||||||
this.status.setText("Connected on port " + this.port + " name " + this.userName);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void reconnect() {
|
|
||||||
if (this.socket != null) {
|
|
||||||
this.socket.close();
|
|
||||||
}
|
|
||||||
this.socket = new Socket(this);
|
|
||||||
this.socket.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,6 @@ import javax.swing.JMenuBar;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author flavio
|
* @author flavio
|
||||||
* @author tony
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class MenuBar extends JMenuBar {
|
public class MenuBar extends JMenuBar {
|
||||||
|
|
||||||
|
|
@ -58,14 +56,9 @@ public class MenuBar extends JMenuBar {
|
||||||
mainWindow.exit();
|
mainWindow.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupMenuActionPerformed(java.awt.event.ActionEvent evt) {
|
||||||
private void setupMenuActionPerformed(java.awt.event.ActionEvent evt) {
|
|
||||||
Login login = new Login();
|
Login login = new Login();
|
||||||
login.setVisible(true);
|
login.setVisible(true);
|
||||||
|
|
||||||
mainWindow.setUserName(login.getName());
|
|
||||||
mainWindow.setPort(login.getPort());
|
|
||||||
|
|
||||||
login.dispose();
|
login.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ import java.util.logging.Logger;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author flavio
|
* @author flavio
|
||||||
* @author tony
|
|
||||||
*/
|
*/
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
|
@ -41,15 +40,14 @@ public class Main {
|
||||||
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
|
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
|
||||||
java.util.logging.Logger.getLogger(pitiupi.GUI.MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
java.util.logging.Logger.getLogger(pitiupi.GUI.MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
|
//</editor-fold>
|
||||||
|
|
||||||
|
/* Create and display the form */
|
||||||
java.awt.EventQueue.invokeLater(new Runnable() {
|
java.awt.EventQueue.invokeLater(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
pitiupi.GUI.Login login = new pitiupi.GUI.Login();
|
|
||||||
login.setVisible(true);
|
|
||||||
|
|
||||||
pitiupi.GUI.MainWindow main;
|
pitiupi.GUI.MainWindow main;
|
||||||
main = new pitiupi.GUI.MainWindow(login.getName(), login.getPort());
|
main = new pitiupi.GUI.MainWindow(System.getProperty("user.name"), 8000);
|
||||||
main.setVisible(true);
|
main.setVisible(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,6 @@ public class Socket extends Thread {
|
||||||
private MulticastSocket multicastSocket;
|
private MulticastSocket multicastSocket;
|
||||||
private InetAddress address;
|
private InetAddress address;
|
||||||
private MainWindow main;
|
private MainWindow main;
|
||||||
private volatile boolean running = true;
|
|
||||||
public final static String INET_ADDR = "224.0.0.3";
|
public final static String INET_ADDR = "224.0.0.3";
|
||||||
|
|
||||||
public Socket(MainWindow main) {
|
public Socket(MainWindow main) {
|
||||||
|
|
@ -57,35 +56,18 @@ public class Socket extends Thread {
|
||||||
multicastSocket.send(msgPacket);
|
multicastSocket.send(msgPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
|
||||||
running = false;
|
|
||||||
if (multicastSocket != null) {
|
|
||||||
try {
|
|
||||||
multicastSocket.leaveGroup(address);
|
|
||||||
} catch (IOException ex) {
|
|
||||||
|
|
||||||
}
|
|
||||||
multicastSocket.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void receive() throws UnknownHostException, IOException, ClassNotFoundException {
|
public void receive() throws UnknownHostException, IOException, ClassNotFoundException {
|
||||||
byte[] buf = new byte[256000];
|
byte[] buf = new byte[256000];
|
||||||
while (running) {
|
while (true) {
|
||||||
DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
|
DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
|
||||||
try {
|
multicastSocket.receive(msgPacket);
|
||||||
multicastSocket.receive(msgPacket);
|
|
||||||
} catch (IOException ex) {
|
|
||||||
if (!running) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
throw ex;
|
|
||||||
}
|
|
||||||
HeartbeatMessage heartbeat = tryParseHeartbeat(msgPacket.getData());
|
HeartbeatMessage heartbeat = tryParseHeartbeat(msgPacket.getData());
|
||||||
if (heartbeat != null) {
|
if (heartbeat != null) {
|
||||||
main.getHeartbeatManager().receiveHeartbeat(heartbeat, msgPacket.getAddress());
|
main.getHeartbeatManager().receiveHeartbeat(heartbeat, msgPacket.getAddress());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Plugin plugin : main.getPlugins()) {
|
for (Plugin plugin : main.getPlugins()) {
|
||||||
System.out.println(msgPacket.getAddress().toString());
|
System.out.println(msgPacket.getAddress().toString());
|
||||||
plugin.receiveMessage(msgPacket.getData());
|
plugin.receiveMessage(msgPacket.getData());
|
||||||
|
|
@ -93,8 +75,6 @@ public class Socket extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private HeartbeatMessage tryParseHeartbeat(byte[] data) {
|
private HeartbeatMessage tryParseHeartbeat(byte[] data) {
|
||||||
try {
|
try {
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue