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 
16 //! IPC calc server
17 
18 extern crate example_calc_ipc_service;
19 extern crate ipc_rust;
20 
21 use example_calc_ipc_service::{ICalc, CalcStub, EXAMPLE_IPC_CALC_SERVICE_ID, init_access_token, add, sub, mul, div};
22 use ipc_rust::{IRemoteBroker, join_work_thread, IpcResult, add_service,};
23 
24 /// example.calc.ipc.ICalcService type
25 pub struct CalcService;
26 
27 impl ICalc for CalcService {
add(&self, num1: i32, num2: i32) -> IpcResult<i32>28     fn add(&self, num1: i32, num2: i32) -> IpcResult<i32> {
29         Ok(add(&num1, &num2))
30     }
sub(&self, num1: i32, num2: i32) -> IpcResult<i32>31     fn sub(&self, num1: i32, num2: i32) -> IpcResult<i32> {
32         Ok(sub(&num1, &num2))
33     }
mul(&self, num1: i32, num2: i32) -> IpcResult<i32>34     fn mul(&self, num1: i32, num2: i32) -> IpcResult<i32> {
35         Ok(mul(&num1, &num2))
36     }
div(&self, num1: i32, num2: i32) -> IpcResult<i32>37     fn div(&self, num1: i32, num2: i32) -> IpcResult<i32> {
38         Ok(div(&num1, &num2))
39     }
40 }
41 
42 impl IRemoteBroker for CalcService {}
43 
main()44 fn main() {
45     init_access_token();
46     // create stub
47     let service = CalcStub::new_remote_stub(CalcService).expect("create CalcService success");
48     add_service(&service.as_object().expect("get ICalc service failed"),
49         EXAMPLE_IPC_CALC_SERVICE_ID).expect("add server to samgr failed");
50     println!("join to ipc work thread");
51     join_work_thread();
52 }