Home | History | Annotate | Download | only in targetprep
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.tradefed.targetprep;
     17 
     18 import com.android.tradefed.build.IBuildInfo;
     19 import com.android.tradefed.config.Option;
     20 import com.android.tradefed.config.OptionClass;
     21 import com.android.tradefed.device.DeviceNotAvailableException;
     22 import com.android.tradefed.device.ITestDevice;
     23 import com.android.tradefed.util.RunUtil;
     24 
     25 /** Target preparer that waits until an ip address is asigned to any of the specified interfaces. */
     26 @OptionClass(alias = "connection-checker")
     27 public class ConnectionChecker extends BaseTargetPreparer {
     28 
     29     @Option(name="max-wait", description="How long to wait for the device to connect, in seconds")
     30     private long mTimeout = 600;
     31 
     32     @Option(name="poll-interval", description="How often to poll the device, in seconds")
     33     private long mInterval = 30;
     34 
     35     @Override
     36     public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
     37             BuildError, DeviceNotAvailableException {
     38         long startTime = System.currentTimeMillis();
     39         long timeout = mTimeout * 1000;
     40         while (!device.checkConnectivity()) {
     41             if (System.currentTimeMillis() - startTime > timeout) {
     42                 throw new TargetSetupError("Device did not connect to the network",
     43                         device.getDeviceDescriptor());
     44             }
     45             RunUtil.getDefault().sleep(mInterval * 1000);
     46         }
     47     }
     48 }
     49