private void startDownload() { // Send Download command to server CommandDownload dwn = (CommandDownload)cmd; Console.WriteLine("DSH" + serverID + " Rozpoczeto download chunka " + dwnfrag + " od " + dwn.Begin + " do " + dwn.End); cmdTrans.sendCmd(cmd); Command recvd = cmdTrans.getCmd(); if (recvd.GetType().Equals(typeof(CommandChunk))) { // Pass the chunk to client Console.WriteLine("DSH" + serverID + " Pobralem chunk " + dwnfrag + " o rozmiarze " + ((CommandChunk)recvd).Data.Length); monitor.addDownloadChunk((CommandChunk)recvd, dwnfrag); // Let scheduler know we succeeded scheduler.success(dwnfrag); scheduler.wakeSch(); } else { Console.WriteLine("DSH: Unexpected server response during download: " + recvd.GetType()); scheduler.addFailed((CommandDownload)cmd); scheduler.wakeSch(); } }
private void startDownload(CommandDownload cmd) { currentClient = cmd.CmdProc; lastFrag = 0; int totalSize = cmd.End; maxFrag = (totalSize + fragSizeDwn - 1) / fragSizeDwn; // Round up monitor.setDownloadBuffer(fileBufferSize); succFrags = new bool[maxFrag]; lastSucc = 0; for (int i = 0; i < maxFrag; i++) { succFrags[i] = false; } CommandDownload dwn; DataServerHandler server; Console.WriteLine("SCH: Zaczynam Download"); while (!token.IsCancellationRequested) { // Wait for first idle DSH server = avaibleServer(); if (token.IsCancellationRequested) { break; } if (failedFrags.Count > 0) // There are some fragments that need to be redownloaded { dwn = failedFrags.Take(token); Console.WriteLine("SCH: Zfailowany fragment " + dwn.Begin); server.addDownload(dwn, dwn.Begin / fragSizeDwn); } else if (lastFrag < maxFrag) // Not all fragments have been downloaded { if (lastFrag >= lastSucc + fileBufferSize) { Console.WriteLine("SCH: Czekam na pobranie " + lastSucc); waitForDwn(); continue; } // Make new Download command for specific fragment and pass it to DSH dwn = makeChunk(cmd, lastFrag); Console.WriteLine("SCH: Zlecam nowy chunk " + lastFrag + " " + dwn.Begin + " " + dwn.End); server.addDownload(dwn, lastFrag); lastFrag++; } // All fragments have started downloading and no one failed yet, // so check if they all succeeded else if (allSucc()) { Console.WriteLine("SCH: Pobieranie zakonczone"); break; } } }
// add... methods - used by scheduler to pass Command and wake up DSH public bool addDownload(CommandDownload _cmd, int fragment) { lock (cmdLock) { Console.WriteLine("DSH: Dodany download"); dwnfrag = fragment; cmd = _cmd; state = States.download; Monitor.Pulse(cmdLock); Console.WriteLine("DSH: Zpulsowano"); return(true); } }
private CommandDownload makeChunk(CommandDownload cmd, int frag) { // Make new Download command for one fragment Console.WriteLine("SCH: Tworze nowy chunk " + frag); CommandDownload newcmd = new CommandDownload(cmd); newcmd.Begin = frag * fragSizeDwn; newcmd.End = (frag + 1) * fragSizeDwn; if (newcmd.Begin > cmd.End || newcmd.Begin < 0) { return(null); } if (newcmd.End > cmd.End) { newcmd.End = cmd.End; } Console.WriteLine("SCH: Chunk " + frag + " od " + newcmd.Begin + " do " + newcmd.End); return(newcmd); }
public void addFailed(CommandDownload cmd) { // Used by DSH to return a failed Download command failedFrags.Add(cmd); }