1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-30 08:53:05 +00:00

beautiful code

This commit is contained in:
nishiki 2014-02-01 00:30:52 +01:00
parent 1f5b37ed3c
commit dbfe118490
2 changed files with 51 additions and 57 deletions

View file

@ -17,13 +17,8 @@ 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}")
@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|
@ -68,6 +63,10 @@ class Server
end end
end end
end end
rescue Exception => e
@log.error("Impossible to start the server: #{e}")
exit 2
end end
# Get a gpg file # Get a gpg file
@ -228,13 +227,11 @@ class Server
# @args: client -> client connection # @args: client -> client connection
# @rtrn: array of the json string, or false if isn't json message # @rtrn: array of the json string, or false if isn't json message
def get_client_msg(client) def get_client_msg(client)
begin msg = client.gets
msg = client.gets return JSON.parse(msg)
return JSON.parse(msg) rescue
rescue closeConnection(client)
closeConnection(client) return false
return false
end
end end
# Close the client connection # Close the client connection
@ -248,46 +245,43 @@ class Server
# @args: file_config -> the configuration file # @args: file_config -> the configuration file
# @rtrn: true if the config file is correct # @rtrn: true if the config file is correct
def checkconfig(file_config) def checkconfig(file_config)
begin config = YAML::load_file(file_config)
config = YAML::load_file(file_config) @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']
@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?
puts I18n.t('checkconfig.fail') puts I18n.t('checkconfig.fail')
puts I18n.t('checkconfig.empty') puts I18n.t('checkconfig.empty')
return false
end
if !Dir.exist?(@data_dir)
puts I18n.t('checkconfig.fail')
puts I18n.t('checkconfig.datadir')
return false
end
if @log_file.nil? || @log_file.empty?
puts I18n.t('checkconfig.fail')
puts I18n.t('checkconfig.log_file_empty')
return false
else
begin
@log = Logger.new(@log_file)
rescue
puts I18n.t('checkconfig.fail')
puts I18n.t('checkconfig.log_file_create')
return false
end
end
rescue Exception => e
puts "#{I18n.t('checkconfig.fail')}\n#{e}"
return false return false
end end
if !Dir.exist?(@data_dir)
puts I18n.t('checkconfig.fail')
puts I18n.t('checkconfig.datadir')
return false
end
if @log_file.nil? || @log_file.empty?
puts I18n.t('checkconfig.fail')
puts I18n.t('checkconfig.log_file_empty')
return false
else
begin
@log = Logger.new(@log_file)
rescue
puts I18n.t('checkconfig.fail')
puts I18n.t('checkconfig.log_file_create')
return false
end
end
return true return true
rescue Exception => e
puts "#{I18n.t('checkconfig.fail')}\n#{e}"
return false
end end
# Create a new config file # Create a new config file
@ -308,16 +302,14 @@ class Server
'log_file' => log_file, 'log_file' => log_file,
'timeout' => timeout}} 'timeout' => timeout}}
begin File.open(file_config, 'w') do |file|
File.open(file_config, 'w') do |file| file << config.to_yaml
file << config.to_yaml
end
rescue Exception => e
puts "#{I18n.t('form.setup.not_valid')}\n#{e}"
return false
end end
return true return true
rescue Exception => e
puts "#{I18n.t('form.setup.not_valid')}\n#{e}"
return false
end end
# Generate a random salt # Generate a random salt

2
mpw
View file

@ -135,11 +135,13 @@ else
begin begin
cli.interactive() cli.interactive()
rescue SystemExit, Interrupt rescue SystemExit, Interrupt
cli.sync_close()
cli = nil cli = nil
return 1 return 1
end end
end end
cli.sync_close() cli.sync_close()
cli = nil
exit 0 exit 0