fixed a lot of things

This commit is contained in:
js 2020-04-10 15:46:30 +02:00
parent b87b3c70ef
commit 20a444cb9b
6 changed files with 68 additions and 48 deletions

View File

@ -161,15 +161,19 @@ public class ArgumentsGen extends javax.swing.JPanel {
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String hostnameServer = jTextField2.getText(); String hostnameServer = jTextField2.getText();
String portServer = jTextField1.getText(); String portServerStr = jTextField1.getText();
String hostnameTracker = jTextField3.getText(); String hostnameTracker = jTextField3.getText();
String portTracker = jTextField4.getText(); String portTrackerStr = jTextField4.getText();
String protocolClient = jComboBox1.getSelectedItem().toString(); String protocolClient = jComboBox1.getSelectedItem().toString();
System.out.println("hostnameServer: " + hostnameServer); System.out.println("hostnameServer: " + hostnameServer);
System.out.println("portServer: " + portServer); System.out.println("portServer: " + portServerStr);
System.out.println("hostnameTracker: " + hostnameTracker); System.out.println("hostnameTracker: " + hostnameTracker);
System.out.println("portTracker: " + portTracker); System.out.println("portTracker: " + portTrackerStr);
System.out.println("protocol: " + protocolClient); 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();
} }
} }

View File

@ -6,7 +6,6 @@ import clientP2P.ClientManagement;
import tools.SearchFile; import tools.SearchFile;
import tools.LogLevel; import tools.LogLevel;
import tools.Logger; import tools.Logger;
import java.util.Scanner;
import localException.ProtocolError; import localException.ProtocolError;
import localException.InternalError; import localException.InternalError;
import localException.ProtocolError; import localException.ProtocolError;
@ -33,17 +32,19 @@ import javax.swing.JFrame;
* @author JS Auge * @author JS Auge
* @version 1.0 * @version 1.0
*/ */
public class ClientInterfaceCLI extends ClientInterface { public class ClientInterfaceGUI extends ClientInterface {
private Scanner scanner;
/** Constructor with clientManagement, logger and scanner. /** Constructor with clientManagement, logger and scanner.
* @param clientManagement ClientManagement used * @param clientManagement ClientManagement used
* @param logger Logger used * @param logger Logger used
* @param scanner Scanner used to read input * @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); super(clientManagement, logger);
this.scanner = scanner; this.loggerC = logger;
} }
/** Implementation of Runnable /** Implementation of Runnable
@ -53,20 +54,42 @@ public class ClientInterfaceCLI extends ClientInterface {
public void run(){ public void run(){
boolean isRunning = initHostList(); boolean isRunning = initHostList();
if (isRunning) { if (isRunning) {
try { try{
int i = 1; int i = 1;
SearchFile searchEngine = new SearchFile(); SearchFile searchEngine = new SearchFile();
int optionSearch = 0; int optionSearch = 0;
String searchInput = ""; String searchInput = "";
String[] list = clientManagement.listDirectory(); String[] list = clientManagement.listDirectory();
//String[] resultArray = {};
MainWindow win = new MainWindow(); MainWindow win = new MainWindow();
win.add(new DownloadSelectionGen(list, clientManagement, logger, scanner)); 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 servers response", LogLevel.Error);
} catch (VersionError e) {
writeLog("Servers 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 clients request", LogLevel.Error);
} catch (VersionRemoteError e) {
writeLog("Server cannot decode this version of the protocol", LogLevel.Error);
} }
} }
}
/** Initialization of hostList with retry in failure /** Initialization of hostList with retry in failure
@ -79,11 +102,7 @@ public class ClientInterfaceCLI extends ClientInterface {
while (!contacted && !stop) { while (!contacted && !stop) {
try { try {
if (!firstLoop) { if (!firstLoop) {
writeLog("Cannot contact tracker. Try again [Y/n] ?", LogLevel.Error); writeLog("Cannot contact tracker... ", LogLevel.Error);
String tryAgain = scanner.nextLine();
if (tryAgain.equals("n") || tryAgain.equals("N")) {
stop = true;
}
} }
firstLoop = false; firstLoop = false;
clientManagement.initHostList(); clientManagement.initHostList();
@ -97,4 +116,6 @@ public class ClientInterfaceCLI extends ClientInterface {
} }
return !stop; return !stop;
} }
} }

View File

@ -21,7 +21,7 @@ import tools.TrackerPortRange;
* @version 1.0 * @version 1.0
*/ */
public class ClientP2P { public class ClientP2Pgui {
private String logDir = "logs/"; private String logDir = "logs/";
private String partsDir = ".parts/"; private String partsDir = ".parts/";
private Logger loggerServer; private Logger loggerServer;
@ -55,26 +55,29 @@ public class ClientP2P {
* @param hostnameTracker hostname of tracker * @param hostnameTracker hostname of tracker
* @param portTracker port 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.hostnameServer = hostnameServer;
this.hostnameTracker = hostnameTracker; this.hostnameTracker = hostnameTracker;
this.portServer = portServer; this.portServer = portServer;
this.portTracker = portTracker; this.portTracker = portTracker;
this.protocolP2P = protocolP2P; this.protocolP2P = protocolP2P;
final ServerPortRange serverPortRange = new ServerPortRange(); final ServerPortRange serverPortRange = new ServerPortRange();
final TrackerPortRange trackerPortRange = new TrackerPortRange(); final TrackerPortRange trackerPortRange = new TrackerPortRange();
if (!serverPortRange.isPortInRange(portServer)){ if (!serverPortRange.isPortInRange(portServer)){
ErrorFrame erreur = new ErrorFrame("SERVER: Port not in range. "); ErrorFrame erreur = new ErrorFrame("SERVER: Port not in range. ");
} }
if (!trackerPortRange.isPortInRange(portTracker)){ if (!trackerPortRange.isPortInRange(portTracker)){
ErrorFrame erreur = new ErrorFrame("TRACKER: Port not in range"); ErrorFrame erreur = new ErrorFrame("TRACKER: Port not in range");
} }
server = new HostItem(hostnameServer, portServer); server = new HostItem(hostnameServer, portServer);
tracker = new HostItem(hostnameTracker, portTracker); tracker = new HostItem(hostnameTracker, portTracker);
initDirectoriesAndLoggers(); initDirectoriesAndLoggers();
@ -106,14 +109,14 @@ public class ClientP2P {
} }
Thread tclient; Thread tclient;
switch (protocolClient) { switch (protocolP2P) {
case "UDP": case "UDP":
case "udp": case "udp":
case "upd": // to avoid users typos case "upd": // to avoid users typos
case "2" : case "2" :
System.out.println("Starting with UDP"); System.out.println("Starting with UDP");
ClientManagementUDP cmudp = new ClientManagementUDP(directories.getDataHomeDirectory(), tracker, directories.getDataHomeDirectory() + partsDir, loggerClient, server); 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; break;
case "TCP": case "TCP":
case "tcp": case "tcp":
@ -121,10 +124,10 @@ public class ClientP2P {
default: default:
System.out.println("Starting with TCP"); System.out.println("Starting with TCP");
ClientManagementTCP cmtcp = new ClientManagementTCP(directories.getDataHomeDirectory(), tracker, directories.getDataHomeDirectory() + partsDir, loggerClient, server); 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; break;
} }
tclient.setName("client P2P-JAVA-PROJECT"); tclient.setName("client P2P-JAVA-PROJECT GUI");
tclient.start(); tclient.start();
try { try {
tclient.join(); tclient.join();

View File

@ -6,7 +6,6 @@ import clientP2P.ClientManagement;
import tools.SearchFile; import tools.SearchFile;
import tools.LogLevel; import tools.LogLevel;
import tools.Logger; import tools.Logger;
import java.util.Scanner;
import localException.ProtocolError; import localException.ProtocolError;
import localException.InternalError; import localException.InternalError;
import localException.ProtocolError; import localException.ProtocolError;
@ -26,32 +25,27 @@ import java.io.IOException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.net.SocketException; import java.net.SocketException;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel;
public class DownloadFileGUI extends JPanel{ public class DownloadFileGUI extends JPanel{
private String fileToDownload; private String fileToDownload;
private ClientManagement clientManagement; private ClientManagement clientManagement;
private Logger logger; 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.fileToDownload = fileToDownload;
this.clientManagement = clientManagement; this.clientManagement = clientManagement;
this.logger = logger; this.logger = logger;
this.scanner = scanner;
} }
public void download(){ public void download(){
try { try {
clientManagement.download(fileToDownload); clientManagement.download(fileToDownload);
ErrorFrame erreur = new ErrorFrame("File " + f + " sucessfully downloaded"); ErrorFrame erreur = new ErrorFrame("File " + fileToDownload + " sucessfully downloaded");
} else {
ErrorFrame erreur = new ErrorFrame("File " + f + " unsucessfully downloaded, wrong number");
}
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
ErrorFrame erreur = new ErrorFrame("File " + f + " unsucessfully downloaded, wrong number"), ErrorFrame erreur = new ErrorFrame("File " + fileToDownload + " unsucessfully downloaded, wrong number");
} }
} }

View File

@ -12,7 +12,6 @@ package gui;
import clientP2P.*; import clientP2P.*;
import tools.LogLevel; import tools.LogLevel;
import tools.Logger; import tools.Logger;
import java.util.Scanner;
/** /**
* @author Louis Royer * @author Louis Royer
@ -30,18 +29,16 @@ public class DownloadSelectionGen extends javax.swing.JPanel {
private static final long serialVersionUID = 13L; private static final long serialVersionUID = 13L;
private String[] listFilesToDownload; private String[] listFilesToDownload;
private ClientManagement clientManagement; private ClientManagement clientManagement;
private Scanner scanner;
private Logger logger; private Logger logger;
// End of variables declaration // End of variables declaration
/** /**
* Creates new form ArgumentsGen * 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.listFilesToDownload = listFilesToDownload;
this.clientManagement = clientManagement; this.clientManagement = clientManagement;
this.logger = logger; this.logger = logger;
this.scanner = scanner;
initComponents(); initComponents();
} }
@ -121,8 +118,9 @@ public class DownloadSelectionGen extends javax.swing.JPanel {
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String fileSelected = jList1.getSelectedValue(); String fileSelected = jList1.getSelectedValue();
DownloadFileGUI interface = new DownloadFileGUI(fileSelected, clientManagement, logger, scanner); System.out.println("File to download: " + fileSelected);
DownloadFileGUI.download(); //DownloadFileGUI interface = new DownloadFileGUI(fileSelected, clientManagement, logger, scanner);
//DownloadFileGUI.download();
} }
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

View File

@ -17,7 +17,7 @@ public class MainWindow extends JFrame{
// "fichier2", "fichier3", "fichier4"}; // "fichier2", "fichier3", "fichier4"};
//fenetre.add(new DownloadSelectionGen(listTest)); //fenetre.add(new DownloadSelectionGen(listTest));
fenetre.setVisible(true); fenetre.setVisible(true);
ErrorFrame erreur = new ErrorFrame("ceci est une erreur"); //ErrorFrame erreur = new ErrorFrame("ceci est une erreur");
} }
} }