From ab463056e3ae47add651d2ac4e7c5b29f170911e Mon Sep 17 00:00:00 2001 From: Michael Pearce Date: Sun, 16 Feb 2014 03:09:05 -0800 Subject: [PATCH] Reinstate cleanup of director CoinJoin message for CLI and add to GUI on Runtime exit. --- cli/application.rb | 49 ++++++++++++++++---------------- gui/application.rb | 12 ++++++++ gui/view/mixing.rb | 4 +-- lib/coinmux/application/mixer.rb | 11 +++++++ 4 files changed, 49 insertions(+), 27 deletions(-) diff --git a/cli/application.rb b/cli/application.rb index 34a16f7..aafc458 100644 --- a/cli/application.rb +++ b/cli/application.rb @@ -51,29 +51,27 @@ def start # ensure we have the key in hex self.input_private_key = bitcoin_crypto_facade.private_key_to_hex!(input_private_key) - Kernel.trap('SIGINT') { clean_up_coin_join } - Kernel.trap('SIGTERM') { clean_up_coin_join } - message "Starting..." data_store.connect Cli::EventQueue.instance.start - build_mixer.tap do |mixer| - mixer.start do |event| - if event.source == :mixer && event.type == :done - Cli::EventQueue.instance.stop + Kernel.trap('SIGINT') { clean_up_coin_join } + Kernel.trap('SIGTERM') { clean_up_coin_join } + + mixer.start do |event| + if event.source == :mixer && event.type == :done + Cli::EventQueue.instance.stop + else + message = event.options[:message] + if event.type == :failed + message "Error - #{message}", event.source + message "Quitting..." else - message = event.options[:message] - if event.type == :failed - message "Error - #{message}", event.source - message "Quitting..." - else - message "#{event.type.to_s.humanize.capitalize}#{" - #{message}" if message}", event.source - if event.source == :participant && event.type == :completed - message "CoinJoin successfully created!" - end + message "#{event.type.to_s.humanize.capitalize}#{" - #{message}" if message}", event.source + if event.source == :participant && event.type == :completed + message "CoinJoin successfully created!" end end end @@ -86,6 +84,10 @@ def start private + def mixer + @mixer ||= build_mixer + end + def build_mixer Coinmux::Application::Mixer.new( event_queue: Cli::EventQueue.instance, @@ -98,15 +100,12 @@ def build_mixer end def clean_up_coin_join - # TODO - # warning "Cleaning up coin_join" - # if !%w(failed completed).include?(director.try(:coin_join_message).try(:state).try(:value).try(:state)) - # director.coin_join_message.status.insert(Coinmux::Message::Status.build(director.coin_join_message, state: 'failed')) do - # Cli::EventQueue.instance.stop - # end - # else - # Cli::EventQueue.instance.stop - # end + if mixer + message "Canceling..." + mixer.cancel do + Cli::EventQueue.instance.stop + end + end end def run_list_coin_joins diff --git a/gui/application.rb b/gui/application.rb index 3b69b7c..c5fcb8d 100644 --- a/gui/application.rb +++ b/gui/application.rb @@ -204,6 +204,10 @@ def show_frame(&block) setLocationRelativeTo(nil) self.coin_join_uri = preferences_view.coin_join_uri + Java::JavaLang::Runtime.getRuntime().addShutdownHook(Java::JavaLang::Thread.new do + clean_up_mixing + end) + yield pack @@ -211,6 +215,14 @@ def show_frame(&block) root_panel.revalidate() # OSX opening with no content about 20% of time. :( end + def clean_up_mixing + if current_view == :mixing && (mixer = views[:mixing].mixer) + Coinmux::Threading.wait_for_callback(mixer, :cancel) do + # do nothing + end + end + end + if Coinmux.os == :macosx class AppleAdapter < Java::ComAppleEawt::ApplicationAdapter def initialize(application) diff --git a/gui/view/mixing.rb b/gui/view/mixing.rb index 787cff5..aab4b2c 100644 --- a/gui/view/mixing.rb +++ b/gui/view/mixing.rb @@ -2,7 +2,7 @@ class Gui::View::Mixing < Gui::View::Base STATES = [:initializing, :waiting_for_other_inputs, :waiting_for_other_outputs, :waiting_for_completed, :completed] TERMINATE_TEXT = "Terminate" - attr_accessor :director, :participant, :transaction_url + attr_accessor :director, :participant, :transaction_url, :mixer import 'java.awt.Component' import 'java.awt.Dimension' @@ -44,7 +44,7 @@ def handle_show reset_status action_button.setLabel(TERMINATE_TEXT) - build_mixer.start do |event| + (self.mixer = build_mixer).start do |event| if event.source == :participant && STATES.include?(event.type) Gui::EventQueue.instance.sync_exec do update_status(event.type, event.options) diff --git a/lib/coinmux/application/mixer.rb b/lib/coinmux/application/mixer.rb index 065cf9b..79f855e 100644 --- a/lib/coinmux/application/mixer.rb +++ b/lib/coinmux/application/mixer.rb @@ -20,6 +20,17 @@ def start(&callback) participant.start(&mixer_callback) end + def cancel(&callback) + if !%w(failed completed).include?(director.try(:coin_join_message).try(:state).try(:value).try(:state)) + director.coin_join_message.status.insert(Coinmux::Message::Status.build(director.coin_join_message, state: 'failed')) do + yield if block_given? + end + else + yield if block_given? + end + end + + private def notify_event(event)