Class SocketInitiator


  • public class SocketInitiator
    extends java.lang.Object
    Lets multiple sockets race the given IP addresses until one has been established.

    Read included Apache License 2.0: HERE

    All NeoVisionaries.WebSockets.* Code Obtained From: GitHub 'NV'  (Takahiko Kawasaki)  Public Archive.


    This follows RFC 6555 (Happy Eyeballs).


    • Constructor Summary

      Constructors 
      Constructor Description
      SocketInitiator​(javax.net.SocketFactory socketFactory, NeoVisionaries.WebSockets.Address address, int connectTimeout, String[] serverNames, DualStackMode mode, int fallbackDelay)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Socket establish​(InetAddress[] addresses)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SocketInitiator

        🡇    
        public SocketInitiator​(javax.net.SocketFactory socketFactory,
                               NeoVisionaries.WebSockets.Address address,
                               int connectTimeout,
                               java.lang.String[] serverNames,
                               DualStackMode mode,
                               int fallbackDelay)
    • Method Detail

      • establish

        🡅    
        public java.net.Socket establish​(java.net.InetAddress[] addresses)
                                  throws java.lang.Exception
        Throws:
        java.lang.Exception
        Code:
        Exact Method Body:
         // Create socket future.
         SocketFuture future = new SocketFuture();
        
         // Create socket racer for each IP address.
         List<SocketRacer> racers = new ArrayList<SocketRacer>(addresses.length);
         int delay = 0;
         Signal startSignal = null;
         for (InetAddress address: addresses)
         {
             // Check if the mode requires us to skip this address.
             if (mMode == DualStackMode.IPV4_ONLY && !(address instanceof Inet4Address)
                 || mMode == DualStackMode.IPV6_ONLY && !(address instanceof Inet6Address))
             {
                 continue;
             }
        
             // Increase the *happy eyeballs* delay (see RFC 6555, sec 5.5).
             delay += mFallbackDelay;
        
             // Create the *done* signal which acts as a *start* signal for the subsequent racer.
             Signal doneSignal = new Signal(delay);
        
             // Create racer to establish the socket.
             SocketAddress socketAddress = new InetSocketAddress(address, mAddress.getPort());
             SocketRacer racer = new SocketRacer(
                     future, mSocketFactory, socketAddress, mServerNames, mConnectTimeout,
                     startSignal, doneSignal);
             racers.add(racer);
        
             // Replace *start* signal with this racer's *done* signal.
             startSignal = doneSignal;
         }
        
         // Wait until one of the sockets has been established, or all failed with an exception.
         return future.await(racers);