Split ClientManagement
into ClientInterface and ClientManagement
This commit is contained in:
parent
f0f18d64b5
commit
23586561a0
41
src/clientP2P/ClientInterface.java
Normal file
41
src/clientP2P/ClientInterface.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package clientP2P;
|
||||||
|
import clientP2P.ClientManagement;
|
||||||
|
import tools.Logger;
|
||||||
|
import tools.LogLevel;
|
||||||
|
|
||||||
|
/** Implementation of P2P-JAVA-PROJECT CLIENT interface
|
||||||
|
* @author Louis Royer
|
||||||
|
* @author Flavien Haas
|
||||||
|
* @author JS Auge
|
||||||
|
* @version 1.0
|
||||||
|
*/
|
||||||
|
public abstract class ClientInterface implements Runnable {
|
||||||
|
protected ClientManagement clientManagement;
|
||||||
|
private Logger logger;
|
||||||
|
|
||||||
|
/** Constructor with clientManagement and logger.
|
||||||
|
* @param clientManagement ClientManagement used
|
||||||
|
* @param logger Logger used
|
||||||
|
*/
|
||||||
|
public ClientInterface(ClientManagement clientManagement, Logger logger) {
|
||||||
|
this.clientManagement = clientManagement;
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Implementation of writeLog
|
||||||
|
* @param text Text to log
|
||||||
|
* @param logLevel level of logging
|
||||||
|
*/
|
||||||
|
protected void writeLog(String text, LogLevel logLevel) {
|
||||||
|
logger.write(text, logLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Implementation of writeLog
|
||||||
|
* @param e exception to log
|
||||||
|
* @param logLevel level of logging
|
||||||
|
*/
|
||||||
|
protected void writeLog(Exception e, LogLevel logLevel) {
|
||||||
|
logger.write(e, logLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
139
src/clientP2P/ClientInterfaceCLI.java
Normal file
139
src/clientP2P/ClientInterfaceCLI.java
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
package clientP2P;
|
||||||
|
import clientP2P.ClientInterface;
|
||||||
|
import clientP2P.ClientManagement;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/** 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 ClientInterfaceCLI(ClientManagement clientManagement, Logger logger, Scanner scanner) {
|
||||||
|
super(clientManagement, logger);
|
||||||
|
this.scanner = scanner;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Implementation of Runnable
|
||||||
|
*/
|
||||||
|
public void run() {
|
||||||
|
boolean isRunning = true;
|
||||||
|
while (isRunning) {
|
||||||
|
try {
|
||||||
|
int i = 1;
|
||||||
|
String[] list = clientManagement.listDirectory();
|
||||||
|
System.out.println("Files present on the server:");
|
||||||
|
System.out.println("R: Refresh file list");
|
||||||
|
System.out.println("0: Exit the program");
|
||||||
|
for(String listItem: list) {
|
||||||
|
System.out.println(i + ": " + listItem);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
System.out.println("Type the number associated with the file to download:");
|
||||||
|
String f = scanner.nextLine();
|
||||||
|
if (f.equals("0")) {
|
||||||
|
isRunning = false;
|
||||||
|
} else if (f.equals("R") || f.equals("r")) {
|
||||||
|
writeLog("File list refresh.", LogLevel.Info);
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
int j = Integer.parseInt(f);
|
||||||
|
if (j <= list.length) {
|
||||||
|
j--;
|
||||||
|
clientManagement.download(list[j]);
|
||||||
|
writeLog("File " + f + " sucessfully downloaded", LogLevel.Info);
|
||||||
|
} else {
|
||||||
|
writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Error);
|
||||||
|
}
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} 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);
|
||||||
|
} catch (NotFound e) {
|
||||||
|
writeLog("Server has not this file in directory", LogLevel.Error);
|
||||||
|
} catch (EmptyFile e) {
|
||||||
|
writeLog("File is empty", LogLevel.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeLog("Exiting client", LogLevel.Info);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package clientP2P;
|
package clientP2P;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Scanner;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
@ -31,6 +31,8 @@ import remoteException.ProtocolRemoteError;
|
|||||||
import remoteException.VersionRemoteError;
|
import remoteException.VersionRemoteError;
|
||||||
import remoteException.NotATracker;
|
import remoteException.NotATracker;
|
||||||
import remoteException.UnknownHost;
|
import remoteException.UnknownHost;
|
||||||
|
import exception.RemoteException;
|
||||||
|
import exception.LocalException;
|
||||||
import tools.ServeErrors;
|
import tools.ServeErrors;
|
||||||
import tools.HostItem;
|
import tools.HostItem;
|
||||||
import tools.Logger;
|
import tools.Logger;
|
||||||
@ -43,14 +45,13 @@ import java.net.SocketException;
|
|||||||
* @author JS Auge
|
* @author JS Auge
|
||||||
* @version 1.0
|
* @version 1.0
|
||||||
*/
|
*/
|
||||||
public abstract class ClientManagement extends ServeErrors implements Runnable {
|
public abstract class ClientManagement extends ServeErrors {
|
||||||
protected String baseDirectory;
|
protected String baseDirectory;
|
||||||
protected String partsSubdir;
|
protected String partsSubdir;
|
||||||
protected List<HostItem> hostList;
|
protected List<HostItem> hostList = new ArrayList<>();
|
||||||
protected HostItem tracker;
|
protected HostItem tracker;
|
||||||
protected HostItem client;
|
protected HostItem client;
|
||||||
protected Logger logger;
|
protected Logger logger;
|
||||||
protected Scanner scanner;
|
|
||||||
protected ClientDownload downLoader;
|
protected ClientDownload downLoader;
|
||||||
|
|
||||||
/** Constructor with baseDirectory, tracker, partsSubdir, logger, and scanner parameters.
|
/** Constructor with baseDirectory, tracker, partsSubdir, logger, and scanner parameters.
|
||||||
@ -58,23 +59,14 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|||||||
* @param tracker Tracker hostItem
|
* @param tracker Tracker hostItem
|
||||||
* @param partsSubdir subdirectory to store file parts
|
* @param partsSubdir subdirectory to store file parts
|
||||||
* @param logger Loggger
|
* @param logger Loggger
|
||||||
* @param scanner Scanner used to read input
|
|
||||||
* @param client HostItem of the application
|
* @param client HostItem of the application
|
||||||
*/
|
*/
|
||||||
public ClientManagement(String baseDirectory, HostItem tracker, String partsSubdir, Logger logger, Scanner scanner, HostItem client) {
|
public ClientManagement(String baseDirectory, HostItem tracker, String partsSubdir, Logger logger, HostItem client) {
|
||||||
this.scanner = scanner;
|
|
||||||
this.baseDirectory = baseDirectory;
|
this.baseDirectory = baseDirectory;
|
||||||
this.tracker = tracker;
|
this.tracker = tracker;
|
||||||
this.partsSubdir = partsSubdir;
|
this.partsSubdir = partsSubdir;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.client = client;
|
this.client = client;
|
||||||
try {
|
|
||||||
initHostList();
|
|
||||||
} catch (InternalError e) {
|
|
||||||
System.exit(-1);
|
|
||||||
} catch (ProtocolError e) {
|
|
||||||
System.exit(-2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Getter for tracker socket
|
/** Getter for tracker socket
|
||||||
@ -93,33 +85,14 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|||||||
/** Initialize hostList from tracker
|
/** Initialize hostList from tracker
|
||||||
* @throws ProtocolError
|
* @throws ProtocolError
|
||||||
* @throws InternalError
|
* @throws InternalError
|
||||||
|
* @throws SocketException
|
||||||
|
* @throws UnknownHostException
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private void initHostList() throws ProtocolError, InternalError {
|
public void initHostList() throws ProtocolError, InternalError, SocketException, UnknownHostException, IOException {
|
||||||
ProtocolP2PPacket<?> d = createProtocolP2PPacket(new DiscoverRequest(null));
|
ProtocolP2PPacket<?> d = createProtocolP2PPacket(new DiscoverRequest(null));
|
||||||
try {
|
try {
|
||||||
boolean contacted = false;
|
d.sendRequest(getTrackerSocket());
|
||||||
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;
|
|
||||||
d.sendRequest(getTrackerSocket());
|
|
||||||
contacted = true;
|
|
||||||
} catch (SocketException e) {
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (stop) {
|
|
||||||
System.exit(3);
|
|
||||||
}
|
|
||||||
Payload p = d.receiveResponse().getPayload();
|
Payload p = d.receiveResponse().getPayload();
|
||||||
assert p instanceof DiscoverResponse : "This payload must be instance of Filelist";
|
assert p instanceof DiscoverResponse : "This payload must be instance of Filelist";
|
||||||
if (!(p instanceof DiscoverResponse)) {
|
if (!(p instanceof DiscoverResponse)) {
|
||||||
@ -131,10 +104,10 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|||||||
} catch (SocketClosed e) {
|
} catch (SocketClosed e) {
|
||||||
writeLog("listDirectory : SocketClosed", LogLevel.Error);
|
writeLog("listDirectory : SocketClosed", LogLevel.Error);
|
||||||
throw new ProtocolError();
|
throw new ProtocolError();
|
||||||
} catch (NotATracker e) {
|
} catch (LocalException e) {
|
||||||
writeLog(e, LogLevel.Error);
|
writeLog(e, LogLevel.Error);
|
||||||
throw new ProtocolError();
|
throw new ProtocolError();
|
||||||
} catch (Exception e) {
|
} catch (RemoteException e) {
|
||||||
writeLog(e, LogLevel.Error);
|
writeLog(e, LogLevel.Error);
|
||||||
throw new ProtocolError();
|
throw new ProtocolError();
|
||||||
}
|
}
|
||||||
@ -144,7 +117,7 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|||||||
* @param filename
|
* @param filename
|
||||||
* @return hashsum
|
* @return hashsum
|
||||||
*/
|
*/
|
||||||
protected byte[] computeHashsum(String filename, HashAlgorithm h) {
|
private byte[] computeHashsum(String filename, HashAlgorithm h) {
|
||||||
try {
|
try {
|
||||||
MessageDigest md = MessageDigest.getInstance(HashAlgorithm.SHA512.getName());
|
MessageDigest md = MessageDigest.getInstance(HashAlgorithm.SHA512.getName());
|
||||||
return md.digest(Files.readAllBytes(Paths.get(baseDirectory + filename)));
|
return md.digest(Files.readAllBytes(Paths.get(baseDirectory + filename)));
|
||||||
@ -181,7 +154,7 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|||||||
* @throws ProtocolRemoteError
|
* @throws ProtocolRemoteError
|
||||||
* @throws VersionRemoteError
|
* @throws VersionRemoteError
|
||||||
*/
|
*/
|
||||||
protected String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError {
|
public String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError {
|
||||||
if (hostList.size() == 0) {
|
if (hostList.size() == 0) {
|
||||||
return new String[0];
|
return new String[0];
|
||||||
}
|
}
|
||||||
@ -234,7 +207,7 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|||||||
* @throws VersionRemoteError
|
* @throws VersionRemoteError
|
||||||
* @throws EmptyFile
|
* @throws EmptyFile
|
||||||
*/
|
*/
|
||||||
private void download(String filename) throws EmptyFile, NotFound, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError {
|
public void download(String filename) throws EmptyFile, NotFound, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError {
|
||||||
initDownloader(filename);
|
initDownloader(filename);
|
||||||
Thread t = new Thread(downLoader);
|
Thread t = new Thread(downLoader);
|
||||||
t.start();
|
t.start();
|
||||||
@ -268,69 +241,15 @@ public abstract class ClientManagement extends ServeErrors implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Implementation of Runnable
|
/** Implementation of writeLog
|
||||||
|
* @param text Text to log
|
||||||
|
* @param logLevel level of logging
|
||||||
*/
|
*/
|
||||||
public void run() {
|
protected abstract void writeLog(String text, LogLevel logLevel);
|
||||||
boolean isRunning = true;
|
|
||||||
while (isRunning) {
|
/** Implementation of writeLog
|
||||||
try {
|
* @param e exception to log
|
||||||
int i = 1;
|
* @param logLevel level of logging
|
||||||
String[] list = listDirectory();
|
*/
|
||||||
System.out.println("Files present on the server:");
|
protected abstract void writeLog(Exception e, LogLevel logLevel);
|
||||||
System.out.println("R: Refresh file list");
|
|
||||||
System.out.println("0: Exit the program");
|
|
||||||
for(String listItem: list) {
|
|
||||||
System.out.println(i + ": " + listItem);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
System.out.println("Type the number associated with the file to download:");
|
|
||||||
String f = scanner.nextLine();
|
|
||||||
if (f.equals("0")) {
|
|
||||||
isRunning = false;
|
|
||||||
} else if (f.equals("R") || f.equals("r")) {
|
|
||||||
writeLog("File list refresh.", LogLevel.Info);
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
int j = Integer.parseInt(f);
|
|
||||||
if (j <= list.length) {
|
|
||||||
j--;
|
|
||||||
download(list[j]);
|
|
||||||
writeLog("File " + f + " sucessfully downloaded", LogLevel.Info);
|
|
||||||
} else {
|
|
||||||
writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Error);
|
|
||||||
}
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
writeLog("File " + f + " unsucessfully downloaded, wrong number", LogLevel.Error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} 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);
|
|
||||||
} catch (NotFound e) {
|
|
||||||
writeLog("Server has not this file in directory", LogLevel.Error);
|
|
||||||
} catch (EmptyFile e) {
|
|
||||||
writeLog("File is empty", LogLevel.Error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writeLog("Exiting client", LogLevel.Info);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -26,11 +26,10 @@ public class ClientManagementTCP extends ClientManagement {
|
|||||||
* @param tracker Tracker hostItem
|
* @param tracker Tracker hostItem
|
||||||
* @param partsSubdir subdirectory to store file parts
|
* @param partsSubdir subdirectory to store file parts
|
||||||
* @param logger Loggger
|
* @param logger Loggger
|
||||||
* @param scanner Scanner used to read input
|
|
||||||
* @param client HostItem of the application
|
* @param client HostItem of the application
|
||||||
*/
|
*/
|
||||||
public ClientManagementTCP(String baseDirectory, HostItem tracker, String partsSubdir, Logger logger, Scanner scanner, HostItem client) {
|
public ClientManagementTCP(String baseDirectory, HostItem tracker, String partsSubdir, Logger logger, HostItem client) {
|
||||||
super(baseDirectory, tracker, partsSubdir, logger, scanner, client);
|
super(baseDirectory, tracker, partsSubdir, logger, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Initialize downloader
|
/** Initialize downloader
|
||||||
|
@ -25,11 +25,10 @@ public class ClientManagementUDP extends ClientManagement {
|
|||||||
* @param tracker tracker HostItem
|
* @param tracker tracker HostItem
|
||||||
* @param partsSubdir subdirectory to store file parts
|
* @param partsSubdir subdirectory to store file parts
|
||||||
* @param logger Loggger
|
* @param logger Loggger
|
||||||
* @param scanner Scanner used to read input
|
|
||||||
* @param client HostItem of the application
|
* @param client HostItem of the application
|
||||||
*/
|
*/
|
||||||
public ClientManagementUDP(String baseDirectory, HostItem tracker, String partsSubdir, Logger logger, Scanner scanner, HostItem client) {
|
public ClientManagementUDP(String baseDirectory, HostItem tracker, String partsSubdir, Logger logger, HostItem client) {
|
||||||
super(baseDirectory, tracker, partsSubdir, logger, scanner, client);
|
super(baseDirectory, tracker, partsSubdir, logger, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Initialize downloader
|
/** Initialize downloader
|
||||||
|
@ -5,6 +5,7 @@ import java.util.List;
|
|||||||
import clientP2P.ClientManagementUDP;
|
import clientP2P.ClientManagementUDP;
|
||||||
import clientP2P.ClientManagementTCP;
|
import clientP2P.ClientManagementTCP;
|
||||||
import serverP2P.ServerManagementUDP;
|
import serverP2P.ServerManagementUDP;
|
||||||
|
import clientP2P.ClientInterfaceCLI;
|
||||||
import serverP2P.ServerManagementTCP;
|
import serverP2P.ServerManagementTCP;
|
||||||
import tools.Logger;
|
import tools.Logger;
|
||||||
import tools.LogLevel;
|
import tools.LogLevel;
|
||||||
@ -166,16 +167,16 @@ public class ClientP2P {
|
|||||||
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(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server);
|
ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.server);
|
||||||
tclient = new Thread(cmudp);
|
tclient = new Thread(new ClientInterfaceCLI(cmudp, c.loggerClient, c.scanner));
|
||||||
break;
|
break;
|
||||||
case "TCP":
|
case "TCP":
|
||||||
case "tcp":
|
case "tcp":
|
||||||
case "1":
|
case "1":
|
||||||
default:
|
default:
|
||||||
System.out.println("Starting with TCP");
|
System.out.println("Starting with TCP");
|
||||||
ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.scanner, c.server);
|
ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.tracker, c.directories.getDataHomeDirectory() + c.partsDir, c.loggerClient, c.server);
|
||||||
tclient = new Thread(cmtcp);
|
tclient = new Thread(new ClientInterfaceCLI(cmtcp, c.loggerClient, c.scanner));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
tclient.setName("client P2P-JAVA-PROJECT");
|
tclient.setName("client P2P-JAVA-PROJECT");
|
||||||
|
@ -50,7 +50,7 @@ public class FileWatcherTCP extends FileWatcher {
|
|||||||
*/
|
*/
|
||||||
protected void registerTracker() {
|
protected void registerTracker() {
|
||||||
try {
|
try {
|
||||||
writeLog("Trying to into tracker", LogLevel.Info);
|
writeLog("Trying to register into tracker", LogLevel.Info);
|
||||||
ProtocolP2PPacket<Register> p = (ProtocolP2PPacket<Register>)new ProtocolP2PPacketTCP<Register>(new Register(server));
|
ProtocolP2PPacket<Register> p = (ProtocolP2PPacket<Register>)new ProtocolP2PPacketTCP<Register>(new Register(server));
|
||||||
p.sendRequest((Object)tracker.tryGetTCPSocket());
|
p.sendRequest((Object)tracker.tryGetTCPSocket());
|
||||||
writeLog("Register request sent.", LogLevel.Debug);
|
writeLog("Register request sent.", LogLevel.Debug);
|
||||||
|
@ -26,6 +26,8 @@ import java.util.Map;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.net.SocketException;
|
||||||
import exception.LocalException;
|
import exception.LocalException;
|
||||||
import localException.InternalError;
|
import localException.InternalError;
|
||||||
import remoteException.UnknownHost;
|
import remoteException.UnknownHost;
|
||||||
@ -253,8 +255,13 @@ public abstract class ServerManagement extends ServeErrors implements Runnable {
|
|||||||
|
|
||||||
|
|
||||||
/** Getter for tracker socket
|
/** Getter for tracker socket
|
||||||
|
* @return Tracker's socket
|
||||||
|
* @throws SocketException
|
||||||
|
* @throws UnknownHostException
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
protected abstract Object getTrackerSocket();
|
protected abstract Object getTrackerSocket() throws SocketException, UnknownHostException, IOException;
|
||||||
|
|
||||||
|
|
||||||
/** Send unregister request to tracker
|
/** Send unregister request to tracker
|
||||||
*/
|
*/
|
||||||
@ -263,7 +270,13 @@ public abstract class ServerManagement extends ServeErrors implements Runnable {
|
|||||||
try {
|
try {
|
||||||
writeLog("Unregistering from tracker", LogLevel.Info);
|
writeLog("Unregistering from tracker", LogLevel.Info);
|
||||||
createProtocolP2PPacket(new Unregister(server)).sendRequest(getTrackerSocket());
|
createProtocolP2PPacket(new Unregister(server)).sendRequest(getTrackerSocket());
|
||||||
} catch (Exception e) {
|
} catch (SocketException e) {
|
||||||
|
writeLog("Cannot unregister from tracker", LogLevel.Error);
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
writeLog("Cannot unregister from tracker", LogLevel.Error);
|
||||||
|
} catch (IOException e) {
|
||||||
|
writeLog("Cannot unregister from tracker", LogLevel.Error);
|
||||||
|
} catch (Exception e) {
|
||||||
writeLog("Cannot unregister from tracker", LogLevel.Error);
|
writeLog("Cannot unregister from tracker", LogLevel.Error);
|
||||||
writeLog(e, LogLevel.Error);
|
writeLog(e, LogLevel.Error);
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ import tools.LogLevel;
|
|||||||
import tools.HostItem;
|
import tools.HostItem;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
import protocolP2P.HashAlgorithm;
|
import protocolP2P.HashAlgorithm;
|
||||||
import protocolP2P.HashRequest;
|
import protocolP2P.HashRequest;
|
||||||
import protocolP2P.HashResponse;
|
import protocolP2P.HashResponse;
|
||||||
@ -169,11 +170,15 @@ public class ServerManagementTCP extends ServerManagement {
|
|||||||
|
|
||||||
/** Getter for tracker socket
|
/** Getter for tracker socket
|
||||||
* @return Tracker's socket
|
* @return Tracker's socket
|
||||||
|
* @throws SocketException
|
||||||
|
* @throws UnknownHostException
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
protected Object getTrackerSocket() {
|
protected Object getTrackerSocket() throws SocketException, UnknownHostException, IOException {
|
||||||
return (Object)tracker.getTCPSocket();
|
return (Object)tracker.tryGetTCPSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Closes socket */
|
/** Closes socket */
|
||||||
protected void closeSocket() {
|
protected void closeSocket() {
|
||||||
try {
|
try {
|
||||||
|
@ -122,12 +122,16 @@ public class ServerManagementUDP extends ServerManagement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Getter for tracker socket
|
/** Getter for tracker socket
|
||||||
* @return Tracker's socket
|
* @return Tracker's socket
|
||||||
|
* @throws SocketException
|
||||||
|
* @throws UnknownHostException
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
protected Object getTrackerSocket() {
|
protected Object getTrackerSocket() throws SocketException, UnknownHostException, IOException {
|
||||||
return (Object)tracker.getUDPSocket();
|
return (Object)tracker.tryGetUDPSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Closes socket */
|
/** Closes socket */
|
||||||
protected void closeSocket() {
|
protected void closeSocket() {
|
||||||
socket.close();
|
socket.close();
|
||||||
|
@ -35,13 +35,16 @@ public class HostItem {
|
|||||||
try {
|
try {
|
||||||
return tryGetTCPSocket();
|
return tryGetTCPSocket();
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
System.err.println("getTCPSocket error: No TCP socket available.");
|
System.err.println("getTCPSocket error: No TCP socket available (" + this +").");
|
||||||
|
e.printStackTrace();
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
System.err.println("getTCPSocket error: Unknown host (" + this + ").");
|
System.err.println("getTCPSocket error: Unknown host (" + this + ").");
|
||||||
|
e.printStackTrace();
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("getTCPSocket error: Cannot create TCP socket (" + this + ").");
|
System.err.println("getTCPSocket error: Cannot create TCP socket (" + this + ").");
|
||||||
|
e.printStackTrace();
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
}
|
}
|
||||||
return null; // java compiler is stupid and doesn't know about System.exit
|
return null; // java compiler is stupid and doesn't know about System.exit
|
||||||
@ -80,7 +83,7 @@ public class HostItem {
|
|||||||
try {
|
try {
|
||||||
return tryGetUDPSocket();
|
return tryGetUDPSocket();
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
System.err.println("getUDPSocket error: No UDP socket available." );
|
System.err.println("getUDPSocket error: No UDP socket available ( " + this + ")." );
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
System.err.println("getUDPSocket error: Unknown host (" + this + ").");
|
System.err.println("getUDPSocket error: Unknown host (" + this + ").");
|
||||||
|
@ -101,6 +101,15 @@ public class Logger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Appends log to filelog and print to stderr.
|
||||||
|
* @param e Exception to log
|
||||||
|
* @param logLevel Type of log
|
||||||
|
*/
|
||||||
|
public void write(Exception e, LogLevel logLevel) {
|
||||||
|
write(e.toString(), logLevel);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
/** Appends log to filelog and print to stderr.
|
/** Appends log to filelog and print to stderr.
|
||||||
* Adds [TCP] in log line.
|
* Adds [TCP] in log line.
|
||||||
* @param text Text to log
|
* @param text Text to log
|
||||||
@ -112,7 +121,7 @@ public class Logger {
|
|||||||
|
|
||||||
/** Appends log to filelog and print to stderr.
|
/** Appends log to filelog and print to stderr.
|
||||||
* Adds [TCP] in log line.
|
* Adds [TCP] in log line.
|
||||||
* @param text Text to log
|
* @param e Exception to log
|
||||||
* @param logLevel Type of log
|
* @param logLevel Type of log
|
||||||
*/
|
*/
|
||||||
public void writeTCP(Exception e, LogLevel logLevel) {
|
public void writeTCP(Exception e, LogLevel logLevel) {
|
||||||
@ -131,7 +140,7 @@ public class Logger {
|
|||||||
|
|
||||||
/** Appends log to filelog and print to stderr.
|
/** Appends log to filelog and print to stderr.
|
||||||
* Adds [UDP] in log line.
|
* Adds [UDP] in log line.
|
||||||
* @param text Text to log
|
* @param e Excetpino to log
|
||||||
* @param logLevel Type of log
|
* @param logLevel Type of log
|
||||||
*/
|
*/
|
||||||
public void writeUDP(Exception e, LogLevel logLevel) {
|
public void writeUDP(Exception e, LogLevel logLevel) {
|
||||||
|
Loading…
Reference in New Issue
Block a user