1/* 2 * Copyright (c) 2023 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 backgroundTaskManager from '@ohos.backgroundTaskManager'; 18import featureAbility from '@ohos.ability.featureAbility'; 19import wantAgent from '@ohos.wantAgent'; 20 21function startContinuousTask() { 22 let wantAgentInfo = { 23 wants: [ 24 { 25 bundleName: "com.ohos.rpcquesttest", 26 abilityName: "com.ohos.rpcquesttest.ServiceAbility" 27 } 28 ], 29 operationType: wantAgent.OperationType.START_SERVICE, 30 requestCode: 0, 31 wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 32 }; 33 34 wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { 35 try{ 36 backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(), 37 backgroundTaskManager.BackgroundMode.MULTI_DEVICE_CONNECTION, wantAgentObj).then(() => { 38 console.info("Operation startBackgroundRunning succeeded"); 39 }).catch((err) => { 40 console.error("Operation startBackgroundRunning failed Cause: " + err); 41 }); 42 }catch(error){ 43 console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`); 44 } 45 }); 46} 47 48function stopContinuousTask() { 49 try{ 50 backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext()).then(() => { 51 console.info("Operation stopBackgroundRunning succeeded"); 52 }).catch((err) => { 53 console.error("Operation stopBackgroundRunning failed Cause: " + err); 54 }); 55 }catch(error){ 56 console.error(`Operation stopBackgroundRunning failed. code is ${error.code} message is ${error.message}`); 57 } 58} 59 60export default { 61 onStart() { 62 console.info('RpcServer: onStart'); 63 startContinuousTask(); 64 console.info('RpcServer: startContinuousTask'); 65 }, 66 onStop() { 67 console.info('RpcServer: onStop'); 68 stopContinuousTask(); 69 console.info('RpcServer: stopContinuousTask'); 70 }, 71 onCommand(want, startId) { 72 console.info('RpcServer: onCommand, want: ' + JSON.stringify(want) +', startId: ' + startId); 73 }, 74 onConnect(want) { 75 console.info('RpcServer: service onConnect called.'); 76 return new Stub("rpcTestAbility"); 77 }, 78 onDisconnect(want) { 79 console.info('RpcServer: service onDisConnect called.'); 80 }, 81 onReconnect(want) { 82 console.info('RpcServer: service onReConnect called.'); 83 } 84} 85 86class Stub extends rpc.RemoteObject { 87 constructor(descriptor) { 88 super(descriptor); 89 } 90 onRemoteRequest(code, data, reply, option) { 91 try{ 92 console.info("onRemoteRequest: " + code); 93 if (code === 2){ 94 console.info("The processing mode of code value 1 is enabled on the server"); 95 let tmp1 = data.readString(); 96 let result = reply.writeString("onRemoteRequest invoking"); 97 return true; 98 } else { 99 console.error("onRemoteRequest default case " + code); 100 return super.onRemoteRequest(code, data, reply, option); 101 } 102 } catch (error) { 103 console.info("onRemoteRequest error: " + error); 104 } 105 return false 106 } 107 async onRemoteMessageRequest(code, data, reply, option) { 108 try{ 109 console.info("async onRemoteMessageRequest: " + code); 110 if (code === 1){ 111 console.info("The processing mode of code value 1 is enabled on the server"); 112 let tmp1 = data.readString(); 113 let result = reply.writeString("async onRemoteMessageRequest invoking"); 114 } else if (code === 2){ 115 console.info("The processing mode of code value 2 is enabled on the server"); 116 let tmp1 = data.readString(); 117 let result = reply.writeString("async onRemoteMessageRequest invoking"); 118 }else { 119 console.error("async onRemoteMessageRequest default case " + code); 120 return super.onRemoteMessageRequest(code, data, reply, option); 121 } 122 await new Promise((resolve)=>{ 123 console.info("new promise") 124 setTimeout(resolve,100); 125 }) 126 return true; 127 } catch (error) { 128 console.info("async onRemoteMessageRequest: " + error); 129 } 130 return false 131 } 132} 133