-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
CustomStorage.java
175 lines (133 loc) · 5.76 KB
/
CustomStorage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.frostwire.jlibtorrent.demo;
import com.frostwire.jlibtorrent.*;
import com.frostwire.jlibtorrent.alerts.BlockFinishedAlert;
import com.frostwire.jlibtorrent.alerts.TorrentFinishedAlert;
import com.frostwire.jlibtorrent.plugins.AbstractStoragePlugin;
import com.frostwire.jlibtorrent.plugins.SwigStoragePlugin;
import com.frostwire.jlibtorrent.swig.*;
import sun.misc.Unsafe;
import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
/**
* @author gubatron
* @author aldenml
*/
public final class CustomStorage {
public static void main(String[] args) throws Throwable {
// comment this line for a real application
args = new String[]{"/Users/aldenml/Downloads/Kellee_Maize_The_5th_Element_FrostClick_FrostWire_MP3_April_14_2014.torrent"};
//args = new String[]{"/Users/aldenml/Downloads/test.torrent"};
File torrentFile = new File(args[0]);
System.out.println("Using libtorrent version: " + LibTorrent.fullVersion());
final Session s = new Session();
final HashMap<String, byte[]> memStorage = new HashMap<String, byte[]>();
final SwigStoragePlugin sp = new SwigStoragePlugin(new AbstractStoragePlugin() {
@Override
public int read(long iov_base, long iov_len, int piece, int offset, int flags, storage_error ec) {
String key = piece + ":" + offset;
if (!memStorage.containsKey(key)) {
System.out.println("Read from non existent key: " + key);
return -1;
}
byte[] data = memStorage.get(key);
Unsafe us = getUnsafe();
for (int i = 0; i < data.length; i++) {
us.putByte(iov_base + i, data[i]);
}
System.out.println("Read from key: " + key + ", to read=" + data.length);
return data.length;
}
@Override
public int write(long iov_base, long iov_len, int piece, int offset, int flags, storage_error ec) {
String key = piece + ":" + offset;
if (!memStorage.containsKey(key)) {
System.out.println("Write to a non existent key: " + key);
}
byte[] data = new byte[(int) iov_len];
Unsafe us = getUnsafe();
for (int i = 0; i < data.length; i++) {
data[i] = us.getByte(iov_base + i);
}
memStorage.put(key, data);
System.out.println("Write to key: " + key + ", to write=" + data.length);
return data.length;
}
@Override
public boolean tick() {
System.out.println("Storage tick");
return true;
}
});
swig_storage_constructor sc = new swig_storage_constructor() {
@Override
public swig_storage create(storage_params params) {
return sp;
}
};
final TorrentHandle th = addTorrentSupport(s.getSwig(), new TorrentInfo(torrentFile), torrentFile.getParentFile(), null, null, false, sc);
final CountDownLatch signal = new CountDownLatch(1);
s.addListener(new TorrentAlertAdapter(th) {
@Override
public void blockFinished(BlockFinishedAlert alert) {
int p = (int) (th.getStatus().getProgress() * 100);
//System.out.println("Progress: " + p + " for torrent name: " + alert.torrentName());
//System.out.println(s.getStats().download());
}
@Override
public void torrentFinished(TorrentFinishedAlert alert) {
System.out.print("Torrent finished");
//signal.countDown();
}
});
th.resume();
signal.await();
}
private static TorrentHandle addTorrentSupport(session s, TorrentInfo ti, File saveDir, Priority[] priorities, File resumeFile, boolean async, swig_storage_constructor sc) {
String savePath = null;
if (saveDir != null) {
savePath = saveDir.getAbsolutePath();
} else if (resumeFile == null) {
throw new IllegalArgumentException("Both saveDir and resumeFile can't be null at the same time");
}
add_torrent_params p = add_torrent_params.create_instance_swig_storage(sc);
p.set_ti(ti.getSwig());
if (savePath != null) {
p.setSave_path(savePath);
}
if (priorities != null) {
p.setFile_priorities(Vectors.priorities2unsigned_char_vector(priorities));
}
p.setStorage_mode(storage_mode_t.storage_mode_sparse);
long flags = p.get_flags();
flags &= ~add_torrent_params.flags_t.flag_auto_managed.swigValue();
if (resumeFile != null) {
try {
byte[] data = Utils.readFileToByteArray(resumeFile);
p.setResume_data(Vectors.bytes2char_vector(data));
flags |= add_torrent_params.flags_t.flag_use_resume_save_path.swigValue();
} catch (Throwable e) {
System.out.println("Unable to set resume data");
}
}
p.set_flags(flags);
if (async) {
s.async_add_torrent(p);
return null;
} else {
error_code ec = new error_code();
torrent_handle th = s.add_torrent(p, ec);
return new TorrentHandle(th);
}
}
public static Unsafe getUnsafe() {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
return (Unsafe) f.get(null);
} catch (Exception e) {
}
return null;
}
}