parent
33f1829cdc
commit
ef5484566e
@ -0,0 +1,156 @@
|
||||
package serverP2P;
|
||||
import tools.Logger;
|
||||
import tools.LogLevel;
|
||||
import tools.HostItem;
|
||||
import java.io.File;
|
||||
import java.util.Vector;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import protocolP2P.HashAlgorithm;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.io.IOException;
|
||||
|
||||
/** Class allowing to keep the tracker informed about file list
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public abstract class FileWatcher implements Runnable {
|
||||
protected String[] fileList = new String[0];
|
||||
protected Logger logger;
|
||||
protected volatile boolean stop;
|
||||
protected long time;
|
||||
protected boolean force;
|
||||
protected HostItem server;
|
||||
protected HostItem tracker;
|
||||
protected String baseDirectory;
|
||||
protected Map<String, byte[]> sha512 = new HashMap<>();
|
||||
|
||||
|
||||
/** Constructor
|
||||
* @param logger Logger
|
||||
* @param millis Time interval before recheck
|
||||
* @param server HostItem for the server
|
||||
* @param tracker HostItem for the tracker
|
||||
* @param baseDirectory Directory to search files
|
||||
*/
|
||||
public FileWatcher(Logger logger, long millis, HostItem server, HostItem tracker, String baseDirectory) {
|
||||
assert logger != null : "Logger is null";
|
||||
assert server != null : "Server is null";
|
||||
assert tracker != null : "Tracker is null";
|
||||
assert baseDirectory != null : "baseDirectory is null";
|
||||
this.logger = logger;
|
||||
time = millis;
|
||||
this.server = server;
|
||||
this.tracker = tracker;
|
||||
this.baseDirectory = baseDirectory;
|
||||
}
|
||||
|
||||
/** FileList getter
|
||||
* @return fileList
|
||||
*/
|
||||
public String[] getFileList() {
|
||||
return fileList;
|
||||
}
|
||||
|
||||
/** Sha512 map getter
|
||||
* @return sha512 hashmap
|
||||
*/
|
||||
public Map<String, byte[]> getSha512Map() {
|
||||
return sha512;
|
||||
}
|
||||
|
||||
/** Allow a manual check
|
||||
*/
|
||||
public void trigger() {
|
||||
if (updateFileList() || force) {
|
||||
force = false;
|
||||
writeLog("File list watcher detected changes. Informing tracker.", LogLevel.Info);
|
||||
registerTracker();
|
||||
}
|
||||
}
|
||||
|
||||
/** Runnable implementation */
|
||||
public void run() {
|
||||
writeLog("File list watcher started : delay " + time + " milliseconds.", LogLevel.Info);
|
||||
while(!stop) {
|
||||
trigger();
|
||||
try {
|
||||
Thread.sleep(time);
|
||||
} catch(InterruptedException e) {
|
||||
writeLog("File list watcher interrupted", LogLevel.Error);
|
||||
setStop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Register server on tracker
|
||||
*/
|
||||
protected abstract void registerTracker();
|
||||
|
||||
/** Update fileList and returns true if different than old list.
|
||||
* @return true if changed
|
||||
*/
|
||||
protected boolean updateFileList() {
|
||||
File folder = new File(baseDirectory);
|
||||
Vector<String> v = new Vector<String>();
|
||||
File[] files = folder.listFiles();
|
||||
/* Add non-recursively files's names to fileList */
|
||||
for (File f : files) {
|
||||
if (f.isFile()) {
|
||||
v.add(f.getName());
|
||||
}
|
||||
}
|
||||
String[] newFileList = new String[v.size()];
|
||||
v.toArray(newFileList);
|
||||
Arrays.sort(newFileList);
|
||||
if (!Arrays.equals(newFileList, fileList)) {
|
||||
fileList = newFileList;
|
||||
initSha512();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Ask the thread to stop
|
||||
*/
|
||||
public void setStop() {
|
||||
stop = true;
|
||||
}
|
||||
|
||||
/** Init sha512 map.
|
||||
*/
|
||||
protected void initSha512() {
|
||||
for(String f: fileList) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance(HashAlgorithm.SHA512.getName());
|
||||
sha512.put(f, md.digest(Files.readAllBytes(Paths.get(baseDirectory + f))));
|
||||
md.reset();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
writeLog("sha512 not supported", LogLevel.Error);
|
||||
} catch (IOException e) {
|
||||
writeLog("cannot read " + f, LogLevel.Warning);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** 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);
|
||||
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package serverP2P;
|
||||
import tools.Logger;
|
||||
import tools.LogLevel;
|
||||
import protocolP2P.ProtocolP2PPacket;
|
||||
import protocolP2P.ProtocolP2PPacketTCP;
|
||||
import protocolP2P.Register;
|
||||
import protocolP2P.Payload;
|
||||
import tools.HostItem;
|
||||
|
||||
/** Class allowing to keep the tracker informed about file list (TCP impl.)
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class FileWatcherTCP extends FileWatcher {
|
||||
|
||||
/** Constructor
|
||||
* @param logger Logger
|
||||
* @param millis Time interval before recheck
|
||||
* @param server HostItem for the server
|
||||
* @param tracker HostItem for the tracker
|
||||
* @param baseDirectory Directory to search files
|
||||
*/
|
||||
public FileWatcherTCP(Logger logger, long millis, HostItem server, HostItem tracker, String baseDirectory) {
|
||||
super(logger, millis, server, tracker, baseDirectory);
|
||||
assert logger != null : "Logger is null";
|
||||
assert server != null : "Server is null";
|
||||
assert tracker != null : "Tracker is null";
|
||||
assert baseDirectory != null : "baseDirectory is null";
|
||||
}
|
||||
|
||||
/** 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);
|
||||
}
|
||||
|
||||
/** Register server on tracker
|
||||
*/
|
||||
protected void registerTracker() {
|
||||
try {
|
||||
writeLog("Trying to into tracker", LogLevel.Info);
|
||||
ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketTCP((Payload)new Register(server));
|
||||
p.sendRequest((Object)tracker.tryGetTCPSocket());
|
||||
writeLog("Register request sent.", LogLevel.Debug);
|
||||
tracker.closeTCPSocket();
|
||||
} catch (Exception e) {
|
||||
// error, trying again at next iteration
|
||||
force = true;
|
||||
writeLog("Cannot contact tracker, trying again at next iteration (" + time + " milliseconds).", LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package serverP2P;
|
||||
import tools.Logger;
|
||||
import tools.LogLevel;
|
||||
import protocolP2P.ProtocolP2PPacket;
|
||||
import protocolP2P.ProtocolP2PPacketUDP;
|
||||
import protocolP2P.Register;
|
||||
import protocolP2P.Payload;
|
||||
import tools.HostItem;
|
||||
|
||||
/** Class allowing to keep the tracker informed about file list (UDP impl.)
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public class FileWatcherUDP extends FileWatcher {
|
||||
|
||||
/** Constructor
|
||||
* @param logger Logger
|
||||
* @param millis Time interval before recheck
|
||||
* @param server HostItem for the server
|
||||
* @param tracker HostItem for the tracker
|
||||
* @param baseDirectory Directory to search files
|
||||
*/
|
||||
public FileWatcherUDP(Logger logger, long millis, HostItem server, HostItem tracker, String baseDirectory) {
|
||||
super(logger, millis, server, tracker, baseDirectory);
|
||||
assert logger != null : "Logger is null";
|
||||
assert server != null : "Server is null";
|
||||
assert tracker != null : "Tracker is null";
|
||||
assert baseDirectory != null : "baseDirectory is null";
|
||||
}
|
||||
|
||||
/** 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);
|
||||
}
|
||||
|
||||
/** Register server on tracker
|
||||
*/
|
||||
protected void registerTracker() {
|
||||
try {
|
||||
writeLog("Trying to register into tracker", LogLevel.Info);
|
||||
ProtocolP2PPacket p = (ProtocolP2PPacket)new ProtocolP2PPacketUDP((Payload)new Register(server));
|
||||
p.sendRequest((Object)tracker.getUDPSocket());
|
||||
writeLog("Register request sent (but cannot ensure reception).", LogLevel.Debug);
|
||||
tracker.closeUDPSocket();
|
||||
} catch (Exception e) {
|
||||
force = true;
|
||||
writeLog("Cannot contact tracker, trying again at next iteration (" + time + " milliseconds).", LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,234 @@
|
||||
package serverP2P;
|
||||
import serverP2P.FileWatcher;
|
||||
import tools.Logger;
|
||||
import tools.LogLevel;
|
||||
import tools.HostItem;
|
||||
import protocolP2P.ProtocolP2PPacket;
|
||||
import protocolP2P.Payload;
|
||||
import protocolP2P.RequestResponseCode;
|
||||
import protocolP2P.FileList;
|
||||
import protocolP2P.FilePart;
|
||||
import protocolP2P.LoadRequest;
|
||||
import protocolP2P.HashRequest;
|
||||
import protocolP2P.HashResponse;
|
||||
import protocolP2P.HashAlgorithm;
|
||||
import protocolP2P.Unregister;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.io.IOException;
|
||||
|
||||
/** Implementation of P2P-JAVA-PROJECT VERSION 1.0 protocol.
|
||||
* @author Louis Royer
|
||||
* @author Flavien Haas
|
||||
* @author JS Auge
|
||||
* @version 1.0
|
||||
*/
|
||||
public abstract class ServerManagement implements Runnable {
|
||||
|
||||
protected volatile boolean stop;
|
||||
protected FileWatcher fileListWatcher;
|
||||
protected Logger logger;
|
||||
protected String baseDirectory;
|
||||
protected HostItem server;
|
||||
protected HostItem tracker;
|
||||
|
||||
/** Constructor */
|
||||
public ServerManagement(String baseDirectory, HostItem server, HostItem tracker, Logger logger) {
|
||||
assert baseDirectory != null : "baseDirectory is null";
|
||||
assert server != null : "server is null";
|
||||
assert tracker != null : "tracker is null";
|
||||
assert logger != null : "logger is null";
|
||||
stop = false;
|
||||
this.baseDirectory = baseDirectory;
|
||||
this.server = server;
|
||||
this.tracker = tracker;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
/** Stop the thread */
|
||||
public void setStop() {
|
||||
stop = true;
|
||||
}
|
||||
|
||||
/** Trigger a manual check of the file list
|
||||
*/
|
||||
public void updateFileList() {
|
||||
if (fileListWatcher != null) {
|
||||
fileListWatcher.trigger();
|
||||
}
|
||||
}
|
||||
|
||||
/** 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 response to list request
|
||||
* @param pd Request received
|
||||
*/
|
||||
protected < T extends ProtocolP2PPacket > void sendListResponse(T pd) {
|
||||
try {
|
||||
String[] fileList = fileListWatcher.getFileList();
|
||||
if (fileList.length == 0) {
|
||||
writeLog("Sending EMPTY_DIRECTORY to host " + pd.getHostItem(), LogLevel.Action);
|
||||
pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.EMPTY_DIRECTORY)));
|
||||
} else {
|
||||
writeLog("Sending LIST_RESPONSE to host " + pd.getHostItem(), LogLevel.Action);
|
||||
pd.sendResponse(createProtocolP2PPacket((Payload)(new FileList(fileList))));
|
||||
}
|
||||
} catch (Exception e2) {
|
||||
writeLog(e2, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/** 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 hash response to hash request
|
||||
* @param pd Request received
|
||||
*/
|
||||
protected < T extends ProtocolP2PPacket > void sendHashResponse(T pd) {
|
||||
Payload p = pd.getPayload();
|
||||
assert p instanceof HashRequest : "payload must be an instance of HashRequest";
|
||||
if (!(p instanceof HashRequest)) {
|
||||
sendInternalError(pd);
|
||||
} else {
|
||||
String filename = ((HashRequest)p).getFilename();
|
||||
if (Arrays.binarySearch(fileListWatcher.getFileList(), filename) >= 0) {
|
||||
Map<HashAlgorithm, byte[]> hashes = new HashMap<>();
|
||||
for (HashAlgorithm h : ((HashRequest)p).getAlgoList()) {
|
||||
switch (h) {
|
||||
case SHA512:
|
||||
hashes.put(h, fileListWatcher.getSha512Map().get(filename));
|
||||
break;
|
||||
case MD5:
|
||||
default:
|
||||
hashes.put(h, new byte[0]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
try {
|
||||
pd.sendResponse(createProtocolP2PPacket((Payload)(new HashResponse(filename, hashes))));
|
||||
} catch (Exception e) {
|
||||
writeLog(e, LogLevel.Error);
|
||||
}
|
||||
} else {
|
||||
// file not found
|
||||
try {
|
||||
pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.NOT_FOUND)));
|
||||
} catch (Exception e) {
|
||||
writeLog(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Send response to load request
|
||||
* @param pd Request received
|
||||
*/
|
||||
protected < T extends ProtocolP2PPacket > void sendLoadResponse(T pd) {
|
||||
Payload p = pd.getPayload();
|
||||
assert p instanceof LoadRequest : "payload must be an instance of LoadRequest";
|
||||
if (!(p instanceof LoadRequest)) {
|
||||
sendInternalError(pd);
|
||||
} else {
|
||||
String filename = ((LoadRequest)p).getFilename();
|
||||
long offset = ((LoadRequest)p).getOffset();
|
||||
long maxSizePartialContent = ((LoadRequest)p).getMaxSizePartialContent();
|
||||
try {
|
||||
byte[] fullLoad = Files.readAllBytes(Paths.get(baseDirectory + filename));
|
||||
long sizeToSend = 0;
|
||||
if (fullLoad.length - offset < maxSizePartialContent) {
|
||||
writeLog("Sending last partialContent", LogLevel.Debug);
|
||||
sizeToSend = fullLoad.length - offset;
|
||||
} else {
|
||||
sizeToSend = maxSizePartialContent;
|
||||
}
|
||||
writeLog("maxSizePartialContent: " + maxSizePartialContent, LogLevel.Debug);
|
||||
writeLog("Sending " + filename + " from " + offset + " to " + (offset + sizeToSend), LogLevel.Debug);
|
||||
byte[] load = Arrays.copyOfRange(fullLoad, (int)offset, (int)(offset + sizeToSend));
|
||||
String[] fileList = fileListWatcher.getFileList();
|
||||
if (Arrays.binarySearch(fileList, filename) >= 0) {
|
||||
try {
|
||||
if (load.length == 0) {
|
||||
pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.EMPTY_FILE)));
|
||||
} else {
|
||||
pd.sendResponse(createProtocolP2PPacket((Payload)(new FilePart(filename, fullLoad.length, offset, load))));
|
||||
}
|
||||
} catch (Exception e2) {
|
||||
writeLog(e2, LogLevel.Error);
|
||||
}
|
||||
} else {
|
||||
writeLog("File requested not found: `" + filename + "` " + Arrays.binarySearch(fileList, filename), LogLevel.Debug);
|
||||
writeLog("File list:", LogLevel.Debug);
|
||||
for (String f: fileList) {
|
||||
writeLog("- " + f, LogLevel.Debug);
|
||||
}
|
||||
|
||||
throw new IOException(); // to send a NOT_FOUND in the catch block
|
||||
}
|
||||
} catch (IOException e) {
|
||||
try {
|
||||
pd.sendResponse(createProtocolP2PPacket(new Payload(RequestResponseCode.NOT_FOUND)));
|
||||
} catch (Exception e2) {
|
||||
writeLog(e2, LogLevel.Debug);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Getter for tracker socket
|
||||
*/
|
||||
protected abstract Object getTrackerSocket();
|
||||
|
||||
/** Send unregister request to tracker
|
||||
*/
|
||||
protected void sendUnregisterRequest() {
|
||||
// unregistering from tracker
|
||||
try {
|
||||
writeLog("Unregistering from tracker", LogLevel.Info);
|
||||
ProtocolP2PPacket p = createProtocolP2PPacket((Payload)new Unregister(server));
|
||||
p.sendRequest(getTrackerSocket());
|
||||
} catch (Exception e) {
|
||||
writeLog("Cannot unregister from tracker", LogLevel.Error);
|
||||
writeLog(e, LogLevel.Error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue