Guest User

Untitled

a guest
Jul 31st, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { spawn } = require('child_process');
  2.  
  3.  
  4. args = process.argv
  5.  
  6. if(args.length < 6) {
  7.   fileName = args[1].split('\\');
  8.   process.stderr.write(`This program needs at least 4 arguments\nIP Port Filename Chunk Count\n\nExample:\n${fileName[fileName.length-1]} 127.0.0.1 25565 test.txt 1\n\n`)
  9.   process.exit();
  10. }
  11.  
  12. downloadIP = args[2];
  13. negotiatedPort = parseInt(args[3]);
  14. fileName = args[4];
  15. chunkCount = parseInt(args[5]);
  16.  
  17. let threadCount = 0;
  18. let threads = {};
  19. let freeThreads = [];
  20. let currChunk = 0;
  21. let chunksReturned = 0;
  22. threadMaxLimited = 10;
  23. if(threadMaxLimited > chunkCount-1) {
  24.   threadMaxLimited = chunkCount-1
  25. }
  26. let threadMax = threadMaxLimited
  27. for(let i = 1; i <= threadMax; i++) {
  28.   freeThreads.push(i)
  29. }
  30.  
  31. let iterations = 1;
  32.  
  33. goodbye = function(myFinalMessage) {
  34.   if(myFinalMessage === 'change da world') {
  35.   changeDaWorld = spawn(`node`, [`downloadChunk.js`,`${downloadIP}`,`${negotiatedPort}`,`${fileName}`,`${iterations}`], {stdio: ['ignore','ipc','inherit']})
  36.   changeDaWorld  .on('data', function(data) {
  37.     if(process.send){
  38.     process.send({chunk:data.chunk, chunk_contents:data.chunk_contents})
  39.     }
  40.     rat = spawn(`node`, [`downloadChunk.js`,`${downloadIP}`,`${negotiatedPort}`,`${fileName}`,`${iterations+1}`], {stdio: ['ignore','ignore','ignore']})
  41.     rat.on('close', function(code) {
  42.       process.exit(code)
  43.     })
  44.     setTimeout(function(){
  45.       rat.kill()
  46.     },400)
  47.   })}
  48. }
  49.  
  50. downloadThread = function () {
  51. while(threadCount < threadMax) {
  52.    if(chunksReturned >= chunkCount-threadMax+1) {
  53.     threadMax = chunkCount-chunksReturned-1
  54.    }
  55.   if(freeThreads.length >= 0) {
  56.       currChunk = iterations;
  57.       process.stdout.write("[" + '\033[31;1;4m' + freeThreads.toString('latin1') + '\033[0m' + "]" + '\n')
  58.       threads[freeThreads[0]] = spawn(`node`, [`downloadChunk.js`,`${downloadIP}`,`${negotiatedPort}`,`${fileName}`,`${iterations}`], {stdio: ['ignore','ipc','inherit']})
  59.       .on('message', function(data) {
  60.         if(data.toString('latin1').split(',')[0] === 'timeout'.toString('latin1')) {
  61.           process.stdout.write('Timeout! ' + data.toString('latin1').split(',')[1].toString('latin1') + '\n')
  62.         } else {
  63.           if(chunksReturned >= chunkCount) {} else {
  64.             chunksReturned++
  65.           }
  66.           if(process.send){
  67.             if(data) {
  68.               process.send({chunk:data.chunk, chunk_contents:data.chunk_contents})
  69.             }
  70.           }
  71.           if(freeThreads.indexOf(Object.values(threads).indexOf(this)+1) != -1) {
  72.             process.stderr.write('OOPS, WE JUST TRIED TO ADD A THREAD THAT WAS STILL IN USE\n')
  73.           }
  74.       freeThreads.push(Object.values(threads).indexOf(this)+1)
  75.       threadCount--
  76.  }})
  77.   freeThreads.splice(0,1)
  78.  threadCount++
  79.  iterations++
  80. }}
  81. if(chunksReturned >= chunkCount-1) {
  82.   return goodbye('change da world')
  83. } else {
  84.   setTimeout(function() {
  85.     return  downloadThread()
  86.   },500)
  87. }}
  88.  
  89. downloadThread()
  90.  
Advertisement
Add Comment
Please, Sign In to add comment