1 /*
2 * Copyright (c) 2024 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 FILEMANAGEMENT_DFS_SERVICE_CONNECT_COUNT_H
17 #define FILEMANAGEMENT_DFS_SERVICE_CONNECT_COUNT_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <unordered_set>
24 #include <vector>
25 
26 #include "ipc/i_file_dfs_listener.h"
27 
28 namespace OHOS {
29 namespace Storage {
30 namespace DistributedFile {
31 struct Connect {
ConnectConnect32     Connect(uint32_t callingTokenId, const std::string &networkId, int32_t num, const sptr<IFileDfsListener> &listener)
33         : callingTokenId(callingTokenId), networkId(networkId), num(num), listener(listener) {}
34 
35     uint32_t callingTokenId;
36     std::string networkId;
37     int32_t num;
38     sptr<IFileDfsListener> listener;
39 };
40 
41 class ConnectCount final {
42 public:
43     ConnectCount() = default;
44     ~ConnectCount() = default;
45     static std::shared_ptr<ConnectCount> GetInstance();
46 
47     void AddConnect(uint32_t callingTokenId, const std::string &networkId, sptr<IFileDfsListener> &listener);
48     bool CheckCount(const std::string &networkId);
49     void RemoveAllConnect();
50     std::vector<std::string> RemoveConnect(uint32_t callingTokenId);
51     void RemoveConnect(const std::string &networkId);
52     void RemoveConnect(uint32_t callingTokenId, const std::string &networkId);
53     std::vector<std::string> GetNetworkIds(uint32_t callingTokenId);
54     void NotifyRemoteReverseObj(const std::string &networkId, int32_t status);
55     int32_t FindCallingTokenIdForListerner(const sptr<IRemoteObject> &listener, uint32_t &callingTokenId);
56 
57 private:
58     static std::shared_ptr<ConnectCount> instance_;
59     std::recursive_mutex connectMutex_;
60     std::unordered_set<std::shared_ptr<Connect>> connectList_;
61 };
62 } // namespace DistributedFile
63 } // namespace Storage
64 } // namespace OHOS
65 #endif // FILEMANAGEMENT_DFS_SERVICE_CONNECT_COUNT_H
66