From 05a2967645db7c88e359a8bd622a3083b4a7dc21 Mon Sep 17 00:00:00 2001 From: js Date: Fri, 3 Apr 2020 12:11:29 +0200 Subject: [PATCH 01/18] first panel of GUI --- src/gui/Gui.java | 12 ++++++++++ src/gui/MainWindow.java | 17 +++++++++++++ src/gui/Parameters.java | 53 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 src/gui/Gui.java create mode 100644 src/gui/MainWindow.java create mode 100644 src/gui/Parameters.java diff --git a/src/gui/Gui.java b/src/gui/Gui.java new file mode 100644 index 0000000..bb8b3e9 --- /dev/null +++ b/src/gui/Gui.java @@ -0,0 +1,12 @@ +package gui; + +import javax.swing.JFrame; + +public class Gui{ + + public static void main(String[] args) { + MainWindow win = new MainWindow(); + } + + +} diff --git a/src/gui/MainWindow.java b/src/gui/MainWindow.java new file mode 100644 index 0000000..42bcc0b --- /dev/null +++ b/src/gui/MainWindow.java @@ -0,0 +1,17 @@ +package gui; + +import javax.swing.JFrame; + +public class MainWindow extends JFrame{ + + public MainWindow(){ + JFrame fenetre = new JFrame(); + fenetre.setTitle("Client"); + fenetre.setSize(550, 200); + fenetre.setLocationRelativeTo(null); + fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + fenetre.add(new Parameters()); + fenetre.setVisible(true); + } + +} diff --git a/src/gui/Parameters.java b/src/gui/Parameters.java new file mode 100644 index 0000000..6db54e1 --- /dev/null +++ b/src/gui/Parameters.java @@ -0,0 +1,53 @@ +package gui; + +import javax.swing.JPanel; +import javax.swing.JLabel; +import javax.swing.JButton; +import javax.swing.JTextField; +import javax.swing.JComboBox; +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.GridLayout; + +public class Parameters extends JPanel{ + + private JLabel label1, label2, label3, label4, label5; + private JTextField field1, field2, field3, field4; + private JButton connectButton; + private JComboBox protocolSelector; + + public Parameters(){ + JPanel p1 = new JPanel(); + p1.setLayout(new GridLayout(5, 2)); + FlowLayout layout = new FlowLayout(); + JPanel p2 = new JPanel(); + p2.setLayout(layout); + String[] elements = {"TCP", "UDP"}; + this.label1 = new JLabel("Server hostname: "); + this.label2 = new JLabel("Server port: "); + this.label3 = new JLabel("Tracker hostname: "); + this.label4 = new JLabel("Tracker port: "); + this.label5 = new JLabel("Protocol: "); + this.field1 = new JTextField(20); + this.field2 = new JTextField(20); + this.field3 = new JTextField(20); + this.field4 = new JTextField(20); + this.connectButton = new JButton("Connect"); + protocolSelector = new JComboBox<>(elements); + p1.add(label1); + p1.add(field1); + p1.add(label2); + p1.add(field2); + p1.add(label3); + p1.add(field3); + p1.add(label4); + p1.add(field4); + p1.add(label5); + p1.add(protocolSelector); + p2.add(connectButton); + this.add(p1, BorderLayout.NORTH); + this.add(p2, BorderLayout.SOUTH); + setVisible(true); + } + +} -- 2.30.2 From 4167cc3aa6cb6fd1a0d93c0c48d8af7dd15648d9 Mon Sep 17 00:00:00 2001 From: js Date: Fri, 3 Apr 2020 14:03:40 +0200 Subject: [PATCH 02/18] added support for displaying list of files but need scrollbar --- src/gui/Gui.java | 6 +-- src/gui/MainWindow.java | 22 ++++++---- src/gui/Parameters.java | 75 +++++++++++++++++----------------- src/gui/downloadSelection.java | 41 +++++++++++++++++++ 4 files changed, 95 insertions(+), 49 deletions(-) create mode 100644 src/gui/downloadSelection.java diff --git a/src/gui/Gui.java b/src/gui/Gui.java index bb8b3e9..748e215 100644 --- a/src/gui/Gui.java +++ b/src/gui/Gui.java @@ -4,9 +4,9 @@ import javax.swing.JFrame; public class Gui{ - public static void main(String[] args) { - MainWindow win = new MainWindow(); - } + public static void main(String[] args) { + MainWindow win = new MainWindow(); + } } diff --git a/src/gui/MainWindow.java b/src/gui/MainWindow.java index 42bcc0b..d6d5cbb 100644 --- a/src/gui/MainWindow.java +++ b/src/gui/MainWindow.java @@ -4,14 +4,18 @@ import javax.swing.JFrame; public class MainWindow extends JFrame{ - public MainWindow(){ - JFrame fenetre = new JFrame(); - fenetre.setTitle("Client"); - fenetre.setSize(550, 200); - fenetre.setLocationRelativeTo(null); - fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - fenetre.add(new Parameters()); - fenetre.setVisible(true); - } + private static final long serialVersionUID = 13L; + + public MainWindow(){ + JFrame fenetre = new JFrame(); + fenetre.setTitle("Client"); + fenetre.setSize(550, 200); + fenetre.setLocationRelativeTo(null); + fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //fenetre.add(new Parameters()); + String[] listTest = {"test", "test2", "test3", "test4", "test5", "test6", "test7"}; + fenetre.add(new downloadSelection(listTest)); + fenetre.setVisible(true); + } } diff --git a/src/gui/Parameters.java b/src/gui/Parameters.java index 6db54e1..a681cd0 100644 --- a/src/gui/Parameters.java +++ b/src/gui/Parameters.java @@ -11,43 +11,44 @@ import java.awt.GridLayout; public class Parameters extends JPanel{ - private JLabel label1, label2, label3, label4, label5; - private JTextField field1, field2, field3, field4; - private JButton connectButton; - private JComboBox protocolSelector; + private static final long serialVersionUID = 13L; + private JLabel label1, label2, label3, label4, label5; + private JTextField field1, field2, field3, field4; + private JButton connectButton; + private JComboBox protocolSelector; - public Parameters(){ - JPanel p1 = new JPanel(); - p1.setLayout(new GridLayout(5, 2)); - FlowLayout layout = new FlowLayout(); - JPanel p2 = new JPanel(); - p2.setLayout(layout); - String[] elements = {"TCP", "UDP"}; - this.label1 = new JLabel("Server hostname: "); - this.label2 = new JLabel("Server port: "); - this.label3 = new JLabel("Tracker hostname: "); - this.label4 = new JLabel("Tracker port: "); - this.label5 = new JLabel("Protocol: "); - this.field1 = new JTextField(20); - this.field2 = new JTextField(20); - this.field3 = new JTextField(20); - this.field4 = new JTextField(20); - this.connectButton = new JButton("Connect"); - protocolSelector = new JComboBox<>(elements); - p1.add(label1); - p1.add(field1); - p1.add(label2); - p1.add(field2); - p1.add(label3); - p1.add(field3); - p1.add(label4); - p1.add(field4); - p1.add(label5); - p1.add(protocolSelector); - p2.add(connectButton); - this.add(p1, BorderLayout.NORTH); - this.add(p2, BorderLayout.SOUTH); - setVisible(true); - } + public Parameters(){ + JPanel p1 = new JPanel(); + p1.setLayout(new GridLayout(5, 2)); + FlowLayout layout = new FlowLayout(); + JPanel p2 = new JPanel(); + p2.setLayout(layout); + String[] elements = {"TCP", "UDP"}; + this.label1 = new JLabel("Server hostname: "); + this.label2 = new JLabel("Server port: "); + this.label3 = new JLabel("Tracker hostname: "); + this.label4 = new JLabel("Tracker port: "); + this.label5 = new JLabel("Protocol: "); + this.field1 = new JTextField(20); + this.field2 = new JTextField(20); + this.field3 = new JTextField(20); + this.field4 = new JTextField(20); + this.connectButton = new JButton("Connect"); + protocolSelector = new JComboBox<>(elements); + p1.add(label1); + p1.add(field1); + p1.add(label2); + p1.add(field2); + p1.add(label3); + p1.add(field3); + p1.add(label4); + p1.add(field4); + p1.add(label5); + p1.add(protocolSelector); + p2.add(connectButton); + this.add(p1, BorderLayout.NORTH); + this.add(p2, BorderLayout.SOUTH); + setVisible(true); + } } diff --git a/src/gui/downloadSelection.java b/src/gui/downloadSelection.java new file mode 100644 index 0000000..9eb809f --- /dev/null +++ b/src/gui/downloadSelection.java @@ -0,0 +1,41 @@ +package gui; + +import javax.swing.JPanel; +import javax.swing.JLabel; +import javax.swing.JButton; +import javax.swing.JTextField; +import javax.swing.JComboBox; +import javax.swing.JList; +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.GridLayout; + +public class downloadSelection extends JPanel { + + private static final long serialVersionUID = 13L; + private String[] listFilesToDownload; + private JList list1; + private JLabel label1; + private JButton downloadButton; + + public downloadSelection(String[] listFilesToDownload){ + JPanel p1 = new JPanel(); + JPanel p3 = new JPanel(); + FlowLayout layout = new FlowLayout(); + this.listFilesToDownload = listFilesToDownload; + this.list1 = new JList<>(listFilesToDownload); + this.label1 = new JLabel("List of files you can download: "); + this.downloadButton = new JButton("download"); + this.list1.setSelectedIndex(0); + p1.setLayout(new GridLayout(1, 2)); + p3.setLayout(layout); + this.setLayout(new GridLayout(2, 1)); + p1.add(label1); + p1.add(list1); + p3.add(downloadButton); + this.add(p1); + this.add(p3); + setVisible(true); + } + +} -- 2.30.2 From 3f3e7fd2f392de91c9840651771a3065448e45da Mon Sep 17 00:00:00 2001 From: js Date: Mon, 6 Apr 2020 11:03:45 +0200 Subject: [PATCH 03/18] added new panels done with NetBeans --- src/exception/LocalException.java | 2 +- src/gui/ArgumentsGen.java | 167 ++++++++++++++++++ ...dSelection.java => DownloadSelection.java} | 21 ++- src/gui/DownloadSelectionGen.java | 99 +++++++++++ src/gui/MainWindow.java | 9 +- src/gui/Parameters.java | 2 +- 6 files changed, 289 insertions(+), 11 deletions(-) create mode 100644 src/gui/ArgumentsGen.java rename src/gui/{downloadSelection.java => DownloadSelection.java} (68%) create mode 100644 src/gui/DownloadSelectionGen.java diff --git a/src/exception/LocalException.java b/src/exception/LocalException.java index 2fbc0f2..6ee4520 100644 --- a/src/exception/LocalException.java +++ b/src/exception/LocalException.java @@ -1,5 +1,5 @@ package exception; public abstract class LocalException extends Exception { - private static final long serialVersionUID = 12L; + private static final long serialVersionUID = 13L; } diff --git a/src/gui/ArgumentsGen.java b/src/gui/ArgumentsGen.java new file mode 100644 index 0000000..aefd8c9 --- /dev/null +++ b/src/gui/ArgumentsGen.java @@ -0,0 +1,167 @@ +package gui; + + +/* + * 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. + */ + +/** +* @author Louis Royer +* @author Flavien Haas +* @author JS Auge + */ +public class ArgumentsGen extends javax.swing.JPanel { + + /** + * Creates new form ArgumentsGen + */ + public ArgumentsGen() { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // + private void initComponents() { + + jLabel1 = new javax.swing.JLabel(); + jLabel2 = new javax.swing.JLabel(); + jLabel3 = new javax.swing.JLabel(); + jLabel4 = new javax.swing.JLabel(); + jLabel5 = new javax.swing.JLabel(); + jLabel6 = new javax.swing.JLabel(); + jComboBox1 = new javax.swing.JComboBox<>(); + jTextField1 = new javax.swing.JTextField(); + jTextField2 = new javax.swing.JTextField(); + jTextField3 = new javax.swing.JTextField(); + jTextField4 = new javax.swing.JTextField(); + jButton1 = new javax.swing.JButton(); + + jLabel1.setText("Enter connection informations:"); + + jLabel2.setText("Server hostname:"); + + jLabel3.setText("Server port:"); + + jLabel4.setText("Tracker hostname:"); + + jLabel5.setText("Tracker port:"); + + jLabel6.setText("Client's protocol:"); + + jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "TCP", "UDP" })); + + jTextField1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jTextField1ActionPerformed(evt); + } + }); + + jTextField2.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jTextField2ActionPerformed(evt); + } + }); + + jButton1.setText("Connect"); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jLabel1) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jLabel2) + .addComponent(jLabel3) + .addComponent(jLabel4) + .addComponent(jLabel5) + .addComponent(jLabel6)) + .addGap(71, 71, 71) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(jTextField1) + .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE) + .addComponent(jTextField3) + .addComponent(jTextField4))))) + .addContainerGap(138, Short.MAX_VALUE)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addGap(19, 19, 19) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel2) + .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(7, 7, 7) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel3) + .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(6, 6, 6) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel4) + .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel5)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButton1) + .addContainerGap(16, Short.MAX_VALUE)) + ); + }// + + private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + + private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + + + // Variables declaration - do not modify + private javax.swing.JButton jButton1; + private javax.swing.JComboBox jComboBox1; + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JLabel jLabel3; + private javax.swing.JLabel jLabel4; + private javax.swing.JLabel jLabel5; + private javax.swing.JLabel jLabel6; + private javax.swing.JTextField jTextField1; + private javax.swing.JTextField jTextField2; + private javax.swing.JTextField jTextField3; + private javax.swing.JTextField jTextField4; + private static final long serialVersionUID = 13L; + // End of variables declaration +} diff --git a/src/gui/downloadSelection.java b/src/gui/DownloadSelection.java similarity index 68% rename from src/gui/downloadSelection.java rename to src/gui/DownloadSelection.java index 9eb809f..213eae5 100644 --- a/src/gui/downloadSelection.java +++ b/src/gui/DownloadSelection.java @@ -6,32 +6,41 @@ import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JComboBox; import javax.swing.JList; +import javax.swing.JScrollPane; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; -public class downloadSelection extends JPanel { +public class DownloadSelection extends JPanel { private static final long serialVersionUID = 13L; private String[] listFilesToDownload; private JList list1; private JLabel label1; private JButton downloadButton; + private JScrollPane scroll; - public downloadSelection(String[] listFilesToDownload){ + public DownloadSelection(String[] listFilesToDownload){ JPanel p1 = new JPanel(); JPanel p3 = new JPanel(); + JPanel container = new JPanel(); FlowLayout layout = new FlowLayout(); + p1.setLayout(new GridLayout(1, 2)); + container.setLayout(layout); + p3.setLayout(layout); + this.setLayout(new GridLayout(2, 1)); this.listFilesToDownload = listFilesToDownload; this.list1 = new JList<>(listFilesToDownload); this.label1 = new JLabel("List of files you can download: "); this.downloadButton = new JButton("download"); this.list1.setSelectedIndex(0); - p1.setLayout(new GridLayout(1, 2)); - p3.setLayout(layout); - this.setLayout(new GridLayout(2, 1)); + scroll = new JScrollPane(list1); + int verticalPolicy = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS; + scroll.setVerticalScrollBarPolicy(verticalPolicy); + //container.add(list1); + container.add(scroll); p1.add(label1); - p1.add(list1); + p1.add(container); p3.add(downloadButton); this.add(p1); this.add(p3); diff --git a/src/gui/DownloadSelectionGen.java b/src/gui/DownloadSelectionGen.java new file mode 100644 index 0000000..2bb9cfd --- /dev/null +++ b/src/gui/DownloadSelectionGen.java @@ -0,0 +1,99 @@ +package gui; + +/* + * 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. + */ + +/** +* @author Louis Royer +* @author Flavien Haas +* @author JS Auge + */ +public class DownloadSelectionGen extends javax.swing.JPanel { + + // Variables declaration - do not modify + private javax.swing.JButton jButton1; + private javax.swing.JLabel jLabel1; + private javax.swing.JList jList1; + private javax.swing.JScrollPane jScrollPane1; + private static final long serialVersionUID = 13L; + private String[] listFilesToDownload; + // End of variables declaration + + /** + * Creates new form ArgumentsGen + */ + public DownloadSelectionGen(String[] listFilesToDownload) { + this.listFilesToDownload = listFilesToDownload; + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // + private void initComponents() { + + jLabel1 = new javax.swing.JLabel(); + jButton1 = new javax.swing.JButton(); + jScrollPane1 = new javax.swing.JScrollPane(); + jList1 = new javax.swing.JList<>(); + + jLabel1.setText("Select a file to download:"); + + jButton1.setText("Download"); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); + + jList1.setModel(new javax.swing.AbstractListModel() { + String[] strings = listFilesToDownload; + public int getSize() { return strings.length; } + public String getElementAt(int i) { return strings[i]; } + }); + jScrollPane1.setViewportView(jList1); + jList1.setSelectedIndex(0); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addGap(0, 0, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addGap(97, 97, 97) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(104, Short.MAX_VALUE)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(23, 23, 23)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButton1) + .addContainerGap(24, Short.MAX_VALUE)) + ); + }// + + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + +} diff --git a/src/gui/MainWindow.java b/src/gui/MainWindow.java index d6d5cbb..ae8b931 100644 --- a/src/gui/MainWindow.java +++ b/src/gui/MainWindow.java @@ -9,12 +9,15 @@ public class MainWindow extends JFrame{ public MainWindow(){ JFrame fenetre = new JFrame(); fenetre.setTitle("Client"); - fenetre.setSize(550, 200); + fenetre.setSize(550, 250); fenetre.setLocationRelativeTo(null); fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //fenetre.add(new Parameters()); - String[] listTest = {"test", "test2", "test3", "test4", "test5", "test6", "test7"}; - fenetre.add(new downloadSelection(listTest)); + //fenetre.add(new ArgumentsGen()); + String[] listTest = {"test", "test2", "test3", "test4", "test5", "test6", "test7", "fichier1", + "fichier2", "fichier3", "fichier4"}; + //fenetre.add(new DownloadSelection(listTest)); + fenetre.add(new DownloadSelectionGen(listTest)); fenetre.setVisible(true); } diff --git a/src/gui/Parameters.java b/src/gui/Parameters.java index a681cd0..a715d1c 100644 --- a/src/gui/Parameters.java +++ b/src/gui/Parameters.java @@ -28,7 +28,7 @@ public class Parameters extends JPanel{ this.label2 = new JLabel("Server port: "); this.label3 = new JLabel("Tracker hostname: "); this.label4 = new JLabel("Tracker port: "); - this.label5 = new JLabel("Protocol: "); + this.label5 = new JLabel("Client's Protocol: "); this.field1 = new JTextField(20); this.field2 = new JTextField(20); this.field3 = new JTextField(20); -- 2.30.2 From 69bfbb00d4745dced1ad4c55b7299efe6b188095 Mon Sep 17 00:00:00 2001 From: js Date: Tue, 7 Apr 2020 11:08:58 +0200 Subject: [PATCH 04/18] added WORKING back button on downloadSelection --- src/gui/DownloadSelectionGen.java | 92 +++++++++++++++++++------------ 1 file changed, 58 insertions(+), 34 deletions(-) diff --git a/src/gui/DownloadSelectionGen.java b/src/gui/DownloadSelectionGen.java index 2bb9cfd..59f25dd 100644 --- a/src/gui/DownloadSelectionGen.java +++ b/src/gui/DownloadSelectionGen.java @@ -6,6 +6,10 @@ package gui; * and open the template in the editor. */ + + import javax.swing.JFrame; + import javax.swing.SwingUtilities; + /** * @author Louis Royer * @author Flavien Haas @@ -15,6 +19,7 @@ public class DownloadSelectionGen extends javax.swing.JPanel { // Variables declaration - do not modify private javax.swing.JButton jButton1; + private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JList jList1; private javax.swing.JScrollPane jScrollPane1; @@ -43,6 +48,7 @@ public class DownloadSelectionGen extends javax.swing.JPanel { jButton1 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jList1 = new javax.swing.JList<>(); + jButton2 = new javax.swing.JButton(); jLabel1.setText("Select a file to download:"); @@ -59,41 +65,59 @@ public class DownloadSelectionGen extends javax.swing.JPanel { public String getElementAt(int i) { return strings[i]; } }); jScrollPane1.setViewportView(jList1); + jList1.setSelectedIndex(0); + jButton2.setText("Back"); + jButton2.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2ActionPerformed(evt); + } + }); - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); - this.setLayout(layout); - layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addComponent(jLabel1) - .addGap(0, 0, Short.MAX_VALUE)) - .addGroup(layout.createSequentialGroup() - .addGap(97, 97, 97) - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap(104, Short.MAX_VALUE)) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(23, 23, 23)) - ); - layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addComponent(jLabel1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton1) - .addContainerGap(24, Short.MAX_VALUE)) - ); - }// - - - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { - // TODO add your handling code here: - } + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addGap(0, 0, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addGap(97, 97, 97) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(104, Short.MAX_VALUE)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGap(25, 25, 25) + .addComponent(jButton2) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(23, 23, 23)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton1) + .addComponent(jButton2)) + .addContainerGap(24, Short.MAX_VALUE)) + ); + }// + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { + ArgumentsGen a = new ArgumentsGen(); + JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); + topFrame.add(a); + this.setVisible(false); + } } -- 2.30.2 From b5c07299d206606c91a938769cc14ccd37184dc2 Mon Sep 17 00:00:00 2001 From: js Date: Thu, 9 Apr 2020 09:32:40 +0200 Subject: [PATCH 05/18] getting the informations for connect with textfield by clicking on connect --- src/gui/ArgumentsGen.java | 42 +++++++++------- src/gui/DownloadSelection.java | 50 ------------------- src/gui/ErrorFrame.java | 87 ++++++++++++++++++++++++++++++++++ src/gui/MainWindow.java | 1 + src/gui/Parameters.java | 54 --------------------- 5 files changed, 113 insertions(+), 121 deletions(-) delete mode 100644 src/gui/DownloadSelection.java create mode 100644 src/gui/ErrorFrame.java delete mode 100644 src/gui/Parameters.java diff --git a/src/gui/ArgumentsGen.java b/src/gui/ArgumentsGen.java index aefd8c9..1c0f719 100644 --- a/src/gui/ArgumentsGen.java +++ b/src/gui/ArgumentsGen.java @@ -14,6 +14,21 @@ package gui; */ public class ArgumentsGen extends javax.swing.JPanel { + + private javax.swing.JButton jButton1; + private javax.swing.JComboBox jComboBox1; + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JLabel jLabel3; + private javax.swing.JLabel jLabel4; + private javax.swing.JLabel jLabel5; + private javax.swing.JLabel jLabel6; + private javax.swing.JTextField jTextField1; + private javax.swing.JTextField jTextField2; + private javax.swing.JTextField jTextField3; + private javax.swing.JTextField jTextField4; + private static final long serialVersionUID = 13L; + /** * Creates new form ArgumentsGen */ @@ -145,23 +160,16 @@ public class ArgumentsGen extends javax.swing.JPanel { } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { - // TODO add your handling code here: + String hostnameServer = jTextField2.getText(); + String portServer = jTextField1.getText(); + String hostnameTracker = jTextField3.getText(); + String portTracker = jTextField4.getText(); + String protocolClient = jComboBox1.getSelectedItem().toString(); + System.out.println("hostnameServer: " + hostnameServer); + System.out.println("portServer: " + portServer); + System.out.println("hostnameTracker: " + hostnameTracker); + System.out.println("portTracker: " + portTracker); + System.out.println("protocol: " + protocolClient); } - - // Variables declaration - do not modify - private javax.swing.JButton jButton1; - private javax.swing.JComboBox jComboBox1; - private javax.swing.JLabel jLabel1; - private javax.swing.JLabel jLabel2; - private javax.swing.JLabel jLabel3; - private javax.swing.JLabel jLabel4; - private javax.swing.JLabel jLabel5; - private javax.swing.JLabel jLabel6; - private javax.swing.JTextField jTextField1; - private javax.swing.JTextField jTextField2; - private javax.swing.JTextField jTextField3; - private javax.swing.JTextField jTextField4; - private static final long serialVersionUID = 13L; - // End of variables declaration } diff --git a/src/gui/DownloadSelection.java b/src/gui/DownloadSelection.java deleted file mode 100644 index 213eae5..0000000 --- a/src/gui/DownloadSelection.java +++ /dev/null @@ -1,50 +0,0 @@ -package gui; - -import javax.swing.JPanel; -import javax.swing.JLabel; -import javax.swing.JButton; -import javax.swing.JTextField; -import javax.swing.JComboBox; -import javax.swing.JList; -import javax.swing.JScrollPane; -import java.awt.BorderLayout; -import java.awt.FlowLayout; -import java.awt.GridLayout; - -public class DownloadSelection extends JPanel { - - private static final long serialVersionUID = 13L; - private String[] listFilesToDownload; - private JList list1; - private JLabel label1; - private JButton downloadButton; - private JScrollPane scroll; - - public DownloadSelection(String[] listFilesToDownload){ - JPanel p1 = new JPanel(); - JPanel p3 = new JPanel(); - JPanel container = new JPanel(); - FlowLayout layout = new FlowLayout(); - p1.setLayout(new GridLayout(1, 2)); - container.setLayout(layout); - p3.setLayout(layout); - this.setLayout(new GridLayout(2, 1)); - this.listFilesToDownload = listFilesToDownload; - this.list1 = new JList<>(listFilesToDownload); - this.label1 = new JLabel("List of files you can download: "); - this.downloadButton = new JButton("download"); - this.list1.setSelectedIndex(0); - scroll = new JScrollPane(list1); - int verticalPolicy = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS; - scroll.setVerticalScrollBarPolicy(verticalPolicy); - //container.add(list1); - container.add(scroll); - p1.add(label1); - p1.add(container); - p3.add(downloadButton); - this.add(p1); - this.add(p3); - setVisible(true); - } - -} diff --git a/src/gui/ErrorFrame.java b/src/gui/ErrorFrame.java new file mode 100644 index 0000000..4f23ce2 --- /dev/null +++ b/src/gui/ErrorFrame.java @@ -0,0 +1,87 @@ +package gui; + + +/* + * 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. + */ + + /** + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + */ +public class ErrorFrame extends javax.swing.JFrame { + + private javax.swing.JLabel jLabel1; + private javax.swing.JToggleButton jToggleButton1; + private String message; + private static final long serialVersionUID = 13L; + // End of variables declaration + + + public ErrorFrame(String message) { + this.message = message; + initComponents(); + this.setVisible(true); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // + private void initComponents() { + + jToggleButton1 = new javax.swing.JToggleButton(); + jLabel1 = new javax.swing.JLabel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + jToggleButton1.setText("Close"); + jToggleButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jToggleButton1ActionPerformed(evt); + } + }); + + jLabel1.setText(message); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1)) + .addGroup(layout.createSequentialGroup() + .addGap(253, 253, 253) + .addComponent(jToggleButton1))) + .addContainerGap(205, Short.MAX_VALUE)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addGap(30, 30, 30) + .addComponent(jToggleButton1) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + + this.setLocationRelativeTo(null); + this.setAlwaysOnTop(true); + pack(); + }// + + private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) { + System.exit(1); + } + + +} diff --git a/src/gui/MainWindow.java b/src/gui/MainWindow.java index ae8b931..d7ac1d4 100644 --- a/src/gui/MainWindow.java +++ b/src/gui/MainWindow.java @@ -19,6 +19,7 @@ public class MainWindow extends JFrame{ //fenetre.add(new DownloadSelection(listTest)); fenetre.add(new DownloadSelectionGen(listTest)); fenetre.setVisible(true); + ErrorFrame erreur = new ErrorFrame("ceci est une erreur"); } } diff --git a/src/gui/Parameters.java b/src/gui/Parameters.java deleted file mode 100644 index a715d1c..0000000 --- a/src/gui/Parameters.java +++ /dev/null @@ -1,54 +0,0 @@ -package gui; - -import javax.swing.JPanel; -import javax.swing.JLabel; -import javax.swing.JButton; -import javax.swing.JTextField; -import javax.swing.JComboBox; -import java.awt.BorderLayout; -import java.awt.FlowLayout; -import java.awt.GridLayout; - -public class Parameters extends JPanel{ - - private static final long serialVersionUID = 13L; - private JLabel label1, label2, label3, label4, label5; - private JTextField field1, field2, field3, field4; - private JButton connectButton; - private JComboBox protocolSelector; - - public Parameters(){ - JPanel p1 = new JPanel(); - p1.setLayout(new GridLayout(5, 2)); - FlowLayout layout = new FlowLayout(); - JPanel p2 = new JPanel(); - p2.setLayout(layout); - String[] elements = {"TCP", "UDP"}; - this.label1 = new JLabel("Server hostname: "); - this.label2 = new JLabel("Server port: "); - this.label3 = new JLabel("Tracker hostname: "); - this.label4 = new JLabel("Tracker port: "); - this.label5 = new JLabel("Client's Protocol: "); - this.field1 = new JTextField(20); - this.field2 = new JTextField(20); - this.field3 = new JTextField(20); - this.field4 = new JTextField(20); - this.connectButton = new JButton("Connect"); - protocolSelector = new JComboBox<>(elements); - p1.add(label1); - p1.add(field1); - p1.add(label2); - p1.add(field2); - p1.add(label3); - p1.add(field3); - p1.add(label4); - p1.add(field4); - p1.add(label5); - p1.add(protocolSelector); - p2.add(connectButton); - this.add(p1, BorderLayout.NORTH); - this.add(p2, BorderLayout.SOUTH); - setVisible(true); - } - -} -- 2.30.2 From 1451b67a5b4850e1817532cf3f094f9a37a852ad Mon Sep 17 00:00:00 2001 From: Louis Royer Date: Fri, 3 Apr 2020 14:33:24 +0200 Subject: [PATCH 06/18] Add .jar for clientGUI --- Jenkinsfile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index 399f133..d1f3664 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -14,6 +14,9 @@ sh 'echo "BUILDING TRACKER"' sh 'echo Main-Class: tracker/Tracker > MANIFEST.MF' sh 'jar -cvmf MANIFEST.MF tracker.jar $(find bin/ -maxdepth 1 -mindepth 1 -printf "-C bin %f\n")' + sh 'echo "BUILDING CLIENT GUI"' + sh 'echo Main-Class: gui/Gui > MANIFEST.MF' + sh 'jar -cvmf MANIFEST.MF clientGUI.jar $(find bin/ -maxdepth 1 -mindepth 1 -printf "-C bin %f\n")' sh 'echo "CREATING sources.tar.gz"' sh 'tar -zcvf sources.tar.gz src/' } @@ -29,6 +32,7 @@ archiveArtifacts artifacts: 'client.jar', fingerprint: true archiveArtifacts artifacts: 'server.jar', fingerprint: true archiveArtifacts artifacts: 'tracker.jar', fingerprint: true + archiveArtifacts artifacts: 'clientGUI.jar', fingerprint:true archiveArtifacts artifacts: 'sources.tar.gz', fingerprint: true } } -- 2.30.2 From b87b3c70ef516e72d46e8103c6294e7dbc6d46dd Mon Sep 17 00:00:00 2001 From: js Date: Fri, 10 Apr 2020 14:03:44 +0200 Subject: [PATCH 07/18] added bugged interactions --- src/gui/ClientP2Pgui.java | 137 ++++++++++++++++++++++++++++++ src/gui/ClientinterfaceGUI.java | 100 ++++++++++++++++++++++ src/gui/DownloadFileGUI.java | 58 +++++++++++++ src/gui/DownloadSelectionGen.java | 16 +++- src/gui/MainWindow.java | 10 +-- 5 files changed, 313 insertions(+), 8 deletions(-) create mode 100644 src/gui/ClientP2Pgui.java create mode 100644 src/gui/ClientinterfaceGUI.java create mode 100644 src/gui/DownloadFileGUI.java diff --git a/src/gui/ClientP2Pgui.java b/src/gui/ClientP2Pgui.java new file mode 100644 index 0000000..97037df --- /dev/null +++ b/src/gui/ClientP2Pgui.java @@ -0,0 +1,137 @@ +package gui; + +import clientP2P.*; +import java.util.List; +import clientP2P.ClientManagementUDP; +import clientP2P.ClientManagementTCP; +import serverP2P.ServerManagementUDP; +import clientP2P.ClientInterfaceCLI; +import serverP2P.ServerManagementTCP; +import tools.Logger; +import tools.LogLevel; +import tools.Directories; +import tools.HostItem; +import tools.ServerPortRange; +import tools.TrackerPortRange; + +/** Client + Server implementation. + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ + +public class ClientP2P { + private String logDir = "logs/"; + private String partsDir = ".parts/"; + private Logger loggerServer; + private Logger loggerClient; + private Directories directories; + private HostItem tracker; + private HostItem server; + final static boolean DEBUG = false; + private String hostnameServer; + private String hostnameTracker; + private int portServer; + private int portTracker; + private String protocolP2P; + + /** Initialize loggers if directories and logger are null, + * else fail silently. + */ + public void initDirectoriesAndLoggers() { + if (directories == null && loggerServer == null && loggerClient == null) { + directories = new Directories("P2P_JAVA_PROJECT_" + server.getPort()); + directories.createSubdir(logDir); + loggerServer = new Logger(directories.getDataHomeDirectory() + logDir + "server.log", DEBUG); + loggerClient = new Logger(directories.getDataHomeDirectory() + logDir + "client.log", DEBUG); + directories.createSubdir(partsDir); + } + } + + /** Constructor. + * @param hostnameServer hostname to bind + * @param portServer port to bind + * @param hostnameTracker hostname of tracker + * @param portTracker port of tracker + */ + public ClientP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker, String protocolP2P) { + + this.hostnameServer = hostnameServer; + this.hostnameTracker = hostnameTracker; + this.portServer = portServer; + this.portTracker = portTracker; + this.protocolP2P = protocolP2P; + + final ServerPortRange serverPortRange = new ServerPortRange(); + final TrackerPortRange trackerPortRange = new TrackerPortRange(); + + if (!serverPortRange.isPortInRange(portServer)){ + ErrorFrame erreur = new ErrorFrame("SERVER: Port not in range. "); + } + + if (!trackerPortRange.isPortInRange(portTracker)){ + ErrorFrame erreur = new ErrorFrame("TRACKER: Port not in range"); + } + + + server = new HostItem(hostnameServer, portServer); + tracker = new HostItem(hostnameTracker, portTracker); + initDirectoriesAndLoggers(); + System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory()); + } + + + public void connect(){ + + System.out.println("using hostname : " + hostnameServer); + System.out.println("using port : " + portServer); + System.out.println("tracker hostname : " + hostnameTracker); + System.out.println("tracker port : " + portTracker); + ServerManagementUDP smudp = new ServerManagementUDP(directories.getDataHomeDirectory(), server, tracker, loggerServer); + ServerManagementTCP smtcp = new ServerManagementTCP(directories.getDataHomeDirectory(), server, tracker, loggerServer); + Thread tudp = new Thread(smudp); + tudp.setName("server UDP P2P-JAVA-PROJECT"); + tudp.start(); + Thread ttcp = new Thread(smtcp); + ttcp.setName("server TCP P2P-JAVA-PROJECT"); + ttcp.start(); + + // Wait a bit before printing client interface + // This is not required, but allow to have a cleaner interface + try { + Thread.sleep(200); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + + Thread tclient; + switch (protocolClient) { + case "UDP": + case "udp": + case "upd": // to avoid users typos + case "2" : + System.out.println("Starting with UDP"); + ClientManagementUDP cmudp = new ClientManagementUDP(directories.getDataHomeDirectory(), tracker, directories.getDataHomeDirectory() + partsDir, loggerClient, server); + tclient = new Thread(new ClientInterfaceCLI(cmudp, loggerClient)); + break; + case "TCP": + case "tcp": + case "1": + default: + System.out.println("Starting with TCP"); + ClientManagementTCP cmtcp = new ClientManagementTCP(directories.getDataHomeDirectory(), tracker, directories.getDataHomeDirectory() + partsDir, loggerClient, server); + tclient = new Thread(new ClientInterfaceCLI(cmtcp, loggerClient)); + break; + } + tclient.setName("client P2P-JAVA-PROJECT"); + tclient.start(); + try { + tclient.join(); + } catch (InterruptedException e) {} + smudp.setStop(); + smtcp.setStop(); + } + + +} diff --git a/src/gui/ClientinterfaceGUI.java b/src/gui/ClientinterfaceGUI.java new file mode 100644 index 0000000..3a60aa6 --- /dev/null +++ b/src/gui/ClientinterfaceGUI.java @@ -0,0 +1,100 @@ +package gui; + +import clientP2P.*; +import clientP2P.ClientInterface; +import clientP2P.ClientManagement; +import tools.SearchFile; +import tools.LogLevel; +import tools.Logger; +import java.util.Scanner; +import localException.ProtocolError; +import localException.InternalError; +import localException.ProtocolError; +import localException.SizeError; +import localException.TransmissionError; +import localException.VersionError; +import localException.SocketClosed; +import remoteException.EmptyFile; +import remoteException.EmptyDirectory; +import remoteException.InternalRemoteError; +import remoteException.NotFound; +import remoteException.ProtocolRemoteError; +import remoteException.VersionRemoteError; +import remoteException.NotATracker; +import remoteException.UnknownHost; +import java.io.IOException; +import java.net.UnknownHostException; +import java.net.SocketException; +import javax.swing.JFrame; + +/** Implementation of P2P-JAVA-PROJECT CLIENT interface for CLI + * @author Louis Royer + * @author Flavien Haas + * @author JS Auge + * @version 1.0 + */ +public class ClientInterfaceCLI extends ClientInterface { + private Scanner scanner; + + /** Constructor with clientManagement, logger and scanner. + * @param clientManagement ClientManagement used + * @param logger Logger used + * @param scanner Scanner used to read input + */ + public ClientInterfaceGUI(ClientManagement clientManagement, Logger logger, Scanner scanner) { + super(clientManagement, logger); + this.scanner = scanner; + } + + /** Implementation of Runnable + */ + + + public void run(){ + boolean isRunning = initHostList(); + if (isRunning) { + try { + int i = 1; + SearchFile searchEngine = new SearchFile(); + int optionSearch = 0; + String searchInput = ""; + String[] list = clientManagement.listDirectory(); + //String[] resultArray = {}; + + + + MainWindow win = new MainWindow(); + win.add(new DownloadSelectionGen(list, clientManagement, logger, scanner)); + } + } + + + /** Initialization of hostList with retry in failure + * @return true if successfully initialized + */ + private boolean initHostList() { + boolean contacted = false; + boolean firstLoop = true; + boolean stop = false; + while (!contacted && !stop) { + try { + if (!firstLoop) { + writeLog("Cannot contact tracker. Try again [Y/n] ?", LogLevel.Error); + String tryAgain = scanner.nextLine(); + if (tryAgain.equals("n") || tryAgain.equals("N")) { + stop = true; + } + } + firstLoop = false; + clientManagement.initHostList(); + contacted = true; + } catch (SocketException e) { + } catch (UnknownHostException e) { + } catch (IOException e) { + } catch (Exception e) { + return false; + } + } + return !stop; + } +} diff --git a/src/gui/DownloadFileGUI.java b/src/gui/DownloadFileGUI.java new file mode 100644 index 0000000..2595c0d --- /dev/null +++ b/src/gui/DownloadFileGUI.java @@ -0,0 +1,58 @@ +package gui; + +import clientP2P.*; +import clientP2P.ClientInterface; +import clientP2P.ClientManagement; +import tools.SearchFile; +import tools.LogLevel; +import tools.Logger; +import java.util.Scanner; +import localException.ProtocolError; +import localException.InternalError; +import localException.ProtocolError; +import localException.SizeError; +import localException.TransmissionError; +import localException.VersionError; +import localException.SocketClosed; +import remoteException.EmptyFile; +import remoteException.EmptyDirectory; +import remoteException.InternalRemoteError; +import remoteException.NotFound; +import remoteException.ProtocolRemoteError; +import remoteException.VersionRemoteError; +import remoteException.NotATracker; +import remoteException.UnknownHost; +import java.io.IOException; +import java.net.UnknownHostException; +import java.net.SocketException; +import javax.swing.JFrame; + +public class DownloadFileGUI extends JPanel{ + + private String fileToDownload; + private ClientManagement clientManagement; + private Logger logger; + private Scanner scanner; + + public DownloadFileGUI(String fileToDownload, ClientManagement clientManagement, Logger logger, Scanner scanner){ + this.fileToDownload = fileToDownload; + this.clientManagement = clientManagement; + this.logger = logger; + this.scanner = scanner; + } + + public void download(){ + try { + clientManagement.download(fileToDownload); + + ErrorFrame erreur = new ErrorFrame("File " + f + " sucessfully downloaded"); + } else { + + ErrorFrame erreur = new ErrorFrame("File " + f + " unsucessfully downloaded, wrong number"); + } + } catch (NumberFormatException e) { + ErrorFrame erreur = new ErrorFrame("File " + f + " unsucessfully downloaded, wrong number"), + } + } + +} diff --git a/src/gui/DownloadSelectionGen.java b/src/gui/DownloadSelectionGen.java index 59f25dd..4f78050 100644 --- a/src/gui/DownloadSelectionGen.java +++ b/src/gui/DownloadSelectionGen.java @@ -9,6 +9,10 @@ package gui; import javax.swing.JFrame; import javax.swing.SwingUtilities; + import clientP2P.*; + import tools.LogLevel; + import tools.Logger; + import java.util.Scanner; /** * @author Louis Royer @@ -25,13 +29,19 @@ public class DownloadSelectionGen extends javax.swing.JPanel { private javax.swing.JScrollPane jScrollPane1; private static final long serialVersionUID = 13L; private String[] listFilesToDownload; + private ClientManagement clientManagement; + private Scanner scanner; + private Logger logger; // End of variables declaration /** * Creates new form ArgumentsGen */ - public DownloadSelectionGen(String[] listFilesToDownload) { + public DownloadSelectionGen(String[] listFilesToDownload, ClientManagement clientManagement, Logger logger, Scanner scanner) { this.listFilesToDownload = listFilesToDownload; + this.clientManagement = clientManagement; + this.logger = logger; + this.scanner = scanner; initComponents(); } @@ -110,7 +120,9 @@ public class DownloadSelectionGen extends javax.swing.JPanel { }// private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { - // TODO add your handling code here: + String fileSelected = jList1.getSelectedValue(); + DownloadFileGUI interface = new DownloadFileGUI(fileSelected, clientManagement, logger, scanner); + DownloadFileGUI.download(); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { diff --git a/src/gui/MainWindow.java b/src/gui/MainWindow.java index d7ac1d4..d6c9638 100644 --- a/src/gui/MainWindow.java +++ b/src/gui/MainWindow.java @@ -12,12 +12,10 @@ public class MainWindow extends JFrame{ fenetre.setSize(550, 250); fenetre.setLocationRelativeTo(null); fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - //fenetre.add(new Parameters()); - //fenetre.add(new ArgumentsGen()); - String[] listTest = {"test", "test2", "test3", "test4", "test5", "test6", "test7", "fichier1", - "fichier2", "fichier3", "fichier4"}; - //fenetre.add(new DownloadSelection(listTest)); - fenetre.add(new DownloadSelectionGen(listTest)); + fenetre.add(new ArgumentsGen()); + //String[] listTest = {"test", "test2", "test3", "test4", "test5", "test6", "test7", "fichier1", + // "fichier2", "fichier3", "fichier4"}; + //fenetre.add(new DownloadSelectionGen(listTest)); fenetre.setVisible(true); ErrorFrame erreur = new ErrorFrame("ceci est une erreur"); } -- 2.30.2 From 20a444cb9b7a5ba2ce1c58f0021c2c4db4701408 Mon Sep 17 00:00:00 2001 From: js Date: Fri, 10 Apr 2020 15:46:30 +0200 Subject: [PATCH 08/18] fixed a lot of things --- src/gui/ArgumentsGen.java | 12 +++-- ...erfaceGUI.java => ClientInterfaceGUI.java} | 51 +++++++++++++------ src/gui/ClientP2Pgui.java | 25 +++++---- src/gui/DownloadFileGUI.java | 14 ++--- src/gui/DownloadSelectionGen.java | 10 ++-- src/gui/MainWindow.java | 2 +- 6 files changed, 67 insertions(+), 47 deletions(-) rename src/gui/{ClientinterfaceGUI.java => ClientInterfaceGUI.java} (62%) diff --git a/src/gui/ArgumentsGen.java b/src/gui/ArgumentsGen.java index 1c0f719..2f3d7f4 100644 --- a/src/gui/ArgumentsGen.java +++ b/src/gui/ArgumentsGen.java @@ -161,15 +161,19 @@ public class ArgumentsGen extends javax.swing.JPanel { private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String hostnameServer = jTextField2.getText(); - String portServer = jTextField1.getText(); + String portServerStr = jTextField1.getText(); String hostnameTracker = jTextField3.getText(); - String portTracker = jTextField4.getText(); + String portTrackerStr = jTextField4.getText(); String protocolClient = jComboBox1.getSelectedItem().toString(); System.out.println("hostnameServer: " + hostnameServer); - System.out.println("portServer: " + portServer); + System.out.println("portServer: " + portServerStr); System.out.println("hostnameTracker: " + hostnameTracker); - System.out.println("portTracker: " + portTracker); + System.out.println("portTracker: " + portTrackerStr); System.out.println("protocol: " + protocolClient); + int portServer = Integer.parseInt(portServerStr); + int portTracker = Integer.parseInt(portTrackerStr); + ClientP2Pgui c = new ClientP2Pgui(hostnameServer, portServer, hostnameTracker, portTracker, protocolClient); + c.connect(); } } diff --git a/src/gui/ClientinterfaceGUI.java b/src/gui/ClientInterfaceGUI.java similarity index 62% rename from src/gui/ClientinterfaceGUI.java rename to src/gui/ClientInterfaceGUI.java index 3a60aa6..892b53f 100644 --- a/src/gui/ClientinterfaceGUI.java +++ b/src/gui/ClientInterfaceGUI.java @@ -6,7 +6,6 @@ import clientP2P.ClientManagement; import tools.SearchFile; import tools.LogLevel; import tools.Logger; -import java.util.Scanner; import localException.ProtocolError; import localException.InternalError; import localException.ProtocolError; @@ -33,17 +32,19 @@ import javax.swing.JFrame; * @author JS Auge * @version 1.0 */ -public class ClientInterfaceCLI extends ClientInterface { - private Scanner scanner; +public class ClientInterfaceGUI extends ClientInterface { /** Constructor with clientManagement, logger and scanner. * @param clientManagement ClientManagement used * @param logger Logger used * @param scanner Scanner used to read input */ - public ClientInterfaceGUI(ClientManagement clientManagement, Logger logger, Scanner scanner) { + + Logger loggerC; + + public ClientInterfaceGUI(ClientManagement clientManagement, Logger logger) { super(clientManagement, logger); - this.scanner = scanner; + this.loggerC = logger; } /** Implementation of Runnable @@ -53,20 +54,42 @@ public class ClientInterfaceCLI extends ClientInterface { public void run(){ boolean isRunning = initHostList(); if (isRunning) { - try { + try{ int i = 1; SearchFile searchEngine = new SearchFile(); int optionSearch = 0; String searchInput = ""; String[] list = clientManagement.listDirectory(); - //String[] resultArray = {}; + MainWindow win = new MainWindow(); + win.add(new DownloadSelectionGen(list, clientManagement, loggerC)); + } catch (EmptyDirectory e) { + writeLog("Server has no file in directory", LogLevel.Error); + } + catch (InternalError e) { + writeLog("Client internal error", LogLevel.Error); + } catch (UnknownHostException e) { + writeLog("Server host is unknown", LogLevel.Error); + } catch (IOException e) { + writeLog("Request cannot be send or response cannot be received", LogLevel.Error); + } catch (TransmissionError e) { + writeLog("Message received is too big", LogLevel.Error); + } catch (ProtocolError e) { + writeLog("Cannot decode server’s response", LogLevel.Error); + } catch (VersionError e) { + writeLog("Server’s response use bad version of the protocol", LogLevel.Error); + } catch (SizeError e) { + writeLog("Cannot handle this packets because of internal representation limitations of numbers on the client", LogLevel.Error); + } catch (InternalRemoteError e) { + writeLog("Server internal error", LogLevel.Error); + } catch (ProtocolRemoteError e) { + writeLog("Server cannot decode client’s request", LogLevel.Error); + } catch (VersionRemoteError e) { + writeLog("Server cannot decode this version of the protocol", LogLevel.Error); - - MainWindow win = new MainWindow(); - win.add(new DownloadSelectionGen(list, clientManagement, logger, scanner)); } } + } /** Initialization of hostList with retry in failure @@ -79,11 +102,7 @@ public class ClientInterfaceCLI extends ClientInterface { while (!contacted && !stop) { try { if (!firstLoop) { - writeLog("Cannot contact tracker. Try again [Y/n] ?", LogLevel.Error); - String tryAgain = scanner.nextLine(); - if (tryAgain.equals("n") || tryAgain.equals("N")) { - stop = true; - } + writeLog("Cannot contact tracker... ", LogLevel.Error); } firstLoop = false; clientManagement.initHostList(); @@ -97,4 +116,6 @@ public class ClientInterfaceCLI extends ClientInterface { } return !stop; } + + } diff --git a/src/gui/ClientP2Pgui.java b/src/gui/ClientP2Pgui.java index 97037df..f31f884 100644 --- a/src/gui/ClientP2Pgui.java +++ b/src/gui/ClientP2Pgui.java @@ -21,7 +21,7 @@ import tools.TrackerPortRange; * @version 1.0 */ -public class ClientP2P { +public class ClientP2Pgui { private String logDir = "logs/"; private String partsDir = ".parts/"; private Logger loggerServer; @@ -55,26 +55,29 @@ public class ClientP2P { * @param hostnameTracker hostname of tracker * @param portTracker port of tracker */ - public ClientP2P(String hostnameServer, int portServer, String hostnameTracker, int portTracker, String protocolP2P) { + public ClientP2Pgui(String hostnameServer, int portServer, String hostnameTracker, int portTracker, String protocolP2P) { + + if (directories == null && loggerServer == null && loggerClient == null) { + directories = new Directories("P2P_JAVA_PROJECT_" + server.getPort()); + directories.createSubdir(logDir); + loggerServer = new Logger(directories.getDataHomeDirectory() + logDir + "server.log", DEBUG); + loggerClient = new Logger(directories.getDataHomeDirectory() + logDir + "client.log", DEBUG); + directories.createSubdir(partsDir); + } this.hostnameServer = hostnameServer; this.hostnameTracker = hostnameTracker; this.portServer = portServer; this.portTracker = portTracker; this.protocolP2P = protocolP2P; - final ServerPortRange serverPortRange = new ServerPortRange(); final TrackerPortRange trackerPortRange = new TrackerPortRange(); - if (!serverPortRange.isPortInRange(portServer)){ ErrorFrame erreur = new ErrorFrame("SERVER: Port not in range. "); } - if (!trackerPortRange.isPortInRange(portTracker)){ ErrorFrame erreur = new ErrorFrame("TRACKER: Port not in range"); } - - server = new HostItem(hostnameServer, portServer); tracker = new HostItem(hostnameTracker, portTracker); initDirectoriesAndLoggers(); @@ -106,14 +109,14 @@ public class ClientP2P { } Thread tclient; - switch (protocolClient) { + switch (protocolP2P) { case "UDP": case "udp": case "upd": // to avoid users typos case "2" : System.out.println("Starting with UDP"); ClientManagementUDP cmudp = new ClientManagementUDP(directories.getDataHomeDirectory(), tracker, directories.getDataHomeDirectory() + partsDir, loggerClient, server); - tclient = new Thread(new ClientInterfaceCLI(cmudp, loggerClient)); + tclient = new Thread(new ClientInterfaceGUI(cmudp, loggerClient)); break; case "TCP": case "tcp": @@ -121,10 +124,10 @@ public class ClientP2P { default: System.out.println("Starting with TCP"); ClientManagementTCP cmtcp = new ClientManagementTCP(directories.getDataHomeDirectory(), tracker, directories.getDataHomeDirectory() + partsDir, loggerClient, server); - tclient = new Thread(new ClientInterfaceCLI(cmtcp, loggerClient)); + tclient = new Thread(new ClientInterfaceGUI(cmtcp, loggerClient)); break; } - tclient.setName("client P2P-JAVA-PROJECT"); + tclient.setName("client P2P-JAVA-PROJECT GUI"); tclient.start(); try { tclient.join(); diff --git a/src/gui/DownloadFileGUI.java b/src/gui/DownloadFileGUI.java index 2595c0d..9fac8fe 100644 --- a/src/gui/DownloadFileGUI.java +++ b/src/gui/DownloadFileGUI.java @@ -6,7 +6,6 @@ import clientP2P.ClientManagement; import tools.SearchFile; import tools.LogLevel; import tools.Logger; -import java.util.Scanner; import localException.ProtocolError; import localException.InternalError; import localException.ProtocolError; @@ -26,32 +25,27 @@ import java.io.IOException; import java.net.UnknownHostException; import java.net.SocketException; import javax.swing.JFrame; +import javax.swing.JPanel; public class DownloadFileGUI extends JPanel{ private String fileToDownload; private ClientManagement clientManagement; private Logger logger; - private Scanner scanner; - public DownloadFileGUI(String fileToDownload, ClientManagement clientManagement, Logger logger, Scanner scanner){ + public DownloadFileGUI(String fileToDownload, ClientManagement clientManagement, Logger logger){ this.fileToDownload = fileToDownload; this.clientManagement = clientManagement; this.logger = logger; - this.scanner = scanner; } public void download(){ try { clientManagement.download(fileToDownload); - ErrorFrame erreur = new ErrorFrame("File " + f + " sucessfully downloaded"); - } else { - - ErrorFrame erreur = new ErrorFrame("File " + f + " unsucessfully downloaded, wrong number"); - } + ErrorFrame erreur = new ErrorFrame("File " + fileToDownload + " sucessfully downloaded"); } catch (NumberFormatException e) { - ErrorFrame erreur = new ErrorFrame("File " + f + " unsucessfully downloaded, wrong number"), + ErrorFrame erreur = new ErrorFrame("File " + fileToDownload + " unsucessfully downloaded, wrong number"); } } diff --git a/src/gui/DownloadSelectionGen.java b/src/gui/DownloadSelectionGen.java index 4f78050..95a861c 100644 --- a/src/gui/DownloadSelectionGen.java +++ b/src/gui/DownloadSelectionGen.java @@ -12,7 +12,6 @@ package gui; import clientP2P.*; import tools.LogLevel; import tools.Logger; - import java.util.Scanner; /** * @author Louis Royer @@ -30,18 +29,16 @@ public class DownloadSelectionGen extends javax.swing.JPanel { private static final long serialVersionUID = 13L; private String[] listFilesToDownload; private ClientManagement clientManagement; - private Scanner scanner; private Logger logger; // End of variables declaration /** * Creates new form ArgumentsGen */ - public DownloadSelectionGen(String[] listFilesToDownload, ClientManagement clientManagement, Logger logger, Scanner scanner) { + public DownloadSelectionGen(String[] listFilesToDownload, ClientManagement clientManagement, Logger logger) { this.listFilesToDownload = listFilesToDownload; this.clientManagement = clientManagement; this.logger = logger; - this.scanner = scanner; initComponents(); } @@ -121,8 +118,9 @@ public class DownloadSelectionGen extends javax.swing.JPanel { private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String fileSelected = jList1.getSelectedValue(); - DownloadFileGUI interface = new DownloadFileGUI(fileSelected, clientManagement, logger, scanner); - DownloadFileGUI.download(); + System.out.println("File to download: " + fileSelected); + //DownloadFileGUI interface = new DownloadFileGUI(fileSelected, clientManagement, logger, scanner); + //DownloadFileGUI.download(); } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { diff --git a/src/gui/MainWindow.java b/src/gui/MainWindow.java index d6c9638..5ee4428 100644 --- a/src/gui/MainWindow.java +++ b/src/gui/MainWindow.java @@ -17,7 +17,7 @@ public class MainWindow extends JFrame{ // "fichier2", "fichier3", "fichier4"}; //fenetre.add(new DownloadSelectionGen(listTest)); fenetre.setVisible(true); - ErrorFrame erreur = new ErrorFrame("ceci est une erreur"); + //ErrorFrame erreur = new ErrorFrame("ceci est une erreur"); } } -- 2.30.2 From 53b7d46fe263571a1b23bd298995645e7be6c07c Mon Sep 17 00:00:00 2001 From: js Date: Fri, 10 Apr 2020 15:54:43 +0200 Subject: [PATCH 09/18] fixed compilation --- src/gui/DownloadFileGUI.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gui/DownloadFileGUI.java b/src/gui/DownloadFileGUI.java index 9fac8fe..561af08 100644 --- a/src/gui/DownloadFileGUI.java +++ b/src/gui/DownloadFileGUI.java @@ -39,9 +39,9 @@ public class DownloadFileGUI extends JPanel{ this.logger = logger; } - public void download(){ + public void download() { try { - clientManagement.download(fileToDownload); + //clientManagement.download(fileToDownload); ErrorFrame erreur = new ErrorFrame("File " + fileToDownload + " sucessfully downloaded"); } catch (NumberFormatException e) { -- 2.30.2 From 13ccc39f141d4902ab11fd95d215579fd5d09e29 Mon Sep 17 00:00:00 2001 From: js Date: Fri, 10 Apr 2020 15:58:36 +0200 Subject: [PATCH 10/18] fix warning serial version --- src/gui/DownloadFileGUI.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/gui/DownloadFileGUI.java b/src/gui/DownloadFileGUI.java index 561af08..a66849a 100644 --- a/src/gui/DownloadFileGUI.java +++ b/src/gui/DownloadFileGUI.java @@ -27,11 +27,14 @@ import java.net.SocketException; import javax.swing.JFrame; import javax.swing.JPanel; + public class DownloadFileGUI extends JPanel{ private String fileToDownload; private ClientManagement clientManagement; private Logger logger; + private static final long serialVersionUID = 13L; + public DownloadFileGUI(String fileToDownload, ClientManagement clientManagement, Logger logger){ this.fileToDownload = fileToDownload; -- 2.30.2 From 27c4fcc5638de974bc6e5d174a9416457d90969c Mon Sep 17 00:00:00 2001 From: js Date: Fri, 10 Apr 2020 16:19:54 +0200 Subject: [PATCH 11/18] fix warning --- src/gui/ClientP2Pgui.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/gui/ClientP2Pgui.java b/src/gui/ClientP2Pgui.java index f31f884..3c1eea1 100644 --- a/src/gui/ClientP2Pgui.java +++ b/src/gui/ClientP2Pgui.java @@ -57,14 +57,6 @@ public class ClientP2Pgui { */ public ClientP2Pgui(String hostnameServer, int portServer, String hostnameTracker, int portTracker, String protocolP2P) { - if (directories == null && loggerServer == null && loggerClient == null) { - directories = new Directories("P2P_JAVA_PROJECT_" + server.getPort()); - directories.createSubdir(logDir); - loggerServer = new Logger(directories.getDataHomeDirectory() + logDir + "server.log", DEBUG); - loggerClient = new Logger(directories.getDataHomeDirectory() + logDir + "client.log", DEBUG); - directories.createSubdir(partsDir); - } - this.hostnameServer = hostnameServer; this.hostnameTracker = hostnameTracker; this.portServer = portServer; -- 2.30.2 From e1fdddac54dc72807c69cf7ace7a1ba878b0a5c5 Mon Sep 17 00:00:00 2001 From: js Date: Fri, 10 Apr 2020 16:51:18 +0200 Subject: [PATCH 12/18] now display default values for each field --- src/gui/ArgumentsGen.java | 8 ++++---- src/gui/ClientInterfaceGUI.java | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/gui/ArgumentsGen.java b/src/gui/ArgumentsGen.java index 2f3d7f4..13ae039 100644 --- a/src/gui/ArgumentsGen.java +++ b/src/gui/ArgumentsGen.java @@ -52,10 +52,10 @@ public class ArgumentsGen extends javax.swing.JPanel { jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox<>(); - jTextField1 = new javax.swing.JTextField(); - jTextField2 = new javax.swing.JTextField(); - jTextField3 = new javax.swing.JTextField(); - jTextField4 = new javax.swing.JTextField(); + jTextField1 = new javax.swing.JTextField("7071"); + jTextField2 = new javax.swing.JTextField("localhost"); + jTextField3 = new javax.swing.JTextField("localhost"); + jTextField4 = new javax.swing.JTextField("6969"); jButton1 = new javax.swing.JButton(); jLabel1.setText("Enter connection informations:"); diff --git a/src/gui/ClientInterfaceGUI.java b/src/gui/ClientInterfaceGUI.java index 892b53f..1523e38 100644 --- a/src/gui/ClientInterfaceGUI.java +++ b/src/gui/ClientInterfaceGUI.java @@ -60,7 +60,8 @@ public class ClientInterfaceGUI extends ClientInterface { int optionSearch = 0; String searchInput = ""; String[] list = clientManagement.listDirectory(); - MainWindow win = new MainWindow(); + JFrame win = new JFrame(); + win.setVisible(true); win.add(new DownloadSelectionGen(list, clientManagement, loggerC)); } catch (EmptyDirectory e) { writeLog("Server has no file in directory", LogLevel.Error); -- 2.30.2 From 4b069392578049bfa1d699d929e1997a47e352d9 Mon Sep 17 00:00:00 2001 From: js Date: Sat, 11 Apr 2020 10:06:16 +0200 Subject: [PATCH 13/18] now list all the files, download button just display on console the file ou want to download --- src/gui/ArgumentsGen.java | 5 +++++ src/gui/ClientInterfaceGUI.java | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/gui/ArgumentsGen.java b/src/gui/ArgumentsGen.java index 13ae039..ec06267 100644 --- a/src/gui/ArgumentsGen.java +++ b/src/gui/ArgumentsGen.java @@ -7,6 +7,9 @@ package gui; * and open the template in the editor. */ + import javax.swing.JFrame; + import javax.swing.SwingUtilities; + /** * @author Louis Royer * @author Flavien Haas @@ -174,6 +177,8 @@ public class ArgumentsGen extends javax.swing.JPanel { int portTracker = Integer.parseInt(portTrackerStr); ClientP2Pgui c = new ClientP2Pgui(hostnameServer, portServer, hostnameTracker, portTracker, protocolClient); c.connect(); + JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); + topFrame.dispose(); } } diff --git a/src/gui/ClientInterfaceGUI.java b/src/gui/ClientInterfaceGUI.java index 1523e38..0d713da 100644 --- a/src/gui/ClientInterfaceGUI.java +++ b/src/gui/ClientInterfaceGUI.java @@ -60,9 +60,17 @@ public class ClientInterfaceGUI extends ClientInterface { int optionSearch = 0; String searchInput = ""; String[] list = clientManagement.listDirectory(); + System.out.println("running ClientInterfaceGUI and creating window"); + for(String listItem: list){ + System.out.println(listItem); + } JFrame win = new JFrame(); - win.setVisible(true); + win.setTitle("Client"); + win.setSize(550, 250); + win.setLocationRelativeTo(null); + win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); win.add(new DownloadSelectionGen(list, clientManagement, loggerC)); + win.setVisible(true); } catch (EmptyDirectory e) { writeLog("Server has no file in directory", LogLevel.Error); } -- 2.30.2 From 6331f9f1442917aa4f6189d02acc7de7e55f611c Mon Sep 17 00:00:00 2001 From: js Date: Sat, 11 Apr 2020 10:48:03 +0200 Subject: [PATCH 14/18] fixed name of ClientP2PGUI and updated javadoc for GUI --- src/gui/ArgumentsGen.java | 15 +++++---------- src/gui/ClientInterfaceGUI.java | 9 ++++----- src/gui/{ClientP2Pgui.java => ClientP2PGUI.java} | 9 +++++---- src/gui/DownloadSelectionGen.java | 11 +++++++---- src/gui/ErrorFrame.java | 11 ++++++++--- src/gui/Gui.java | 8 ++++++++ src/gui/MainWindow.java | 13 +++++++++---- 7 files changed, 46 insertions(+), 30 deletions(-) rename src/gui/{ClientP2Pgui.java => ClientP2PGUI.java} (95%) diff --git a/src/gui/ArgumentsGen.java b/src/gui/ArgumentsGen.java index ec06267..7e70511 100644 --- a/src/gui/ArgumentsGen.java +++ b/src/gui/ArgumentsGen.java @@ -1,12 +1,5 @@ package gui; - -/* - * 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. - */ - import javax.swing.JFrame; import javax.swing.SwingUtilities; @@ -14,6 +7,7 @@ package gui; * @author Louis Royer * @author Flavien Haas * @author JS Auge +*@version 1.0 */ public class ArgumentsGen extends javax.swing.JPanel { @@ -41,9 +35,8 @@ public class ArgumentsGen extends javax.swing.JPanel { /** * This method is called from within the constructor to initialize the form. - * WARNING: Do NOT modify this code. The content of this method is always - * regenerated by the Form Editor. */ + @SuppressWarnings("unchecked") // private void initComponents() { @@ -162,6 +155,8 @@ public class ArgumentsGen extends javax.swing.JPanel { // TODO add your handling code here: } + +/** Actions when click on connect: creat a clientP2Pgui with the infos from textfields*/ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String hostnameServer = jTextField2.getText(); String portServerStr = jTextField1.getText(); @@ -175,7 +170,7 @@ public class ArgumentsGen extends javax.swing.JPanel { System.out.println("protocol: " + protocolClient); int portServer = Integer.parseInt(portServerStr); int portTracker = Integer.parseInt(portTrackerStr); - ClientP2Pgui c = new ClientP2Pgui(hostnameServer, portServer, hostnameTracker, portTracker, protocolClient); + ClientP2PGUI c = new ClientP2PGUI(hostnameServer, portServer, hostnameTracker, portTracker, protocolClient); c.connect(); JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); topFrame.dispose(); diff --git a/src/gui/ClientInterfaceGUI.java b/src/gui/ClientInterfaceGUI.java index 0d713da..76f819b 100644 --- a/src/gui/ClientInterfaceGUI.java +++ b/src/gui/ClientInterfaceGUI.java @@ -37,7 +37,6 @@ public class ClientInterfaceGUI extends ClientInterface { /** Constructor with clientManagement, logger and scanner. * @param clientManagement ClientManagement used * @param logger Logger used - * @param scanner Scanner used to read input */ Logger loggerC; @@ -60,10 +59,10 @@ public class ClientInterfaceGUI extends ClientInterface { int optionSearch = 0; String searchInput = ""; String[] list = clientManagement.listDirectory(); - System.out.println("running ClientInterfaceGUI and creating window"); - for(String listItem: list){ - System.out.println(listItem); - } + //System.out.println("running ClientInterfaceGUI and creating window"); + //for(String listItem: list){ + //System.out.println(listItem); + //} JFrame win = new JFrame(); win.setTitle("Client"); win.setSize(550, 250); diff --git a/src/gui/ClientP2Pgui.java b/src/gui/ClientP2PGUI.java similarity index 95% rename from src/gui/ClientP2Pgui.java rename to src/gui/ClientP2PGUI.java index 3c1eea1..10b377e 100644 --- a/src/gui/ClientP2Pgui.java +++ b/src/gui/ClientP2PGUI.java @@ -14,14 +14,14 @@ import tools.HostItem; import tools.ServerPortRange; import tools.TrackerPortRange; -/** Client + Server implementation. +/** Client + Server implementation in GUI * @author Louis Royer * @author Flavien Haas * @author JS Auge * @version 1.0 */ -public class ClientP2Pgui { +public class ClientP2PGUI { private String logDir = "logs/"; private String partsDir = ".parts/"; private Logger loggerServer; @@ -54,8 +54,9 @@ public class ClientP2Pgui { * @param portServer port to bind * @param hostnameTracker hostname of tracker * @param portTracker port of tracker + * @param protocolP2P protocol used */ - public ClientP2Pgui(String hostnameServer, int portServer, String hostnameTracker, int portTracker, String protocolP2P) { + public ClientP2PGUI(String hostnameServer, int portServer, String hostnameTracker, int portTracker, String protocolP2P) { this.hostnameServer = hostnameServer; this.hostnameTracker = hostnameTracker; @@ -76,9 +77,9 @@ public class ClientP2Pgui { System.out.println("Server will listen on port " + portServer + " and serve files from " + directories.getDataHomeDirectory()); } +/**Initiate a connection using fields from ArgumentsGen*/ public void connect(){ - System.out.println("using hostname : " + hostnameServer); System.out.println("using port : " + portServer); System.out.println("tracker hostname : " + hostnameTracker); diff --git a/src/gui/DownloadSelectionGen.java b/src/gui/DownloadSelectionGen.java index 95a861c..c9c8d8b 100644 --- a/src/gui/DownloadSelectionGen.java +++ b/src/gui/DownloadSelectionGen.java @@ -17,10 +17,10 @@ package gui; * @author Louis Royer * @author Flavien Haas * @author JS Auge +* @version 1.0 */ public class DownloadSelectionGen extends javax.swing.JPanel { - // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; @@ -30,9 +30,11 @@ public class DownloadSelectionGen extends javax.swing.JPanel { private String[] listFilesToDownload; private ClientManagement clientManagement; private Logger logger; - // End of variables declaration /** + * @param listFilesToDownload list of files to display + * @param clientManagement clientManagement + * @param logger logger * Creates new form ArgumentsGen */ public DownloadSelectionGen(String[] listFilesToDownload, ClientManagement clientManagement, Logger logger) { @@ -44,8 +46,6 @@ public class DownloadSelectionGen extends javax.swing.JPanel { /** * This method is called from within the constructor to initialize the form. - * WARNING: Do NOT modify this code. The content of this method is always - * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // @@ -116,6 +116,8 @@ public class DownloadSelectionGen extends javax.swing.JPanel { ); }// + + /** Actions to initiate when button "Download" is pressed */ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String fileSelected = jList1.getSelectedValue(); System.out.println("File to download: " + fileSelected); @@ -123,6 +125,7 @@ public class DownloadSelectionGen extends javax.swing.JPanel { //DownloadFileGUI.download(); } + /** Actions to initiate when button "Back" is initiate */ private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { ArgumentsGen a = new ArgumentsGen(); JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); diff --git a/src/gui/ErrorFrame.java b/src/gui/ErrorFrame.java index 4f23ce2..be94d56 100644 --- a/src/gui/ErrorFrame.java +++ b/src/gui/ErrorFrame.java @@ -11,6 +11,7 @@ package gui; * @author Louis Royer * @author Flavien Haas * @author JS Auge + * @version 1.0 */ public class ErrorFrame extends javax.swing.JFrame { @@ -18,9 +19,13 @@ public class ErrorFrame extends javax.swing.JFrame { private javax.swing.JToggleButton jToggleButton1; private String message; private static final long serialVersionUID = 13L; - // End of variables declaration + /** + * @param message error message that will be displayed + * Create new Errorframe, used to display an error and stop the program when needed + */ + public ErrorFrame(String message) { this.message = message; initComponents(); @@ -29,8 +34,6 @@ public class ErrorFrame extends javax.swing.JFrame { /** * This method is called from within the constructor to initialize the form. - * WARNING: Do NOT modify this code. The content of this method is always - * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // @@ -79,6 +82,8 @@ public class ErrorFrame extends javax.swing.JFrame { pack(); }// + + /**force exit the program when the button "Close" is pressed*/ private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) { System.exit(1); } diff --git a/src/gui/Gui.java b/src/gui/Gui.java index 748e215..1272a84 100644 --- a/src/gui/Gui.java +++ b/src/gui/Gui.java @@ -2,6 +2,14 @@ package gui; import javax.swing.JFrame; +/** Class to download file +* @author Louis Royer +* @author Flavien Haas +* @author JS Auge +* @version 1.0 +* Class used to create the Graphical interface +*/ + public class Gui{ public static void main(String[] args) { diff --git a/src/gui/MainWindow.java b/src/gui/MainWindow.java index 5ee4428..fd3dbe7 100644 --- a/src/gui/MainWindow.java +++ b/src/gui/MainWindow.java @@ -2,6 +2,15 @@ package gui; import javax.swing.JFrame; +/** Class to download file +* @author Louis Royer +* @author Flavien Haas +* @author JS Auge +* @version 1.0 +* Class used to create the main window of the GUI +*/ + + public class MainWindow extends JFrame{ private static final long serialVersionUID = 13L; @@ -13,11 +22,7 @@ public class MainWindow extends JFrame{ fenetre.setLocationRelativeTo(null); fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fenetre.add(new ArgumentsGen()); - //String[] listTest = {"test", "test2", "test3", "test4", "test5", "test6", "test7", "fichier1", - // "fichier2", "fichier3", "fichier4"}; - //fenetre.add(new DownloadSelectionGen(listTest)); fenetre.setVisible(true); - //ErrorFrame erreur = new ErrorFrame("ceci est une erreur"); } } -- 2.30.2 From 0a1cce3127eeb540a541d3fc30c50ce806da853b Mon Sep 17 00:00:00 2001 From: js Date: Sat, 11 Apr 2020 15:30:34 +0200 Subject: [PATCH 15/18] added ErrorFrame support --- src/gui/ClientInterfaceGUI.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/gui/ClientInterfaceGUI.java b/src/gui/ClientInterfaceGUI.java index 76f819b..58486ce 100644 --- a/src/gui/ClientInterfaceGUI.java +++ b/src/gui/ClientInterfaceGUI.java @@ -72,29 +72,38 @@ public class ClientInterfaceGUI extends ClientInterface { win.setVisible(true); } catch (EmptyDirectory e) { writeLog("Server has no file in directory", LogLevel.Error); + ErrorFrame err = new ErrorFrame("Server has no file in directory"); } catch (InternalError e) { writeLog("Client internal error", LogLevel.Error); + ErrorFrame err = new ErrorFrame("Client internal error"); } catch (UnknownHostException e) { writeLog("Server host is unknown", LogLevel.Error); + ErrorFrame err = new ErrorFrame("Server host is unknown"); } catch (IOException e) { writeLog("Request cannot be send or response cannot be received", LogLevel.Error); + ErrorFrame err = new ErrorFrame("Request cannot be send or response cannot be received"); } catch (TransmissionError e) { writeLog("Message received is too big", LogLevel.Error); + ErrorFrame err = new ErrorFrame("Message received is too big"); } catch (ProtocolError e) { writeLog("Cannot decode server’s response", LogLevel.Error); + ErrorFrame err = new ErrorFrame("Cannot decode server’s response"); } catch (VersionError e) { writeLog("Server’s response use bad version of the protocol", LogLevel.Error); + ErrorFrame err = new ErrorFrame("Server’s response use bad version of the protocol"); } catch (SizeError e) { writeLog("Cannot handle this packets because of internal representation limitations of numbers on the client", LogLevel.Error); + ErrorFrame err = new ErrorFrame("Cannot handle this packets because of internal representation limitations of numbers on the client"); } catch (InternalRemoteError e) { writeLog("Server internal error", LogLevel.Error); + ErrorFrame err = new ErrorFrame("Server internal error"); } catch (ProtocolRemoteError e) { writeLog("Server cannot decode client’s request", LogLevel.Error); + ErrorFrame err = new ErrorFrame("Server cannot decode client’s request"); } catch (VersionRemoteError e) { writeLog("Server cannot decode this version of the protocol", LogLevel.Error); - - + ErrorFrame err = new ErrorFrame("Server cannot decode this version of the protocol"); } } } -- 2.30.2 From a646836f979f91499ebe1149b6d0e673a98f90d1 Mon Sep 17 00:00:00 2001 From: js Date: Sat, 11 Apr 2020 16:01:09 +0200 Subject: [PATCH 16/18] added download features but thrown internal error --- src/gui/DownloadFileGUI.java | 28 ++++++++++++++++++++++++++-- src/gui/DownloadSelectionGen.java | 4 ++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/gui/DownloadFileGUI.java b/src/gui/DownloadFileGUI.java index a66849a..384c8bc 100644 --- a/src/gui/DownloadFileGUI.java +++ b/src/gui/DownloadFileGUI.java @@ -44,11 +44,35 @@ public class DownloadFileGUI extends JPanel{ public void download() { try { - //clientManagement.download(fileToDownload); - + System.out.println("Downloading " + fileToDownload + " ... "); + clientManagement.download(fileToDownload); ErrorFrame erreur = new ErrorFrame("File " + fileToDownload + " sucessfully downloaded"); } catch (NumberFormatException e) { ErrorFrame erreur = new ErrorFrame("File " + fileToDownload + " unsucessfully downloaded, wrong number"); + } catch (InternalError e) { + ErrorFrame err = new ErrorFrame("Client internal error"); + } catch (UnknownHostException e) { + ErrorFrame err = new ErrorFrame("Server host is unknown"); + } catch (IOException e) { + ErrorFrame err = new ErrorFrame("Request cannot be send or response cannot be received"); + } catch (TransmissionError e) { + ErrorFrame err = new ErrorFrame("Message received is too big"); + } catch (ProtocolError e) { + ErrorFrame err = new ErrorFrame("Cannot decode server’s response"); + } catch (VersionError e) { + ErrorFrame err = new ErrorFrame("Server’s response use bad version of the protocol"); + } catch (SizeError e) { + ErrorFrame err = new ErrorFrame("Cannot handle this packets because of internal representation limitations of numbers on the client"); + } catch (InternalRemoteError e) { + ErrorFrame err = new ErrorFrame("Server internal error"); + } catch (ProtocolRemoteError e) { + ErrorFrame err = new ErrorFrame("Server cannot decode client’s request"); + } catch (VersionRemoteError e) { + ErrorFrame err = new ErrorFrame("Server cannot decode this version of the protocol"); + } catch (NotFound e) { + ErrorFrame err = new ErrorFrame("Server has not this file in directory"); + } catch (EmptyFile e) { + ErrorFrame err = new ErrorFrame("File is empty"); } } diff --git a/src/gui/DownloadSelectionGen.java b/src/gui/DownloadSelectionGen.java index c9c8d8b..4785687 100644 --- a/src/gui/DownloadSelectionGen.java +++ b/src/gui/DownloadSelectionGen.java @@ -121,8 +121,8 @@ 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 interface = new DownloadFileGUI(fileSelected, clientManagement, logger, scanner); - //DownloadFileGUI.download(); + DownloadFileGUI dl = new DownloadFileGUI(fileSelected, clientManagement, logger); + dl.download(); } /** Actions to initiate when button "Back" is initiate */ -- 2.30.2 From d775ee7cbb3985871f6ad4aa76d851f140e778ee Mon Sep 17 00:00:00 2001 From: js Date: Sun, 12 Apr 2020 17:54:59 +0200 Subject: [PATCH 17/18] replaced spaces with tabs + right default server port --- src/gui/ArgumentsGen.java | 288 +++++++++++++++--------------- src/gui/DownloadSelectionGen.java | 158 ++++++++-------- src/gui/ErrorFrame.java | 100 +++++------ 3 files changed, 273 insertions(+), 273 deletions(-) diff --git a/src/gui/ArgumentsGen.java b/src/gui/ArgumentsGen.java index 7e70511..c7bb5d3 100644 --- a/src/gui/ArgumentsGen.java +++ b/src/gui/ArgumentsGen.java @@ -1,7 +1,7 @@ package gui; - import javax.swing.JFrame; - import javax.swing.SwingUtilities; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; /** * @author Louis Royer @@ -12,152 +12,152 @@ package gui; public class ArgumentsGen extends javax.swing.JPanel { - private javax.swing.JButton jButton1; - private javax.swing.JComboBox jComboBox1; - private javax.swing.JLabel jLabel1; - private javax.swing.JLabel jLabel2; - private javax.swing.JLabel jLabel3; - private javax.swing.JLabel jLabel4; - private javax.swing.JLabel jLabel5; - private javax.swing.JLabel jLabel6; - private javax.swing.JTextField jTextField1; - private javax.swing.JTextField jTextField2; - private javax.swing.JTextField jTextField3; - private javax.swing.JTextField jTextField4; + private javax.swing.JButton jButton1; + private javax.swing.JComboBox jComboBox1; + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JLabel jLabel3; + private javax.swing.JLabel jLabel4; + private javax.swing.JLabel jLabel5; + private javax.swing.JLabel jLabel6; + private javax.swing.JTextField jTextField1; + private javax.swing.JTextField jTextField2; + private javax.swing.JTextField jTextField3; + private javax.swing.JTextField jTextField4; private static final long serialVersionUID = 13L; - /** - * Creates new form ArgumentsGen - */ - public ArgumentsGen() { - initComponents(); - } - - /** - * This method is called from within the constructor to initialize the form. - */ - - @SuppressWarnings("unchecked") - // - private void initComponents() { - - jLabel1 = new javax.swing.JLabel(); - jLabel2 = new javax.swing.JLabel(); - jLabel3 = new javax.swing.JLabel(); - jLabel4 = new javax.swing.JLabel(); - jLabel5 = new javax.swing.JLabel(); - jLabel6 = new javax.swing.JLabel(); - jComboBox1 = new javax.swing.JComboBox<>(); - jTextField1 = new javax.swing.JTextField("7071"); - jTextField2 = new javax.swing.JTextField("localhost"); - jTextField3 = new javax.swing.JTextField("localhost"); - jTextField4 = new javax.swing.JTextField("6969"); - jButton1 = new javax.swing.JButton(); - - jLabel1.setText("Enter connection informations:"); - - jLabel2.setText("Server hostname:"); - - jLabel3.setText("Server port:"); - - jLabel4.setText("Tracker hostname:"); - - jLabel5.setText("Tracker port:"); - - jLabel6.setText("Client's protocol:"); - - jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "TCP", "UDP" })); - - jTextField1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jTextField1ActionPerformed(evt); - } - }); - - jTextField2.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jTextField2ActionPerformed(evt); - } - }); - - jButton1.setText("Connect"); - jButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton1ActionPerformed(evt); - } - }); - - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); - this.setLayout(layout); - layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jLabel1) - .addGroup(layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jLabel2) - .addComponent(jLabel3) - .addComponent(jLabel4) - .addComponent(jLabel5) - .addComponent(jLabel6)) - .addGap(71, 71, 71) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(jTextField1) - .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE) - .addComponent(jTextField3) - .addComponent(jTextField4))))) - .addContainerGap(138, Short.MAX_VALUE)) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap()) - ); - layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addComponent(jLabel1) - .addGap(19, 19, 19) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jLabel2) - .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGap(7, 7, 7) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jLabel3) - .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGap(6, 6, 6) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jLabel4) - .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jLabel5)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jButton1) - .addContainerGap(16, Short.MAX_VALUE)) - ); - }// - - private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { - // TODO add your handling code here: - } - - private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) { - // TODO add your handling code here: - } + /** + * Creates new form ArgumentsGen + */ + public ArgumentsGen() { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + */ + + @SuppressWarnings("unchecked") + // + private void initComponents() { + + jLabel1 = new javax.swing.JLabel(); + jLabel2 = new javax.swing.JLabel(); + jLabel3 = new javax.swing.JLabel(); + jLabel4 = new javax.swing.JLabel(); + jLabel5 = new javax.swing.JLabel(); + jLabel6 = new javax.swing.JLabel(); + jComboBox1 = new javax.swing.JComboBox<>(); + jTextField1 = new javax.swing.JTextField("7070"); + jTextField2 = new javax.swing.JTextField("localhost"); + jTextField3 = new javax.swing.JTextField("localhost"); + jTextField4 = new javax.swing.JTextField("6969"); + jButton1 = new javax.swing.JButton(); + + jLabel1.setText("Enter connection informations:"); + + jLabel2.setText("Server hostname:"); + + jLabel3.setText("Server port:"); + + jLabel4.setText("Tracker hostname:"); + + jLabel5.setText("Tracker port:"); + + jLabel6.setText("Client's protocol:"); + + jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "TCP", "UDP" })); + + jTextField1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jTextField1ActionPerformed(evt); + } + }); + + jTextField2.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jTextField2ActionPerformed(evt); + } + }); + + jButton1.setText("Connect"); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jLabel1) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jLabel2) + .addComponent(jLabel3) + .addComponent(jLabel4) + .addComponent(jLabel5) + .addComponent(jLabel6)) + .addGap(71, 71, 71) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(jTextField1) + .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE) + .addComponent(jTextField3) + .addComponent(jTextField4))))) + .addContainerGap(138, Short.MAX_VALUE)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addGap(19, 19, 19) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel2) + .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(7, 7, 7) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel3) + .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(6, 6, 6) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel4) + .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel5)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButton1) + .addContainerGap(16, Short.MAX_VALUE)) + ); + }// + + private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + + private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } /** Actions when click on connect: creat a clientP2Pgui with the infos from textfields*/ - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String hostnameServer = jTextField2.getText(); String portServerStr = jTextField1.getText(); String hostnameTracker = jTextField3.getText(); @@ -174,6 +174,6 @@ public class ArgumentsGen extends javax.swing.JPanel { c.connect(); JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); topFrame.dispose(); - } + } } diff --git a/src/gui/DownloadSelectionGen.java b/src/gui/DownloadSelectionGen.java index 4785687..c166447 100644 --- a/src/gui/DownloadSelectionGen.java +++ b/src/gui/DownloadSelectionGen.java @@ -31,106 +31,106 @@ public class DownloadSelectionGen extends javax.swing.JPanel { private ClientManagement clientManagement; private Logger logger; - /** + /** * @param listFilesToDownload list of files to display * @param clientManagement clientManagement * @param logger logger - * Creates new form ArgumentsGen - */ - public DownloadSelectionGen(String[] listFilesToDownload, ClientManagement clientManagement, Logger logger) { + * Creates new form ArgumentsGen + */ + public DownloadSelectionGen(String[] listFilesToDownload, ClientManagement clientManagement, Logger logger) { this.listFilesToDownload = listFilesToDownload; this.clientManagement = clientManagement; this.logger = logger; - initComponents(); - } - - /** - * This method is called from within the constructor to initialize the form. - */ - @SuppressWarnings("unchecked") - // - private void initComponents() { - - jLabel1 = new javax.swing.JLabel(); - jButton1 = new javax.swing.JButton(); - jScrollPane1 = new javax.swing.JScrollPane(); - jList1 = new javax.swing.JList<>(); - jButton2 = new javax.swing.JButton(); - - jLabel1.setText("Select a file to download:"); - - jButton1.setText("Download"); - jButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton1ActionPerformed(evt); - } - }); - - jList1.setModel(new javax.swing.AbstractListModel() { - String[] strings = listFilesToDownload; - public int getSize() { return strings.length; } - public String getElementAt(int i) { return strings[i]; } - }); - jScrollPane1.setViewportView(jList1); + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + */ + @SuppressWarnings("unchecked") + // + private void initComponents() { + + jLabel1 = new javax.swing.JLabel(); + jButton1 = new javax.swing.JButton(); + jScrollPane1 = new javax.swing.JScrollPane(); + jList1 = new javax.swing.JList<>(); + jButton2 = new javax.swing.JButton(); + + jLabel1.setText("Select a file to download:"); + + jButton1.setText("Download"); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); + + jList1.setModel(new javax.swing.AbstractListModel() { + String[] strings = listFilesToDownload; + public int getSize() { return strings.length; } + public String getElementAt(int i) { return strings[i]; } + }); + jScrollPane1.setViewportView(jList1); jList1.setSelectedIndex(0); jButton2.setText("Back"); - jButton2.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jButton2ActionPerformed(evt); - } - }); - - - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); - this.setLayout(layout); - layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addComponent(jLabel1) - .addGap(0, 0, Short.MAX_VALUE)) - .addGroup(layout.createSequentialGroup() - .addGap(97, 97, 97) - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE) - .addContainerGap(104, Short.MAX_VALUE)) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addGap(25, 25, 25) - .addComponent(jButton2) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE) - .addGap(23, 23, 23)) - ); - layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addComponent(jLabel1) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jButton1) - .addComponent(jButton2)) - .addContainerGap(24, Short.MAX_VALUE)) - ); - }// + jButton2.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2ActionPerformed(evt); + } + }); + + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addGap(0, 0, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addGap(97, 97, 97) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(104, Short.MAX_VALUE)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addGap(25, 25, 25) + .addComponent(jButton2) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(23, 23, 23)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButton1) + .addComponent(jButton2)) + .addContainerGap(24, Short.MAX_VALUE)) + ); + }// /** Actions to initiate when button "Download" is pressed */ - private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { + 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); dl.download(); - } + } /** Actions to initiate when button "Back" is initiate */ - private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { ArgumentsGen a = new ArgumentsGen(); JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); topFrame.add(a); this.setVisible(false); - } + } } diff --git a/src/gui/ErrorFrame.java b/src/gui/ErrorFrame.java index be94d56..2cdb577 100644 --- a/src/gui/ErrorFrame.java +++ b/src/gui/ErrorFrame.java @@ -12,11 +12,11 @@ package gui; * @author Flavien Haas * @author JS Auge * @version 1.0 - */ + */ public class ErrorFrame extends javax.swing.JFrame { - private javax.swing.JLabel jLabel1; - private javax.swing.JToggleButton jToggleButton1; + private javax.swing.JLabel jLabel1; + private javax.swing.JToggleButton jToggleButton1; private String message; private static final long serialVersionUID = 13L; @@ -26,67 +26,67 @@ public class ErrorFrame extends javax.swing.JFrame { * Create new Errorframe, used to display an error and stop the program when needed */ - public ErrorFrame(String message) { + public ErrorFrame(String message) { this.message = message; - initComponents(); + initComponents(); this.setVisible(true); - } + } - /** - * This method is called from within the constructor to initialize the form. - */ - @SuppressWarnings("unchecked") - // - private void initComponents() { + /** + * This method is called from within the constructor to initialize the form. + */ + @SuppressWarnings("unchecked") + // + private void initComponents() { - jToggleButton1 = new javax.swing.JToggleButton(); - jLabel1 = new javax.swing.JLabel(); + jToggleButton1 = new javax.swing.JToggleButton(); + jLabel1 = new javax.swing.JLabel(); - setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jToggleButton1.setText("Close"); - jToggleButton1.addActionListener(new java.awt.event.ActionListener() { - public void actionPerformed(java.awt.event.ActionEvent evt) { - jToggleButton1ActionPerformed(evt); - } - }); - - jLabel1.setText(message); - - javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); - getContentPane().setLayout(layout); - layout.setHorizontalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() - .addContainerGap() - .addComponent(jLabel1)) - .addGroup(layout.createSequentialGroup() - .addGap(253, 253, 253) - .addComponent(jToggleButton1))) - .addContainerGap(205, Short.MAX_VALUE)) - ); - layout.setVerticalGroup( - layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addContainerGap() - .addComponent(jLabel1) - .addGap(30, 30, 30) - .addComponent(jToggleButton1) - .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - ); + jToggleButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jToggleButton1ActionPerformed(evt); + } + }); + + jLabel1.setText(message); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1)) + .addGroup(layout.createSequentialGroup() + .addGap(253, 253, 253) + .addComponent(jToggleButton1))) + .addContainerGap(205, Short.MAX_VALUE)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel1) + .addGap(30, 30, 30) + .addComponent(jToggleButton1) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); this.setLocationRelativeTo(null); this.setAlwaysOnTop(true); - pack(); - }// + pack(); + }// /**force exit the program when the button "Close" is pressed*/ private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) { - System.exit(1); - } + System.exit(1); + } } -- 2.30.2 From 7bd31a31c59c69370b3ab6cb833fc1e86b0a56ea Mon Sep 17 00:00:00 2001 From: js Date: Sun, 12 Apr 2020 18:30:17 +0200 Subject: [PATCH 18/18] 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(); } -- 2.30.2