github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/containers/compilers/rump/nodejs/node-wrapper/node-wrapper-udp.js (about)

     1  process.chdir('/tmp');
     2  
     3  var StdOutFixture = require('./fixture-stdout.js');
     4  var stdOutFixture = new StdOutFixture();
     5  
     6  //set up log server
     7  var _log = [];
     8  stdOutFixture.capture( function onWrite (string, encoding, fd) {
     9    _log.push(string);
    10    return true;
    11  });
    12  var StdErrFixture = require('./fixture-stderr');
    13  var stdErrFixture = new StdErrFixture();
    14  stdErrFixture.capture( function onWrite (string, encoding, fd) {
    15    _log.push(string);
    16    return true;
    17  });
    18  
    19  const PORT=9967;
    20  var http = require('http');
    21  function serveLogs(request, response){
    22      response.end(_log.join(""));
    23  }
    24  var server = http.createServer(serveLogs);
    25  server.listen(PORT, function(){
    26      console.log("Log server started on: http://localhost:%s", PORT);
    27  });
    28  
    29  console.log('UDP Server starting');
    30  const HOST = '0.0.0.0';
    31  var dgram = require('dgram');
    32  var udpServer = dgram.createSocket('udp4');
    33  udpServer.on('listening', function () {
    34      var address = server.address();
    35      console.log('UDP Server listening on ' + address.address + ":" + address.port);
    36  });
    37  var listenerIp = "";
    38  udpServer.on('message', function (message, remote) {
    39      message = message.toString().replace(/\0/g, '');
    40      console.log("udp listener received: "+remote.address + ':' + remote.port +' - ' + message + " length: "+message.length);
    41      if (message.indexOf("unik") > -1) {
    42        listenerIp = message.split(":")[1]
    43        registerWithListener(listenerIp)
    44        udpServer.close();
    45      }
    46  });
    47  udpServer.bind(PORT, HOST);
    48  
    49  console.log("unik v0.0 boostrapping beginning udp broadcast...");
    50  function registerWithListener(listenerIp) {
    51    require('macaddress').one(function (err, mac) {
    52      console.log("Mac address for this host: %s", mac);
    53      var options = {
    54        hostname: listenerIp,
    55        port: 3000,
    56        path: '/register?mac_address='+mac,
    57        method: 'POST',
    58      };
    59      var req = http.request(options, function(res) {
    60        console.log('Status: ' + res.statusCode);
    61        console.log('Headers: ' + JSON.stringify(res.headers));
    62        res.setEncoding('utf8');
    63        res.on('data', function (body) {
    64          console.log('Response: ' + body);
    65          env = JSON.parse(body);
    66          Object.keys(env).forEach(function(key) {
    67            var val = env[key];
    68            process.env[key] = val;
    69            console.log("Set env var: "+key+"="+val)
    70          });
    71          console.log("unik v0.0 boostrapping finished!\ncalling main");
    72          //CALL_NODE_MAIN_HERE
    73        });
    74      });
    75      req.end();
    76    });
    77  }