Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow socket client to select which events blode broadcasts to it

This should speed up the parsing on the client side.

Also:
- rinfo.address is not available in http.createServer()
- added test socket client that limits the broadcast level to a
randomly selected setting
  • Loading branch information...
commit 33088d99722abf930f37204f1aa6ec69194efe36 1 parent 3f9addc
@toddtreece toddtreece authored
Showing with 76 additions and 2 deletions.
  1. +34 −2 blode.js
  2. +42 −0 test/tcp_broadcast_level.js
View
36 blode.js
@@ -52,7 +52,6 @@ http.createServer(function(request, response) {
var log = request.extract_message();
log_buffer.id = Math.uuid();
- log_buffer.source = rinfo.address;
log_buffer.severity = log.severity;
log_buffer.message = log.message;
@@ -90,8 +89,22 @@ sys.puts("Event UDP capture daemon started at http://" + HOST + ":" + config.dgr
// TCP socket event broadcast
function Client(stream) {
this.stream = stream;
+ this.broadcast_events = [
+ 'none',
+ 'debug',
+ 'info',
+ 'notice',
+ 'warning',
+ 'err',
+ 'crit',
+ 'alert',
+ 'emerge'
+ ];
}
+
var socket_clients = [];
+var socket_buffer = '';
+
var server = net.createServer(function(stream) {
stream.setEncoding('utf8');
stream.on("connect", function() {
@@ -102,6 +115,24 @@ var server = net.createServer(function(stream) {
socket_clients.remove(client);
client.stream.end();
});
+
+ stream.on('data', function(data) {
+ socket_buffer += data;
+ var message = socket_buffer.indexOf("\r");
+ if (message !== -1) {
+ var json = socket_buffer.slice(0, message);
+ try {
+
+ var broadcast_events = JSON.parse(json);
+
+ if(broadcast_events instanceof Array)
+ client.broadcast_events = broadcast_events;
+
+ } catch(e) { }
+
+ socket_buffer = socket_buffer.slice(message + 1);
+ }
+ });
stream.on('error', function() {
socket_clients.remove(client);
@@ -142,7 +173,8 @@ sys.puts("Event web socket broadcast daemon started at " + HOST + ":" + config.w
emitter.on("log", function(severity, message) {
socket_clients.forEach(function(client) {
try {
- client.stream.write(JSON.stringify(log_buffer) + "\r\n");
+ if(client.broadcast_events.indexOf(severity) !== -1)
+ client.stream.write(JSON.stringify(log_buffer) + "\r\n");
} catch(e) {
socket_clients.remove(client);
}
View
42 test/tcp_broadcast_level.js
@@ -0,0 +1,42 @@
+/**
+ * This is an example socket client
+ * that limits the broadcast
+ * events to a randomly selected
+ * setting.
+ */
+var net = require('net');
+var socket = new net.Socket();
+var socket_buffer = '';
+var config = require('../config').config;
+var valid_events = [
+ 'debug',
+ 'info',
+ 'notice',
+ 'warning',
+ 'err',
+ 'crit',
+ 'alert',
+ 'emerge',
+ 'none'
+];
+
+socket.connect(config.broadcast_socket_port, 'localhost', function() {
+
+ socket.setEncoding('utf8');
+
+ var random = Math.floor(Math.random() * valid_events.length + 1) - 1;
+ var test_event = new Array(valid_events[random]);
+ console.log('Test Level: ' + test_event);
+ socket.write(JSON.stringify(test_event) + "\r\n");
+
+ socket.on('data', function(data) {
+ socket_buffer += data;
+ var message = socket_buffer.indexOf("\r");
+ if (message !== -1) {
+ var json = socket_buffer.slice(0, message);
+ console.log(json);
+ socket_buffer = socket_buffer.slice(message + 1);
+ }
+ });
+
+});
Please sign in to comment.
Something went wrong with that request. Please try again.