public RouterAddress() { _options = new OrderedProperties(); }
public RouterInfo() { _addresses = new ArrayList<RouterAddress>(2); _options = new OrderedProperties(); }
/** * Configure a set of options or statistics that the router can expose. * Makes a copy. * * Warning, clears all capabilities, must be called BEFORE addCapability(). * * @param options if null, clears current options * @throws IllegalStateException if LeaseSet2 is already signed */ public void setOptions(Properties options) { if (_signature != null) throw new IllegalStateException(); if (_options != null) _options.clear(); else _options = new OrderedProperties(); if (options != null) _options.putAll(options); }
@Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append("[GetDateMessage]"); buf.append("\n\tVersion: ").append(_version); if (_options != null && !_options.isEmpty()) { buf.append("\n\tOptions: #: ").append(_options.size()); Properties sorted = new OrderedProperties(); sorted.putAll(_options); for (Map.Entry<Object, Object> e : sorted.entrySet()) { String key = (String) e.getKey(); String val = (String) e.getValue(); buf.append("\n\t\t[").append(key).append("] = [").append(val).append("]"); } } return buf.toString(); } }
@Override public String toString() { StringBuilder buf = new StringBuilder("[SessionConfig: "); buf.append("\n\tDestination: ").append(getDestination()); buf.append("\n\tSignature: ").append(getSignature()); buf.append("\n\tCreation Date: ").append(getCreationDate()); buf.append("\n\tOptions: #: ").append(_options.size()); Properties sorted = new OrderedProperties(); sorted.putAll(_options); for (Map.Entry<Object, Object> e : sorted.entrySet()) { String key = (String) e.getKey(); String val = (String) e.getValue(); buf.append("\n\t\t[").append(key).append("] = [").append(val).append("]"); } buf.append("]"); return buf.toString(); } }
/** Read a mapping from the stream, as defined by the I2P data structure spec, * and store it into a Properties object. * * A mapping is a set of key / value pairs. It starts with a 2 byte Integer (ala readLong(rawStream, 2)) * defining how many bytes make up the mapping. After that comes that many bytes making * up a set of UTF-8 encoded characters. The characters are organized as key=value;. * The key is a String (ala readString(rawStream)) unique as a key within the current * mapping that does not include the UTF-8 characters '=' or ';'. After the key * comes the literal UTF-8 character '='. After that comes a String (ala readString(rawStream)) * for the value. Finally after that comes the literal UTF-8 character ';'. This key=value; * is repeated until there are no more bytes (not characters!) left as defined by the * first two byte integer. * * As of 0.9.18, throws DataFormatException on duplicate key * * @param rawStream stream to read the mapping from * @throws DataFormatException if the format is invalid * @throws IOException if there is a problem reading the data * @return an OrderedProperties */ public static Properties readProperties(InputStream rawStream) throws DataFormatException, IOException { Properties props = new OrderedProperties(); readProperties(rawStream, props); return props; }
/** * @param line part after the #! * @throws IllegalArgumentException on dup key and other errors */ private static OrderedProperties parseProps(String line) throws IllegalArgumentException { line = line.trim(); OrderedProperties rv = new OrderedProperties(); String[] entries = DataHelper.split(line, "#"); for (int i = 0; i < entries.length; i++) { String kv = entries[i]; int eq = kv.indexOf('='); if (eq <= 0 || eq == kv.length() - 1) throw new IllegalArgumentException("No value: \"" + kv + '"'); String k = kv.substring(0, eq); String v = kv.substring(eq + 1); Object old = rv.setProperty(k, v); if (old != null) throw new IllegalArgumentException("Dup key: " + k); } return rv; }
/** * Save the current config options (returning true if save was * successful, false otherwise) * * Synchronized with file read in getConfig() */ public boolean saveConfig() { try { Properties ordered = new OrderedProperties(); synchronized(_configFileLock) { ordered.putAll(_config); DataHelper.storeProps(ordered, new File(_configFilename)); } } catch (IOException ioe) { // warning, _log will be null when called from constructor if (_log != null) _log.error("Error saving the config to " + _configFilename, ioe); else System.err.println("Error saving the config to " + _configFilename + ": " + ioe); return false; } return true; }
private Properties createConfig() { Properties rv = new OrderedProperties(); rv.setProperty(PROP_FORMAT, new String(_format)); rv.setProperty(PROP_DATEFORMAT, _dateFormatPattern);
/** * Save the configuration of all known tunnels to the given file * */ public synchronized void saveConfig(String configFile) throws IOException { File cfgFile = new File(configFile); if (!cfgFile.isAbsolute()) cfgFile = new File(I2PAppContext.getGlobalContext().getConfigDir(), configFile); File parent = cfgFile.getParentFile(); if ( (parent != null) && (!parent.exists()) ) parent.mkdirs(); Properties map = new OrderedProperties(); _controllersLock.readLock().lock(); try { for (int i = 0; i < _controllers.size(); i++) { TunnelController controller = _controllers.get(i); Properties cur = controller.getConfig("tunnel." + i + "."); map.putAll(cur); } } finally { _controllersLock.readLock().unlock(); } DataHelper.storeProps(map, cfgFile); }
/** * The config for a torrent * @param ih 20-byte infohash * @return non-null, possibly empty * @since 0.9.15 */ private Properties getConfig(byte[] ih) { Properties rv = new OrderedProperties(); File conf = configFile(_configDir, ih); synchronized(_configLock) { // one lock for all try { DataHelper.loadProps(rv, conf); } catch (IOException ioe) {} } return rv; }
/** * Store the current keys to disk in the location specified on creation. */ private void storeKeys() { synchronized (nameToPrivKeys) { File file = new File(persistFilename); // now in config dir but check base dir too... if (!file.exists() && !file.isAbsolute()) file = new File(I2PAppContext.getGlobalContext().getConfigDir(), persistFilename); try { Properties props = new OrderedProperties(); props.putAll(nameToPrivKeys); DataHelper.storeProps(props, file); if (_log.shouldInfo()) _log.info("Saved " + nameToPrivKeys.size() + " private keys to " + file); } catch (IOException ioe) { _log.error("Error writing out the SAM keys to " + file, ioe); } } }
/** * Outbound only, NTCP2 with "s" and "v" only * @since 0.9.36 */ private void setOutboundNTCP2Address() { OrderedProperties props = new OrderedProperties(); addNTCP2Options(props); RouterAddress myAddress = new RouterAddress(STYLE2, props, NTCP2_OUTBOUND_COST); replaceAddress(myAddress); }
@Deprecated public static int toProperties(byte target[], int offset, Properties props) throws DataFormatException, IOException { if (props != null) { OrderedProperties p = new OrderedProperties(); p.putAll(props); ByteArrayOutputStream baos = new ByteArrayOutputStream(p.size() * 64); for (Map.Entry<Object, Object> entry : p.entrySet()) { String key = (String) entry.getKey(); String val = (String) entry.getValue(); writeStringUTF8(baos, key); baos.write('='); writeStringUTF8(baos, val); baos.write(';'); } if (baos.size() > 65535) throw new DataFormatException("Properties too big (65535 max): " + baos.size()); byte propBytes[] = baos.toByteArray(); toLong(target, offset, 2, propBytes.length); offset += 2; System.arraycopy(propBytes, 0, target, offset, propBytes.length); offset += propBytes.length; return offset; } else { toLong(target, offset, 2, 0); return offset + 2; } }
private static RouterAddress createTCPAddress(int num) { OrderedProperties props = new OrderedProperties(); String name = "blah.random.host.org"; String port = "" + (1024+num); props.setProperty("host", name); props.setProperty("port", port); RouterAddress addr = new RouterAddress("TCP", props, 10); return addr; }
public DataStructure createDataStructure() throws DataFormatException { //addr.setExpiration(new Date(1000*60*60*24)); // jan 2 1970 OrderedProperties options = new OrderedProperties(); options.setProperty("hostname", "localhost"); options.setProperty("portnum", "1234"); RouterAddress addr = new RouterAddress("Blah", options, 42); return addr; } public DataStructure createStructureToRead() { return new RouterAddress(); }
/** * This only creates an address if the hostname AND port are set in router.config, * which should be rare. * Otherwise, notifyReplaceAddress() below takes care of it. * Note this is only called from startListening() via configureLocalAddress() * * TODO return a list of one or more * TODO unlike in UDP rebuildExternalAddress(), this only runs once, at startup, * so we won't pick up IP changes. * TODO only returns non-null if port is configured * * @since IPv6 moved from CSFI */ private RouterAddress createNTCPAddress() { int p = _context.getProperty(PROP_I2NP_NTCP_PORT, -1); if (p <= 0 || p >= 64*1024) return null; String name = getConfiguredIP(); if (name == null) return null; OrderedProperties props = new OrderedProperties(); props.setProperty(RouterAddress.PROP_HOST, name); props.setProperty(RouterAddress.PROP_PORT, Integer.toString(p)); addNTCP2Options(props); int cost = getDefaultCost(false); RouterAddress addr = new RouterAddress(STYLE, props, cost); return addr; }
@Test public void testToString(){ //addr.setExpiration(new Date(1000*60*60*24)); // jan 2 1970 OrderedProperties options = new OrderedProperties(); options.setProperty("hostname", "localhost"); options.setProperty("portnum", "1234"); RouterAddress addr = new RouterAddress("Blah", options, 42); String ret = addr.toString(); //assertEquals("[RouterAddress: \n\tTransportStyle: Blah\n\tCost: 42\n\tExpiration: Fri Jan 02 00:00:00 UTC 1970\n\tOptions: #: 2\n\t\t[hostname] = [localhost]\n\t\t[portnum] = [1234]]", ret); assertEquals("[RouterAddress: \n\tType: Blah\n\tCost: 42\n\tOptions (2):\n\t\t[hostname] = [localhost]\n\t\t[portnum] = [1234]]", ret); } }
@SuppressWarnings("deprecation") @Test public void testSetOptionsAgain(){ OrderedProperties options = new OrderedProperties(); options.setProperty("hostname", "localhost"); options.setProperty("portnum", "1234"); RouterAddress addr = new RouterAddress("Blah", options, 42); options.setProperty("portnum", "2345"); exception.expect(IllegalStateException.class); addr.setOptions(options); }
@Test public void testNullEquals(){ //addr.setExpiration(new Date(1000*60*60*24)); // jan 2 1970 OrderedProperties options = new OrderedProperties(); options.setProperty("hostname", "localhost"); options.setProperty("portnum", "1234"); RouterAddress addr = new RouterAddress("Blah", options, 42); assertFalse(addr.equals(null)); assertFalse(addr.equals("")); }