Commit 47202de0 authored by Leo Iannacone's avatar Leo Iannacone

added Broadcaster class and moved relative bradcast functions there

parent b74ea8c0
...@@ -5,17 +5,15 @@ ...@@ -5,17 +5,15 @@
var express = require('express') var express = require('express')
, routes = require('./routes') , routes = require('./routes')
, fs = require('fs')
, path = require('path')
, Tail = require('tail').Tail
, config = require('./lib/config.js') , config = require('./lib/config.js')
, utils = require('./lib/utils.js') , utils = require('./lib/utils.js')
, Client = require('./lib/client.js') , Client = require('./lib/client.js')
, Broadcaster = require('./lib/broadcaster.js')
var app = module.exports = express.createServer(); var app = module.exports = express.createServer();
var io = require('socket.io').listen(app); var io = require('socket.io').listen(app);
// status // statuses
var status = {} var status = {}
status.packages = [] status.packages = []
...@@ -43,49 +41,17 @@ app.configure('production', function(){ ...@@ -43,49 +41,17 @@ app.configure('production', function(){
app.get('/', routes.index); app.get('/', routes.index);
app.get(config.routes.distribution, routes.distribution) app.get(config.routes.distribution, routes.distribution)
var broadcast = new Broadcaster(io.sockets, status)
io.sockets.on('connection', function(socket) { io.sockets.on('connection', function(socket) {
utils.send_distributions(socket)
socket.emit('status', status)
client = new Client(socket) client = new Client(socket)
client.send_status(status)
}); });
io.sockets.on('disconnect', function(socket){ io.sockets.on('disconnect', function(socket){
}); });
// watcher on new distributions
fs.watch(config.debomatic.path, { persistent: true }, function (event, fileName) {
utils.send_distributions(io.sockets);
});
// watcher on build_status
status_watcher = new Tail(config.debomatic.jsonfile)
status_watcher.on('line', function(new_content) {
data = null
try {
data = JSON.parse(new_content)
} catch (error) { return }
if (data.status == 'building') {
status.packages.push(data)
}
else if (data.status == 'build-successed'
|| data.status == 'build-failed' )
{
for(i = 0; i < status.packages.length; i++)
{
p = status.packages[i]
if ( p.package == data.package
&& p.distribution == data.distribution )
{
status.packages.splice(i, 1)
break
}
}
}
io.sockets.emit(config.events.broadcast.status_update, data)
})
var server = app.listen(config.port, function(){ var server = app.listen(config.port, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
}); });
var config = require('./config.js')
, fs = require('fs')
, Tail = require('tail').Tail
// watcher on build_status
function __watch_build_status (socket, status) {
status_watcher = new Tail(config.debomatic.jsonfile)
status_watcher.on('line', function(new_content) {
var data = null
try {
data = JSON.parse(new_content)
} catch (error) { return }
if (data.status == 'building') {
status.packages.push(data)
}
else if (data.status == 'build-successed'
|| data.status == 'build-failed' )
{
for(i = 0; i < status.packages.length; i++)
{
p = status.packages[i]
if ( p.package == data.package
&& p.distribution == data.distribution )
{
status.packages.splice(i, 1)
break
}
}
}
socket.emit(config.events.broadcast.status_update, data)
})
}
// watcher on new distributions
function __watch_distributions (socket) {
fs.watch(config.debomatic.path, { persistent: true }, function (event, fileName) {
utils.get_files_list(config.debomatic.path, true, function(distros) {
socket.emit(config.events.broadcast.distributions, distros);
})
})
}
function Broadcaster (sockets, status) {
var sockets = sockets
__watch_build_status(sockets, status)
__watch_distributions(sockets)
return {
}
}
module.exports = Broadcaster
...@@ -98,12 +98,6 @@ function __generic_handler_watcher(event_name, socket, data, watch_path, callbac ...@@ -98,12 +98,6 @@ function __generic_handler_watcher(event_name, socket, data, watch_path, callbac
callback(event_name, socket, data) callback(event_name, socket, data)
} }
function __send_distributions(event_name, socket) {
__get_files_list(config.debomatic.path, true, function(distros){
socket.emit(event_name, distros);
});
}
utils = { utils = {
check_data_distribution: function(data) { check_data_distribution: function(data) {
return __check_data_distribution(data) return __check_data_distribution(data)
...@@ -132,11 +126,6 @@ utils = { ...@@ -132,11 +126,6 @@ utils = {
generic_handler_watcher: function(event_name, socket, data, watch_path, callback) { generic_handler_watcher: function(event_name, socket, data, watch_path, callback) {
return __generic_handler_watcher(event_name, socket, data, watch_path, callback); return __generic_handler_watcher(event_name, socket, data, watch_path, callback);
}, },
send_distributions: function (socket, event_name) {
if (! event_name)
event_name = config.events.broadcast.distributions
return __send_distributions(event_name, socket);
},
} }
module.exports = utils module.exports = utils
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