package tools; import tools.LogLevel; import protocolP2P.ProtocolP2PPacket; import protocolP2P.Payload; import protocolP2P.RequestResponseCode; public abstract class ServeErrors { /** Implementation of writeLog * @param text Text to log * @param logLevel level of logging */ protected abstract void writeLog(String text, LogLevel logLevel); /** Implementation of writeLog * @param e exception to log * @param logLevel level of logging */ protected abstract void writeLog(Exception e, LogLevel logLevel); /** Create packets * @param payload Payload */ protected abstract < T extends Payload > ProtocolP2PPacket createProtocolP2PPacket(T payload); /** Send a NotATracker error message. * @param pd Request received */ protected < T extends ProtocolP2PPacket > void sendNotATracker(T pd) { try { pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.NOT_A_TRACKER))); } catch (Exception e) { writeLog(e, LogLevel.Error); } } /** Send an internal error message. * @param pd Request received */ protected < T extends ProtocolP2PPacket > void sendInternalError(T pd) { writeLog("Internal Error", LogLevel.Warning); try { pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.INTERNAL_ERROR))); } catch (Exception e) { writeLog(e, LogLevel.Error); } } /** Send a not found message. * @param pd Request received */ protected < T extends ProtocolP2PPacket > void sendNotFound(T pd) { try { pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.NOT_FOUND))); } catch (Exception e) { writeLog(e, LogLevel.Error); } } /** Send an empty directory message. * @param pd Request received */ protected < T extends ProtocolP2PPacket > void sendEmptyDirectory(T pd) { try { pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.EMPTY_DIRECTORY))); } catch (Exception e) { writeLog(e, LogLevel.Error); } } /** Send an empty file message. * @param pd Request received */ protected < T extends ProtocolP2PPacket > void sendEmptyFile(T pd) { try { pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.EMPTY_FILE))); } catch (Exception e) { writeLog(e, LogLevel.Error); } } /** Send an unknown host message. * @param pd Request received */ protected < T extends ProtocolP2PPacket > void sendUnknownHost(T pd) { try { pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.UNKNOWN_HOST))); } catch (Exception e) { writeLog(e, LogLevel.Error); } } }