Corrige Preferences: name/port do Login agora atualizam MainWindow
- Login.commitEdit() no JSpinner para capturar porta digitada manualmente - Main.java agora exibe o Login antes de criar o MainWindow - MenuBar aplica os valores do Login no MainWindow via setUserName/setPort - MainWindow: campos userName/port deixam de ser final; setPort() reconecta o socket - Socket.java ganha close() para permitir reconexão limpa em nova porta
This commit is contained in:
parent
5e835f4d75
commit
668b2d417b
|
|
@ -22,6 +22,7 @@ import javax.swing.JTextField;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author flavio
|
* @author flavio
|
||||||
|
* @author tony
|
||||||
*/
|
*/
|
||||||
public final class Login extends JDialog {
|
public final class Login extends JDialog {
|
||||||
|
|
||||||
|
|
@ -82,10 +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 {
|
||||||
|
portField.commitEdit();
|
||||||
|
} catch (java.text.ParseException ex) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
Login.this.setVisible(false);
|
Login.this.setVisible(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
cancelButton.addActionListener((ActionEvent e) -> {
|
cancelButton.addActionListener((ActionEvent e) -> {
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,8 @@ public class MainWindow extends javax.swing.JFrame {
|
||||||
|
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
|
|
||||||
private final String userName;
|
private String userName;
|
||||||
private final int port;
|
private int port;
|
||||||
|
|
||||||
private PluginLoader pluginManager;
|
private PluginLoader pluginManager;
|
||||||
private final List<Plugin> plugins;
|
private final List<Plugin> plugins;
|
||||||
|
|
@ -111,4 +111,28 @@ 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,6 +11,8 @@ import javax.swing.JMenuBar;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author flavio
|
* @author flavio
|
||||||
|
* @author tony
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public class MenuBar extends JMenuBar {
|
public class MenuBar extends JMenuBar {
|
||||||
|
|
||||||
|
|
@ -56,9 +58,14 @@ 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,6 +12,7 @@ import java.util.logging.Logger;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author flavio
|
* @author flavio
|
||||||
|
* @author tony
|
||||||
*/
|
*/
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
|
@ -40,14 +41,15 @@ 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(System.getProperty("user.name"), 8000);
|
main = new pitiupi.GUI.MainWindow(login.getName(), login.getPort());
|
||||||
main.setVisible(true);
|
main.setVisible(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ 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) {
|
||||||
|
|
@ -56,18 +57,35 @@ 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 (true) {
|
while (running) {
|
||||||
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());
|
||||||
|
|
@ -75,6 +93,8 @@ 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