1 /*
2  * Copyright (c) 2023-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_SOFTBUS_HANDLER_H
17 #define FILEMANAGEMENT_DFS_SERVICE_SOFTBUS_HANDLER_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 
25 #include "transport/socket.h"
26 #include "transport/trans_type.h"
27 
28 namespace OHOS {
29 namespace Storage {
30 namespace DistributedFile {
31 typedef enum {
32     DFS_CHANNLE_ROLE_SOURCE = 0,
33     DFS_CHANNLE_ROLE_SINK = 1,
34 } DFS_CHANNEL_ROLE;
35 class SoftBusHandler {
36 public:
37     SoftBusHandler();
38     ~SoftBusHandler();
39     static SoftBusHandler &GetInstance();
40     int32_t CreateSessionServer(const std::string &packageName, const std::string &sessionName,
41         DFS_CHANNEL_ROLE role, const std::string physicalPath);
42     int32_t OpenSession(const std::string &mySessionName, const std::string &peerSessionName,
43         const std::string &peerDevId, int32_t &socketId);
44     int32_t CopySendFile(int32_t socketId,
45                          const std::string &sessionName,
46                          const std::string &srcUri,
47                          const std::string &dstUri);
48 
49     void ChangeOwnerIfNeeded(int32_t sessionId, const std::string sessionName);
50     void CloseSession(int32_t sessionId, const std::string sessionName);
51     void CloseSessionWithSessionName(const std::string sessionName);
52     static std::string GetSessionName(int32_t sessionId);
53     static void OnSinkSessionOpened(int32_t sessionId, PeerSocketInfo info);
54     static bool IsSameAccount(const std::string &networkId);
55     void RemoveNetworkId(int32_t socketId);
56     void CopyOnStop(const std::string &peerNetworkId);
57 
58 private:
59     bool CreatSocketId(const std::string &mySessionName, const std::string &peerSessionName,
60         const std::string &peerDevId, int32_t &socketId);
61     std::vector<int32_t> GetsocketIdFromPeerNetworkId(const std::string &peerNetworkId);
62     bool IsService(std::string &sessionName);
63 
64     std::mutex socketMutex_;
65     static std::mutex clientSessNameMapMutex_;
66     static std::map<int32_t, std::string> clientSessNameMap_;
67     static std::mutex serverIdMapMutex_;
68     static std::map<std::string, int32_t> serverIdMap_;
69     static inline const std::string SERVICE_NAME{"ohos.storage.distributedfile.daemon"};
70     std::map<DFS_CHANNEL_ROLE, ISocketListener> sessionListener_;
71 
72     static std::mutex networkIdMapMutex_;
73     static std::map<int32_t, std::string> networkIdMap_;
74 };
75 } // namespace DistributedFile
76 } // namespace Storage
77 } // namespace OHOS
78 
79 #endif // FILEMANAGEMENT_DFS_SERVICE_SOFTBUS_HANDLER_H