Class WebSeedClient

    • Field Detail

      • CLOCK

        protected static final Clock CLOCK
        Measures the retry delay. A ten-second delay might become either nothing or an hour if we used local time during the start or end of Daylight Saving Time, but it's fine if we occasionally wait 9 or 11 seconds instead of 10 because of a leap-second adjustment. See Tom Scott's video about the various considerations involved in this choice of clock.
      • JSON_PARSER

        protected static final org.json.simple.parser.JSONParser JSON_PARSER
        Made available to parse JSON responses.
      • lock

        protected final Lock lock
        Held while downloading, so that two requests to the same server won't be pending at the same time.
      • earliestNextAttempt

        protected volatile Instant earliestNextAttempt
        The earliest time we'll try again if there's been a previous IOE, or when the server requests throttling.
      • userAgent

        protected final String userAgent
        The value for the HTTP User-Agent header.
    • Constructor Detail

      • WebSeedClient

        @Deprecated
        protected WebSeedClient​(@Nullable
                                Proxy proxy,
                                @Nullable
                                SSLSocketFactory socketFactory,
                                boolean useRetryDelay)
        Deprecated.
        Use one of the other overloads, which allow specifying the delay before retry.
        Parameters:
        proxy - the proxy to use with this server, or null to use the JVM default
        socketFactory - the socket factory, or null for the JVM default
        useRetryDelay - whether to wait 10 seconds before trying again after an IOException
      • WebSeedClient

        protected WebSeedClient​(WebSeedClientConfiguration webSeedClientConfiguration)
        Parameters:
        webSeedClientConfiguration - configuration
      • WebSeedClient

        protected WebSeedClient()
    • Method Detail

      • checkedGetObject

        protected static <T> T checkedGetObject​(org.json.simple.JSONObject parent,
                                                String key,
                                                Class<T> outputClass)
        Reads a field value from a JSON object and checks that it is the correct type.
        Type Parameters:
        T - the type of outputClass
        Parameters:
        parent - the JSON object to retrieve a field from
        key - the field name
        outputClass - the object class that we expect the value to be
        Returns:
        the field value
        Throws:
        SeedException - if the field is missing or the wrong type
      • getMaxRequestSize

        protected abstract int getMaxRequestSize()
        Returns the maximum number of bytes that can be obtained with one request to the service. When a seed larger than this is needed, it is obtained using multiple requests.
        Returns:
        the maximum number of bytes per request
      • getConnectionUrl

        protected abstract URL getConnectionUrl​(int numBytes)
      • downloadBytes

        protected abstract void downloadBytes​(HttpURLConnection connection,
                                              byte[] seed,
                                              int offset,
                                              int length)
                                       throws IOException
        Performs a single request for random bytes.
        Parameters:
        connection - the connection to download from
        seed - the array to save them to
        offset - the first index to save them to in the array
        length - the number of bytes to download
        Throws:
        IOException - if a connection error occurs
        SeedException - if a malformed response is received
      • generateSeed

        public void generateSeed​(byte[] seed)
                          throws SeedException
        Description copied from interface: SeedGenerator
        Generates a seed value for a random number generator in an existing array.
        Specified by:
        generateSeed in interface SeedGenerator
        Parameters:
        seed - The array that is to be populated with the seed.
        Throws:
        SeedException - If a seed cannot be generated for any reason.
      • divideRoundingUp

        protected static int divideRoundingUp​(int dividend,
                                              int divisor)
      • modRange1ToM

        protected static int modRange1ToM​(int dividend,
                                          int modulus)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • getProxy

        @Nullable
        protected Proxy getProxy()
        The proxy to use with this server, or null to use the JVM default.
      • getRetryDelayMs

        protected long getRetryDelayMs()
        Wait this many milliseconds before trying again after an IOException.