1 /*
2  * Copyright (C) 2011 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.command.remote;
17 
18 import com.android.tradefed.command.ICommandScheduler.IScheduledInvocationListener;
19 import com.android.tradefed.device.FreeDeviceState;
20 import com.android.tradefed.device.ITestDevice;
21 import com.android.tradefed.invoker.IInvocationContext;
22 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
23 import com.android.tradefed.util.proto.TfMetricProtoUtil;
24 
25 import com.google.common.collect.ImmutableMap;
26 
27 import java.io.ByteArrayOutputStream;
28 import java.io.PrintStream;
29 import java.util.HashMap;
30 import java.util.Map;
31 
32 class ExecCommandTracker implements IScheduledInvocationListener {
33 
34     private CommandResult.Status mStatus = CommandResult.Status.EXECUTING;
35     private String mErrorDetails = null;
36     private FreeDeviceState mState = null;
37     private HashMap<String, Metric> mRunMetrics = new HashMap<>();
38 
39     @Override
invocationFailed(Throwable cause)40     public void invocationFailed(Throwable cause) {
41         // TODO: replace with StreamUtil.getStackTrace
42         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
43         PrintStream bytePrintStream = new PrintStream(outputStream);
44         cause.printStackTrace(bytePrintStream);
45         mErrorDetails = outputStream.toString();
46     }
47 
48     @Override
invocationComplete(IInvocationContext metadata, Map<ITestDevice, FreeDeviceState> devicesStates)49     public void invocationComplete(IInvocationContext metadata,
50             Map<ITestDevice, FreeDeviceState> devicesStates) {
51         // FIXME: CommandTracker should handle multiple device states.
52         mState = devicesStates.get(metadata.getDevices().get(0));
53         if (mErrorDetails != null) {
54             mStatus = CommandResult.Status.INVOCATION_ERROR;
55         } else {
56             mStatus = CommandResult.Status.INVOCATION_SUCCESS;
57         }
58     }
59 
60     @Override
testRunEnded(long elapsedTime, HashMap<String, Metric> runMetrics)61     public void testRunEnded(long elapsedTime, HashMap<String, Metric> runMetrics) {
62         mRunMetrics.putAll(runMetrics);
63     }
64 
65     /**
66      * Returns the current state as a {@link com.android.tradefed.command.remote.CommandResult}.
67      */
getCommandResult()68     CommandResult getCommandResult() {
69         return new CommandResult(
70                 mStatus,
71                 mErrorDetails,
72                 mState,
73                 new ImmutableMap.Builder<String, String>()
74                         .putAll(TfMetricProtoUtil.compatibleConvert(mRunMetrics))
75                         .build());
76     }
77 }
78