Projet_JAVA_P2P_STRI2A/src/clientP2P/ClientDownloadPartUDP.java

89 lines
2.4 KiB
Java
Raw Normal View History

2020-03-12 17:52:31 +01:00
package clientP2P;
import java.util.List;
import java.util.ArrayList;
import java.net.DatagramSocket;
import protocolP2P.ProtocolP2PPacketUDP;
2020-03-22 16:55:05 +01:00
import protocolP2P.ProtocolP2PPacket;
2020-03-12 17:52:31 +01:00
import protocolP2P.Payload;
import protocolP2P.LoadRequest;
import protocolP2P.FilePart;
2020-03-19 13:48:39 +01:00
import localException.InternalError;
2020-03-12 17:52:31 +01:00
import remoteException.EmptyDirectory;
import remoteException.EmptyFile;
2020-03-19 13:48:39 +01:00
import localException.ProtocolError;
2020-03-12 17:52:31 +01:00
import remoteException.InternalRemoteError;
import remoteException.VersionRemoteError;
2020-03-19 13:48:39 +01:00
import localException.TransmissionError;
2020-03-12 17:52:31 +01:00
import remoteException.ProtocolRemoteError;
2020-03-19 13:48:39 +01:00
import localException.VersionError;
import localException.SizeError;
2020-03-12 17:52:31 +01:00
import remoteException.NotFound;
2020-03-20 11:27:57 +01:00
import remoteException.NotATracker;
2020-03-12 17:52:31 +01:00
import java.nio.file.Files;
import java.io.File;
import java.io.IOException;
2020-03-19 17:49:39 +01:00
import tools.Logger;
import tools.LogLevel;
2020-03-27 16:22:42 +01:00
import tools.HostItem;
2020-03-22 16:55:05 +01:00
import clientP2P.ClientDownloadPart;
2020-03-12 17:52:31 +01:00
/** Class to download file parts on udp.
* @author Louis Royer
* @author Flavien Haas
* @author JS Auge
* @version 1.0
*/
2020-03-22 16:55:05 +01:00
public class ClientDownloadPartUDP extends ClientDownloadPart {
2020-03-12 17:52:31 +01:00
private DatagramSocket socket;
2020-03-22 16:55:05 +01:00
2020-03-12 17:52:31 +01:00
/** Constructor with filename, socket, and part subdir
* @param filename name of file to download
* @param socket socket to use
* @param partsSubdir directory to store .part files
2020-03-22 16:55:05 +01:00
* @param logger Logger
2020-03-27 16:22:42 +01:00
* @param client HostItem of the application
2020-03-28 16:31:24 +01:00
* @param server HostItem of the server application
2020-03-12 17:52:31 +01:00
*/
2020-03-28 16:31:24 +01:00
public ClientDownloadPartUDP(ClientDownload manager, String filename, DatagramSocket socket, String partsSubdir, Logger logger, HostItem client, HostItem server) {
super(manager, filename, partsSubdir, logger, client, server);
2020-03-12 17:52:31 +01:00
this.socket = socket;
}
2020-03-22 16:55:05 +01:00
/** Get the socket */
protected Object getSocket() {
return (Object) socket;
2020-03-12 17:52:31 +01:00
}
2020-03-22 16:55:05 +01:00
/** Close the socket
2020-03-12 17:52:31 +01:00
*/
2020-03-22 16:55:05 +01:00
protected void closeSocket() throws IOException {
2020-03-12 17:52:31 +01:00
socket.close();
}
2020-03-22 16:55:05 +01:00
/** Implementation of writeLog
* @param text Text to log
* @param logLevel level of logging
2020-03-12 17:52:31 +01:00
*/
2020-03-22 16:55:05 +01:00
protected void writeLog(String text, LogLevel logLevel) {
logger.writeUDP(text, logLevel);
2020-03-12 17:52:31 +01:00
}
2020-03-22 16:55:05 +01:00
/** Implementation of writeLog
* @param e exception to log
* @param logLevel level of logging
2020-03-19 13:30:49 +01:00
*/
2020-03-22 16:55:05 +01:00
protected void writeLog(Exception e, LogLevel logLevel) {
logger.writeUDP(e, logLevel);
2020-03-12 17:52:31 +01:00
}
2020-03-22 16:55:05 +01:00
/** Create packets
* @param payload Payload
2020-03-19 13:30:49 +01:00
*/
2020-03-22 16:55:05 +01:00
protected < T extends Payload > ProtocolP2PPacket<T> createProtocolP2PPacket(T payload) {
return (ProtocolP2PPacket<T>)new ProtocolP2PPacketUDP<T>(payload);
2020-03-12 17:52:31 +01:00
}
}