/*
 * st4ck 2015 Monitor
 * it seem to work (maybe)
 *
 * I do all of this like a fool... 
 * i have to look fo an object oriented version
 * 
 * comment : idClient is incremented and older disconnected idClient never can be resused
 * have to get better approch (ID is realy needed ?)
 *
 */

var host = "localhost", port = 33333;
var dgram = require( "dgram" );
var server = dgram.createSocket( "udp4" );

var idClient = 0;
var listCLient = [];
var timeInterval = 5000; // check every 5 seconds 
var timeOut = 10000; // timeout is 10 seconds max

// client struct
function Client(ID, ip, port, msg, date) {
  this.ID = ID;
  this.ip = ip;
  this.port = port;
  this.msg = msg;
  this.date = date;
}

/*
 *
 * Check if clientList contains client
 *
 */
Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i].ip === obj.ip) {

            var now = new Date();
            this[i].date = now;
            return true;
        }
    }
    return false;
}

/*
 *
 * Executed each time server receive udp datagram
 *
 */
server.on( "message", function( msg, rinfo ) {

    var p1 = new Client(idClient++, rinfo.address, rinfo.port, msg);
    console.log('new msg received');

    if ( !listCLient.contains(p1) ) {
      listCLient.push(p1);
      console.log('client est '  + p1.ID + ' : ' + p1.ip + ' : ' + p1.port + ' msg: ' + p1.msg, new Date());
    }
    else {
      delete p1;
    }    

});

server.bind( port, host );

/* mutlicast
server.setBroadcast(true)
server.setMulticastTTL(128);
server.addMembership('230.185.192.108'); 
*/

/*
 *
 * Check each timeInterval for all client in clientList 
 * and control timeOut expiration
 *
 */
setInterval( function() {

  var now = new Date();

   console.log('array lenght : ' + listCLient.length);

  for (var i = 0, len = listCLient.length; i < len; i++) {
    if ( now - listCLient[i].date > timeOut) {
      listCLient.pop(listCLient[i]);
      console.log('client removed');
    }
  }

}, timeInterval);
@@ -405,26 +405,192 @@
 );%0A%0A
-var idClient = 0
+/* dockerode API */%0Avar Docker = require('dockerode');%0Avar docker = new Docker(); // to check don't know if correct%0Avar container = docker.getContainer('name_of_loadBalancer_or_ID')
 ;%0A
+%0A
 var 
@@ -772,24 +772,8 @@
 ) %7B%0A
-  this.ID = ID;%0A
   th
@@ -841,24 +841,535 @@
  = date;%0A%7D%0A%0A
+%0A/*%0A *%0A * rewrite conf file + do reboot of loadbalancer%0A *%0A */%0Afunction UpdateConfig(listOfClient) %7B%0A    /* write conf file  GO =%3E HandleBars */%0A  /*Handlebars.registerHelper('list', function(listOfClient) %7B%0A    var out = %22%22;%0A%0A    for(var i=0, l=listOfClient.length; i%3Cl; i++) %7B%0A      out = out + listOfClient%5Bi%5D.ip + %22:%22 + listOfClient%5Bi%5D.port + %22%5Cn%22;%0A    %7D%0A%0A    return out;%0A  %7D);*/%0A%0A  /*do reboot loadbalancer */%0A  /*container.start(function (err, data) %7B // maybe restart ?%0A    console.log(data);%0A  %7D);*/%0A%7D%0A%0A
 /*%0A *%0A * Che
@@ -1799,19 +1799,8 @@
 ent(
-idClient++,
  rin
@@ -1867,16 +1867,42 @@
 ved');%0A%0A
+%0A    /*new client come */%0A
     if (
@@ -1958,16 +1958,50 @@
 sh(p1);%0A
+%0A      UpdateConfig(listCLient);%0A%0A
       co
@@ -2025,23 +2025,8 @@
  est
- '  + p1.ID + '
  : '
@@ -2592,24 +2592,24 @@
  timeOut) %7B%0A
-
       listCL
@@ -2633,16 +2633,48 @@
 nt%5Bi%5D);%0A
+      UpdateConfig(listCLient);%0A
       co

add heartBeatMonitor.js script