Commit c7008c40 authored by Leo Iannacone's avatar Leo Iannacone

renamed send.js in client.js - moved socket handler completly to client - created Client class

parent 0e689cb9
...@@ -8,7 +8,7 @@ var express = require('express') ...@@ -8,7 +8,7 @@ var express = require('express')
, fs = require('fs') , fs = require('fs')
, path = require('path') , path = require('path')
, config = require('./lib/config.js') , config = require('./lib/config.js')
, send = require('./lib/send.js') , client = require('./lib/client.js')
, utils = require('./lib/utils.js') , utils = require('./lib/utils.js')
var app = module.exports = express.createServer(); var app = module.exports = express.createServer();
...@@ -40,37 +40,13 @@ app.get('/', routes.index); ...@@ -40,37 +40,13 @@ app.get('/', routes.index);
app.get(config.routes.distribution, routes.distribution) app.get(config.routes.distribution, routes.distribution)
io.sockets.on('connection', function(socket) { io.sockets.on('connection', function(socket) {
send.distributions(socket); client(socket)
// send distribution packages
socket.on('get_distribution_packages', function (data) {
if (! utils.check_data_distribution(data))
return
send.distribution_packages(socket, data);
})
socket.on('get_package_files_list', function(data) {
if (! utils.check_data_package(data))
return
send.package_files_list(socket, data)
})
socket.on('get_file', function (data){
if (! utils.check_data_file(data))
return
send.file(socket, data)
})
}); });
io.sockets.on('disconnect', function(socket){ io.sockets.on('disconnect', function(socket){
}); });
fs.watch(config.debomatic.path, { persistent: true }, function (event, fileName) {
send.distributions(io.sockets);
});
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);
}); });
...@@ -97,6 +97,12 @@ function __send_file (event_name, socket, data) { ...@@ -97,6 +97,12 @@ function __send_file (event_name, socket, data) {
}); });
} }
function __send_distributions(event_name, socket, data) {
__get_files_list(config.debomatic.path, true, function(distros){
socket.emit(event_name, distros);
});
}
function __watch_path_onsocket(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) {
...@@ -137,33 +143,57 @@ function __file_newcontent(event_name, socket, data) { ...@@ -137,33 +143,57 @@ function __file_newcontent(event_name, socket, data) {
socket.emit(event_name, data) socket.emit(event_name, data)
} }
sender = { function __handler_get_distributions (socket, data) {
event_name = 'distributions'
__watch_path_onsocket(event_name,socket, data, config.debomatic.path, __send_distributions)
__send_distributions(event_name, socket, data)
}
distributions: function(socket) { function __handler_get_package_files_list (socket, data) {
__get_files_list(config.debomatic.path, true, function(distros){ event_name = 'package_files_list'
socket.emit('distributions', distros); package_path = utils.get_package_path(data)
}); __watch_path_onsocket(event_name, socket, data, package_path, __send_package_files_list)
}, __send_package_files_list(event_name, socket, data)
}
package_files_list: function(socket, data) {
event_name = 'package_files_list' function __handler_get_distribution_packages (socket, data) {
package_path = utils.get_package_path(data) event_name = 'distribution_packages'
__watch_path_onsocket(event_name, socket, data, package_path, __send_package_files_list) distribution_path = path.join(config.debomatic.path, data.distribution.name, 'pool')
__send_package_files_list(event_name, socket, data) __watch_path_onsocket(event_name, socket, data, distribution_path, __send_distribution_packages)
}, __send_distribution_packages(event_name, socket, data)
}
distribution_packages: function(socket, data) {
event_name = 'distribution_packages' function __handler_get_file (socket, data) {
distribution_path = path.join(config.debomatic.path, data.distribution.name, 'pool') file_path = utils.get_file_path(data)
__watch_path_onsocket(event_name, socket, data, distribution_path, __send_distribution_packages) __watch_path_onsocket('file_newcontent', socket, data, file_path, __file_newcontent)
__send_distribution_packages(event_name, socket, data) __send_file('file', socket, data)
}, }
Client = function (socket) {
__handler_get_distributions(socket);
// send distribution packages
socket.on('get_distribution_packages', function (data) {
if (! utils.check_data_distribution(data))
return
__handler_get_distribution_packages(socket, data);
})
socket.on('get_package_files_list', function(data) {
if (! utils.check_data_package(data))
return
__handler_get_package_files_list(socket, data)
})
file: function(socket, data) { socket.on('get_file', function (data){
file_path = utils.get_file_path(data) if (! utils.check_data_file(data))
__watch_path_onsocket('file_newcontent', socket, data, file_path, __file_newcontent) return
__send_file('file', socket, data) __handler_get_file(socket, data)
}, })
return {
}
} }
module.exports = sender module.exports = Client
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