/**
 * Created by tungtouch on 2/5/15.
 */

/*
* Listen UDP from Device: 128.199.126.250
* */

var dgram = require("dgram");
var colors = require('colors');
var async = require('async');
var redis = require("redis");
var rClient = redis.createClient();
var client;
var d = new Date();


//var ports = [4000, 4001, 4002, 3333, 4003, 4004, 4005, 4006, 4007, 4008, 4009, 4010, 3001, 3002, 3003, 3004, 3005, 3006, 3007, 3008, 3009, 3010, 3011];
var ports = [4444];
var r = 0;

rClient.on("error", function (err) {
    console.log("Error " + err);
});


var currentTime = d.getTime();
console.log("Current Time:", currentTime);

for(var i=0; i < ports.length; i++){

    client = dgram.createSocket('udp4', function(data){
        var ttl = d.getTime();

        console.log("[1. Data Raw]: ".green, data);
        console.log("[2. JSON Data]: ".yellow, JSON.stringify(data));
        //console.log("[3. Decoder:]".blue, typeof data, data.toString('utf8'));
        console.log("-------------------------------------------------");
        console.log("Total request: ".blue, r++, ttl - currentTime);

        rClient.hset("raw", "data"+r, data, redis.print);

    });

    console.log("Start listen port ",i,":",ports[i]);

    client.on("error", function (err) {
        console.log("Server error:\n".red + err.stack);
        client.close();
    });
    //
    client.on("message", function (msg, rinfo) {
        console.log("Server got: ".yellow + " IP: " +
        rinfo.address + " - Port:" + rinfo.port);

    });

    //client.on("listening", function () {
    //    var address = client.address();
    //    console.log("Server listening ".blue +
    //    address.address + ":" + address.port);
    //});

    client.bind(ports[i]);
}



@@ -250,20 +250,8 @@
 ();%0A
-var client;%0A
 var 
@@ -267,17 +267,16 @@
 ate();%0A%0A
-%0A
 //var po
@@ -453,16 +453,58 @@
 r = 0;%0A%0A
+var client = dgram.createSocket('udp4');%0A%0A
 rClient.
@@ -575,159 +575,259 @@
 ;%0A%0A%0A
-var currentTime = d.getTime();%0Aconsole.log(%22Current Time:%22, currentTime);%0A%0Afor(var i=0; i %3C ports.length; i++)%7B%0A%0A    client = dgram.createSocket('udp4'
+for(var i=0; i %3C ports.length; i++)%7B%0A%0A    console.log(%22Start listen port %22,i,%22:%22,ports%5Bi%5D);%0A%0A    client.on(%22error%22, function (err) %7B%0A        console.log(%22Server error:%5Cn%22.red + err.stack);%0A        client.close();%0A    %7D);%0A    //%0A    client.on(%22message%22
 , fu
@@ -836,46 +836,24 @@
 tion
+ 
 (data
-)%7B%0A        var ttl = d.getTime();
+, rinfo) %7B
 %0A%0A  
@@ -1178,27 +1178,8 @@
  r++
-, ttl - currentTime
 );%0A%0A
@@ -1186,16 +1186,18 @@
         
+//
 rClient.
@@ -1243,256 +1243,8 @@
 );%0A%0A
-    %7D);%0A%0A    console.log(%22Start listen port %22,i,%22:%22,ports%5Bi%5D);%0A%0A    client.on(%22error%22, function (err) %7B%0A        console.log(%22Server error:%5Cn%22.red + err.stack);%0A        client.close();%0A    %7D);%0A    //%0A    client.on(%22message%22, function (msg, rinfo) %7B%0A
     
@@ -1339,25 +1339,24 @@
 info.port);%0A
-%0A
     %7D);%0A%0A   

Update UDP