Class: Leecher::Aria2::Client

Inherits:
XMLRPC::Client
  • Object
show all
Defined in:
lib/leecher/aria2/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



12
13
14
15
# File 'lib/leecher/aria2/client.rb', line 12

def initialize(config)
  @config = config
  super("127.0.0.1", "/rpc", config.port, nil, nil, config.user, config.password)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/leecher/aria2/client.rb', line 10

def config
  @config
end

Instance Method Details

#add_uri(uris, options) ⇒ Object



30
31
32
33
# File 'lib/leecher/aria2/client.rb', line 30

def add_uri(uris, options)
  rewrite_aria_dir_opt(options)
  call("addUri", uris, options)
end

#call(method, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/leecher/aria2/client.rb', line 17

def call(method, *args)
  retried = false
  response = super("aria2.#{method}", *args)
  @http.finish if @http.started?
  response
rescue Errno::ECONNREFUSED
  retried = true
  start_aria2
  Kernel.sleep(3) # Give it a chance to start.
  retry unless retried
  raise
end

#callback_commandObject



90
91
92
# File 'lib/leecher/aria2/client.rb', line 90

def callback_command
  File.expand_path("../../../bin/leecher-aria2-event-hook", File.dirname(__FILE__))
end

#get_versionObject



56
57
58
# File 'lib/leecher/aria2/client.rb', line 56

def get_version
  call("getVersion")
end

#global_optsObject

WEIRD: sometimes we get nil back here.



36
37
38
# File 'lib/leecher/aria2/client.rb', line 36

def global_opts
  call("getGlobalOption")
end

#make_aria_execObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/leecher/aria2/client.rb', line 68

def make_aria_exec
  args = config.args.dup
  [
   ["--conf-path", config.config],
   ["--on-download-complete", callback_command],
   ["--on-download-error", callback_command],
   ["--on-download-pause", callback_command],
   ["--on-download-start", callback_command],
   ["--on-download-stop", callback_command],
  ].each do |option , value|
    args.push(option + "=" + value)
  end

  [config.bin, args.join(" ")].join(" ")
end

#rewrite_aria_dir_opt(options) ⇒ Object



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

def rewrite_aria_dir_opt(options)
  # Attempt to rewrite +dir+ to a relative path
  if (dir = options.delete("dir"))
    global_dir = global_opts.delete("dir")
    options["dir"] = File.join(global_dir, dir)
  end
end

#start_aria2Object



84
85
86
87
88
# File 'lib/leecher/aria2/client.rb', line 84

def start_aria2
  command = make_aria_exec
  IO.popen(command) {}
  raise UnableToStartAriaError unless $?.success?
end

#tell_activeObject



60
61
62
# File 'lib/leecher/aria2/client.rb', line 60

def tell_active
  call("tellActive")
end

#tell_status(gid, keys = nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/leecher/aria2/client.rb', line 48

def tell_status(gid, keys = nil)
  if keys.nil? or keys.empty?
    call("tellStatus", gid.to_s)
  else
    call("tellStatus", gid.to_s, keys)
  end
end

#tell_waiting(offset, limit) ⇒ Object



64
65
66
# File 'lib/leecher/aria2/client.rb', line 64

def tell_waiting(offset, limit)
  call("tellWaiting", offset, limit)
end