-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
DhtStorageBase.java
211 lines (172 loc) · 6.22 KB
/
DhtStorageBase.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.frostwire.jlibtorrent.plugins;
import com.frostwire.jlibtorrent.Address;
import com.frostwire.jlibtorrent.DhtSettings;
import com.frostwire.jlibtorrent.Sha1Hash;
import com.frostwire.jlibtorrent.TcpEndpoint;
import com.frostwire.jlibtorrent.swig.*;
import java.util.*;
/**
* @author gubatron
* @author aldenml
*/
public class DhtStorageBase implements DhtStorage {
private final Sha1Hash id;
private final DhtSettings settings;
private final Counters counters;
private final HashMap<Sha1Hash, TorrentEntry> map;
public DhtStorageBase(Sha1Hash id, DhtSettings settings) {
this.id = id;
this.settings = settings;
this.counters = new Counters();
this.map = new HashMap<Sha1Hash, TorrentEntry>();
}
@Override
public boolean getPeers(Sha1Hash infoHash, boolean noseed, boolean scrape, entry peers) {
TorrentEntry v = map.get(infoHash);
if (v == null) {
return false;
}
if (!v.name.isEmpty()) {
peers.set("n", v.name);
}
if (scrape) {
bloom_filter_256 downloaders = new bloom_filter_256();
bloom_filter_256 seeds = new bloom_filter_256();
for (PeerEntry peer : v.peers) {
sha1_hash iphash = new sha1_hash();
libtorrent.sha1_hash_address(peer.addr.address(), iphash);
if (peer.seed) {
seeds.set(iphash);
} else {
downloaders.set(iphash);
}
}
peers.set("BFpe", downloaders.to_bytes());
peers.set("BFsd", seeds.to_bytes());
} else {
int num = Math.min(v.peers.size(), settings.maxPeersReply());
Iterator<PeerEntry> iter = v.peers.iterator();
entry_list pe = peers.get("values").list();
byte_vector endpoint = new byte_vector();
for (int t = 0, m = 0; m < num && iter.hasNext(); ++t) {
PeerEntry e = iter.next();
if ((Math.random() / (Integer.MAX_VALUE + 1.f)) * (num - t) >= num - m) continue;
if (noseed && e.seed) continue;
endpoint.resize(18);
int n = libtorrent.write_tcp_endpoint(e.addr, endpoint);
endpoint.resize(n);
pe.push_back(new entry(endpoint));
++m;
}
}
return true;
}
@Override
public void announcePeer(Sha1Hash infoHash, TcpEndpoint endp, String name, boolean seed) {
TorrentEntry v = map.get(infoHash);
if (v == null) {
// we don't have this torrent, add it
// do we need to remove another one first?
if (!map.isEmpty() && map.size() >= settings.maxTorrents()) {
// we need to remove some. Remove the ones with the
// fewest peers
int num_peers = Integer.MAX_VALUE;
Sha1Hash candidateKey = null;
for (Map.Entry<Sha1Hash, TorrentEntry> kv : map.entrySet()) {
if (kv.getValue().peers.size() > num_peers) {
continue;
}
if (infoHash.equals(kv.getKey())) {
continue;
}
num_peers = kv.getValue().peers.size();
candidateKey = kv.getKey();
}
map.remove(candidateKey);
counters.peers -= num_peers;
counters.torrents -= 1;
}
counters.torrents += 1;
v = new TorrentEntry();
map.put(infoHash, v);
}
// the peer announces a torrent name, and we don't have a name
// for this torrent. Store it.
if (!name.isEmpty() && v.name.isEmpty()) {
String tname = name;
if (tname.length() > 100) {
tname = tname.substring(0, 99);
}
v.name = tname;
}
PeerEntry peer = new PeerEntry();
peer.addr = endp.swig();
peer.added = System.currentTimeMillis();
peer.seed = seed;
if (v.peers.contains(peer)) {
v.peers.remove(peer);
counters.peers -= 1;
} else if (v.peers.size() >= settings.maxPeers()) {
// when we're at capacity, there's a 50/50 chance of dropping the
// announcing peer or an existing peer
if (Math.random() > 0.5) {
return;
}
PeerEntry t = v.peers.lower(peer);
if (t == null) {
t = v.peers.first();
}
v.peers.remove(t);
counters.peers -= 1;
}
v.peers.add(peer);
counters.peers += 1;
}
@Override
public boolean getImmutableItem(Sha1Hash target, entry item) {
return false;
}
@Override
public void putImmutableItem(Sha1Hash target, byte[] buf, Address addr) {
}
@Override
public long getMutableItemSeq(Sha1Hash target) {
return -1;
}
@Override
public boolean getMutableItem(Sha1Hash target, long seq, boolean forceFill, entry item) {
return false;
}
@Override
public void putMutableItem(Sha1Hash target, byte[] buf, byte[] sig, long seq, byte[] pk, byte[] salt, Address addr) {
}
@Override
public void tick() {
}
@Override
public Counters counters() {
return counters;
}
static final class PeerEntry {
public long added;
public tcp_endpoint addr;
public boolean seed;
public static final Comparator<PeerEntry> COMPARATOR = new Comparator<PeerEntry>() {
@Override
public int compare(PeerEntry o1, PeerEntry o2) {
tcp_endpoint a1 = o1.addr;
tcp_endpoint a2 = o2.addr;
int r = address.compare(a1.address(), a2.address());
return r == 0 ? Integer.compare(a1.port(), a2.port()) : r;
}
};
}
static final class TorrentEntry {
public TorrentEntry() {
this.name = "";
this.peers = new TreeSet<PeerEntry>(PeerEntry.COMPARATOR);
}
public String name;
public TreeSet<PeerEntry> peers;
}
}