• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import rpc from '@ohos.rpc';
17import ApiMessage from '../common/apiMessage.js';
18import ApiResult from '../common/apiResult.js';
19import ReflectCallApi from './ReflectCallApi.js';
20
21let CODE_INVOKE = 1;
22let logTag = "RpcServer:  ";
23
24export default class Stub extends rpc.RemoteObject {
25    constructor(descriptor) {
26        console.log(logTag +" Stub Create");
27        super(descriptor);
28    }
29
30    onRemoteRequest(code, data, reply, option) {
31        try {
32            console.log(logTag +" ============onRemoteRequest: code is" + code);
33            switch (code) {
34                case CODE_INVOKE:
35                {
36                    console.info(logTag +" case CODE_INVOKE start");
37                    let testBundle = new ApiMessage(null, null, null, null, null, null, null);
38                    var tmp = data.readSequenceable(testBundle);
39                    console.log( logTag +" read result is " + tmp + JSON.stringify(testBundle));
40
41                    let resultCall = new ApiResult();
42                    var resCallApi = -1;
43                    const reflectCallApi = new ReflectCallApi();
44                    resCallApi = reflectCallApi.call(testBundle);
45                    console.log(logTag+"_methodName  call success,result is "+resCallApi);
46                    if (resCallApi == 1)  {
47                        resultCall._resultCode = true;
48                        resultCall._result = 1;
49                    } else {
50                        resultCall._resultCode = false;
51                        resultCall._result = -1;
52                    }
53                    console.log(logTag + "The server's writeSequenceable result is " + JSON.stringify(resultCall));
54
55                    testBundle._apiResult=JSON.stringify(resultCall);
56		            console.log(logTag +" The testBundle is " + JSON.stringify(testBundle));
57                    let result = reply.writeSequenceable(testBundle);
58                    console.log(logTag +" writeSequenceable result is " + result);
59                    return true;
60                }
61                default:
62                    console.error(logTag +" default case " + code);
63                    return super.onRemoteRequest(code, data, reply, option);
64            }
65        } catch (error) {
66            console.log(logTag +"ERROR: onRemoteRequest: " + error);
67        }
68        return false;
69    }
70}
71