1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-27 07:33: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,14 +227,12 @@ 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
# @args: client -> client connection # @args: client -> client connection
@ -248,7 +245,6 @@ 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
@ -282,14 +278,12 @@ class Server
end end
end end
return true
rescue Exception => e rescue Exception => e
puts "#{I18n.t('checkconfig.fail')}\n#{e}" puts "#{I18n.t('checkconfig.fail')}\n#{e}"
return false return false
end end
return true
end
# Create a new config file # Create a new config file
# @args: file_config -> the configuration file # @args: file_config -> the configuration file
# @rtrn: true if le config file is create # @rtrn: true if le config file is create
@ -308,18 +302,16 @@ 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 end
return true
rescue Exception => e rescue Exception => e
puts "#{I18n.t('form.setup.not_valid')}\n#{e}" puts "#{I18n.t('form.setup.not_valid')}\n#{e}"
return false return false
end end
return true
end
# Generate a random salt # Generate a random salt
# @args: length -> the length salt # @args: length -> the length salt
# @rtrn: a random string # @rtrn: a random string

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