Add multiple servers choice, but dl only from one
flavien's git/Projet_JAVA_P2P_STRI2A/pipeline/pr-master This commit looks good Details

Je suis en train de faire en sorte qu’on télécharge depuis plusieurs
pull/30/head
Louis Royer 4 years ago
parent 11e965acc8
commit f88f5b1d40

@ -33,6 +33,8 @@ import protocolP2P.HashRequest;
import protocolP2P.HashResponse; import protocolP2P.HashResponse;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import tools.HostItem;
import java.util.List;
/** Implementation of P2P-JAVA-PROJECT CLIENT /** Implementation of P2P-JAVA-PROJECT CLIENT
* @author Louis Royer * @author Louis Royer
@ -42,10 +44,7 @@ import java.security.NoSuchAlgorithmException;
*/ */
public class ClientManagementTCP implements Runnable { public class ClientManagementTCP implements Runnable {
private String baseDirectory; private String baseDirectory;
private int TCPPort; private List<HostItem> hostList;
private String host;
private Socket socket;
/** Constructor for TCP implementation, with baseDirectory and TCPPort parameters. /** Constructor for TCP implementation, with baseDirectory and TCPPort parameters.
@ -53,22 +52,9 @@ public class ClientManagementTCP implements Runnable {
* @param host hostname of the server * @param host hostname of the server
* @param TCPPort the server will listen on this port * @param TCPPort the server will listen on this port
*/ */
public ClientManagementTCP(String baseDirectory, String host, int TCPPort) { public ClientManagementTCP(String baseDirectory, List<HostItem> hostList) {
this.baseDirectory = baseDirectory; this.baseDirectory = baseDirectory;
this.host = host; this.hostList = hostList;
this.TCPPort = TCPPort;
try {
socket = new Socket(InetAddress.getByName(host), TCPPort);
} catch (SocketException e) {
System.err.println("Error: No TCP socket available.");
System.exit(-1);
} catch (UnknownHostException e) {
System.err.println("Error: Unknown host.");
System.exit(-1);
} catch (IOException e) {
System.err.println("Error: Cannot create TCP socket");
System.exit(-1);
}
} }
/** Implementation of Runnable /** Implementation of Runnable
@ -114,11 +100,8 @@ public class ClientManagementTCP implements Runnable {
} catch (EmptyFile e) { } catch (EmptyFile e) {
System.err.println("Error: File is empty"); System.err.println("Error: File is empty");
} finally { } finally {
try { for (HostItem h: hostList) {
System.err.println("Closing socket"); h.closeTCPSocket();
socket.close();
} catch (IOException e2) {
System.err.println("Error: cannot close socket");
} }
} }
} }
@ -145,7 +128,7 @@ public class ClientManagementTCP implements Runnable {
HashAlgorithm[] hashesAlgo = new HashAlgorithm[1]; HashAlgorithm[] hashesAlgo = new HashAlgorithm[1];
hashesAlgo[0] = HashAlgorithm.SHA512; hashesAlgo[0] = HashAlgorithm.SHA512;
ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new HashRequest(filename, hashesAlgo)); ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP((Payload) new HashRequest(filename, hashesAlgo));
d.sendRequest((Object)socket); d.sendRequest((Object)hostList.get(0).getTCPSocket());
try { try {
Payload pHash = d.receiveResponse().getPayload(); Payload pHash = d.receiveResponse().getPayload();
assert pHash instanceof HashResponse : "This payload must be instance of HashResponse"; assert pHash instanceof HashResponse : "This payload must be instance of HashResponse";
@ -164,7 +147,7 @@ public class ClientManagementTCP implements Runnable {
} }
d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, 0, MAX_PARTIAL_SIZE)); d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, 0, MAX_PARTIAL_SIZE));
d.sendRequest((Object)socket); d.sendRequest((Object)hostList.get(0).getTCPSocket());
boolean fileFullyWritten = false; boolean fileFullyWritten = false;
long offset = 0; long offset = 0;
do { do {
@ -194,7 +177,7 @@ public class ClientManagementTCP implements Runnable {
if (offset != fp.getTotalSize()) { if (offset != fp.getTotalSize()) {
System.err.println("Sending following request with offset: " + offset + " maxpartialsize: " + MAX_PARTIAL_SIZE); System.err.println("Sending following request with offset: " + offset + " maxpartialsize: " + MAX_PARTIAL_SIZE);
d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, offset, MAX_PARTIAL_SIZE)); d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, offset, MAX_PARTIAL_SIZE));
d.sendRequest((Object)socket); d.sendRequest((Object)hostList.get(0).getTCPSocket());
} else { } else {
fileFullyWritten = true; fileFullyWritten = true;
} }
@ -214,7 +197,7 @@ public class ClientManagementTCP implements Runnable {
} else { } else {
// next partialContentRequest // next partialContentRequest
d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, offset, MAX_PARTIAL_SIZE)); d = new ProtocolP2PPacketTCP((Payload) new LoadRequest(filename, offset, MAX_PARTIAL_SIZE));
d.sendRequest((Object)socket); d.sendRequest((Object)hostList.get(0).getTCPSocket());
} }
} else { } else {
@ -242,7 +225,7 @@ public class ClientManagementTCP implements Runnable {
System.err.println(""); System.err.println("");
throw new InternalError(); throw new InternalError();
} }
socket.close(); hostList.get(0).closeTCPSocket();
} }
/** list servers directory content /** list servers directory content
@ -262,7 +245,7 @@ public class ClientManagementTCP implements Runnable {
*/ */
private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, SocketClosed, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, SocketClosed, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError {
ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.LIST_REQUEST)); ProtocolP2PPacketTCP d = new ProtocolP2PPacketTCP(new Payload(RequestResponseCode.LIST_REQUEST));
d.sendRequest((Object)socket); d.sendRequest((Object)hostList.get(0).getTCPSocket());
try { try {
Payload p = d.receiveResponse().getPayload(); Payload p = d.receiveResponse().getPayload();
assert p instanceof FileList : "This payload must be instance of Filelist"; assert p instanceof FileList : "This payload must be instance of Filelist";

@ -12,15 +12,17 @@ import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError; import remoteException.VersionRemoteError;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Scanner; import java.util.Scanner;
import java.net.InetAddress; //import java.net.InetAddress;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.net.SocketException; //import java.net.SocketException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.io.File; import java.io.File;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import tools.HostItem;
import protocolP2P.ProtocolP2PPacketUDP; import protocolP2P.ProtocolP2PPacketUDP;
import protocolP2P.Payload; import protocolP2P.Payload;
import protocolP2P.RequestResponseCode; import protocolP2P.RequestResponseCode;
@ -41,31 +43,16 @@ import java.security.NoSuchAlgorithmException;
*/ */
public class ClientManagementUDP implements Runnable { public class ClientManagementUDP implements Runnable {
private String baseDirectory; private String baseDirectory;
private int UDPPort; private List<HostItem> hostList;
private String host;
private DatagramSocket socket;
/** Constructor for UDP implementation, with baseDirectory and UDPPort parameters. /** Constructor for UDP implementation, with baseDirectory and UDPPort parameters.
* @param baseDirectory the root directory where files are stored * @param baseDirectory the root directory where files are stored
* @param host hostname of the server * @param host hostname of the server
* @param UDPPort the server will listen on this port * @param UDPPort the server will listen on this port
*/ */
public ClientManagementUDP(String baseDirectory, String host, int UDPPort) { public ClientManagementUDP(String baseDirectory, List<HostItem> hostList) {
this.baseDirectory = baseDirectory; this.baseDirectory = baseDirectory;
this.host = host; this.hostList = hostList;
this.UDPPort = UDPPort;
try {
socket = new DatagramSocket();
socket.connect(InetAddress.getByName(host), UDPPort);
} catch (SocketException e) {
System.err.println("Error: No UDP socket available.");
System.exit(-1);
} catch (UnknownHostException e) {
System.err.println("Error: Unknown host.");
System.exit(-1);
}
} }
/** Implementation of Runnable /** Implementation of Runnable
@ -133,7 +120,7 @@ public class ClientManagementUDP implements Runnable {
HashAlgorithm[] hashesAlgo = new HashAlgorithm[1]; HashAlgorithm[] hashesAlgo = new HashAlgorithm[1];
hashesAlgo[0] = HashAlgorithm.SHA512; hashesAlgo[0] = HashAlgorithm.SHA512;
ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new HashRequest(filename, hashesAlgo)); ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP((Payload) new HashRequest(filename, hashesAlgo));
d.sendRequest((Object)socket); d.sendRequest((Object)hostList.get(0).getUDPSocket());
try { try {
Payload pHash = d.receiveResponse().getPayload(); Payload pHash = d.receiveResponse().getPayload();
assert pHash instanceof HashResponse : "This payload must be instance of HashResponse"; assert pHash instanceof HashResponse : "This payload must be instance of HashResponse";
@ -152,7 +139,7 @@ public class ClientManagementUDP implements Runnable {
} }
d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, 0, MAX_PARTIAL_SIZE)); d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, 0, MAX_PARTIAL_SIZE));
d.sendRequest((Object)socket); d.sendRequest((Object)hostList.get(0).getUDPSocket());
boolean fileFullyWritten = false; boolean fileFullyWritten = false;
long offset = 0; long offset = 0;
do { do {
@ -182,7 +169,7 @@ public class ClientManagementUDP implements Runnable {
if (offset != fp.getTotalSize()) { if (offset != fp.getTotalSize()) {
System.err.println("Sending following request with offset: " + offset + " maxpartialsize: " + MAX_PARTIAL_SIZE); System.err.println("Sending following request with offset: " + offset + " maxpartialsize: " + MAX_PARTIAL_SIZE);
d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, offset, MAX_PARTIAL_SIZE)); d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, offset, MAX_PARTIAL_SIZE));
d.sendRequest((Object)socket); d.sendRequest((Object)hostList.get(0).getUDPSocket());
} else { } else {
fileFullyWritten = true; fileFullyWritten = true;
} }
@ -202,7 +189,7 @@ public class ClientManagementUDP implements Runnable {
} else { } else {
// next partialContentRequest // next partialContentRequest
d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, offset, MAX_PARTIAL_SIZE)); d = new ProtocolP2PPacketUDP((Payload) new LoadRequest(filename, offset, MAX_PARTIAL_SIZE));
d.sendRequest((Object)socket); d.sendRequest((Object)hostList.get(0).getUDPSocket());
} }
} else { } else {
@ -248,7 +235,7 @@ public class ClientManagementUDP implements Runnable {
*/ */
private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError { private String[] listDirectory() throws EmptyDirectory, InternalError, UnknownHostException, IOException, TransmissionError, ProtocolError, VersionError, SizeError, InternalRemoteError, ProtocolRemoteError, VersionRemoteError {
ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.LIST_REQUEST)); ProtocolP2PPacketUDP d = new ProtocolP2PPacketUDP(new Payload(RequestResponseCode.LIST_REQUEST));
d.sendRequest((Object)socket); d.sendRequest((Object)hostList.get(0).getUDPSocket());
try { try {
Payload p = d.receiveResponse().getPayload(); Payload p = d.receiveResponse().getPayload();
assert p instanceof FileList : "This payload must be instance of Filelist"; assert p instanceof FileList : "This payload must be instance of Filelist";

@ -3,17 +3,25 @@ import clientP2P.ClientManagementUDP;
import clientP2P.ClientManagementTCP; import clientP2P.ClientManagementTCP;
import tools.Directories; import tools.Directories;
import java.util.Scanner; import java.util.Scanner;
import java.util.List;
import tools.HostItem;
import tools.HostList;
public class ClientP2P { public class ClientP2P {
private String host; private String host;
private int port; private int port;
private Directories directories; private Directories directories;
private List<HostItem> hostList;
private String parts = ".parts";
public ClientP2P() { public ClientP2P() {
directories = new Directories("P2P_JAVA_PROJECT_CLIENT"); directories = new Directories("P2P_JAVA_PROJECT_CLIENT");
directories.createSubdir(parts);
host = "localhost"; host = "localhost";
port = 40001; port = 40001;
System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directories.getDataHomeDirectory()); System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + directories.getDataHomeDirectory());
directories.askOpenDataHomeDirectory(null); directories.askOpenDataHomeDirectory(null);
System.out.println("Please enter list of servers to use; first one will be used to ask list of files");
hostList = HostList.getServList();
} }
public static void main(String [] args) { public static void main(String [] args) {
@ -27,7 +35,7 @@ public class ClientP2P {
case "udp": case "udp":
case "2" : case "2" :
System.out.println("Starting with UDP"); System.out.println("Starting with UDP");
ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.host, c.port); ClientManagementUDP cmudp = new ClientManagementUDP(c.directories.getDataHomeDirectory(), c.hostList);
t = new Thread(cmudp); t = new Thread(cmudp);
break; break;
case "TCP": case "TCP":
@ -35,7 +43,7 @@ public class ClientP2P {
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.host, c.port); ClientManagementTCP cmtcp = new ClientManagementTCP(c.directories.getDataHomeDirectory(), c.hostList);
t = new Thread(cmtcp); t = new Thread(cmtcp);
break; break;
} }

@ -0,0 +1,84 @@
package tools;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.Socket;
import java.net.DatagramSocket;
import java.net.UnknownHostException;
import java.io.IOException;
/** Class to store hostnames + ports
*/
public class HostItem {
private String hostname;
private int port;
private Socket tcpSocket;
private DatagramSocket udpSocket;
/** Constructor with hostname and port
* @param hostname Hostname
* @param port Port
*/
public HostItem(String hostname, int port) {
this.port = port;
this.hostname = hostname;
}
/** Get TCP Socket.
* @return TCP Socket
*/
public Socket getTCPSocket() {
if (tcpSocket == null) {
try {
tcpSocket = new Socket(InetAddress.getByName(hostname), port);
} catch (SocketException e) {
System.err.println("Error: No TCP socket available.");
System.exit(-1);
} catch (UnknownHostException e) {
System.err.println("Error: Unknown host.");
System.exit(-1);
} catch (IOException e) {
System.err.println("Error: Cannot create TCP socket");
System.exit(-1);
}
}
return tcpSocket;
}
/** Closes tcp socket
*/
public void closeTCPSocket() {
if (tcpSocket != null) {
try {
tcpSocket.close();
} catch (IOException e2) {
System.err.println("Error: cannot close socket");
}
}
tcpSocket = null;
}
/** Get UDP Socket
* return UDP Socket
*/
public DatagramSocket getUDPSocket() {
if (udpSocket == null) {
try {
udpSocket = new DatagramSocket();
udpSocket.connect(InetAddress.getByName(hostname), port);
} catch (SocketException e) {
System.err.println("Error: No UDP socket available.");
System.exit(-1);
} catch (UnknownHostException e) {
System.err.println("Error: Unknown host.");
System.exit(-1);
}
}
return udpSocket;
}
public void closeUDPSocket() {
if (udpSocket != null) {
udpSocket.close();
}
udpSocket = null;
}
}

@ -2,36 +2,54 @@ package tools;
import java.util.Scanner; import java.util.Scanner;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.InputMismatchException;
import tools.HostItem;
/** Helper to get the server list from the user
public class HostList{ * @author Louis Royer
/** Helper to get the server list from the user * @author Flavien Haas
* @author Louis Royer * @author JS Auge
* @author Flavien Haas * @version 1.0
* @author JS Auge
* @version 1.0
*/
/**
* Let the user enter all server and puts it in a list
* @return list of servers
*/ */
public class HostList {
public List<String> getServList(){ /**
List<String> serverList = new ArrayList<String>(); * Let the user enter all server and puts it in a list
Scanner scanner = new Scanner(System.in); * @return list of servers
String servName = ""; */
do { public static List<HostItem> getServList() {
System.out.println("Name of the next server: (or \"stop\" when finished)"); List<HostItem> serverList = new ArrayList<HostItem>();
servName = scanner.nextLine(); Scanner scanner = new Scanner(System.in);
if (servName != "stop"){ String servName;
serverList.add(servName); int port = 0;
} do {
} while (servName != "stop"); System.out.println("Enter hostname of next server: (or \"stop\" when finished, default: localhost).");
return serverList; servName = scanner.nextLine();
} if (servName.equals("")) {
servName = "localhost";
}
if (!servName.equals("stop")) {
boolean err = false;
do {
System.out.println("Enter port for this server");
try {
port = scanner.nextInt();
scanner.nextLine();
if (port > 65535 || port <= 0) {
err = true;
System.out.println("Port number must be in 1-65535 range. Try again.");
} else {
err = false;
}
} catch (InputMismatchException e) {
System.out.println("Invalid number. Try again.");
err = true;
}
} while (err);
serverList.add(new HostItem(servName, port));
}
} while (!servName.equals("stop"));
return serverList;
}
} }

Loading…
Cancel
Save