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