aboutsummaryrefslogtreecommitdiff
path: root/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/RpcUtil.java
blob: 4601e93b35bb67fa53994d8dddb9e18404deadc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * Copyright (C) 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.mobly.snippet.util;

import com.google.android.mobly.snippet.event.EventCache;
import com.google.android.mobly.snippet.event.SnippetEvent;
import com.google.android.mobly.snippet.manager.SnippetManager;
import com.google.android.mobly.snippet.rpc.JsonRpcResult;
import com.google.android.mobly.snippet.rpc.MethodDescriptor;
import com.google.android.mobly.snippet.rpc.RpcError;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Class that implements APIs to schedule other RPCs.
 *
 * <p>If a device is required to be disconnected (e.g., USB power off), no RPCs can be made while
 * device is offline.
 *
 * <p>However, We still need snippet continue to run and execute previously scheduled RPCs
 *
 * <p>The return value of the scheduled RPC is cached in {@link EventCache} and can be retrieved
 * later after device is back online.
 */
public class RpcUtil {
    // RPC ID is used for reporting responses back to the client. However, the results of
    // scheduled RPCs are reported back to the client via events instead of through synchronous
    // responses, so the RPC ID is unused. We pass an arbitrary value of 0.
    private static final int DEFAULT_ID = 0;
    private final SnippetManager mReceiverManager;
    private final EventCache mEventCache = EventCache.getInstance();

    public RpcUtil() {
        mReceiverManager = SnippetManager.getInstance();
    }

    /**
     * Schedule given RPC with some delay.
     *
     * @param callbackId The callback ID used to cache RPC results.
     * @param methodName The RPC name to be scheduled.
     * @param delayMs The delay in ms
     * @param params Array of the parameters to the RPC
     */
    public void scheduleRpc(
            final String callbackId,
            final String methodName,
            final long delayMs,
            final JSONArray params)
            throws Throwable {
        Timer timer = new Timer();
        TimerTask task =
                new TimerTask() {
                    @Override
                    public void run() {
                        SnippetEvent event = new SnippetEvent(callbackId, methodName);
                        try {
                            JSONObject obj = invokeRpc(methodName, params, DEFAULT_ID, callbackId);
                            // Cache RPC method return value.
                            for (int i = 0; i < obj.names().length(); i++) {
                                String key = obj.names().getString(i);
                                event.getData().putString(key, obj.get(key).toString());
                            }
                        } catch (JSONException e) {
                            String stackTrace = JsonRpcResult.getStackTrace(e);
                            event.getData().putString("error", stackTrace);
                        } finally {
                            mEventCache.postEvent(event);
                        }
                    }
                };
        timer.schedule(task, delayMs);
    }

    /**
     * Invoke the RPC.
     *
     * @param methodName The RPC name to be invoked.
     * @param params Array of the parameters to the RPC
     * @param id The ID that identifies an RPC
     * @param UID Globally unique session ID.
     */
    public JSONObject invokeRpc(String methodName, JSONArray params, int id, Integer UID)
            throws JSONException {
        return invokeRpc(methodName, params, id, String.format(Locale.US, "%d-%d", UID, id));
    }

    /**
     * Invoke the RPC.
     *
     * @param methodName The RPC name to be invoked.
     * @param params Array of the parameters to the RPC
     * @param id The ID that identifies an RPC
     * @param callbackId The callback ID used to cache RPC results.
     */
    public JSONObject invokeRpc(String methodName, JSONArray params, int id, String callbackId)
            throws JSONException {
        MethodDescriptor rpc = mReceiverManager.getMethodDescriptor(methodName);
        if (rpc == null) {
            return JsonRpcResult.error(id, new RpcError("Unknown RPC: " + methodName));
        }
        try {
            JSONArray newParams = new JSONArray();
            /** If calling an {@link AsyncRpc}, put the message ID as the first param. */
            if (rpc.isAsync()) {
                newParams.put(callbackId);
                for (int i = 0; i < params.length(); i++) {
                    newParams.put(params.get(i));
                }
                Object returnValue = rpc.invoke(mReceiverManager, newParams);
                return JsonRpcResult.callback(id, returnValue, callbackId);
            } else {
                Object returnValue = rpc.invoke(mReceiverManager, params);
                return JsonRpcResult.result(id, returnValue);
            }
        } catch (Throwable t) {
            Log.e("Invocation error.", t);
            return JsonRpcResult.error(id, t);
        }
    }
}