• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 NETWORK_AGENT_TEMPLATE_H
17 #define NETWORK_AGENT_TEMPLATE_H
18 
19 #include <list>
20 #include <memory>
21 #include <mutex>
22 #include <vector>
23 #include <string_view>
24 
25 #include "device/device_info.h"
26 #include "dfsu_actor.h"
27 #include "dfsu_startable.h"
28 #include "dfsu_thread.h"
29 #include "mountpoint/mount_point.h"
30 #include "network/kernel_talker.h"
31 #include "network/session_pool.h"
32 #include "network/softbus/softbus_session_dispatcher.h"
33 #include "nlohmann/json.hpp"
34 #include "utils_directory.h"
35 
36 namespace OHOS {
37 namespace Storage {
38 namespace DistributedFile {
39 constexpr uint8_t LINK_TYPE_AP = 1;
40 constexpr uint8_t LINK_TYPE_P2P = 2;
41 constexpr std::string_view GROUP_TYPE_AP = "hmdfs_WifiGroup";
42 constexpr std::string_view GROUP_TYPE_P2P = "hmdfs_P2PGroup";
43 
44 class NetworkAgentTemplate : public DfsuStartable, public DfsuActor<NetworkAgentTemplate> {
45 public:
NetworkAgentTemplate(std::weak_ptr<MountPoint> mountPoint)46     explicit NetworkAgentTemplate(std::weak_ptr<MountPoint> mountPoint)
47         : DfsuActor<NetworkAgentTemplate>(this),
48           mountPoint_(mountPoint),
49           kernerlTalker_(std::make_shared<KernelTalker>(
50               mountPoint,
51               [&](NotifyParam &param) { GetSessionProcess(param); },
52               [&](int32_t fd) { CloseSessionForOneDevice(fd); })),
53           sessionPool_(kernerlTalker_)
54     {
55     }
~NetworkAgentTemplate()56     virtual ~NetworkAgentTemplate() {}
57     void Start();
58     void Stop();
59     void ConnectOnlineDevices();
60     void DisconnectAllDevices();
61     void ConnectDeviceAsync(const DeviceInfo info);
62     void DisconnectDevice(const DeviceInfo info);
63     void DisconnectDeviceByP2P(const DeviceInfo info);
64     void DisconnectDeviceByP2PHmdfs(const DeviceInfo info);
65     void OccupySession(int32_t sessionId, uint8_t linkType);
66     bool FindSession(int32_t sessionId);
67     void AcceptSession(std::shared_ptr<BaseSession> session, const std::string backStage);
68     void ConnectDeviceByP2PAsync(const DeviceInfo info);
GetMountPoint()69     std::shared_ptr<MountPoint> GetMountPoint()
70     {
71         return mountPoint_.lock();
72     };
73 protected:
74     virtual void JoinDomain() = 0;
75     virtual void QuitDomain() = 0;
76     virtual void StopTopHalf() = 0;
77     virtual void StopBottomHalf() = 0;
78     virtual int32_t OpenSession(const DeviceInfo &info, const uint8_t &linkType) = 0;
79     virtual void OpenApSession(const DeviceInfo &info, const uint8_t &linkType) = 0;
80     virtual void CloseSession(std::shared_ptr<BaseSession> session) = 0;
81 
82     std::weak_ptr<MountPoint> mountPoint_;
83 
84 private:
85     void HandleAllNotify(int fd);
86     void NotifyHandler(NotifyParam &param);
87     void GetSessionProcess(NotifyParam &param);
88     void GetSession(const std::string &cid, uint8_t linkType);
89     void CloseSessionForOneDevice(int32_t fd);
90     void GetSessionProcessInner(NotifyParam param);
91 
92     std::mutex taskMut_;
93     std::list<Utils::DfsuThread> tasks_;
94     std::shared_ptr<KernelTalker> kernerlTalker_;
95     SessionPool sessionPool_;
96 };
97 } // namespace DistributedFile
98 } // namespace Storage
99 } // namespace OHOS
100 #endif // NETWORK_AGENT_TEMPLATE_H