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