From 7bd31a31c59c69370b3ab6cb833fc1e86b0a56ea Mon Sep 17 00:00:00 2001 From: js Date: Sun, 12 Apr 2020 18:30:17 +0200 Subject: [PATCH] fixed closing of server, now at the END --- src/gui/ClientInterfaceGUI.java | 11 +++++++++-- src/gui/ClientP2PGUI.java | 8 ++++---- src/gui/DownloadFileGUI.java | 13 +++++++++++-- src/gui/DownloadSelectionGen.java | 11 +++++++++-- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/gui/ClientInterfaceGUI.java b/src/gui/ClientInterfaceGUI.java index 58486ce..238f950 100644 --- a/src/gui/ClientInterfaceGUI.java +++ b/src/gui/ClientInterfaceGUI.java @@ -25,6 +25,8 @@ import java.io.IOException; import java.net.UnknownHostException; import java.net.SocketException; import javax.swing.JFrame; +import serverP2P.ServerManagementUDP; +import serverP2P.ServerManagementTCP; /** Implementation of P2P-JAVA-PROJECT CLIENT interface for CLI * @author Louis Royer @@ -40,10 +42,15 @@ public class ClientInterfaceGUI extends ClientInterface { */ Logger loggerC; + private ServerManagementTCP smtcp; + private ServerManagementUDP smudp; - public ClientInterfaceGUI(ClientManagement clientManagement, Logger logger) { + + public ClientInterfaceGUI(ClientManagement clientManagement, Logger logger, ServerManagementTCP smtcp, ServerManagementUDP smudp) { super(clientManagement, logger); this.loggerC = logger; + this.smtcp = smtcp; + this.smudp = smudp; } /** Implementation of Runnable @@ -68,7 +75,7 @@ public class ClientInterfaceGUI extends ClientInterface { win.setSize(550, 250); win.setLocationRelativeTo(null); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - win.add(new DownloadSelectionGen(list, clientManagement, loggerC)); + win.add(new DownloadSelectionGen(list, clientManagement, loggerC, smtcp, smudp)); win.setVisible(true); } catch (EmptyDirectory e) { writeLog("Server has no file in directory", LogLevel.Error); diff --git a/src/gui/ClientP2PGUI.java b/src/gui/ClientP2PGUI.java index 10b377e..e054cb9 100644 --- a/src/gui/ClientP2PGUI.java +++ b/src/gui/ClientP2PGUI.java @@ -109,7 +109,7 @@ public class ClientP2PGUI { case "2" : System.out.println("Starting with UDP"); ClientManagementUDP cmudp = new ClientManagementUDP(directories.getDataHomeDirectory(), tracker, directories.getDataHomeDirectory() + partsDir, loggerClient, server); - tclient = new Thread(new ClientInterfaceGUI(cmudp, loggerClient)); + tclient = new Thread(new ClientInterfaceGUI(cmudp, loggerClient, smtcp, smudp)); break; case "TCP": case "tcp": @@ -117,7 +117,7 @@ public class ClientP2PGUI { default: System.out.println("Starting with TCP"); ClientManagementTCP cmtcp = new ClientManagementTCP(directories.getDataHomeDirectory(), tracker, directories.getDataHomeDirectory() + partsDir, loggerClient, server); - tclient = new Thread(new ClientInterfaceGUI(cmtcp, loggerClient)); + tclient = new Thread(new ClientInterfaceGUI(cmtcp, loggerClient, smtcp, smudp)); break; } tclient.setName("client P2P-JAVA-PROJECT GUI"); @@ -125,8 +125,8 @@ public class ClientP2PGUI { try { tclient.join(); } catch (InterruptedException e) {} - smudp.setStop(); - smtcp.setStop(); + //smudp.setStop(); + //smtcp.setStop(); } diff --git a/src/gui/DownloadFileGUI.java b/src/gui/DownloadFileGUI.java index 384c8bc..bf08432 100644 --- a/src/gui/DownloadFileGUI.java +++ b/src/gui/DownloadFileGUI.java @@ -26,26 +26,35 @@ import java.net.UnknownHostException; import java.net.SocketException; import javax.swing.JFrame; import javax.swing.JPanel; +import serverP2P.ServerManagementUDP; +import serverP2P.ServerManagementTCP; public class DownloadFileGUI extends JPanel{ + + private static final long serialVersionUID = 13L; private String fileToDownload; private ClientManagement clientManagement; private Logger logger; - private static final long serialVersionUID = 13L; + private ServerManagementTCP smtcp; + private ServerManagementUDP smudp; - public DownloadFileGUI(String fileToDownload, ClientManagement clientManagement, Logger logger){ + public DownloadFileGUI(String fileToDownload, ClientManagement clientManagement, Logger logger, ServerManagementTCP smtcp, ServerManagementUDP smudp){ this.fileToDownload = fileToDownload; this.clientManagement = clientManagement; this.logger = logger; + this.smtcp = smtcp; + this.smudp = smudp; } public void download() { try { System.out.println("Downloading " + fileToDownload + " ... "); clientManagement.download(fileToDownload); + smudp.setStop(); + smtcp.setStop(); ErrorFrame erreur = new ErrorFrame("File " + fileToDownload + " sucessfully downloaded"); } catch (NumberFormatException e) { ErrorFrame erreur = new ErrorFrame("File " + fileToDownload + " unsucessfully downloaded, wrong number"); diff --git a/src/gui/DownloadSelectionGen.java b/src/gui/DownloadSelectionGen.java index c166447..a48b8b1 100644 --- a/src/gui/DownloadSelectionGen.java +++ b/src/gui/DownloadSelectionGen.java @@ -12,6 +12,8 @@ package gui; import clientP2P.*; import tools.LogLevel; import tools.Logger; + import serverP2P.ServerManagementUDP; + import serverP2P.ServerManagementTCP; /** * @author Louis Royer @@ -30,6 +32,9 @@ public class DownloadSelectionGen extends javax.swing.JPanel { private String[] listFilesToDownload; private ClientManagement clientManagement; private Logger logger; + private ServerManagementTCP smtcp; + private ServerManagementUDP smudp; + /** * @param listFilesToDownload list of files to display @@ -37,10 +42,12 @@ public class DownloadSelectionGen extends javax.swing.JPanel { * @param logger logger * Creates new form ArgumentsGen */ - public DownloadSelectionGen(String[] listFilesToDownload, ClientManagement clientManagement, Logger logger) { + public DownloadSelectionGen(String[] listFilesToDownload, ClientManagement clientManagement, Logger logger, ServerManagementTCP smtcp, ServerManagementUDP smudp) { this.listFilesToDownload = listFilesToDownload; this.clientManagement = clientManagement; this.logger = logger; + this.smtcp = smtcp; + this.smudp = smudp; initComponents(); } @@ -121,7 +128,7 @@ public class DownloadSelectionGen extends javax.swing.JPanel { private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String fileSelected = jList1.getSelectedValue(); System.out.println("File to download: " + fileSelected); - DownloadFileGUI dl = new DownloadFileGUI(fileSelected, clientManagement, logger); + DownloadFileGUI dl = new DownloadFileGUI(fileSelected, clientManagement, logger, smtcp, smudp); dl.download(); }