Commit 89690de7 authored by Leo Iannacone's avatar Leo Iannacone

implement tail send/receive on new file content

parent 28a72f8b
...@@ -10,6 +10,7 @@ var express = require('express') ...@@ -10,6 +10,7 @@ var express = require('express')
, fs = require('fs') , fs = require('fs')
, path = require('path') , path = require('path')
, utils = require('./utils.js') , utils = require('./utils.js')
, Tail = require('tail').Tail
var app = module.exports = express.createServer(); var app = module.exports = express.createServer();
...@@ -39,16 +40,28 @@ var io = require('socket.io').listen(app); ...@@ -39,16 +40,28 @@ var io = require('socket.io').listen(app);
// Routes // Routes
app.get('/', routes.index); app.get('/', routes.index);
function watch_dir(event_name, socket, data, watch_path, updater) { function watch_path_onsocket(event_name, socket, data, watch_path, updater) {
name = "watcher-" + event_name name = "watcher-" + event_name
socket.get(name, function (err, watcher) { socket.get(name, function (err, watcher) {
if (watcher) if (watcher)
watcher.close() watcher.close()
try { try {
fs.stat(watch_path, function(err, stats) {
watcher = null
if (stats.isDirectory()) {
watcher = fs.watch(watch_path, {persistent: true}, function (event, fileName) { watcher = fs.watch(watch_path, {persistent: true}, function (event, fileName) {
if(event == 'rename') if(event == 'rename')
updater(socket, data) updater(socket, data)
}) })
}
else {
watcher = new Tail(watch_path)
watcher.on('line', function(new_content) {
data.file.new_content = new_content + '\n'
updater(socket, data)
})
}
})
socket.set(name, watcher) socket.set(name, watcher)
} catch (err_watch) {} } catch (err_watch) {}
}) })
...@@ -62,7 +75,7 @@ io.sockets.on('connection', function(socket) { ...@@ -62,7 +75,7 @@ io.sockets.on('connection', function(socket) {
if (! utils.check_data_distribution(data)) if (! utils.check_data_distribution(data))
return return
distribution_path = path.join(config.debomatic_path, data.distribution.name, 'pool') distribution_path = path.join(config.debomatic_path, data.distribution.name, 'pool')
watch_dir('get_distribution_packages', socket, data, distribution_path, send.distribution_packages) watch_path_onsocket('get_distribution_packages', socket, data, distribution_path, send.distribution_packages)
send.distribution_packages(socket, data); send.distribution_packages(socket, data);
}) })
...@@ -70,7 +83,7 @@ io.sockets.on('connection', function(socket) { ...@@ -70,7 +83,7 @@ io.sockets.on('connection', function(socket) {
if (! utils.check_data_package(data)) if (! utils.check_data_package(data))
return return
package_path = utils.get_package_path(data) package_path = utils.get_package_path(data)
watch_dir('get_package_file_list', socket, data, package_path, send.package_file_list) watch_path_onsocket('get_package_file_list', socket, data, package_path, send.package_file_list)
send.package_file_list(socket, data) send.package_file_list(socket, data)
}) })
...@@ -78,6 +91,8 @@ io.sockets.on('connection', function(socket) { ...@@ -78,6 +91,8 @@ io.sockets.on('connection', function(socket) {
socket.on('get_file', function (data){ socket.on('get_file', function (data){
if (! utils.check_data_file(data)) if (! utils.check_data_file(data))
return return
file_path = utils.get_file_path(data)
watch_path_onsocket('get_file', socket, data, file_path, send.file_newcontent)
send.file(socket, data) send.file(socket, data)
}) })
}); });
......
...@@ -8,6 +8,6 @@ ...@@ -8,6 +8,6 @@
, "ejs": ">= 0.0.1" , "ejs": ">= 0.0.1"
, "socket.io": "*" , "socket.io": "*"
, "node-fs": "*" , "node-fs": "*"
, "node-tail": "*" , "tail": "*"
} }
} }
...@@ -111,6 +111,10 @@ debomatic_sender = { ...@@ -111,6 +111,10 @@ debomatic_sender = {
file: function(socket, data) { file: function(socket, data) {
__send_file(socket, data) __send_file(socket, data)
},
file_newcontent: function(socket, data) {
socket.emit('file_newcontent', data);
} }
} }
......
...@@ -21,7 +21,8 @@ ...@@ -21,7 +21,8 @@
socket.emit("get_package_file_list", data) socket.emit("get_package_file_list", data)
} }
if (info.length >= 4) { if (info.length >= 4) {
data.file = info[3] data.file = {}
data.file.name = info[3]
socket.emit("get_file", data) socket.emit("get_file", data)
} }
} }
...@@ -50,6 +51,15 @@ ...@@ -50,6 +51,15 @@
}) })
}) })
socket.on('file', function (data) {
$("#file").html(data.file.content)
})
socket.on('file_newcontent', function(data) {
new_html = $("#file").html() + data.file.new_content
$("#file").html(new_html)
})
socket.on('error', function() { console.error(arguments) }); socket.on('error', function() { console.error(arguments) });
$(window).on('hashchange', function() { $(window).on('hashchange', function() {
...@@ -66,6 +76,7 @@ ...@@ -66,6 +76,7 @@
<ul id="distributions"></ul> <ul id="distributions"></ul>
<ul id="packages"></ul> <ul id="packages"></ul>
<ul id="files"></ul> <ul id="files"></ul>
<pre id="file"></pre>
<%- body %> <%- body %>
</body> </body>
</html> </html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment