From 1390850e1808b27890f19d7454b666a58b1282c6 Mon Sep 17 00:00:00 2001 From: nishiki Date: Sun, 31 Aug 2014 10:59:33 +0200 Subject: [PATCH] add timeout --- MPW/Sync/MPWSync.rb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/MPW/Sync/MPWSync.rb b/MPW/Sync/MPWSync.rb index 8e6c700..cdb05bf 100644 --- a/MPW/Sync/MPWSync.rb +++ b/MPW/Sync/MPWSync.rb @@ -11,6 +11,7 @@ module MPW require 'i18n' require 'socket' require 'json' + require 'timeout' class MPWSync @@ -37,9 +38,20 @@ module MPW @password = password @suffix = path - TCPSocket.open(@host, @port) do - @enable = true + Timeout.timeout(10) do + begin + TCPSocket.open(@host, @port) do + puts 'testi2' + @enable = true + end + rescue Errno::ENETUNREACH + retry + end end + rescue Timeout::Error + puts 'timeout' + @error_msg = "#{I18n.t('error.timeout')}\n#{e}" + @enable = false rescue Exception => e @error_msg = "#{I18n.t('error.sync.connection')}\n#{e}" @enable = false @@ -58,9 +70,9 @@ module MPW msg = nil TCPSocket.open(@host, @port) do |socket| send_msg = {:action => 'get', - :gpg_key => @gpg_key, - :password => @password, - :suffix => @suffix} + :gpg_key => @gpg_key, + :password => @password, + :suffix => @suffix} socket.puts send_msg.to_json msg = JSON.parse(socket.gets)