1/* 2 * Copyright (c) 2022-2024 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 ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility'; 17import rpc from "@ohos.rpc"; 18import fileio from '@ohos.fileio'; 19import fs from '@ohos.file.fs'; 20import wantAgent from '@ohos.wantAgent'; 21import backgroundTaskManager from '@ohos.backgroundTaskManager'; 22 23function startContinuousTask() { 24 let wantAgentInfo = { 25 wants: [ 26 { 27 bundleName: "com.acts.fileio.test.server", 28 abilityName: "com.acts.fileio.test.server.ServiceAbility" 29 } 30 ], 31 operationType: wantAgent.OperationType.START_SERVICE, 32 requestCode: 0, 33 wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 34 }; 35 36 wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => { 37 try{ 38 backgroundTaskManager.startBackgroundRunning(this.context, backgroundTaskManager.BackgroundMode.MULTI_DEVICE_CONNECTION, wantAgentObj).then(() => { 39 console.info("Operation startBackgroundRunning succeeded"); 40 }).catch((err) => { 41 console.error("Operation startBackgroundRunning failed Cause: " + err); 42 }); 43 }catch(error){ 44 console.error(`Operation startBackgroundRunning failed. code is ${error.code} message is ${error.message}`); 45 } 46 }); 47} 48 49function stopContinuousTask() { 50 try{ 51 backgroundTaskManager.stopBackgroundRunning(this.context).then(() => { 52 console.info("Operation stopBackgroundRunning succeeded"); 53 }).catch((err) => { 54 console.error("Operation stopBackgroundRunning failed Cause: " + err); 55 }); 56 }catch(error){ 57 console.error(`Operation stopBackgroundRunning failed. code is ${error.code} message is ${error.message}`); 58 } 59} 60 61export default class ServiceAbility extends ServiceExtension { 62 onCreate(want) { 63 // Called to return a FormBindingData object. 64 console.info("FileioServer ServiceAbility onCreate") 65 } 66 67 onConnect(want) { 68 // Called when the form provider is notified that a temporary form is successfully 69 console.info("FileioServer ServiceAbility onConnect") 70 return new Stub("rpcTestAbility"); 71 } 72 73 onDisconnect(want) { 74 // Called to notify the form provider to update a specified form. 75 console.info("FileioServer ServiceAbility onDisconnect") 76 } 77 78 onRequest(want, startId){ 79 console.info("FileioServer ServiceAbility onRequest") 80 81 } 82 83 onDestroy() { 84 // Called to notify the form provider that a specified form has been destroyed. 85 console.info("IpcStageServer ServiceAbility onCronDestroyeate") 86 87 } 88} 89 90function checkDirExists(destDirPath) { 91 console.info("start check dir :" + destDirPath); 92 try { 93 let res = fs.accessSync(destDirPath); 94 console.info('------------------- dest dir exists.'); 95 return res; 96 } catch (e) { 97 console.info('------------------- dest dir is not exists.'); 98 return false; 99 } 100} 101 102function checkFileExists(destFilePath,) { 103 console.info("start check file :" + destFilePath); 104 try { 105 let res = fs.accessSync(destFilePath); 106 console.info('------------------- ' + destFilePath + ' exists.'); 107 return res; 108 } catch (e) { 109 console.info('------------------- ' + destFilePath + ' not exists.'); 110 return false; 111 } 112} 113 114function getFileContent(fpath) { 115 console.info("start get file content:" + fpath); 116 let content = ""; 117 try { 118 content = fs.readTextSync(fpath); 119 console.info("-------------- dest file content :" + content); 120 } catch (e) { 121 content = "serverSide readTextSync failed"; 122 console.info("-------------- read dest file content failed." + e); 123 } 124 return content; 125} 126 127const CODE_MK_DIR = 1; 128const CODE_RM_DIR = 2; 129const CODE_CREATE_FILE = 3; 130const CODE_DELETE_FILE = 4; 131const CODE_GET_FILE_CONTENT = 5; 132const CODE_GET_FILE_STAT = 6; 133const CODE_FSYNC_FILE = 7; 134 135class Stub extends rpc.RemoteObject { 136 constructor(descriptor) { 137 super(descriptor); 138 } 139 140 async onRemoteMessageRequest(code, data, reply, option) { 141 try{ 142 console.info("async onRemoteMessageRequest: " + code); 143 switch (code) { 144 case CODE_MK_DIR: 145 { 146 console.info("case CODE_MK_DIR start") 147 let path = data.readString() 148 console.info("The server's readString result is " + path); 149 let checkResult = checkDirExists(path); 150 let result; 151 if (checkResult == true) { 152 result = reply.writeString("SUCCESS"); 153 } else { 154 result = reply.writeString("Server side dir synchronization creation failed."); 155 } 156 157 console.info("The server's writeString result is " + result); 158 return true; 159 } 160 case CODE_RM_DIR: 161 { 162 console.info("case CODE_RM_DIR start"); 163 let path = data.readString(); 164 console.info("The server's readString result is " + path); 165 let result; 166 try { 167 let file = fs.openSync(path); 168 result = reply.writeString("Server side dir synchronization delete failed!"); 169 fs.closeSync(file); 170 } catch (error) { 171 result = reply.writeString("SUCCESS"); 172 } 173 174 console.info("The server's writeString result is " + result); 175 return true; 176 } 177 case CODE_CREATE_FILE: 178 { 179 console.info("case CODE_CREATE_FILE start"); 180 let path = data.readString(); 181 console.info("The server's readString result is " + path); 182 183 let checkResult = checkFileExists(path); 184 let result; 185 if (checkResult == true) { 186 result = reply.writeString("SUCCESS"); 187 } else { 188 result = reply.writeString("Server side file synchronization creation failed!"); 189 } 190 191 console.info("The server's writeString result is " + result); 192 return true; 193 } 194 case CODE_DELETE_FILE: 195 { 196 console.info("case CODE_DELETE_FILE start"); 197 let path = data.readString(); 198 console.info("The server's readString result is " + path); 199 let result; 200 try { 201 let file = fs.openSync(path); 202 result = reply.writeString("Server side file synchronization delete failed!"); 203 fs.closeSync(file); 204 } catch (error) { 205 result = reply.writeString("SUCCESS"); 206 } 207 208 console.info("The server's writeString result is " + result); 209 return true; 210 } 211 case CODE_GET_FILE_CONTENT: 212 { 213 console.info("case CODE_GET_FILE_CONTENT start"); 214 let path = data.readString(); 215 console.info("The server's readString result is " + path); 216 checkFileExists(path); 217 let content =""; 218 try { 219 console.info("---------------- start get file content:" + path); 220 content = getFileContent(path); 221 console.info("-------------- dest file content :" + content); 222 } catch (e) { 223 console.info("-------------- read dest file content failed." + e); 224 } 225 226 let result = reply.writeString(content); 227 console.info("The server's writeString result is " + result); 228 return true; 229 } 230 case CODE_GET_FILE_STAT: 231 { 232 console.info("case CODE_GET_FILE_STAT start"); 233 let path = data.readString(); 234 console.info("The server's readString result is " + path); 235 let localStat = fileio.statSync(path); 236 let localStatInfo = "localStat.size = " + localStat.size + ",localStat.mode = " + localStat.mode; 237 let result = reply.writeString(localStatInfo); 238 console.info("The server's writeString result is " + result); 239 return true; 240 } 241 case CODE_FSYNC_FILE: 242 { 243 console.info("case CODE_FSYNC_FILE start"); 244 let path = data.readString(); 245 console.info("The server's readString result is " + path); 246 let result; 247 try{ 248 let file = fs.openSync(path, fs.OpenMode.READ_WRITE); 249 fs.fsyncSync(file.fd); 250 fs.closeSync(file); 251 console.info("sync data succeed"); 252 result = reply.writeString("SUCCESS"); 253 } catch (e) { 254 console.info("sync data failed with error:" + e); 255 result = reply.writeString("FAILED"); 256 } 257 console.info("The server's writeString result is " + result); 258 return true; 259 } 260 default: 261 console.error("default case " + code); 262 return super.onRemoteMessageRequest(code, data, reply, option); 263 } 264 } catch (error) { 265 console.info("async onRemoteMessageRequest: " + error); 266 } 267 return false 268 } 269}