bin/PastehubSync in pastehub-0.2.6 vs bin/PastehubSync in pastehub-0.4.0

- old
+ new

@@ -1,11 +1,11 @@ #!/usr/bin/env ruby # -*- coding: utf-8 -*- # # PastehubSync - PasteHub's sync program for UNIX client. # -# Copyright (c) 2009-2011 Kiyoka Nishiyama <kiyoka@sumibi.org> +# Copyright (c) 2013-2014 Kiyoka Nishiyama <kiyoka@sumibi.org> # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # @@ -35,73 +35,99 @@ # require 'thread' require 'pastehub' PasteHub::Config.instance.loadClient -RETRY_INTERVAL = 60 -def setup +def setup( argv ) if not PasteHub.setupDirectory( ) exit( 1 ) end - return PasteHub.signIn( ) + + if 0 < argv.size + case argv[0] + when "batch" + return [ :ok, PasteHub.hostname( ) ] + else + exit( 1 ) + end + else + return [ :ok, PasteHub.hostname( ) ] + end end -def main - ret = setup() - username = ret[0] - secretKey = ret[1] - password = ret[2] +def main( argv ) + params = OptionParser.getopts( argv, "v" ) + config = PasteHub::Config.instance() + if params[ 'v' ] + config.setVerbose( true ) + end + ret = setup( argv ) + errorCode = ret[0] + hostname = ret[1] + + if not :ok == errorCode + exit( 1 ) + end + # save pid file PasteHub.savePid( Process.pid ) # create clientSync - clientSync = PasteHub::ClientSync.new( - PasteHub::Config.instance.listItems / 2, - PasteHub::Config.instance.localDbPath, - 0.5 ) - clientSync.addNoitfyCallback( lambda { puts '<< COUNTUP >>' }, lambda { puts '<< ONLINE >>' }, lambda { puts '<< offline >>' } ) - - while true - begin - - case RbConfig::CONFIG['host_os'] - when /mingw32|mswin|windows/i - # Windows - else - # Other - Signal.trap( :SIGUSR1 ) { - STDERR.puts( "Info: caught signal." ) - clientSync.syncNow( username, secretKey, password ) + clientSync = PasteHub::ClientSync.new( hostname, 1.0 ) + + # add notification handler + receiveNotifyFunc = + case RbConfig::CONFIG['host_os'] + when /^darwin/ + # MacOS X + notifier_path = RbConfig::CONFIG['bindir'] + "/" + "terminal-notifier" + if File.exist?( notifier_path ) + STDERR.puts( "Info: found terminal-notifier for MacOS X." ) + lambda { |x| + str = if config.notifyMessageMax < x.size + x[0...config.notifyMessageMax] + " ..." + else + x + end + command = sprintf( "%s -title 'PasteHub' -message '\\%s' ", notifier_path, str ) + system( command ) } + else + nil end + else + lambda { |x| STDERR.puts '<< COUNTUP >>' } + end - if username - threads = [] - threads.push(Thread.new { clientSync.syncMain( username, secretKey, password ) }) - threads.push(Thread.new { clientSync.clipboardCheck( username, secretKey, password ) }) - threads.each {|t| t.join} - end + clientSync.addNoitfyCallback( receiveNotifyFunc ) - rescue Errno::ECONNREFUSED => e - STDERR.puts "retrying... pastehub server is down(1)" - sleep RETRY_INTERVAL - rescue Errno::ETIMEDOUT => e - STDERR.puts "retrying... network is offline(1)" - sleep RETRY_INTERVAL - rescue SocketError => e - STDERR.puts "retrying... network is offline(2)" - sleep RETRY_INTERVAL - rescue Timeout::Error => e - # ONLINE, but server is not helthy - STDERR.puts "retrying... pastehub server is down(2)" - sleep RETRY_INTERVAL - rescue Errno::ECONNRESET => e - STDERR.puts "retrying... pastehub server is down(3)" - sleep RETRY_INTERVAL + while true + signals = 0 + + case RbConfig::CONFIG['host_os'] + when /mingw32|mswin|windows/i + # Windows + else + # Other + Signal.trap( :SIGUSR1 ) { + STDERR.puts( "Info: caught USR1 signal." ) + signals += 1 + } + Signal.trap( :SIGTERM ) { + STDERR.puts( "Info: caught TERM signal to terminate." ) + exit( 0 ) + } end + + if hostname + threads = [] + threads.push(Thread.new { clientSync.sync_main( ) }) + threads.push(Thread.new { clientSync.clipboard_check( ) }) + threads.each {|t| t.join} + end end end -main +main( ARGV )