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