/
ProbeRequest.java
132 lines (111 loc) · 4.55 KB
/
ProbeRequest.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
package freenet.node.fcp;
import com.db4o.ObjectContainer;
import freenet.node.FSParseException;
import freenet.node.Node;
import freenet.node.probe.Error;
import freenet.node.probe.Listener;
import freenet.node.probe.Probe;
import freenet.node.probe.Type;
import freenet.support.SimpleFieldSet;
/**
* FCP Message which is received from a client and requests a network probe of a specific type.
* <ul>
* <li>Identifier: Optional; identifier to match probe request with results.</li>
* <li>type: Mandatory; denotes the desired response type. Valid values are:
* <ul>
* <li>BANDWIDTH - returns outgoing bandwidth limit in KiB per second.</li>
* <li>BUILD - returns Freenet build / main version.</li>
* <li>IDENTIFIER - returns identifier and integer 7-day uptime percentage.</li>
* <li>LINK_LENGTHS - returns link lengths between the endpoint and its connected peers.</li>
* <li>LOCATION - returns the endpoint's location.</li>
* <li>STORE_SIZE - returns store size in GiB.</li>
* <li>UPTIME_48H - returns 48-hour uptime percentage.</li>
* <li>UPTIME_7D - returns 7-day uptime percentage.</li>
* </ul></li>
* <li>hopsToLive: Optional; approximately how many hops the probe will take before possibly returning a result.
* Valid values are [1, Probe.MAX_HTL]. If omitted Probe.MAX_HTL is used.</li>
* </ul>
*/
public class ProbeRequest extends FCPMessage {
public static final String NAME = "ProbeRequest";
private final String identifier;
private final Type type;
private final byte htl;
public ProbeRequest(SimpleFieldSet fs) throws MessageInvalidException {
/* If not defined in the field set Identifier will be null. As adding a null value to the field set does
* not actually add something under the key, it will also be omitted in the response messages.
*/
this.identifier = fs.get(IDENTIFIER);
try {
this.type = Type.valueOf(fs.get(TYPE));
//If HTL is not specified default to MAX_HTL.
this.htl = fs.get(HTL) == null ? Probe.MAX_HTL : fs.getByte(HTL);
if (this.htl < 0) {
throw new MessageInvalidException(ProtocolErrorMessage.INVALID_MESSAGE,
"hopsToLive cannot be negative.", null, false);
}
} catch (IllegalArgumentException e) {
throw new MessageInvalidException(ProtocolErrorMessage.INVALID_MESSAGE, "Unrecognized parse probe type \"" + fs.get(TYPE) + "\": " + e, null, false);
} catch (FSParseException e) {
//Getting a String from a SimpleFieldSet does not throw - it can at worst return null.
throw new MessageInvalidException(ProtocolErrorMessage.INVALID_MESSAGE, "Unable to parse hopsToLive: " + e, null, false);
}
}
@Override
public SimpleFieldSet getFieldSet() {
return new SimpleFieldSet(true);
}
@Override
public String getName() {
return NAME;
}
@Override
public void removeFrom(ObjectContainer container) {
throw new UnsupportedOperationException();
}
@Override
public void run(final FCPConnectionHandler handler, Node node) throws MessageInvalidException {
if(!handler.hasFullAccess()) {
throw new MessageInvalidException(ProtocolErrorMessage.ACCESS_DENIED, "Probe requires full access.", identifier, false);
}
Listener listener = new Listener() {
@Override
public void onError(Error error, Byte code) {
handler.outputHandler.queue(new ProbeError(identifier, error, code));
}
@Override
public void onRefused() {
handler.outputHandler.queue(new ProbeRefused(identifier));
}
@Override
public void onOutputBandwidth(float outputBandwidth) {
handler.outputHandler.queue(new ProbeBandwidth(identifier, outputBandwidth));
}
@Override
public void onBuild(int build) {
handler.outputHandler.queue(new ProbeBuild(identifier, build));
}
@Override
public void onIdentifier(long probeIdentifier, byte percentageUptime) {
handler.outputHandler.queue(new ProbeIdentifier(identifier, probeIdentifier, percentageUptime));
}
@Override
public void onLinkLengths(float[] linkLengths) {
handler.outputHandler.queue(new ProbeLinkLengths(identifier, linkLengths));
}
@Override
public void onLocation(float location) {
handler.outputHandler.queue(new ProbeLocation(identifier, location));
}
@Override
public void onStoreSize(float storeSize) {
handler.outputHandler.queue(new ProbeStoreSize(identifier, storeSize));
}
@Override
public void onUptime(float uptimePercent) {
handler.outputHandler.queue(new ProbeUptime(identifier, uptimePercent));
}
};
node.startProbe(htl, node.random.nextLong(), type, listener);
}
}