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 #ifndef DISTRIBUTEDDATAMGR_COMMUNICATOR_CONTEXT_H 17 #define DISTRIBUTEDDATAMGR_COMMUNICATOR_CONTEXT_H 18 19 #include "app_device_change_listener.h" 20 #include "executor_pool.h" 21 #include "commu_types.h" 22 #include "concurrent_map.h" 23 #include "visibility.h" 24 25 namespace OHOS::DistributedData { 26 class API_EXPORT CommunicatorContext { 27 public: 28 using Status = OHOS::DistributedKv::Status; 29 using DevChangeListener = OHOS::AppDistributedKv::AppDeviceChangeListener; 30 using DeviceInfo = OHOS::AppDistributedKv::DeviceInfo; 31 using OnCloseAble = std::function<void(const std::string &deviceId)>; 32 33 API_EXPORT static CommunicatorContext &GetInstance(); 34 API_EXPORT void SetThreadPool(std::shared_ptr<ExecutorPool> executors); 35 std::shared_ptr<ExecutorPool> GetThreadPool(); 36 Status RegSessionListener(const DevChangeListener *observer); 37 Status UnRegSessionListener(const DevChangeListener *observer); 38 void NotifySessionReady(const std::string &deviceId, int32_t errCode); 39 void NotifySessionClose(const std::string &deviceId); 40 void SetSessionListener(const OnCloseAble &closeAbleCallback); 41 bool IsSessionReady(const std::string &deviceId); 42 43 private: 44 CommunicatorContext() = default; 45 ~CommunicatorContext() = default; 46 CommunicatorContext(CommunicatorContext const &) = delete; 47 void operator=(CommunicatorContext const &) = delete; 48 CommunicatorContext(CommunicatorContext &&) = delete; 49 CommunicatorContext &operator=(CommunicatorContext &&) = delete; 50 51 mutable std::mutex sessionMutex_; 52 OnCloseAble closeListener_; 53 std::shared_ptr<ExecutorPool> executors_; 54 std::mutex mutex_; 55 std::vector<const DevChangeListener *> observers_; 56 ConcurrentMap<const std::string, const std::string> devices_ {}; 57 }; 58 } // namespace OHOS::DistributedData 59 #endif // DISTRIBUTEDDATAMGR_COMMUNICATOR_CONTEXT_H