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.config.Configuration; 20 import com.android.tradefed.device.FreeDeviceState; 21 import com.android.tradefed.device.ITestDevice; 22 import com.android.tradefed.invoker.IInvocationContext; 23 import com.android.tradefed.invoker.InvocationContext; 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 Map<String, String> mRunMetrics = new HashMap<String, String>(); 38 39 @Override 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 /** 49 * {@inheritDoc} 50 * @deprecated use {@link #invocationComplete(IInvocationContext, Map)} instead. 51 */ 52 @Deprecated 53 @Override 54 public void invocationComplete(ITestDevice device, FreeDeviceState deviceState) { 55 IInvocationContext stubMeta = new InvocationContext(); 56 stubMeta.addAllocatedDevice(Configuration.DEVICE_NAME, device); 57 // Stub metadata for compatibility 58 Map<ITestDevice, FreeDeviceState> state = new HashMap<>(); 59 state.put(device, deviceState); 60 invocationComplete(stubMeta, state); 61 } 62 63 @Override 64 public void invocationComplete(IInvocationContext metadata, 65 Map<ITestDevice, FreeDeviceState> devicesStates) { 66 // FIXME: CommandTracker should handle multiple device states. 67 mState = devicesStates.get(metadata.getDevices().get(0)); 68 if (mErrorDetails != null) { 69 mStatus = CommandResult.Status.INVOCATION_ERROR; 70 } else { 71 mStatus = CommandResult.Status.INVOCATION_SUCCESS; 72 } 73 } 74 75 @Override 76 public void testRunEnded(long elapsedTime, Map<String, String> runMetrics) { 77 mRunMetrics.putAll(runMetrics); 78 } 79 80 /** 81 * Returns the current state as a {@link com.android.tradefed.command.remote.CommandResult}. 82 */ 83 CommandResult getCommandResult() { 84 return new CommandResult(mStatus, mErrorDetails, mState, 85 new ImmutableMap.Builder<String, String>() 86 .putAll(mRunMetrics).build()); 87 } 88 } 89