1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-27 07:33:05 +00:00

add log on server

This commit is contained in:
nishiki 2014-01-25 11:35:12 +01:00
parent 66f07e907c
commit 961117338c
2 changed files with 45 additions and 4 deletions

View file

@ -4,11 +4,17 @@ require 'socket'
require 'json' require 'json'
require 'highline/import' require 'highline/import'
require 'digest' require 'digest'
require 'logger'
require "#{APP_ROOT}/lib/MPW.rb" require "#{APP_ROOT}/lib/MPW.rb"
class Server class Server
INFO = 0
WARNING = 1
ERROR = 2
DEBUG = 3
attr_accessor :error_msg attr_accessor :error_msg
# Constructor # Constructor
@ -18,9 +24,18 @@ class Server
# Start the server # Start the server
def start() def start()
begin
server = TCPServer.open(@host, @port) server = TCPServer.open(@host, @port)
@log.info("The server is started on #{@host}:#{@port}")
rescue Exception => e
@log.error("Impossible to start the server: #{e}")
exit 2
end
loop do loop do
Thread.start(server.accept) do |client| Thread.start(server.accept) do |client|
@log.info("#{client.peeraddr[3]} is connected")
while true do while true do
msg = getClientMessage(client) msg = getClientMessage(client)
@ -35,14 +50,19 @@ class Server
case msg['action'] case msg['action']
when 'get' when 'get'
@log.debug("#{client.peeraddr[3]} GET gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}")
client.puts getFile(msg) client.puts getFile(msg)
when 'update' when 'update'
@log.debug("#{client.peeraddr[3]} UPDATE gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}")
client.puts updateFile(msg) client.puts updateFile(msg)
when 'delete' when 'delete'
@log.debug("#{client.peeraddr[3]} DELETE gpg_key=#{msg['gpg_key']} suffix=#{msg['suffix']}")
client.puts deleteFile(msg) client.puts deleteFile(msg)
when 'close' when 'close'
@log.info("#{client.peeraddr[3]} is disconnected")
closeConnection(client) closeConnection(client)
else else
@log.warning("#{client.peeraddr[3]} is disconnected for unkwnow command")
send_msg = {:action => 'unknown', send_msg = {:action => 'unknown',
:gpg_key => msg['gpg_key'], :gpg_key => msg['gpg_key'],
:error => 'server.error.client.unknown'} :error => 'server.error.client.unknown'}
@ -237,6 +257,7 @@ class Server
@host = config['config']['host'] @host = config['config']['host']
@port = config['config']['port'].to_i @port = config['config']['port'].to_i
@data_dir = config['config']['data_dir'] @data_dir = config['config']['data_dir']
@log_file = config['config']['log_file']
@timeout = config['config']['timeout'].to_i @timeout = config['config']['timeout'].to_i
if @host.empty? || @port <= 0 || @data_dir.empty? if @host.empty? || @port <= 0 || @data_dir.empty?
@ -251,6 +272,24 @@ class Server
return false return false
end end
if @log_file.nil? || @log_file.empty?
puts I18n.t('server.checkconfig.fail')
puts I18n.t('server.checkconfig.log_file_empty')
return false
#elsif !File.writable?(@log_file)
# puts I18n.t('server.checkconfig.fail')
# puts I18n.t('server.checkconfig.log_file_ro')
# return false
else
begin
@log = Logger.new(@log_file)
rescue
puts I18n.t('server.checkconfig.fail')
puts I18n.t('server.checkconfig.log_file_create')
return false
end
end
rescue Exception => e rescue Exception => e
puts "#{I18n.t('server.checkconfig.fail')}\n#{e}" puts "#{I18n.t('server.checkconfig.fail')}\n#{e}"
return false return false
@ -268,11 +307,13 @@ class Server
host = ask(I18n.t('server.form.setup.host')).to_s host = ask(I18n.t('server.form.setup.host')).to_s
port = ask(I18n.t('server.form.setup.port')).to_s port = ask(I18n.t('server.form.setup.port')).to_s
data_dir = ask(I18n.t('server.form.setup.data_dir')).to_s data_dir = ask(I18n.t('server.form.setup.data_dir')).to_s
log_file = ask(I18n.t('server.form.setup.log_file')).to_s
timeout = ask(I18n.t('server.form.setup.timeout')).to_s timeout = ask(I18n.t('server.form.setup.timeout')).to_s
config = {'config' => {'host' => host, config = {'config' => {'host' => host,
'port' => port, 'port' => port,
'data_dir' => data_dir, 'data_dir' => data_dir,
'log_file' => log_file,
'timeout' => timeout}} 'timeout' => timeout}}
begin begin
@ -286,5 +327,4 @@ class Server
return true return true
end end
end end

View file

@ -53,8 +53,9 @@ if options[:checkconfig]
elsif options[:setup] elsif options[:setup]
server.setup(options[:config]) server.setup(options[:config])
else else
server.checkconfig(options[:config]) if server.checkconfig(options[:config])
server.start() server.start()
end end
end
exit 0 exit 0