1a34a8711Sopenharmony_ci/*
2a34a8711Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3a34a8711Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a34a8711Sopenharmony_ci * you may not use this file except in compliance with the License.
5a34a8711Sopenharmony_ci * You may obtain a copy of the License at
6a34a8711Sopenharmony_ci *
7a34a8711Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a34a8711Sopenharmony_ci *
9a34a8711Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a34a8711Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a34a8711Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a34a8711Sopenharmony_ci * See the License for the specific language governing permissions and
13a34a8711Sopenharmony_ci * limitations under the License.
14a34a8711Sopenharmony_ci */
15a34a8711Sopenharmony_ci
16a34a8711Sopenharmony_ci//! IPC calc server
17a34a8711Sopenharmony_ci
18a34a8711Sopenharmony_ciextern crate example_calc_ipc_service;
19a34a8711Sopenharmony_ciextern crate ipc_rust;
20a34a8711Sopenharmony_ci
21a34a8711Sopenharmony_ciuse example_calc_ipc_service::{ICalc, CalcStub, EXAMPLE_IPC_CALC_SERVICE_ID, init_access_token, add, sub, mul, div};
22a34a8711Sopenharmony_ciuse ipc_rust::{IRemoteBroker, join_work_thread, IpcResult, add_service,};
23a34a8711Sopenharmony_ci
24a34a8711Sopenharmony_ci/// example.calc.ipc.ICalcService type
25a34a8711Sopenharmony_cipub struct CalcService;
26a34a8711Sopenharmony_ci
27a34a8711Sopenharmony_ciimpl ICalc for CalcService {
28a34a8711Sopenharmony_ci    fn add(&self, num1: i32, num2: i32) -> IpcResult<i32> {
29a34a8711Sopenharmony_ci        Ok(add(&num1, &num2))
30a34a8711Sopenharmony_ci    }
31a34a8711Sopenharmony_ci    fn sub(&self, num1: i32, num2: i32) -> IpcResult<i32> {
32a34a8711Sopenharmony_ci        Ok(sub(&num1, &num2))
33a34a8711Sopenharmony_ci    }
34a34a8711Sopenharmony_ci    fn mul(&self, num1: i32, num2: i32) -> IpcResult<i32> {
35a34a8711Sopenharmony_ci        Ok(mul(&num1, &num2))
36a34a8711Sopenharmony_ci    }
37a34a8711Sopenharmony_ci    fn div(&self, num1: i32, num2: i32) -> IpcResult<i32> {
38a34a8711Sopenharmony_ci        Ok(div(&num1, &num2))
39a34a8711Sopenharmony_ci    }
40a34a8711Sopenharmony_ci}
41a34a8711Sopenharmony_ci
42a34a8711Sopenharmony_ciimpl IRemoteBroker for CalcService {}
43a34a8711Sopenharmony_ci
44a34a8711Sopenharmony_cifn main() {
45a34a8711Sopenharmony_ci    init_access_token();
46a34a8711Sopenharmony_ci    // create stub
47a34a8711Sopenharmony_ci    let service = CalcStub::new_remote_stub(CalcService).expect("create CalcService success");
48a34a8711Sopenharmony_ci    add_service(&service.as_object().expect("get ICalc service failed"),
49a34a8711Sopenharmony_ci        EXAMPLE_IPC_CALC_SERVICE_ID).expect("add server to samgr failed");
50a34a8711Sopenharmony_ci    println!("join to ipc work thread");
51a34a8711Sopenharmony_ci    join_work_thread();
52a34a8711Sopenharmony_ci}