public bool addUpload(CommandUpload _cmd) { lock (cmdLock) { cmd = _cmd; state = States.upload; Monitor.Pulse(cmdLock); return(true); } }
private void startUpload() { Console.WriteLine("DSH" + serverID + " Rozpoczeto upload"); // Send Upload command to server, wait for Accept cmdTrans.sendCmd(cmd); CommandUpload upl = (CommandUpload)cmd; long sentSize = 0; CommandChunk chunk; Command recvd = cmdTrans.getCmd(); if (recvd.GetType().Equals(typeof(CommandAccept))) { Console.WriteLine("DSH" + serverID + " Upload zaakceptowany"); scheduler.ConfirmAccept(serverID); // Upload all the chunks int frag = 0; while (sentSize < upl.Size) { // Wait for the next chunk from scheduler and send it chunk = scheduler.takeUplChunk(frag, serverID);; Console.WriteLine("DSH" + serverID + ": Wysylam chunk " + frag + " Lacznie wyslano juz " + sentSize); cmdTrans.sendCmd(chunk); // Let scheduler know we uploaded this chunk scheduler.uplSucc(frag); scheduler.wakeSch(); sentSize += chunk.Data.Length; frag++; } Console.WriteLine("DSH" + serverID + ": Wyslano wszystkie chunki."); // Send commit recvd = cmdTrans.getCmd(); if (!recvd.GetType().Equals(typeof(CommandCommitRdy))) { Console.WriteLine("DSH: Unexpected server response after upload: " + recvd.GetType()); } else { state = States.uplwait; commit(); } } else { Console.WriteLine("DSH: Unexpected server response during upload: " + recvd.GetType()); } }
private void startUpload(CommandUpload cmd) { // Upload file on every server Console.WriteLine("Rozpoczynam upload"); foreach (DataServerHandler server in serverList) { server.addUpload(cmd); // Notify every server about upload } lock (schLock) { // Wait for Accept from every server while (accepted < serverList.Count) { Monitor.Wait(schLock); } } Console.WriteLine("Zaakceptowano upload na wszystkich serwerach"); // Prepare upload chunks for DSHs and wake them up cmd.CmdProc.Incoming.Add(new CommandAccept(), token); uplBuff = new CommandChunk[fileBufferSize]; maxFrag = (int)((cmd.Size + fragSizeUpl - 1) / fragSizeUpl); // Round up uplFrags = new byte[maxFrag]; lastSucc = 0; for (int i = 0; i < maxFrag; i++) { uplFrags[i] = 0; } for (int i = 0; i < fileBufferSize && i < maxFrag && !token.IsCancellationRequested; i++) { Console.WriteLine("SCH: Wstawiam do kolejki chunka " + i); uplBuff[i] = monitor.UploadChunkQueue.Take(token); // Ready chunks queue } Console.WriteLine("SCH: maxfrag = " + maxFrag + " caly size = " + cmd.Size); lock (dshLock) { Monitor.PulseAll(dshLock); } // Start uploading chunks while (!token.IsCancellationRequested) { try { lock (schLock) { // Wait for at least one DSH to complete/fail upload of one chunk Monitor.Wait(schLock); // Check if all chunks have been uploaded if (lastSucc >= maxFrag) { // All chunks taken by DSHs, wait for all CommitRdys while (waitingForCommit < serverList.Count && !token.IsCancellationRequested) { Monitor.Wait(schLock); } if (token.IsCancellationRequested) { break; } commit = new CommandCommit(); Console.WriteLine("Commit gotowy"); lock (dshLock) Monitor.PulseAll(dshLock); // Submit commit, wait for all CommitAcks while (commited < serverList.Count && !token.IsCancellationRequested) { Monitor.Wait(schLock); } if (token.IsCancellationRequested) { break; } Console.WriteLine("Zuploadowano plik na kazdy serwer"); cmd.CmdProc.Incoming.Add(new CommandAccept(), token); break; } // Check if one chunk has been uploaded by all DSHs lock (dshLock) { if ((int)uplFrags[lastSucc] == serverList.Count) { Console.WriteLine("SCH: Wszystkie zassały chunka " + lastSucc); // All DSHs uploaded one chunk, swap it with a new one uplFrags[lastSucc]++; if (lastSucc < maxFrag && maxFrag - lastSucc > fileBufferSize) { Console.WriteLine("SCH: Wstawiam chunka na miejsce " + lastSucc % fileBufferSize); uplBuff[lastSucc % fileBufferSize] = monitor.UploadChunkQueue.Take(token); } lastSucc++; } Monitor.PulseAll(dshLock); } } } catch (Exception e) { //TEMP } } // Token is cancelled }