-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
TorrentAlertAdapter.java
155 lines (112 loc) · 3.94 KB
/
TorrentAlertAdapter.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
package com.frostwire.jlibtorrent;
import com.frostwire.jlibtorrent.alerts.*;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* @author gubatron
* @author aldenml
*/
public class TorrentAlertAdapter implements AlertListener {
private static final Logger LOG = Logger.getLogger(TorrentAlertAdapter.class);
protected final TorrentHandle th;
private static final Map<String, CallAlertFunction> CALL_TABLE = buildCallAlertTable();
public TorrentAlertAdapter(TorrentHandle th) {
this.th = th;
}
@Override
public final void alert(Alert<?> alert) {
if (!(alert instanceof TorrentAlert<?>)) {
return;
}
if (!((TorrentAlert<?>) alert).getHandle().getSwig().op_eq(th.getSwig())) {
return;
}
CallAlertFunction function = CALL_TABLE.get(alert.getClass().getName());
if (function != null) {
function.invoke(this, alert);
}
}
public void torrentAdded(TorrentAddedAlert alert) {
}
public void torrentFinished(TorrentFinishedAlert alert) {
}
public void torrentRemoved(TorrentRemovedAlert alert) {
}
public void torrentUpdate(TorrentUpdateAlert alert) {
}
public void torrentDeleted(TorrentDeletedAlert alert) {
}
public void addTorrent(AddTorrentAlert alert) {
}
public void blockFinished(BlockFinishedAlert alert) {
}
public void metadataReceived(MetadataReceivedAlert alert) {
}
public void metadataFailed(MetadataFailedAlert alert) {
}
public void saveResumeData(SaveResumeDataAlert alert) {
}
public void fileCompleted(FileCompletedAlert alert) {
}
public void fileRenamed(FileRenamedAlert alert) {
}
public void fileRenameFailed(FileRenameFailedAlert alert) {
}
public void fileError(FileErrorAlert alert) {
}
public void trackerAnnounce(TrackerAnnounceAlert alert) {
}
public void trackerReply(TrackerReplyAlert alert) {
}
public void trackerWarning(TrackerWarningAlert alert) {
}
public void trackerError(TrackerErrorAlert alert) {
}
public void readPiece(ReadPieceAlert alert) {
}
public void stateChanged(StateChangedAlert alert) {
}
public void dhtReply(DhtReplyAlert alert) {
}
public void scrapeReply(ScrapeReplyAlert alert) {
}
public void scrapeFailed(ScrapeFailedAlert alert) {
}
public void torrentPrioritize(TorrentPrioritizeAlert alert) {
}
private static Map<String, CallAlertFunction> buildCallAlertTable() {
Map<String, CallAlertFunction> map = new HashMap<String, CallAlertFunction>();
for (Method m : TorrentAlertAdapter.class.getDeclaredMethods()) {
Class<?> returnType = m.getReturnType();
Class<?>[] parameterTypes = m.getParameterTypes();
if (isAlertMethod(returnType, parameterTypes)) {
try {
Class<?> clazz = parameterTypes[0];
CallAlertFunction function = new CallAlertFunction(m);
map.put(clazz.getName(), function);
} catch (Throwable e) {
LOG.warn(e.toString());
}
}
}
return Collections.unmodifiableMap(map);
}
private static boolean isAlertMethod(Class<?> returnType, Class<?>[] parameterTypes) {
return returnType.equals(void.class) && parameterTypes.length == 1 && Alert.class.isAssignableFrom(parameterTypes[0]);
}
private static final class CallAlertFunction {
private final Method method;
public CallAlertFunction(Method method) {
this.method = method;
}
public void invoke(TorrentAlertAdapter adapter, Alert<?> alert) {
try {
method.invoke(adapter, alert);
} catch (Throwable e) {
LOG.warn(e.toString());
}
}
}
}