Permalink
Browse files

Merge pull request #10 from msmathers/socket.io_v0.8

Upgrades Log.io to socket.io v0.8.x

- Uses custom events (emit) instead of message_type properties
- Server uses rooms per log file for selective broadcasting

Harvester uses "official" socket.io-client instead of Socket.io-node-client

Unit test updates
  • Loading branch information...
2 parents d15d525 + f60f7e4 commit d2c5a9692fe4c564f556ba9fe1769f1bfd6d9ba6 @msmathers msmathers committed Sep 23, 2011
View
@@ -0,0 +1,17 @@
+# 0.2.0 / 2011-09-23
+
+- Updates all components to use socket.io v0.8.x
+ - Uses custom events (emit) instead of message_type properties
+ - Server uses rooms per log file for selective broadcasting
+- Harvester uses "official" socket.io-client instead of Socket.io-node-client
+- Unit test updates
+
+# 0.1.2 / 2011-07-01
+
+- socket.io v0.7 warnings, configuration updates
+- Fixes broken Socket.io-node-client dependency
+- Readme updates
+
+# 0.1.1 / 2011-05-20
+
+- Initial release
View
@@ -13,14 +13,14 @@ Users create *stream* and *history* screens to view and search log messages.
[node.js](http://nodejs.org)
-[socket.io v0.6.17](http://socket.io) (Log.io is currently incompatible with socket.io v0.7.x)
+[socket.io](http://socket.io)
+
+[socket.io-client](https://github.com/LearnBoost/socket.io-client)
[connect](http://senchalabs.github.com/connect/)
[underscore](http://documentcloud.github.com/underscore/)
-[Socket.io-node-client](https://github.com/msmathers/Socket.io-node-client)
-
## Compatibility
Harvesters & server have been tested on *Ubuntu 11.04*
View
@@ -4,23 +4,26 @@ try {
e("Checking for node.js package 'socket.io'...");
var io = require('socket.io');
var version = io.version;
- if (version.indexOf("0.7") >= 0) {
- e("ERROR: Log.io is currently incompatible with socket.io v0.7");
- e("Be sure to use v0.6.17");
- e("");
- e("sudo npm install -g socket.io@0.6.17");
- e("... or ...");
- e("https://github.com/LearnBoost/socket.io/tree/0.6.17");
+ if (version.indexOf("0.8") < 0) {
+ e("ERROR: Log.io is incompatible with pre-0.8.x versions of socket.io");
process.exit(1);
}
} catch (err) {
e("ERROR: Could not find socket.io package");
- e("sudo npm install -g socket.io@0.6.17");
+ e("sudo npm install -g socket.io");
e("... or ...");
- e("https://github.com/LearnBoost/socket.io/tree/0.6.17");
- e("");
- e("WARNING: Log.io is currently incompatible with socket.io v0.7");
- e("Be sure to use v0.6.17");
+ e("https://github.com/LearnBoost/socket.io/");
+ process.exit(1);
+}
+
+try {
+ e("Checking for node.js package 'socket.io-client'...");
+ var io = require('socket.io-client');
+} catch (err) {
+ e("ERROR: Could not find socket.io-client package");
+ e("sudo npm install -g socket.io-client");
+ e("... or ...");
+ e("https://github.com/LearnBoost/socket.io-client/");
process.exit(1);
}
@@ -46,26 +49,4 @@ try {
process.exit(1);
}
-try {
- e("Checking for node.js package 'Socket.io-node-client (msmathers)'...");
- var client_io = require('Socket.io-node-client/io-client.js').io;
-} catch (err) {
- e("ERROR: Could not find Socket.io-node-client (msmathers) package");
- e("https://github.com/msmathers/Socket.io-node-client");
- e("");
- e("NOTE: This one is tricky since there's no npm package, as well as");
- e("a git tag checkout to downgrade its socket.io dependency.");
- e("This is a temporary workaround until socket.io-client v0.7 works");
- e("outside a browser context.");
- e("");
- e("# Install:");
- e("cd /usr/local/lib/node_modules/");
- e("sudo git clone git://github.com/msmathers/Socket.io-node-client.git");
- e("cd Socket.io-node-client");
- e("sudo git submodule update --init --recursive");
- e("cd socket.io-node");
- e("sudo git checkout 0.6.17");
- process.exit(1);
-}
-
e("Success! Now run the install script in ./install/*");
View
@@ -18,8 +18,7 @@ var History = function(web_client) {
History.prototype = {
get_history: function(log_file) {
- this.web_client.socket.send({
- type: 'history_request',
+ this.web_client.socket.emit('history_request', {
node: log_file.node.label,
log_file: log_file.label,
history_id: this._id
@@ -33,8 +33,7 @@ LogFile.prototype = {
this._enabled = true;
// Tell server to watch log file
- this.node.web_client.socket.send({
- type: 'enable_log',
+ this.node.web_client.socket.emit('enable_log', {
node: this.node.label,
log_file: this.label
});
@@ -51,8 +50,7 @@ LogFile.prototype = {
if (!this._enabled) {
// Tell server we don't care about log file anymore
- this.node.web_client.socket.send({
- type: 'disable_log',
+ this.node.web_client.socket.emit('disable_log', {
node: this.node.label,
log_file: this.label
});
@@ -31,55 +31,51 @@ var WebClient = function(io) {
var wc = this;
// Create socket
- this.socket = new io.Socket(null, {
- rememberTransport: false,
- transports: ['websocket', 'flashsocket']
- });
+ this.socket = io.connect();
// Register connect callback
this.socket.on('connect', function() {
wc.connected = true;
- wc.socket.send({type: 'announce', client_type: 'web_client'});
+ wc.socket.emit('announce_web_client');
+ });
+
+ // Add a new Node to pool
+ this.socket.on('add_node', function(message) {
+ wc.add_node(message.node, message.logs);
+ });
+
+ // Remove Node from pool
+ this.socket.on('remove_node', function(message) {
+ wc.remove_node(message.node);
+ });
+
+ // Render new log message to screen
+ this.socket.on('log', function(message) {
+ var log_file = wc.nodes[message.node].log_files[message.log_file];
+ log_file.log(message.msg);
+ });
+
+ // LogFile ping
+ this.socket.on('ping', function(message) {
+ var log_file = wc.nodes[message.node].log_files[message.log_file];
+ log_file.ping();
+ wc.stats.messages++;
+ });
+
+ // Render history response to screen
+ this.socket.on('history_response', function(message) {
+ var history = wc.histories[message.history_id];
+ history.add_lines(message.lines);
});
- // Register add/remove node, log, history_response callbacks from LogServer
- this.socket.on('message', function (message) {
-
- // Add a new Node to pool
- if (message.type == 'add_node') {
- wc.add_node(message.node, message.logs);
-
- // Remove Node from pool
- } else if (message.type == 'remove_node') {
- wc.remove_node(message.node);
-
- // Render new log message to screen
- } else if (message.type == 'log') {
- var log_file = wc.nodes[message.node].log_files[message.log_file];
- log_file.log(message.msg);
-
- // LogFile ping
- } else if (message.type == 'ping') {
- var log_file = wc.nodes[message.node].log_files[message.log_file];
- log_file.ping();
- wc.stats.messages++;
-
- // Render history response to screen
- } else if (message.type == 'history_response') {
- var history = wc.histories[message.history_id];
- history.add_lines(message.lines);
-
- // Update total message count stats
- } else if (message.type == 'stats') {
- if (!wc.stats.message_offset) {
- wc.stats.message_offset = message.message_count;
- }
- wc.stats.messages = message.message_count - wc.stats.message_offset;
+ // Update total message count stats
+ this.socket.on('stats', function(message) {
+ if (!wc.stats.message_offset) {
+ wc.stats.message_offset = message.message_count;
}
+ wc.stats.messages = message.message_count - wc.stats.message_offset;
});
- // Connect socket
- this.socket.connect();
}
WebClient.prototype = {
@@ -58,8 +58,7 @@ LogFile.prototype = {
// Sends log message to server
send_log: function(message) {
- this.harvester._send({
- type: this.harvester._conf.message_type,
+ this.harvester._send(this.harvester._conf.message_type, {
node: this.harvester._conf.node,
log_file: this.label,
msg: message
@@ -82,8 +81,7 @@ LogFile.prototype = {
} catch(err) {}
// Send log lines to LogServer
- this.harvester._send({
- type: 'history_response',
+ this.harvester._send('history_response', {
node: this.harvester._conf.node,
history_id: history_id,
client_id: client_id,
@@ -94,8 +92,7 @@ LogFile.prototype = {
// Sends ping to LogServer
ping: function() {
- this.harvester._send({
- type: 'ping',
+ this.harvester._send('ping', {
node: this.harvester._conf.node,
log_file: this.label
});
@@ -5,7 +5,7 @@
*/
var __ = require('underscore');
-var io = require('Socket.io-node-client/io-client.js').io;
+var io = require('socket.io-client');
var lf = require('./log_file.js');
var STATUS_INTERVAL = 60 * 1000; // 60 seconds
@@ -48,10 +48,13 @@ LogHarvester.prototype = {
// Create socket, bind callbacks, connect to server
connect: function() {
var harvester = this;
+ var uri = harvester._conf.server.host + ":" + harvester._conf.server.port;
+ harvester.socket = io.connect(uri);
- harvester.socket = new io.Socket(harvester._conf.server.host, {
- port: harvester._conf.server.port,
- transports: ['websocket']
+ // TODO(msmathers): Ensure this works once socket.io bug has been fixed...
+ // https://github.com/LearnBoost/socket.io/issues/473
+ harvester.socket.on('error', function(reason) {
+ harvester.reconnect();
});
// Register announcement callback
@@ -63,43 +66,49 @@ LogHarvester.prototype = {
harvester.last_heartbeat = new Date().getTime();
});
- // Register callbacks to messages from LogServer
- harvester.socket.on('message', function(message) {
+ // Server heartbeat
+ harvester.socket.on('heartbeat', function() {
+ LOGGER("Received server heartbeat");
+ harvester.last_heartbeat = new Date().getTime();
+ return;
+ });
- // Server heartbeat
- if (message.type == 'heartbeat') {
- LOGGER("Received server heartbeat");
- harvester.last_heartbeat = new Date().getTime();
- return;
- }
+ // Node with same label already exists on server, kill process
+ harvester.socket.on('node_already_exists', function() {
+ LOGGER("ERROR: A node of the same name is already registered");
+ LOGGER("with the log server. Change this harvester's instance_name.");
+ LOGGER("Exiting.");
+ process.exit(1);
+ });
+ function _log_file_helper(message, func) {
var log_file = harvester.log_files[message.log_file];
+ if (log_file == null) { return; }
+ return func(log_file);
+ }
- // Begin sending log messages to server
- if (message.type == 'enable_log') {
+ // Begin sending log messages to server
+ harvester.socket.on('enable_log', function(message) {
+ _log_file_helper(message, function(log_file) {
LOGGER("Enabling log file: " + log_file.label);
log_file.enable();
+ });
+ });
- // Stop sending log messages to server
- } else if (message.type == 'disable_log') {
+ // Stop sending log messages to server
+ harvester.socket.on('disable_log', function(message) {
+ _log_file_helper(message, function(log_file) {
LOGGER("Disabling log file: " + log_file.label);
log_file.disable();
+ });
+ });
- // Respond to history request from WebClient
- } else if (message.type == 'history_request') {
+ // Respond to history request from WebClient
+ harvester.socket.on('history_request', function(message) {
+ _log_file_helper(message, function(log_file) {
log_file.send_history(message.client_id, message.history_id);
-
- // Node with same label already exists on server, kill process
- } else if (message.type == 'node_already_exists') {
- LOGGER("ERROR: A node of the same name is already registered");
- LOGGER("with the log server. Change this harvester's instance_name.");
- LOGGER("Exiting.");
- process.exit(1);
- }
+ });
});
-
- // Connect to LogServer
- harvester.socket.connect();
},
// Run log harvester
@@ -133,8 +142,7 @@ LogHarvester.prototype = {
// Sends announcement to LogServer
announce: function() {
- this._send({
- type:'announce',
+ this._send('announce_node', {
client_type:'node',
logs: __(this.log_files).keys(),
label: this._conf.node
@@ -159,9 +167,9 @@ LogHarvester.prototype = {
},
// Sends message to LogServer, gracefully handles connection failure
- _send: function(message) {
+ _send: function(event, message) {
try {
- this.socket.send(message);
+ this.socket.emit(event, message);
// If server is down, a non-writeable stream error is thrown.
} catch(err) {
LOGGER("ERROR: Unable to send message over socket.");
Oops, something went wrong.

0 comments on commit d2c5a96

Please sign in to comment.