var fs = require('fs');
var request = require('superagent');
var async = require('async');

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;
  });
  from_host.status = 'satisfied';

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

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

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

  console.log('>>>>>>>' + set0);
  console.log('>>>>>>>' + set1);
}


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;
  });
  from_host.status = 'satisfied';

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

  to_host.status = 'hungry';

  console.log('>>>>>>><<' + set0);
  console.log('>>>>>>><<' + set1);
}

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

function provoke_trasfer(from_host, to_host) {
  console.log('Provoke trasferation, from ' + from_host.host + ':' + from_host.file_path + ', to ' + to_host.host + ':' + to_host.file_path);
  request.post(from_host.host + ':' + port)
    .send(
      {'cmd' : 'transfer',
       'from' : from_host.host,
       'from_path' : from_host.file_path,
       'to' : to_host.host,
       'to_path' : to_host.file_path
      })
    .end(function(error, res){
      if (error) {
	console.error('Send transfer command to ' + from_host.host + ' error!' + error);
	return ;
      }
      change_transfering_host_status(from_host, to_host);

      console.log('After matching hosts:');
      console.log('==================================');
      console.log(set0);
      console.log(set1);
      console.log('==================================');

      do_match_hosts();
    });
}


exports.load_clients = function (cb) {
  var clients_arr = fs.readFileSync(client_list).toString().split("\n");
  clients_arr = clients_arr.filter(function(value){
    return value != '';
  });
  async.map(clients_arr, spike_client, function(err, results){
    if (err) {
      console.log(err);
      process.exit(1);
    }
    cb(clients_arr, results);
  });

}

function spike_client(hostname, cb) {
  if ( hostname === '')
      return ;

  request.post(hostname + ':' + port)
    .send({'cmd': 'spike'})
    .end(function(error, res){
      if (error) {
	console.error('Spike client host ' + hostname +' error!' + error);
	return ;
      }
      
      var body = "";  // Extract path from client respond
      res.on('data', function (chunk) {
	body += chunk;
      });
      res.on('end', function () {
	cb(null, body);
      });
    });
}

@@ -1205,32 +1205,35 @@
  from_addr;%0A  %7D)
+%5B0%5D
 ;%0A  from_host.st
@@ -1331,24 +1331,27 @@
 o_addr;%0A  %7D)
+%5B0%5D
 ;%0A%0A  var to_
@@ -1753,24 +1753,27 @@
 m_addr;%0A  %7D)
+%5B0%5D
 ;%0A  from_hos
@@ -1875,24 +1875,27 @@
 o_addr;%0A  %7D)
+%5B0%5D
 ;%0A%0A  to_host

Fix error