Brett Chabot | 9c27c90 | 2013-09-27 11:00:56 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 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 org.json.JSONArray; |
| 19 | import org.json.JSONException; |
| 20 | import org.json.JSONObject; |
| 21 | |
| 22 | /** |
| 23 | * Remote operation for adding a command to the local tradefed scheduler. |
| 24 | */ |
| 25 | class AddCommandOp extends RemoteOperation { |
| 26 | |
| 27 | private static final String COMMAND_ARGS = "commandArgs"; |
| 28 | private static final String TIME = "time"; |
| 29 | long mTotalTime; |
| 30 | String[] mCommandArgs; |
| 31 | |
| 32 | AddCommandOp() { |
| 33 | this(0, new String[]{}); |
| 34 | } |
| 35 | |
| 36 | AddCommandOp(long totalTime, String... commandArgs) { |
| 37 | mTotalTime = totalTime; |
| 38 | mCommandArgs = commandArgs; |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | protected void unpackFromJson(JSONObject json) throws RemoteException, JSONException { |
| 43 | mTotalTime = json.getLong(TIME); |
| 44 | JSONArray jsonArgs = json.getJSONArray(COMMAND_ARGS); |
| 45 | mCommandArgs = new String[jsonArgs.length()]; |
| 46 | for (int i=0; i < mCommandArgs.length; i++) { |
| 47 | mCommandArgs[i] = jsonArgs.getString(i); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | |
| 52 | @Override |
| 53 | protected OperationType getType() { |
| 54 | return OperationType.ADD_COMMAND; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected void packIntoJson(JSONObject j) throws JSONException { |
| 59 | j.put(TIME, mTotalTime); |
| 60 | JSONArray jsonArgs = new JSONArray(); |
| 61 | for (String arg : mCommandArgs) { |
| 62 | jsonArgs.put(arg); |
| 63 | } |
| 64 | j.put(COMMAND_ARGS, jsonArgs); |
| 65 | } |
| 66 | } |