Class: Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/bconv.rb

Instance Method Summary collapse

Constructor Details

#initializeConverter

Returns a new instance of Converter.



31
32
33
34
35
36
37
# File 'lib/bconv.rb', line 31

def initialize
    @source_files = []
    @launcher_thread = nil
    @semaphore = Mutex.new
    @jobs_slots = (number_of_processors * 1.25 + 1).to_i rescue 1
    @jobs_slots = 1 if @jobs_slots < 1
end

Instance Method Details

#preload_dir(dir, &block) ⇒ Object

Asynchronously get directory listing



40
41
42
43
44
45
46
# File 'lib/bconv.rb', line 40

def preload_dir(dir, &block)
    @counting_thread.terminate if @counting_thread
    @counting_thread = Thread.new {
        list_directory(dir)
        block.call(@source_files.size) if block_given?
    }
end

#process(src_dir, dest_dir, size, ratio, &progress) ⇒ Object

Process directory



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bconv.rb', line 49

def process(src_dir, dest_dir, size, ratio, &progress)
    @launcher_thread.join if @launcher_thread   # supposed to be quick enough
    @launcher_thread = Thread.new {
        # get directory listing
        if @counting_thread
            @counting_thread.join
            @counting_thread = nil
        else
            list_directory(src_dir)
        end

        unless @source_files.empty?
            pulse_step = 1 / @source_files.size.to_f
            @jobs=[]
            @source_files.each_slice(((@source_files.size + @jobs_slots - 1) / @jobs_slots).to_i)  do |files|
                @jobs << Thread.new do
                    process_directory(src_dir, dest_dir, files, size, ratio, pulse_step, &progress)
                end
            end
        else
            progress.call(nil, nil, true)
        end
    }
end

#stopObject

Terminate all running threads



75
76
77
78
79
# File 'lib/bconv.rb', line 75

def stop
    @launcher_thread.terminate if @launcher_thread
    @jobs.each { |thread| thread.terminate } if @jobs
    @launcher_thread, @jobs = nil, nil
end