Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var client_ids = [];
  2. var clients = {};
  3.  
  4. function update_client(client_id, client_data)
  5. {
  6.     clients[client_id] = client_data;
  7.     $('td[client_id=' + client_id + '][field=name]').text(client_data["name"]);
  8.     $('td[client_id=' + client_id + '][field=host_ip]').text(client_data["host_ip"]);
  9.     $('td[client_id=' + client_id + '][field=client_action]').text(client_data["client_action"]["name"]);
  10.     $('td[client_id=' + client_id + '][field=status]').text(client_data["status"]);
  11. }
  12.  
  13.  
  14. function add_client(client_id, client_data)
  15. {
  16.     client_ids.push(client_id);
  17.     clients[client_id] = client_data;
  18.     $('#clients_rows').append(
  19.         jQuery('<tr />', {
  20.             "client_id": client_id,
  21.             html: jQuery("<td />", {
  22.                 "client_id": client_id,
  23.                 "field": "name",
  24.                 text: client_data["name"]
  25.             })
  26.         }).append(
  27.             jQuery('<td />', {
  28.                 "client_id": client_id,
  29.                 "field": "host_ip",
  30.                 text: client_data["host_ip"]
  31.             })
  32.         ).append(
  33.             jQuery('<td />', {
  34.                 "client_id": client_id,
  35.                 "field": "client_action",
  36.                 text: client_data["client_action"]["name"]
  37.             })
  38.         ).append(
  39.             jQuery('<td />', {
  40.                 "client_id": client_id,
  41.                 "field": "status",
  42.                 text: client_data["status"]
  43.             })
  44.         )
  45.     );
  46. }
  47.  
  48.  
  49. function remove_client(client_id)
  50. {
  51.     var client_id_index = client_ids.indexOf(client_id);
  52.     client_ids.splice(client_id_index, 1);
  53.     delete clients[client_id];
  54.     $('tr[client_id=' + client_id + ']').remove();
  55. }
  56.  
  57.  
  58. function get_client_ids()
  59. {
  60.     return new Promise(function(resolve, reject) {
  61.         $.ajax({
  62.             type: "POST",
  63.             url: "/client/",
  64.             data: JSON.stringify({
  65.                 "action": "get_client_ids"
  66.             }),
  67.             dataType: "json",
  68.             success: function (response) {
  69.                 resolve(response);
  70.             }
  71.         });
  72.     });
  73. }
  74.  
  75.  
  76. function get_client_data(client_id)
  77. {
  78.     return new Promise(function(resolve, reject) {
  79.         $.ajax({
  80.             type: "POST",
  81.             url: "/client/",
  82.             data: JSON.stringify({
  83.                 "action": "get_client_data",
  84.                 "client_id": client_id
  85.             }),
  86.             dataType: "json",
  87.             success: function (client_data) {
  88.                 resolve(client_data);
  89.             }
  90.         });
  91.     });
  92. }
  93.  
  94.  
  95. function get_client_hash(client_id)
  96. {
  97.     return new Promise(function(resolve, reject) {
  98.         $.ajax({
  99.             type: "POST",
  100.             url: "/client/",
  101.             data: JSON.stringify({
  102.                 "action": "get_client_hash",
  103.                 "client_id": client_id
  104.             }),
  105.             dataType: "json",
  106.             cache: false,
  107.             success: function(hash_data){
  108.                 resolve(hash_data);
  109.             }
  110.         });
  111.     });
  112. }
  113.  
  114.  
  115. function client_id_exists(client_id)
  116. {
  117.     return new Promise(function(resolve, reject) {
  118.         $.ajax({
  119.             type: "POST",
  120.             url: "/client/",
  121.             data: JSON.stringify({
  122.                 "action": "client_id_exists",
  123.                 "client_id": client_id
  124.             }),
  125.             dataType: "json",
  126.             success: function(exists_data) {
  127.                 resolve(exists_data);
  128.             }
  129.         });
  130.     });
  131. }
  132.  
  133.  
  134. function sync()
  135. {
  136.     get_client_ids().then(function(remote_client_ids) {
  137.             $.each(remote_client_ids, function (client_ids_index, client_id) {
  138.                 var client_id_index = $.inArray(client_id, client_ids);
  139.                 if (client_id_index === -1) {
  140.                     get_client_data(client_id).then(function(client_data){
  141.                         add_client(client_id, client_data);
  142.                     });
  143.                 }
  144.                 else {
  145.                     get_client_hash(client_id).then(function(hash_data) {
  146.                         var local_hash = clients[client_id]["hash"];
  147.                         var remote_hash = hash_data["hash"];
  148.                         if (local_hash !== remote_hash) {
  149.                             get_client_data(client_id).then(function(client_data) {
  150.                                 update_client(client_id, client_data);
  151.                             });
  152.  
  153.                         }
  154.                     });
  155.  
  156.                 }
  157.             });
  158.         }
  159.     );
  160.     $.each(client_ids, function(client_id_index, client_id) {
  161.         client_id_exists(client_id).then(function(exists_data) {
  162.             if (!exists_data["exists"]) {
  163.                 remove_client(client_id);
  164.             }
  165.         });
  166.  
  167.     });
  168. }
  169.  
  170.  
  171. (function(){
  172.     sync();
  173.     setTimeout(arguments.callee, 10000);
  174. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement