<commit_before>var fs = require('fs');
var superagent = require('superagent');
var async = require('async');
var _ = require('underscore');

global.client_list = './peers-list.txt'


global.set0 = []; // set0 contains hosts have the complete file
global.set1 = []; // set1 contains hosts don't have it yet


function do_match_hosts() {
  if (set1.length === 0) {
    console.log('Complete file distribution, bye~');
    process.exit(0);
  }

  var hungry_host_arr = set1.filter(function(node){
    return node.status === 'hungry';
  });

  if (hungry_host_arr.length === 0) {
    console.log('No more hungry hosts, just wait');
    return ;
  }

  var satisfied_host_arr = set0.filter(function(node){
    return node.status === 'satisfied';
  });

  if (satisfied_host_arr.length === 0) {
    console.log('No more satisfied hosts, just wait');
    return ;
  }
  
  var hungry_host = hungry_host_arr[0];
  var satisfied_host = satisfied_host_arr[0];

  provoke_trasfer(satisfied_host, hungry_host);
}

exports.match_hosts = function() {
  do_match_hosts();
}


exports.change_transferred_host_status = function(cmd_obj) {
  var from_addr = cmd_obj.from;
  var to_addr = cmd_obj.to;

  var from_host = set0.filter(function(node){
    return node.host === from_addr;
  })[0];
  from_host.status = 'satisfied';

  var to_host = set1.filter(function(node){
    return node.host === to_addr;
  })[0];

  var to_host_index = set1.indexOf(to_host);
  set1.splice(to_host_index, 1);

  to_host.status = 'satisfied';
  set0.push(to_host);

  console.log('\nAfter change transfer succeeded host status:');
  console.log('==============================================');
  console.log(set0);
  console.log(set1);
  console.log('==============================================');
}


exports.change_transfer_failed_host_status = function(cmd_obj) {
  var from_addr = cmd_obj.from;
  var to_addr = cmd_obj.to;

  var from_host = set0.filter(function(node){
    return node.host === from_addr;
  })[0];
  from_host.status = 'satisfied';

  var to_host = set1.filter(function(node){
    return node.host === to_addr;
  })[0];

  to_host.status = 'hungry';

  console.log('\nAfter change transfer failed host status:');
  console.log('==============================================');  
  console.log(set0);
  console.log(set1);
  console.log('==============================================');
}

function change_transfering_host_status(from_host, to_host) {
  from_host.status = 'sending';
  to_host.status = 'receving';

  console.log('\nAfter change transferring host status:');
  console.log('==============================================');
  console.log(set0);
  console.log(set1);
  console.log('==============================================');
}

function provoke_trasfer(from_host, to_host) {
  console.log('Provoke transfer, from ' + from_host.host + ':' + from_host.file_path + ', to ' + to_host.host + ':' + to_host.file_path);
  superagent.post(to_host.host + ':' + cmd_port)
    .send(
      {'cmd' : 'receive',
       'to_path' : to_host.file_path
      })<commit_msg>Refactor to stateless, first step<commit_after>var fs = require('fs');
var superagent = require('superagent');
var async = require('async');
var _ = require('underscore');

global.client_list = './peers-list.txt'


global.set0 = []; // set0 contains hosts have the complete file
global.set1 = []; // set1 contains hosts don't have it yet
global.set2 = []; // set1 contains hosts sending or receiving the file


function do_match_hosts() {
  if (set1.length === 0 && set2.length === 0) {
    console.log('Complete file distribution, bye~');
    process.exit(0);
  }

  if (set0.length === 0) {
    console.log('No more hungry hosts, we are doing last round transfer, just wait');
    return ;
  }

  if (set0.length === 0) {
    console.log('No more satisfied hosts, just wait');
    return ;
  }

  var satisfied_host = set0[0];
  var hungry_host = set1[0];

  provoke_trasfer(satisfied_host, hungry_host);
}

exports.match_hosts = function() {
  do_match_hosts();
}


exports.change_transferred_host_status = function(cmd_obj) {
  var from_addr = cmd_obj.from;
  var to_addr = cmd_obj.to;

  var from_host = set2.filter(function(node){
    return node.host === from_addr;
  })[0];
  from_host.status = 'satisfied';

  var to_host = set2.filter(function(node){
    return node.host === to_addr;
  })[0];
  to_host.status = 'satisfied';

  var from_host_index = set2.indexOf(from_host);
  set2.splice(from_host_index, 1);
  var to_host_index = set2.indexOf(to_host);
  set2.splice(to_host_index, 1);

  set0.push(from_host);
  set0.push(to_host);

  console.log('\nAfter change transfer succeeded host status:');
  console.log('==============================================');
  console.log(set0);
  console.log(set1);
  console.log(set1);
  console.log('==============================================');
}


exports.change_transfer_failed_host_status = function(cmd_obj) {
  var from_addr = cmd_obj.from;
  var to_addr = cmd_obj.to;

  var from_host = set2.filter(function(node){
    return node.host === from_addr;
  })[0];
  from_host.status = 'satisfied';

  var to_host = set2.filter(function(node){
    return node.host === to_addr;
  })[0];
  to_host.status = 'hungry';

  var from_host_index = set2.indexOf(from_host);
  set2.splice(from_host_index, 1);
  var to_host_index = set2.indexOf(to_host);
  set2.splice(to_host_index, 1);

  set0.push(from_host);
  set1.push(to_host);

  console.log('\nAfter change transfer failed host status:');
  console.log('==============================================');  
  console.log(set0);
  console.log(set1);
  console.log(set2);
  console.log('==============================================');
}

function change_transfering_host_status(from_host, to_host) {
  var from_host_index = set0.indexOf(from_host);
  set0.splice(from_host_index, 1);
  var to_host_index = set1.indexOf(to_host);
  set1.splice(to_host_index, 1);

  from_host.status = 'sending';
  to_host.status = 'receving';

  set2.push(from_host);
  set2.push(to_host);

  console.log('\nAfter change transferring host status:');
  console.log('==============================================');
  console.log(set0);
  console.log(set1);
  console.log(set2);
  console.log('==============================================');
}

function provoke_trasfer(from_host, to_host) {
  console.log('Provoke transfer, from ' + from_host.host + ':' + from_host.file_path + ', to ' + to_host.host + ':' + to_host.file_path);
  superagent.post(to_host.host + ':' + cmd_port)
    .send(
      {'cmd' : 'receive',
       'to_path' : to_host.file_path
      })
{'fraction_non_alphanumeric': 0.2244867149758454, 'fraction_numerical': 0.01373792270531401, 'mean_word_length': 4.54857621440536, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3350497', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2198, 'n_words': 430}