Changeset 1252


Ignore:
Timestamp:
Oct 22, 2010, 10:45:07 AM (13 years ago)
Author:
Nicklas Nordborg
Message:

Adding support for specifying which local network address to bind to (including the wildcard address). New methods:

  • Client.startProtocol(InetSocketAddress)
  • NetworkReader.connect(InetSocketAddress)
Location:
extensions/net.sf.basedb.torrent/trunk/src/external/hpbtc/hpbtc/protocol
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.torrent/trunk/src/external/hpbtc/hpbtc/protocol/network/NetworkReader.java

    r1250 r1252  
    1313import java.net.InetSocketAddress;
    1414import java.net.ServerSocket;
     15import java.net.SocketAddress;
    1516import java.nio.channels.ClosedChannelException;
    1617import java.nio.channels.SelectableChannel;
     
    3839
    3940    public void connect(final int port) throws IOException {
     41      connect(new InetSocketAddress(InetAddress.getLocalHost(), port));
     42    }
     43
     44    /**
     45      Connect the reader to the local address and port.
     46      @param address The local network address to bind to, or null to let the system
     47        decide
     48      @return The port bound to
     49    */
     50    public InetSocketAddress connect(final InetSocketAddress address)
     51      throws IOException
     52    {
    4053        serverCh = ServerSocketChannel.open();
    41         serverCh.socket().bind(new InetSocketAddress(
    42                 InetAddress.getLocalHost(), port));
     54      ServerSocket socket = serverCh.socket();
     55      socket.bind(address);
    4356        serverCh.configureBlocking(false);
    4457        super.connect();
     58        return (InetSocketAddress)socket.getLocalSocketAddress();
    4559    }
    46 
     60   
    4761    @Override
    4862    protected void registerSelectors(Selector selector)
     
    5569    @Override
    5670    public int connect() throws IOException {
    57         serverCh = ServerSocketChannel.open();
    58         ServerSocket s = serverCh.socket();
    59         s.bind(null);
    60         serverCh.configureBlocking(false);
    61         super.connect();
    62         return s.getLocalPort();
     71      return connect(new InetSocketAddress(InetAddress.getLocalHost(), 0)).getPort();
    6372    }
    6473
  • extensions/net.sf.basedb.torrent/trunk/src/external/hpbtc/hpbtc/protocol/processor/Client.java

    r1249 r1252  
    1515import hpbtc.util.TorrentUtil;
    1616import java.net.InetAddress;
     17import java.net.InetSocketAddress;
     18import java.net.SocketAddress;
    1719import java.util.logging.Logger;
    1820
     
    7678    }
    7779   
     80    public InetSocketAddress startProtocol(InetSocketAddress address)
     81      throws IOException
     82    {
     83        initNetwork();
     84        InetSocketAddress connected = netReader.connect(address);
     85        this.port = connected.getPort();
     86        netWriter.connect();
     87        logger.fine("Started client on " + connected);
     88        return connected;
     89    }
     90   
    7891    public void startProtocol(int port) throws IOException {
    79         this.port = port;
    80         initNetwork();
    81         netReader.connect(port);
    82         netWriter.connect();
    83         logger.fine("Started client on " + InetAddress.getLocalHost() + ":" + port);
     92      startProtocol(new InetSocketAddress(InetAddress.getLocalHost(), port));
    8493    }
    8594   
    8695    public int startProtocol() throws IOException {
    87         initNetwork();
    88         port = netReader.connect();
    89         netWriter.connect();
    90         logger.fine("Started client on " + InetAddress.getLocalHost() + ":" + port);
    91         return port;
     96      return startProtocol(new InetSocketAddress(InetAddress.getLocalHost(), 0)).getPort();
    9297    }
    9398
Note: See TracChangeset for help on using the changeset viewer.