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