torrent_client 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16bce221ec57ce23550242b63a6c4b4ca29514f0
4
- data.tar.gz: ff62f2894030b4243cecc603c31963d2b464623d
3
+ metadata.gz: 03d06ee01d006e8d24a119e266b2aaf08f4d7b64
4
+ data.tar.gz: 71da685aebeea8cfa4b1f29d62d6ea071c4fa6c3
5
5
  SHA512:
6
- metadata.gz: 94ce50a5192dfd6eb1f8bad579aca25689547de5c1b234d0f1303f5d6e575529d287101c6619a83c30d65b204939ff92e6f886af399aa9d5950bb57a60ce604d
7
- data.tar.gz: 2b5e697f5c6fb2f7ca41d60b8b79ece0dc51ce9f96e0fc57af9562106840791fd988d2106ac908cab86dfe20fe87f7318f200cb222ac8789b354eb97d01bd407
6
+ metadata.gz: f5bdbb04d6fb3fe9ecd5429a114b70378443cc6a7f5d3925b6c7fd7d0bcdae5a803a2f0e1e308931667314e271feed68250d5fbeeca72b9fe71c3b99085ebbf0
7
+ data.tar.gz: 85e7c24edc7c27b66c5822a88e9dc060f03f25df28f53efd71708d14e2267fcc9ef64b10f6424915ab7bf609e87e08f34533b3a12adbaeabbd748edccddb97ec
data/CHANGELOG.md CHANGED
@@ -1 +1,11 @@
1
1
  # Changelog
2
+
3
+ ## [0.2.0] - 2016-04-06
4
+
5
+ Adds TorrentClient.verify, moves libtorrent session creation to happen once when
6
+ the module is loaded, and adds instructions in the README.
7
+
8
+ ## [0.1.0] - 2016-04-05
9
+
10
+ Initial release, supporting TorrentClient.download.
11
+
data/README.md CHANGED
@@ -2,4 +2,20 @@
2
2
 
3
3
  A simple Ruby BitTorrent client based on libtorrent-rasterbar
4
4
 
5
+ ## Setup
6
+
5
7
  Requires [Boost](https://boost.org) and [libtorrent](http://libtorrent.org).
8
+ On Mac OS X these can be setup with Homebrew
9
+ ```
10
+ brew install boost libtorrent-rasterbar
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```
16
+ # Returns true if the torrent payload is complete on-disk, false otherwise.
17
+ TorrentClient.verify '<file>.torrent' '<destination_dir>'
18
+
19
+ # Downloads the torrent payload to the destination_dir. Returns when complete.
20
+ TorrentClient.download '<file>.torrent' '<destination_dir>'
21
+ ```
@@ -5,33 +5,40 @@
5
5
  #include <libtorrent/torrent_info.hpp>
6
6
  #include <libtorrent/alert_types.hpp>
7
7
 
8
+ namespace lt = libtorrent;
9
+
8
10
  VALUE TorrentClient = Qnil;
11
+ std::auto_ptr<lt::session> _session;
9
12
 
10
13
  extern "C" {
11
14
  VALUE method_torrentclient_download(VALUE self, VALUE torrent_file, VALUE destination_dir);
15
+ VALUE method_torrentclient_verify(VALUE self, VALUE torrent_file, VALUE destination_dir);
12
16
 
13
17
  void Init_torrent_client()
14
18
  {
15
19
  TorrentClient = rb_define_module("TorrentClient");
16
20
  rb_define_singleton_method(TorrentClient, "download", RUBY_METHOD_FUNC(method_torrentclient_download), 2);
17
- }
21
+ rb_define_singleton_method(TorrentClient, "verify", RUBY_METHOD_FUNC(method_torrentclient_verify), 2);
18
22
 
19
- VALUE method_torrentclient_download(VALUE self, VALUE torrent_file, VALUE destination_dir)
20
- {
21
23
  try {
22
- namespace lt = libtorrent;
23
-
24
- lt::session s;
25
- s.set_alert_mask(lt::alert::status_notification | lt::alert::error_notification);
24
+ _session.reset(new lt::session());
25
+ _session->set_alert_mask(lt::alert::status_notification | lt::alert::error_notification);
26
26
 
27
27
  lt::error_code ec;
28
- s.listen_on(std::make_pair(6881, 6889), ec);
28
+ _session->listen_on(std::make_pair(6881, 6889), ec);
29
29
  if (ec) {
30
30
  std::string err = "failed to open listen socket: " + ec.message();
31
31
  rb_raise(rb_eRuntimeError, err.c_str());
32
- return Qnil;
33
32
  }
33
+ } catch (std::exception &e) {
34
+ rb_raise(rb_eRuntimeError, "failed from unknown exception");
35
+ }
36
+ }
34
37
 
38
+ VALUE method_torrentclient_download(VALUE self, VALUE torrent_file, VALUE destination_dir)
39
+ {
40
+ try {
41
+ lt::error_code ec;
35
42
  lt::add_torrent_params p;
36
43
  p.save_path = StringValueCStr(destination_dir);
37
44
  p.ti = new lt::torrent_info(StringValueCStr(torrent_file), ec);
@@ -40,20 +47,62 @@ extern "C" {
40
47
  return Qnil;
41
48
  }
42
49
 
43
- s.add_torrent(p, ec);
50
+ lt::torrent_handle handle = _session->add_torrent(p, ec);
44
51
  if (ec) {
45
52
  rb_raise(rb_eRuntimeError, ec.message().c_str());
46
53
  return Qnil;
47
54
  }
48
55
 
49
56
  while (true) {
50
- if (s.wait_for_alert(lt::milliseconds(100))) {
51
- std::auto_ptr<lt::alert> a = s.pop_alert();
57
+ if (_session->wait_for_alert(lt::seconds(1))) {
58
+ std::auto_ptr<lt::alert> a = _session->pop_alert();
52
59
  if (a->type() == lt::torrent_finished_alert::alert_type) {
53
- return Qtrue;
60
+ break;
54
61
  }
55
62
  }
56
63
  }
64
+ _session->remove_torrent(handle);
65
+ return Qtrue;
66
+ } catch (std::exception &e) {
67
+ rb_raise(rb_eRuntimeError, "failed from unknown exception");
68
+ }
69
+ return Qnil;
70
+ }
71
+
72
+ VALUE method_torrentclient_verify(VALUE self, VALUE torrent_file, VALUE destination_dir)
73
+ {
74
+ try {
75
+ lt::error_code ec;
76
+ lt::add_torrent_params p;
77
+ p.save_path = StringValueCStr(destination_dir);
78
+ p.flags = lt::add_torrent_params::flag_paused;
79
+ p.ti = new lt::torrent_info(StringValueCStr(torrent_file), ec);
80
+ if (ec) {
81
+ rb_raise(rb_eRuntimeError, ec.message().c_str());
82
+ return Qnil;
83
+ }
84
+
85
+ lt::torrent_handle handle = _session->add_torrent(p, ec);
86
+ if (ec) {
87
+ rb_raise(rb_eRuntimeError, ec.message().c_str());
88
+ return Qnil;
89
+ }
90
+
91
+ lt::torrent_status status = handle.status();
92
+ while (status.state == lt::torrent_status::checking_files ||
93
+ status.state == lt::torrent_status::checking_resume_data) {
94
+ _session->wait_for_alert(lt::seconds(1));
95
+ status = handle.status();
96
+ }
97
+
98
+ _session->remove_torrent(handle);
99
+
100
+ if (status.state == lt::torrent_status::seeding ||
101
+ status.state == lt::torrent_status::queued_for_checking) {
102
+ return Qtrue;
103
+ } else {
104
+ return Qfalse;
105
+ }
57
106
  } catch (std::exception &e) {
58
107
  rb_raise(rb_eRuntimeError, "failed from unknown exception");
59
108
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: torrent_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-06 00:00:00.000000000 Z
11
+ date: 2016-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler