Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var client_ids = [];
- var clients = {};
- function update_client(client_id, client_data)
- {
- clients[client_id] = client_data;
- $('td[client_id=' + client_id + '][field=name]').text(client_data["name"]);
- $('td[client_id=' + client_id + '][field=host_ip]').text(client_data["host_ip"]);
- $('td[client_id=' + client_id + '][field=client_action]').text(client_data["client_action"]["name"]);
- $('td[client_id=' + client_id + '][field=status]').text(client_data["status"]);
- }
- function add_client(client_id, client_data)
- {
- client_ids.push(client_id);
- clients[client_id] = client_data;
- $('#clients_rows').append(
- jQuery('<tr />', {
- "client_id": client_id,
- html: jQuery("<td />", {
- "client_id": client_id,
- "field": "name",
- text: client_data["name"]
- })
- }).append(
- jQuery('<td />', {
- "client_id": client_id,
- "field": "host_ip",
- text: client_data["host_ip"]
- })
- ).append(
- jQuery('<td />', {
- "client_id": client_id,
- "field": "client_action",
- text: client_data["client_action"]["name"]
- })
- ).append(
- jQuery('<td />', {
- "client_id": client_id,
- "field": "status",
- text: client_data["status"]
- })
- )
- );
- }
- function remove_client(client_id)
- {
- var client_id_index = client_ids.indexOf(client_id);
- client_ids.splice(client_id_index, 1);
- delete clients[client_id];
- $('tr[client_id=' + client_id + ']').remove();
- }
- function get_client_ids()
- {
- return new Promise(function(resolve, reject) {
- $.ajax({
- type: "POST",
- url: "/client/",
- data: JSON.stringify({
- "action": "get_client_ids"
- }),
- dataType: "json",
- success: function (response) {
- resolve(response);
- }
- });
- });
- }
- function get_client_data(client_id)
- {
- return new Promise(function(resolve, reject) {
- $.ajax({
- type: "POST",
- url: "/client/",
- data: JSON.stringify({
- "action": "get_client_data",
- "client_id": client_id
- }),
- dataType: "json",
- success: function (client_data) {
- resolve(client_data);
- }
- });
- });
- }
- function get_client_hash(client_id)
- {
- return new Promise(function(resolve, reject) {
- $.ajax({
- type: "POST",
- url: "/client/",
- data: JSON.stringify({
- "action": "get_client_hash",
- "client_id": client_id
- }),
- dataType: "json",
- cache: false,
- success: function(hash_data){
- resolve(hash_data);
- }
- });
- });
- }
- function client_id_exists(client_id)
- {
- return new Promise(function(resolve, reject) {
- $.ajax({
- type: "POST",
- url: "/client/",
- data: JSON.stringify({
- "action": "client_id_exists",
- "client_id": client_id
- }),
- dataType: "json",
- success: function(exists_data) {
- resolve(exists_data);
- }
- });
- });
- }
- function sync()
- {
- get_client_ids().then(function(remote_client_ids) {
- $.each(remote_client_ids, function (client_ids_index, client_id) {
- var client_id_index = $.inArray(client_id, client_ids);
- if (client_id_index === -1) {
- get_client_data(client_id).then(function(client_data){
- add_client(client_id, client_data);
- });
- }
- else {
- get_client_hash(client_id).then(function(hash_data) {
- var local_hash = clients[client_id]["hash"];
- var remote_hash = hash_data["hash"];
- if (local_hash !== remote_hash) {
- get_client_data(client_id).then(function(client_data) {
- update_client(client_id, client_data);
- });
- }
- });
- }
- });
- }
- );
- $.each(client_ids, function(client_id_index, client_id) {
- client_id_exists(client_id).then(function(exists_data) {
- if (!exists_data["exists"]) {
- remove_client(client_id);
- }
- });
- });
- }
- (function(){
- sync();
- setTimeout(arguments.callee, 10000);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement