1 // Copyright 2022 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 15 package dev.pigweed.pw_transfer; 16 17 import com.google.auto.value.AutoValue; 18 import com.google.common.base.Preconditions; 19 20 /** 21 * Transfer client settings for timeouts and retries. 22 */ 23 @AutoValue 24 public abstract class TransferTimeoutSettings { 25 /** Amount of time to wait for a packet before resending the last packet. */ timeoutMillis()26 public abstract int timeoutMillis(); 27 28 /** Amount of time to wait for the first packet before retrying the transfer. */ initialTimeoutMillis()29 public abstract int initialTimeoutMillis(); 30 31 /** Maximum number of times to retry sending a packet. */ maxRetries()32 public abstract int maxRetries(); 33 34 /** Maximum number of retries to allow before aborting the transfer. */ maxLifetimeRetries()35 public abstract int maxLifetimeRetries(); 36 37 /** Creates a builder with defaults applied to all fields. */ builder()38 public static TransferTimeoutSettings.Builder builder() { 39 return new AutoValue_TransferTimeoutSettings.Builder() 40 .setTimeoutMillis(3000) 41 .setInitialTimeoutMillis(6000) 42 .setMaxRetries(5) 43 .setMaxLifetimeRetries(5000); 44 } 45 46 @AutoValue.Builder 47 public abstract static class Builder { setTimeoutMillis(int timeoutMillis)48 public abstract Builder setTimeoutMillis(int timeoutMillis); 49 setInitialTimeoutMillis(int initialTimeoutMillis)50 public abstract Builder setInitialTimeoutMillis(int initialTimeoutMillis); 51 setMaxRetries(int maxRetries)52 public abstract Builder setMaxRetries(int maxRetries); 53 setMaxLifetimeRetries(int maxLifetimeRetries)54 public abstract Builder setMaxLifetimeRetries(int maxLifetimeRetries); 55 build()56 public final TransferTimeoutSettings build() { 57 TransferTimeoutSettings settings = autoBuild(); 58 Preconditions.checkState( 59 settings.timeoutMillis() >= 0, "Negative timeouts are not permitted"); 60 Preconditions.checkState(settings.initialTimeoutMillis() >= settings.timeoutMillis(), 61 "The initial timeout must be at least as long as the regular timeout"); 62 Preconditions.checkState(settings.maxRetries() >= 0, "Retries must be positive"); 63 Preconditions.checkState(settings.maxLifetimeRetries() >= settings.maxRetries(), 64 "Lifetime max retries cannot be smaller than max retries"); 65 return settings; 66 } 67 autoBuild()68 abstract TransferTimeoutSettings autoBuild(); 69 } 70 } 71