From fea16bc16be01b844313886aadf65b276eeb8766 Mon Sep 17 00:00:00 2001 From: duzhaoteng <997607625@qq.com> Date: Thu, 13 Jul 2023 20:53:52 +0800 Subject: [PATCH] =?UTF-8?q?ipc=5Fconnect=20=E6=96=B0=E5=A2=9E=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cloud_extension/src/ipc_conn/connect.rs | 203 ++++++++++++++++---- src/cloud_extension/src/ipc_conn/error.rs | 22 +++ src/cloud_extension/src/lib.rs | 21 +- 3 files changed, 201 insertions(+), 45 deletions(-) diff --git a/src/cloud_extension/src/ipc_conn/connect.rs b/src/cloud_extension/src/ipc_conn/connect.rs index 535265a..480b869 100644 --- a/src/cloud_extension/src/ipc_conn/connect.rs +++ b/src/cloud_extension/src/ipc_conn/connect.rs @@ -12,76 +12,194 @@ // limitations under the License. use std::collections::HashMap; +use std::sync::Mutex; +use ipc_rust::{get_context_object, IRemoteObj, MsgParcel}; +use crate::ipc_conn::error::Error; + +/************** All AppInfo struct-related structures and methods **************/ + +pub(crate) enum RecordFieldType { + Null, + Int, + Double, + String, + Bool, + Blob, + List, + Map, + Asset, + Reference, +} -pub struct AppInfo { - app_bundle_name: String, - app_id: String, - app_fingerprint: String, - // And more... +pub(crate) struct SchemaField { + pub(crate) name: String, + pub(crate) record_field_type: RecordFieldType, + pub(crate) primary: bool, + pub(crate) nullable: bool, + pub(crate) sortable: bool, + pub(crate) searchable: bool, + pub(crate) queryable: bool, + // 当 record_field_type 为 List 时,list_type 表示 List 的类型 + pub(crate) list_type: RecordFieldType, + // 当 record_field_type 或 list_type 为 Reference 类型时,该字段表示被引用的 RecordType + pub(crate) reference_type: String +} + +pub(crate) struct SchemaNode { + pub(crate) record_type: String, + pub(crate) table_name: String, + pub(crate) fields: HashMap, + pub(crate) dup_check_fields: Vec, +} + +pub(crate) struct OrderTable { + pub(crate) record_type: String, + pub(crate) table_name: String, +} + +#[derive(Default)] +pub(crate) struct Schema { + pub(crate) version: i32, + pub(crate) record_types: HashMap, + pub(crate) schema_data: String, + pub(crate) order_tables: Vec, +} + +#[derive(Default)] +pub(crate) struct AppInfo { + pub(crate) bundle_name: String, + pub(crate) id: String, + pub(crate) fingerprint: String, + pub(crate) schema: Schema, } -pub enum CloudStatus { +/************** All UserInfo struct-related structures and methods **************/ + +pub(crate) enum CloudStatus { Unknown, UnActivated, Login, Logout, } -pub enum SpaceStatus { + +impl Default for CloudStatus { + fn default() -> Self { + Self::Unknown + } +} + +pub(crate) enum SpaceStatus { Normal, Full, } -pub struct UserInfo { + +impl Default for SpaceStatus { + fn default() -> Self { + Self::Normal + } +} + +#[derive(Default)] +pub(crate) struct UserInfo { account_id: String, cloud_status: CloudStatus, space_status: SpaceStatus, total_space: u64, left_space: u64, - // And more... } -pub struct Connect { +/************** All Connect struct-related structures and methods **************/ + +pub(crate) struct Connect { infos: HashMap, } impl Connect { - pub fn new() -> Self { - todo!() + /// Creates a structure `Connect` for connecting to the cloud and getting various information about the cloud. + pub(crate) fn new() -> Self { + Self { + infos: HashMap::default(), + } } - pub fn get(user_id: i32) -> Infos { - todo!() + /// Finds the corresponding `Infos` by the specified user_id. + pub(crate) fn get(&mut self, user_id: i32) -> &mut Infos { + if !self.infos.contains_key(&user_id) { + self.infos.insert(user_id, Infos::default()); + } + self.infos.get_mut(&user_id).unwrap() } } -pub struct Infos { - app_infos: HashMap, - user_infos: UserInfo, +#[derive(Default)] +pub(crate) struct Infos { + app_infos: Mutex>, + user_infos: Mutex, } impl Infos { - pub fn get_app_info(&self) -> &AppInfo { - todo!() + /// Sends `MsgParcel` to the cloud, + /// wait for the return value from the cloud, + /// get the required app infos from the cloud. + pub(crate) fn get_app_info(&mut self, bundle_names: Vec<&str>) -> Result<&HashMap, Error> { + let mut lock = self.app_infos.lock().unwrap(); + + let mut msg_parcel = MsgParcel::new().ok_or(Error::CreateMsgParcelFailed)?; + for bundle_name in bundle_names { + // TODO! 这里和云端确认顺序 + msg_parcel.write(bundle_name).map_err(|_| Error::WriteMsgParcelFailed)?; + } + + // TODO! 这里和云端确认调用的函数编码 + let function_number = 0; + let remote_obj = get_context_object().ok_or(Error::GetProxyObjectFailed)?; + let mut receive = remote_obj.send_request(function_number, &msg_parcel, false).map_err(|_| Error::SendRequestFailed)?; + + // TODO! 这里和云端确认顺序,从 receive 中取出对应的数据,并设置到当前结构体中 + + drop(lock); + + Ok(self.app_infos.get_mut().unwrap()) } - pub fn get_user_info(&self) -> &UserInfo { - todo!() + /// Sends `MsgParcel` to the cloud, + /// wait for the return value from the cloud, + /// get the required user info from the cloud. + pub(crate) fn get_user_info(&mut self) -> Result<&UserInfo, Error> { + let mut lock = self.user_infos.lock(); + + let mut msg_parcel = MsgParcel::new().ok_or(Error::CreateMsgParcelFailed)?; + // TODO! 这里啥都不填?全靠 function_number? + + // TODO! 这里和云端确认调用的函数编码 + let function_number = 0; + let remote_obj = get_context_object().ok_or(Error::CreateMsgParcelFailed)?; + let mut receive = remote_obj.send_request(function_number, &msg_parcel, false).map_err(|_| Error::SendRequestFailed)?; + + // TODO! 这里和云端确认顺序,从 receive 中取出对应的数据,并设置到当前结构体中 + + drop(lock); + + Ok(self.user_infos.get_mut().unwrap()) } } -pub struct RecordResponse { +/************** All Database struct-related structures and methods **************/ + +pub(crate) struct RecordResponse { time: i64, device_name: String, app_id: String, } -pub enum OperationType { +pub(crate) enum OperationType { None, Add, Update, Delete, } -pub struct Asset { +pub(crate) struct Asset { uri: String, asset_name: String, operation_type: OperationType, @@ -91,12 +209,12 @@ pub struct Asset { sub_path: String, } -pub struct Reference { +pub(crate) struct Reference { record_id: String, record_type: String, } -pub enum RecordField { +pub(crate) enum RecordField { Null(), Int(i64), Double(f64), @@ -109,7 +227,7 @@ pub enum RecordField { Reference(Reference), } -pub struct Record { +pub(crate) struct Record { record_id: String, record_type: String, create_info: RecordResponse, @@ -121,47 +239,54 @@ pub struct Record { } impl Record { - pub fn get_record_id(&self) -> &str { + pub(crate) fn record_id(&self) -> &str { self.record_id.as_str() } - pub fn get_record_type(&self) -> &str { + pub(crate) fn record_type(&self) -> &str { self.record_type.as_str() } - pub fn get_create_info(&self) -> &RecordResponse { + pub(crate) fn create_info(&self) -> &RecordResponse { &self.create_info } - pub fn get_modified_info(&self) -> &RecordResponse { + pub(crate) fn modified_info(&self) -> &RecordResponse { &self.modified_info } - pub fn get_record_data(&self) -> &HashMap { + pub(crate) fn record_data(&self) -> &HashMap { &self.record_data } - pub fn get_is_delete(&self) -> bool { + pub(crate) fn is_delete(&self) -> bool { self.is_delete } - pub fn get_version(&self) -> u32 { + pub(crate) fn version(&self) -> u32 { self.version } - pub fn get_is_new_create(&self) -> bool { + pub(crate) fn is_new_create(&self) -> bool { self.is_new_create } - - // And more... } -pub struct Lock { +pub(crate) struct Lock { interval: i32, session_id: String, } -pub struct Database { +pub(crate) struct RecordIdRule { + rule_id: u32, + +} + +pub(crate) struct Database { + bundle_name: String, + schema: Schema, + record_id_rule: RecordIdRule, + is_sync_id_rule: bool, // And more... } diff --git a/src/cloud_extension/src/ipc_conn/error.rs b/src/cloud_extension/src/ipc_conn/error.rs index 8b13789..bfbd537 100644 --- a/src/cloud_extension/src/ipc_conn/error.rs +++ b/src/cloud_extension/src/ipc_conn/error.rs @@ -1 +1,23 @@ +use std::fmt::{Debug, Display, Formatter}; +#[derive(Debug)] +/// Indicates IPC connection and data acquisition errors. +pub enum Error { + CreateMsgParcelFailed, + WriteMsgParcelFailed, + GetProxyObjectFailed, + SendRequestFailed, +} + +impl Display for Error { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Error::CreateMsgParcelFailed => write!(f, "Creates MsgParcel Failed"), + Error::WriteMsgParcelFailed => write!(f, "Writes MsgParcel Failed"), + Error::GetProxyObjectFailed => write!(f, "Gets Proxy Object Failed"), + Error::SendRequestFailed => write!(f, "Sends Request Failed"), + } + } +} + +impl std::error::Error for Error {} diff --git a/src/cloud_extension/src/lib.rs b/src/cloud_extension/src/lib.rs index d7159e1..4671bc3 100644 --- a/src/cloud_extension/src/lib.rs +++ b/src/cloud_extension/src/lib.rs @@ -1,9 +1,18 @@ +// Copyright (c) 2023 Huawei Device Co., Ltd. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #[cfg(feature = "c_adapter")] pub mod c_adapter; -mod ipc_conn; -mod service_impl; - -pub use service_impl::*; -// TODO: 可能改名 -pub use ipc_conn::IpcConfig; +pub mod ipc_conn; +pub mod service_impl; \ No newline at end of file -- Gitee