Skip to content

Instantly share code, notes, and snippets.

@franquis
Created December 19, 2011 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franquis/1497953 to your computer and use it in GitHub Desktop.
Save franquis/1497953 to your computer and use it in GitHub Desktop.
Testing FS and Socket.IO
var i = 0;
var socket = io.connect('http://127.0.0.1:8000');
/*
* data = [{'log':'path/to/log1','result':'/path/to/result1'},{'log':'path/to/log2','result':'/path/to/result2'}];
*/
//Asking for files content
var send = function(){
//Asking for the path/to/files1 and then path/to/files2
if(i < (data.length)){
socket.emit('init',data[i]);
} else {
socket.emit('disconnect');
}
};
socket.on('connect', function(server) {
//On connect, ask for files
send();
});
socket.on('tail', function(message) {
//Do stuff when the LOG file changes.
//$('.output').append(message.data)...
});
socket.on('status',function(message){
if(message.status == true){
//Do stuff if the RESULT file contains TRUE
} else {
//Do stuff if the RESULT file contains FALSE
}
//Asking for the next couple of files...
i = message.id + 1;
send();
});
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
url = require('url'),
path = require('path'),
fs = require('fs');
app.listen(8000);
util.puts('> Server Running on port 8000');
function handler(req,res){
//...
}
io.sockets.on('connection', function (client) {
//On 'Init' event, read the Result and the Log File
client.on('init',function(host){
//Dont know how to make it clean
readResult(host, function(err, data){
if(err){
errorHandler(err);
} else {
client.emit('status',data);
//I need to stop both read***() functions...
};
});
readLog(host, function(err, data){
if(err){
errorHandler(err);
} else {
client.emit('tail',data);
}
});
});
client.on('disconnect', function () {
console.log("Disconnected!");
});
});
function errorHandler(error) {
console.log(error);
throw error;
}
function readResult(DATA,callback){
console.log(' >> readResult('+DATA.host+');');
var status;
var resultfile = path.join(path.normalize(DATA.path), DATA.type, DATA.host, DATA.node,"RESULT.txt");
var w = fs.watch(resultfile,function(filestatus, file){
console.log(' readResult(): listenning...');
if(filestatus == "change"){
console.log(' readResult(): File changed');
fs.readFile(resultfile, "utf8", function (err, body) {
if (err) {
if (error.errno === process.ENOENT) {
callback(null, "");
} else {
callback(err);
}
} else {
if (body.match(/Done.*?/)) {
status = true;
console.log(" #"+DATA.i+" Done");
} else if (body.match(/Error.*?/)) {
status = false;
console.log(" #"+DATA.i+" Error");
}
callback(null, {'id':DATA.i,'host':DATA.host,'status':status});
console.log(' << readResult('+DATA.host+');');
}
});
}
});
}
function readLog(DATA, callback){
console.log(' >> readLog('+DATA.nm+');');
var logfile = path.join(path.normalize(DATA.path), DATA.type, DATA.nm, DATA.node,"LOG.txt");
var curr_size = 0;
var w = fs.watch(logfile,function(status, file){
fs.stat(logfile,function(err,stats){
if (err) {
if (error.errno === process.ENOENT) {
callback(null, "");
console.log(' logFile() > File not found');
} else {
/
callback(err);
}
} else if(stats.size == 0){
//DoSomeThing.
} else if(stats.size < curr_size){
var start = stats.size - curr_size;
} else {
var start = curr_size;
}
stream = fs.createReadStream(logfile, { start: start, end: stats.size});
stream.addListener("data", function(lines) {
callback(null, {'id':DATA.i,'host': DATA.host, 'tail' : lines.toString('utf-8').split("\n")});
});
curr_size = stats.size;
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment