Commit fadf0818 authored by Leo Iannacone's avatar Leo Iannacone

refactory on client

parent 159e0294
config = require("./config")
fs = require("fs")
utils = require("./utils")
Tail = require("./tail")
__watch_status_check_same_obj = (obj1, obj2) -> __watch_status_check_same_obj = (obj1, obj2) ->
if obj1.status is obj2.status if obj1.status is obj2.status
if obj1.distribution is obj2.distribution if obj1.distribution is obj2.distribution
...@@ -84,9 +89,4 @@ Broadcaster = (sockets, status) -> ...@@ -84,9 +89,4 @@ Broadcaster = (sockets, status) ->
__watch_distributions(sockets) __watch_distributions(sockets)
__watch_pidfile(sockets) __watch_pidfile(sockets)
"use strict"
config = require("./config")
fs = require("fs")
utils = require("./utils")
Tail = require("./tail")
module.exports = Broadcaster module.exports = Broadcaster
__get_files_list_from_package = (data, callback) -> fs = require("fs")
path = require("path")
config = require("./config")
utils = require("./utils")
e = config.events.client
get_files_list_from_package = (data, callback) ->
package_path = utils.get_package_path(data) package_path = utils.get_package_path(data)
utils.get_files_list package_path, false, (files) -> utils.get_files_list package_path, false, (files) ->
data.package.files = [] data.package.files = []
...@@ -28,165 +34,110 @@ __get_files_list_from_package = (data, callback) -> ...@@ -28,165 +34,110 @@ __get_files_list_from_package = (data, callback) ->
callback(data) callback(data)
__send_package_files_list = (event_name, socket, data) -> read_package_status = (data, cb) ->
__get_files_list_from_package data, (new_data) ->
socket.emit event_name, new_data
return
return
__read_package_status = (data, cb) ->
package_path = utils.get_package_path(data) package_path = utils.get_package_path(data)
package_json = path.join(package_path, data.package.orig_name + ".json") package_json = path.join(package_path, data.package.orig_name + ".json")
fs.readFile package_json, {encoding: "utf8"}, (err, content) -> fs.readFile package_json, {encoding: "utf8"}, (err, content) ->
if err if err
utils.errors_handler "Client:__read_package_status:", err utils.errors_handler "Client:read_package_status:", err
return return
try try
content = JSON.parse(content) content = JSON.parse(content)
catch parse_err catch parseerr
utils.errors_handler("Client:" + utils.errors_handler("Client:" +
"__read_package_status:parse_err:", "read_package_status:parseerr:",
parse_err) parseerr)
return return
cb content cb content
return
return
__send_package_info = (socket, data) ->
__read_package_status data, (content) ->
socket.emit _e.package_info, content
return
return
__send_package_status = (socket, data) ->
__read_package_status data, (content) ->
socket.emit _e.distribution_packages_status, content
return
return
__send_distribution_packages = (event_name, socket, data) ->
distro_path = utils.get_distribution_pool_path(data)
utils.get_files_list distro_path, true, (packages) ->
data.distribution.packages = []
packages.forEach (p) ->
pack = {}
info = p.split("_")
pack.name = info[0]
pack.version = info[1]
pack.orig_name = p
__send_package_status socket,
distribution: data.distribution
package: pack
data.distribution.packages.push pack
return
socket.emit event_name, data class Client
return
constructor: (@socket) ->
send_package_files_list: (data) ->
get_files_list_from_package data, (new_data) =>
@socket.emit e.package_files_list, new_data
send_distribution_packages: (data) ->
distro_path = utils.get_distribution_pool_path(data)
utils.get_files_list distro_path, true, (packages) =>
data.distribution.packages = []
for p in packages
pack = {}
info = p.split("_")
pack.name = info[0]
pack.version = info[1]
pack.orig_name = p
read_package_status {distribution: data.distribution, package: pack}, (content) =>
@socket.emit e.distribution_packages_status, content
data.distribution.packages.push pack
@socket.emit e.distribution_packages, data
send_file: (data) ->
file_path = utils.get_file_path(data)
get_preview = data.file.name in config.web.file.preview and not data.file.force
fs.readFile file_path, "utf8", (err, content) =>
if err
utils.errors_handler("client:send_file", err, @socket)
return
data.file.orig_name = file_path.split("/").pop()
if get_preview and config.web.file.num_lines > 0
last_lines = config.web.file.num_lines
data.file.content = content.split("\n")[-last_lines..].join("\n")
else
data.file.content = content
data.file.path = file_path.replace(config.debomatic.path,
config.routes.debomatic)
@socket.emit e.file, data
return send_status: (status) ->
__send_file = (event_name, socket, data, last_lines) -> @socket.emit e.status, status
file_path = utils.get_file_path(data)
fs.readFile file_path, "utf8", (err, content) ->
if err
utils.errors_handler("client:__send_file", err, socket)
return
data.file.orig_name = file_path.split("/").pop()
if last_lines > 0
data.file.content = content.split("\n")[-last_lines..].join("\n")
else
data.file.content = content
data.file.path = file_path.replace(config.debomatic.path,
config.routes.debomatic)
socket.emit event_name, data
return
return
__handler_get_file = (socket, data) ->
file_path = utils.get_file_path(data)
send = (event_name, socket, data) ->
data.file.content = null
socket.emit(event_name, data)
utils.watch_path_onsocket(_e.file_newcontent, socket, data, file_path, send)
if data.file.name in config.web.file.preview and not data.file.force
__send_file(_e.file, socket, data, config.web.file.num_lines)
else
__send_file(_e.file, socket, data)
return
Client = (socket) ->
@start = ->
send_status_debomatic: ->
fs.exists config.debomatic.pidfile, (exists) =>
@socket.emit config.events.broadcast.status_debomatic,
running: exists
start: ->
# init send distributions # init send distributions
utils.send_distributions socket utils.send_distributions @socket
# init events # init events
socket.on _e.distribution_packages, (data) -> @socket.on e.distribution_packages, (data) =>
return unless utils.check_data_distribution(data) return unless utils.check_data_distribution(data)
distribution_path = utils.get_distribution_pool_path(data) distribution_path = utils.get_distribution_pool_path(data)
utils.generic_handler_watcher(_e.distribution_packages, @send_distribution_packages(data)
socket, utils.watch_path_onsocket e.distribution_packages, @socket, data, distribution_path, (new_data) =>
data, @send_distribution_packages(new_data)
distribution_path,
__send_distribution_packages)
data = null
return
socket.on _e.package_files_list, (data) -> @socket.on e.package_files_list, (data) =>
return unless utils.check_data_package(data) return unless utils.check_data_package(data)
package_path = utils.get_package_path(data) package_path = utils.get_package_path(data)
utils.generic_handler_watcher(_e.package_files_list, @send_package_files_list(data)
socket, utils.watch_path_onsocket e.package_files_list, @socket, data, package_path, (new_data) =>
data, @send_package_files_list(new_data)
package_path,
__send_package_files_list)
data = null
return
socket.on _e.file, (data) -> @socket.on e.file, (data) =>
return unless utils.check_data_file(data) return unless utils.check_data_file(data)
__handler_get_file socket, data file_path = utils.get_file_path(data)
data = null data.file.content = null
return @send_file(data)
utils.watch_path_onsocket e.file_newcontent, @socket, data, file_path, (new_data) =>
@socket.emit(e.file_newcontent, new_data)
socket.on _e.package_info, (data) -> @socket.on e.package_info, (data) =>
return unless utils.check_data_package(data) return unless utils.check_data_package(data)
__send_package_info socket, data read_package_status data, (content) =>
data = null @socket.emit e.package_info, content
return
# on client disconnection close all watchers # on client disconnection close all watchers
socket.on "disconnect", -> @socket.on "disconnect", =>
socket_watchers = socket.watchers socket_watchers = @socket.watchers
return unless socket_watchers return unless socket_watchers
for key of socket_watchers for key of socket_watchers
try try
socket_watchers[key].close() socket_watchers[key].close()
catch error_watch catch error_watch
utils.errors_handler "client:disconnect", error_watch utils.errors_handler "client:disconnect", error_watch
return
return
@send_status = (status) ->
socket.emit _e.status, status
return
@send_status_debomatic = ->
fs.exists config.debomatic.pidfile, (exists) ->
socket.emit config.events.broadcast.status_debomatic,
running: exists
return
return
return
"use strict"
fs = require("fs")
path = require("path")
config = require("./config")
utils = require("./utils")
_e = config.events.client
module.exports = Client module.exports = Client
...@@ -75,13 +75,13 @@ watch_path_onsocket = (event_name, socket, data, watch_path, callback) -> ...@@ -75,13 +75,13 @@ watch_path_onsocket = (event_name, socket, data, watch_path, callback) ->
persistent: true, persistent: true,
(event, fileName) -> (event, fileName) ->
if event is "rename" if event is "rename"
callback(event_name, socket, data)) callback(data))
else if stats.isFile() else if stats.isFile()
watcher = new Tail(watch_path) watcher = new Tail(watch_path)
watcher.on "line", (new_content, tailInfo) -> watcher.on "line", (new_content, tailInfo) ->
data.file.new_content = new_content + "\n" data.file.new_content = new_content + "\n"
callback(event_name, socket, data) callback(data)
watcher.on "error", (msg) -> watcher.on "error", (msg) ->
socket.emit config.events.error, msg socket.emit config.events.error, msg
...@@ -95,12 +95,6 @@ watch_path_onsocket = (event_name, socket, data, watch_path, callback) -> ...@@ -95,12 +95,6 @@ watch_path_onsocket = (event_name, socket, data, watch_path, callback) ->
err, socket) err, socket)
return return
generic_handler_watcher = (event_name, socket, data, watch_path, callback) ->
callback event_name, socket, data
watch_path_onsocket event_name, socket, data, watch_path, callback
send_distributions = (socket) -> send_distributions = (socket) ->
get_files_list config.debomatic.path, true, (directories) -> get_files_list config.debomatic.path, true, (directories) ->
distributions = [] distributions = []
...@@ -128,6 +122,5 @@ module.exports.get_package_path = get_package_path ...@@ -128,6 +122,5 @@ module.exports.get_package_path = get_package_path
module.exports.get_file_path = get_file_path module.exports.get_file_path = get_file_path
module.exports.get_files_list = get_files_list module.exports.get_files_list = get_files_list
module.exports.watch_path_onsocket = watch_path_onsocket module.exports.watch_path_onsocket = watch_path_onsocket
module.exports.generic_handler_watcher = generic_handler_watcher
module.exports.send_distributions = send_distributions module.exports.send_distributions = send_distributions
module.exports.errors_handler = errors_handler module.exports.errors_handler = errors_handler
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