- All Implemented Interfaces:
Serializable, DataStructure
For efficiency, the options methods and structures here are unsynchronized. Initialize the structure with readBytes(InputStream), or call the setOptions(). Don't change it after that.
To ensure integrity of the RouterInfo, methods that change an element of the RouterInfo will throw an IllegalStateException after the RouterInfo is signed.
Expiration is accepted and returned, but the router MUST write all zeros: the field is ignored on readin and a non-zero value fails the signature check. Implementing expiration, or any other use of the field, requires several releases to propagate because it is backwards-incompatible.
- See Also:
Field Summary
Modifier and TypeFieldDescriptionprivate shortCost value (0-255).private longExpiration timestamp.private byte[]Cached IP address.private final PropertiesTransport options.private intCached port number.private StringTransport style string.private static final intLabels are padded to this width so the colons and values line up in a monospace block.Human-readable names for the option keys, fortoHtml().private static final StringPool of spaces used to pad the rendered HTMLprivate static final intKey-value pairs per line before the log form wraps.private static final byte[]Marker for a cost that has not been measured yetstatic final StringProperty key for host.static final StringProperty key for port.Constructor Summary
ConstructorsConstructorDescriptionCreate an address with empty options.RouterAddress(String style, OrderedProperties options, int cost) Creates an address with a copy of the given options, so the caller may reuse or modify them after construction.Method Summary
Modifier and TypeMethodDescriptionprivate static boolean_hiddenOption(String key) Options omitted fromtoString()as debugging detail.private static String_htmlLabel(String key) Human-readable name for an option key, or the capitalized key if it is unknown.private static void_htmlPair(StringBuilder buf, String label, String value) Appends one<span class=key>label</span> <span class=value>value</span>line, padding the label and escaping the value.private static StringDisplay label for an option key: MTU uppercased, others capitalized.private static void_logSep(StringBuilder buf, int pairs) Separator before a pair in the log form: the plain" * "bullet, or that bullet at the start of a fresh line onceLOG_WRAPpairs have been written.booleandeepEquals(RouterAddress addr) Everything, including Transport, host, port, options, and cost.booleanTransport, host, and port only.intgetCost()Retrieve the weighted cost of this address, relative to other methods of contacting this router.getHost()Convenience, same as getOption("host").byte[]getIP()Caching version ofInetAddress.getByName(getOption("host")).getAddress(), which is slow.Transport option value for the given key.Retrieve the transport specific options necessary for communication.intgetPort()Caching version ofInteger.parseInt(String, int)getOption("port")) Caches valid ports 1-65535 only.Retrieve the type of transport that must be used to communicate on this address.inthashCode()Just use a few items for speed (expiration is always null).voidreadBytes(InputStream in) Restores the expiration field, which is accepted but must be all zeros: it is ignored on readin and a non-zero value fails the signature check.voidsetCost(int cost) Configure the weighted cost of using the address.toHtml()HTML rendering for the console info page: one key-value pair per line, each side in its own span so the theme can style keys and values apart.toString()Form for logs and peer pages: style, host:port, then the remaining options and cost, sorted byOrderedProperties, wrapped onto a continuation line everyLOG_WRAPpairs.voidwriteBytes(OutputStream out) Write the cost, expiration, transport style, and options.Methods inherited from class DataStructureImpl
calculateHash, fromBase64, fromByteArray, read, toBase64, toByteArrayModifier and TypeMethodDescriptionCalculate the SHA256 value of this object (useful for a few scenarios).voidfromBase64(String data) Load the structure from the provided base64 string.voidfromByteArray(byte[] data) Loads structure from the data providedprotected intread(InputStream in, byte[] target) Repeated reads until buffer is full or IOException is thrown.toBase64()Render the structure into modified base 64 notation.byte[]Convert the structure to a byte array.
Field Details
_cost
private volatile short _costCost value (0-255)._expiration
private long _expirationExpiration timestamp._transportStyle
Transport style string._options
Transport options._ip
private byte[] _ipCached IP address._port
private int _portCached port number.PROP_HOST
PROP_PORT
NOT_LOOKED_UP
private static final byte[] NOT_LOOKED_UPMarker for a cost that has not been measured yetLOG_WRAP
private static final int LOG_WRAPKey-value pairs per line before the log form wraps. A full SSU2 address carries up to thirty options, and one unbroken line buries the values that matter.- See Also:
HTML_LABELS
Human-readable names for the option keys, fortoHtml(). The introducer keys are prefixes that take a slot index, such asih0, and are looked up without the index. These mirror the option-key constants in the transport packages, which this package cannot reference without a dependency cycle, so keep the two in step by hand.Names are deliberately kept to a similar length so the values line up in the console's monospace block.
HTML_LABEL_WIDTH
private static final int HTML_LABEL_WIDTHLabels are padded to this width so the colons and values line up in a monospace block. A longer label is written out unchanged.- See Also:
HTML_PAD
Pool of spaces used to pad the rendered HTML- See Also:
Constructor Details
RouterAddress
public RouterAddress()Create an address with empty options.RouterAddress
Creates an address with a copy of the given options, so the caller may reuse or modify them after construction.- Parameters:
style- transport style naming the protocol to use for this addressoptions- may be nullcost- 0-255
Method Details
getCost
public int getCost()Retrieve the weighted cost of this address, relative to other methods of contacting this router. The value 0 means free and 255 means really expensive. No value above 255 is allowed.Unused before 0.7.12.
- Returns:
- 0-255
setCost
public void setCost(int cost) Configure the weighted cost of using the address. No value negative or above 255 is allowed.WARNING - do not change cost on a published address or it will break the RI sig. There is no check here. Rarely used, use 3-arg constructor.
NTCP is set to 10 and SSU to 5 by default, unused before 0.7.12.
- Parameters:
cost- 0-255, where 0 is free and 255 is most expensive
getTransportStyle
Retrieve the type of transport that must be used to communicate on this address.- Returns:
- the transport style
getOptionsMap
getOption
getIP
public byte[] getIP()Caching version ofInetAddress.getByName(getOption("host")).getAddress(), which is slow. Caches numeric host names, and negative caches also.As of 0.9.32, this works for literal IP addresses only, and does NOT resolve host names. If a host name is specified in the options, this will return null. Use
getHost()if you need the host name.- Returns:
- IP or null
getHost
getPort
public int getPort()Caching version ofInteger.parseInt(String, int)getOption("port")) Caches valid ports 1-65535 only.- Returns:
- 1-65535 or 0 if invalid
readBytes
Restores the expiration field, which is accepted but must be all zeros: it is ignored on readin and a non-zero value fails the signature check.- Parameters:
in- stream to read from- Throws:
IllegalStateException- if was already read inDataFormatException- if the data is improperly formattedIOException- if there was a problem reading the stream
writeBytes
Write the cost, expiration, transport style, and options. As of 0.9.3 the expiration MUST be all zeros, as it is ignored on readin and the signature will fail.- Parameters:
out- stream to write to- Throws:
DataFormatException- if the data was incomplete or not yet ready to be writtenIOException- if there was a problem writing to the stream
equals
deepEquals
Everything, including Transport, host, port, options, and cost.- Parameters:
addr- may be null- Returns:
- true only if every field of both addresses matches
hashCode
toString
Form for logs and peer pages: style, host:port, then the remaining options and cost, sorted byOrderedProperties, wrapped onto a continuation line everyLOG_WRAPpairs. The" * "separator is kept on every pair so the console renders it as a bullet. Keys (i, s), version (v), capability bits (caps) and post-quantum flags (pq) are debugging detail and are omitted; usetoHtml()where they are wanted._logSep
Separator before a pair in the log form: the plain" * "bullet, or that bullet at the start of a fresh line onceLOG_WRAPpairs have been written.- Parameters:
buf- the builder the separator is appended topairs- the number of pairs written so far, a line break comes every LOG_WRAP of them
toHtml
HTML rendering for the console info page: one key-value pair per line, each side in its own span so the theme can style keys and values apart. UnliketoString()this keeps the option keys the log form omits, names them readably, escapes the values, and orders the lines by the name the reader sees rather than by the option key. The transport style and its host:port stay leading.- Returns:
- the address as HTML lines, each pair newline terminated
_htmlPair
Appends one<span class=key>label</span> <span class=value>value</span>line, padding the label and escaping the value. There is no punctuation between the two spans - the theme styles the key span, and that styling is what separates the label from the value.- Parameters:
buf- the builder the line is appended tolabel- the display name, right-padded to HTML_LABEL_WIDTHvalue- the text to HTML-escape and place in the value span
_htmlLabel
Human-readable name for an option key, or the capitalized key if it is unknown. Introducer keys are a prefix plus a slot index, soih0reads as theihname followed by the slot number.- Parameters:
key- the option key, possibly with a numeric slot suffix- Returns:
- the human-readable name, with the slot number appended for suffixed keys, or the capitalized key if unknown
_label