added bugged interactions
parent
1451b67a5b
commit
b87b3c70ef
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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"),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue