artdaq_node_server  v1_00_07
 All Classes Namespaces Files Variables Pages
dispatcher-receiver_module.js
1 // dispatcher-receiver_module.js
2 // Author: Eric Flumerfelt, FNAL RSI
3 //
4 // This module receives UDP broadcast packets from the JSONDispatcher class in artdaq-demo
5 //
6 
7 var dgram = require('dgram');
8 var emitter = require('events').EventEmitter;
9 var arc = new emitter();
10 
11 function startJSONListener(workerData, partition)
12 {
13  var server = dgram.createSocket('udp4');
14 
15  console.log("Setting up JSON listener for partition " + partition);
16  server.on("error", function(err) {
17  console.log("artdaq-runcontrol listen server error:\n" + err.stack);
18  server.close();
19  });
20 
21  server.on("message", function(msg, rinfo) {;
22  var thisEvent = JSON.parse(msg);
23  //console.log("Dispatcher received event:" + thisEvent.event)
24  arc.emit("message", {name:"artdaq-runcontrol",target:"p"+partition+"onmon",data:true});
25  arc.emit("message", {name:"artdaq-runcontrol",target:"p"+partition+"evt",method:"push",data:thisEvent});
26  });
27 
28  server.on("listening", function () {
29  var address = server.address();
30  console.log("server listening " +
31  address.address + ":" + address.port);
32  });
33 
34  console.log("Binding Server to port " + (35555 + partition) );
35  server.bind(35555 + partition);
36  return server;
37 }
38 
39 arc.MasterInitFunction = function( workerData ) {
40  return null;
41 };
42 
43 arc.WorkerInitFunction = function( workerData ) {
44  startJSONListener(workerData, 0);
45  startJSONListener(workerData, 1);
46  startJSONListener(workerData, 2);
47  startJSONListener(workerData, 3);
48  return null;
49 }
50 
51 module.exports = function ( module_holder ) {
52  module_holder["dispatcher-receiver"] = arc;
53 };