added bugged interactions

This commit is contained in:
js 2020-04-10 14:03:44 +02:00
parent 1451b67a5b
commit b87b3c70ef
5 changed files with 313 additions and 8 deletions

137
src/gui/ClientP2Pgui.java Normal file
View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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"),
}
}
}

View File

@ -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 {
}// </editor-fold>
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) {

View File

@ -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");
}