|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
package clientP2P;
|
|
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
@ -24,6 +26,7 @@ import remoteException.ProtocolRemoteError;
|
|
|
|
|
import remoteException.NotFound;
|
|
|
|
|
import remoteException.InternalRemoteError;
|
|
|
|
|
import remoteException.NotATracker;
|
|
|
|
|
import remoteException.UnknownHost;
|
|
|
|
|
import protocolP2P.HashAlgorithm;
|
|
|
|
|
import protocolP2P.HashResponse;
|
|
|
|
|
import protocolP2P.HashRequest;
|
|
|
|
@ -32,6 +35,7 @@ import protocolP2P.FilePart;
|
|
|
|
|
import protocolP2P.SizeRequest;
|
|
|
|
|
import protocolP2P.SizeResponse;
|
|
|
|
|
import protocolP2P.ProtocolP2PPacket;
|
|
|
|
|
import protocolP2P.UpdateRatio;
|
|
|
|
|
import clientP2P.ClientDownloadPart;
|
|
|
|
|
import tools.HostItem;
|
|
|
|
|
import tools.Logger;
|
|
|
|
@ -49,6 +53,7 @@ public abstract class ClientDownload extends ServeErrors implements Runnable {
|
|
|
|
|
protected String filename;
|
|
|
|
|
protected byte[] hash512;
|
|
|
|
|
protected List<ClientDownloadPart> sockList = new ArrayList<ClientDownloadPart>();
|
|
|
|
|
protected Map<HostItem, Long> ratioUpdater = new HashMap<>();
|
|
|
|
|
protected List<Long> offsetsToAsk = new ArrayList<Long>();
|
|
|
|
|
protected List<Long> offsetsPending = new ArrayList<Long>();
|
|
|
|
|
protected boolean stop;
|
|
|
|
@ -59,6 +64,7 @@ public abstract class ClientDownload extends ServeErrors implements Runnable {
|
|
|
|
|
protected boolean success = false;
|
|
|
|
|
protected Logger logger;
|
|
|
|
|
protected HostItem client;
|
|
|
|
|
protected HostItem tracker;
|
|
|
|
|
|
|
|
|
|
/** Constructor with parameters: filename, list of hosts, parts subdirectory and dirStorage
|
|
|
|
|
* @param filename name of file to download
|
|
|
|
@ -67,14 +73,16 @@ public abstract class ClientDownload extends ServeErrors implements Runnable {
|
|
|
|
|
* @param dirStorage directory to write assembled file
|
|
|
|
|
* @param logger Logger
|
|
|
|
|
* @param client HostItem of the application
|
|
|
|
|
* @param tracker HostItem of the tracker
|
|
|
|
|
*/
|
|
|
|
|
public ClientDownload(String filename, List<HostItem> hostList, String partsSubdir, String dirStorage, Logger logger, HostItem client) {
|
|
|
|
|
public ClientDownload(String filename, List<HostItem> hostList, String partsSubdir, String dirStorage, Logger logger, HostItem client, HostItem tracker) {
|
|
|
|
|
this.partsSubdir = partsSubdir;
|
|
|
|
|
this.dirStorage = dirStorage;
|
|
|
|
|
this.filename = filename;
|
|
|
|
|
this.hostList = hostList;
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
this.client = client;
|
|
|
|
|
this.tracker = tracker;
|
|
|
|
|
this.stop = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -165,6 +173,7 @@ public abstract class ClientDownload extends ServeErrors implements Runnable {
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
throw new InternalError();
|
|
|
|
|
}
|
|
|
|
|
ratioUpdater.put(c.getServer(), c.getReceivedBytesCount());
|
|
|
|
|
}
|
|
|
|
|
writeLog("Task check status: " + offsetsToAsk.size() + " to asks, " + offsetsPending.size() + " pending", LogLevel.Info);
|
|
|
|
|
if (offsetsToAsk.isEmpty() && offsetsPending.isEmpty()) {
|
|
|
|
@ -180,6 +189,25 @@ public abstract class ClientDownload extends ServeErrors implements Runnable {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Send Ratio update to the tracker
|
|
|
|
|
*/
|
|
|
|
|
public void sendRatioUpdate() {
|
|
|
|
|
for(HostItem server: ratioUpdater.keySet()) {
|
|
|
|
|
Long r = ratioUpdater.get(server);
|
|
|
|
|
if (r != null) {
|
|
|
|
|
long rl = r.longValue();
|
|
|
|
|
if (rl != 0) {
|
|
|
|
|
try {
|
|
|
|
|
ProtocolP2PPacket<?> d = createProtocolP2PPacket(new UpdateRatio(client, server, rl));
|
|
|
|
|
d.sendRequest(getHostItemSocket(tracker));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
writeLog(e, LogLevel.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Get hashsum from server.
|
|
|
|
|
* @param hostItem server to ask hash
|
|
|
|
|
* @return hash512sum
|
|
|
|
@ -350,7 +378,6 @@ public abstract class ClientDownload extends ServeErrors implements Runnable {
|
|
|
|
|
while(!stop) {
|
|
|
|
|
assignTasks();
|
|
|
|
|
checkTasksStatus();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
writeLog("Reassembling file parts.", LogLevel.Info);
|
|
|
|
|