package clientP2P;

import java.util.Scanner;
import protocolP2P.Payload;
import protocolP2P.ProtocolP2PPacket;
import protocolP2P.ProtocolP2PPacketTCP;
import tools.HostItem;
import tools.Logger;
import tools.LogLevel;
import clientP2P.ClientDownloadTCP;
import clientP2P.ClientManagement;


/** Implementation of P2P-JAVA-PROJECT CLIENT
 * @author	Louis Royer
 * @author	Flavien Haas
 * @author	JS Auge
 * @version	1.0
 */
public class ClientManagementTCP extends ClientManagement {
	/** Constructor for TCP implementation, with baseDirectory, tracker, partsSubdir, logger, and scanner parameters.
	 * @param baseDirectory	the root directory where files are stored
	 * @param tracker Tracker hostItem
	 * @param partsSubdir	subdirectory to store file parts
	 * @param logger	Loggger
	 * @param scanner	Scanner used to read input
	 * @param client	HostItem of the application
	 */
	public ClientManagementTCP(String baseDirectory, HostItem tracker, String partsSubdir, Logger logger, Scanner scanner, HostItem client) {
		super(baseDirectory, tracker, partsSubdir, logger, scanner, client);
	}

	/** Initialize downloader
	 * @param filename	Name of the file to download
	 */
	protected void initDownloader(String filename) {
		downLoader = (ClientDownload) new ClientDownloadTCP(filename, hostList, partsSubdir, baseDirectory, logger, client, tracker);
	}
	


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

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

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

	/** Getter for tracker socket
	 */
	protected Object getTrackerSocket() {
		return (Object)tracker.getTCPSocket();
	}

	/** Getter for HostItem socket
	 * @param hostItem	HostItem
	 */
	protected Object getHostItemSocket(HostItem hostItem) {
		return (Object)hostItem.getTCPSocket();
	}
}
