Changeset dd47389 for core


Ignore:
Timestamp:
Jun 25, 2015 5:00:52 PM (5 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
23f2261b
Parents:
25268e7
Message:

Console: Use registered host/port for eepsite link (ticket #1604)
Jetty starter: Register host/port when started
PortMapper?: Add hostname support

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/util/PortMapper.java

    r25268e7 rdd47389  
    33import java.io.IOException;
    44import java.io.Writer;
     5import java.net.InetSocketAddress;
    56import java.util.ArrayList;
    67import java.util.Collections;
     
    1718 */
    1819public class PortMapper {
    19     private final ConcurrentHashMap<String, Integer> _dir;
     20    private final ConcurrentHashMap<String, InetSocketAddress> _dir;
    2021
    2122    public static final String SVC_CONSOLE = "console";
     
    3839     */
    3940    public PortMapper(I2PAppContext context) {
    40         _dir = new ConcurrentHashMap<String, Integer>(8);
     41        _dir = new ConcurrentHashMap<String, InetSocketAddress>(8);
    4142    }
    4243
     
    4748     */
    4849    public boolean register(String service, int port) {
    49         if (port <= 0)
     50        return register(service, "127.0.0.1", port);
     51    }
     52
     53    /**
     54     *  Add the service
     55     *  @param port > 0
     56     *  @return success, false if already registered
     57     *  @since 0.9.21
     58     */
     59    public boolean register(String service, String host, int port) {
     60        if (port <= 0 || port > 65535)
    5061            return false;
    51         return _dir.putIfAbsent(service, Integer.valueOf(port)) == null;
     62        return _dir.putIfAbsent(service, InetSocketAddress.createUnresolved(host, port)) == null;
    5263    }
    5364
     
    7485     */
    7586    public int getPort(String service, int def) {
    76         Integer port = _dir.get(service);
    77         if (port == null)
     87        InetSocketAddress ia = _dir.get(service);
     88        if (ia == null)
    7889            return def;
    79         return port.intValue();
     90        return ia.getPort();
     91    }
     92
     93    /**
     94     *  Get the registered host for a service.
     95     *  Will return "127.0.0.1" if the service was registered without a host.
     96     *  @param def default
     97     *  @return def if not registered
     98     *  @since 0.9.21
     99     */
     100    public String getHost(String service, String def) {
     101        InetSocketAddress ia = _dir.get(service);
     102        if (ia == null)
     103            return def;
     104        return ia.getHostName();
    80105    }
    81106
     
    86111    public void renderStatusHTML(Writer out) throws IOException {
    87112        List<String> services = new ArrayList(_dir.keySet());
    88         out.write("<h2>Port Mapper</h2><table><tr><th>Service<th>Port\n");
     113        out.write("<h2>Port Mapper</h2><table><tr><th>Service<th>Host<th>Port\n");
    89114        Collections.sort(services);
    90115        for (String s : services) {
    91             out.write("<tr><td>" + s + "<td>" + _dir.get(s) + '\n');
     116            InetSocketAddress ia = _dir.get(s);
     117            if (ia == null)
     118                continue;
     119            out.write("<tr><td>" + s + "<td>" + ia.getHostName() + "<td>" + ia.getPort() + '\n');
    92120        }
    93121        out.write("</table>\n");
Note: See TracChangeset for help on using the changeset viewer.