package clientP2P;
import java.util.List;
import java.util.ArrayList;
import java.net.DatagramSocket;
import protocolP2P.ProtocolP2PPacketUDP;
import protocolP2P.ProtocolP2PPacket;
import protocolP2P.Payload;
import protocolP2P.LoadRequest;
import protocolP2P.FilePart;
import localException.InternalError;
import remoteException.EmptyDirectory;
import remoteException.EmptyFile;
import localException.ProtocolError;
import remoteException.InternalRemoteError;
import remoteException.VersionRemoteError;
import localException.TransmissionError;
import remoteException.ProtocolRemoteError;
import localException.VersionError;
import localException.SizeError;
import remoteException.NotFound;
import remoteException.NotATracker;
import java.nio.file.Files;
import java.io.File;
import java.io.IOException;
import tools.Logger;
import tools.LogLevel;
import tools.HostItem;
import clientP2P.ClientDownloadPart;

/** Class to download file parts on udp.
* @author	Louis Royer
* @author	Flavien Haas
* @author	JS Auge
* @version	1.0
*/
public class ClientDownloadPartUDP extends ClientDownloadPart {

	private DatagramSocket socket;


	/** 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
	 * @param logger	Logger
	 * @param client	HostItem of the application
	 * @param server	HostItem of the server application
	 */
	public ClientDownloadPartUDP(ClientDownload manager, String filename, DatagramSocket socket, String partsSubdir, Logger logger, HostItem client, HostItem server) {
		super(manager, filename, partsSubdir, logger, client, server);
		this.socket = socket;
	}

	/** Get the socket */
	protected Object getSocket() {
		return (Object) socket;
	}
	
	/** Close the socket
	 */
	protected void closeSocket() throws IOException {
		socket.close();
	}


	/** Implementation of writeLog
	 * @param text	Text to log
	 * @param logLevel	level of logging
	 */
	protected void writeLog(String text, LogLevel logLevel) {
		logger.writeUDP(text, logLevel);
	}

	/** Implementation of writeLog
	 * @param e	exception to log
	 * @param logLevel	level of logging
	 */
	protected void writeLog(Exception e, LogLevel logLevel) {
		logger.writeUDP(e, logLevel);
	}

	/** Create packets
	 * @param payload	Payload
	 */
	protected < T extends Payload > ProtocolP2PPacket<T> createProtocolP2PPacket(T payload) {
		return (ProtocolP2PPacket<T>)new ProtocolP2PPacketUDP<T>(payload);
	}
}
