• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "distributed_server.h"
16 
17 #include "socket.h"
18 #include "session.h"
19 #include "distributed_device_data.h"
20 #include "dm_device_info.h"
21 #include "ans_log_wrapper.h"
22 #include "distributed_socket.h"
23 
24 namespace OHOS {
25 namespace Notification {
26 
27 using namespace DistributedHardware;
28 
GetInstance()29 DistributedServer& DistributedServer::GetInstance()
30 {
31     static DistributedServer distributedServer;
32     return distributedServer;
33 }
34 
ReleaseServer()35 void DistributedServer::ReleaseServer()
36 {
37     ANS_LOGI("Release server socket %{public}d.", (int32_t)(serverSocket_.size()));
38     for (auto& item : serverSocket_) {
39         CloseSocket(item.second);
40     }
41     serverSocket_.clear();
42     std::lock_guard<ffrt::mutex> lock(serverLock_);
43     for (auto& item : peerSockets_) {
44         CloseSocket(item->socketId_);
45     }
46     peerSockets_.clear();
47     init.store(false);
48 }
49 
CheckServer()50 void DistributedServer::CheckServer()
51 {
52     InitServer(localDevice_.deviceId_, localDevice_.deviceType_);
53 }
54 
InitServer(const std::string & deviceId,uint16_t deviceType)55 int32_t DistributedServer::InitServer(const std::string &deviceId, uint16_t deviceType)
56 {
57     if (init.load()) {
58         ANS_LOGI("Server has inited %{public}d.", (int32_t)(serverSocket_.size()));
59         return 0;
60     }
61     int32_t socketId = 0;
62     localDevice_.deviceId_ = deviceId;
63     localDevice_.deviceType_ = deviceType;
64     int32_t ret = ServiceListen(ANS_SOCKET_CMD, ANS_SOCKET_PKG, TransDataType::DATA_TYPE_MESSAGE, socketId);
65     if (ret != ERR_OK) {
66         return ret;
67     }
68 
69     std::string key = std::to_string(TransDataType::DATA_TYPE_MESSAGE) + "_" + std::to_string(deviceType);
70     serverSocket_[key] = socketId;
71     // Not phone, create msg socket for receive notification
72     if (deviceType != DmDeviceType::DEVICE_TYPE_PHONE) {
73         ret = ServiceListen(ANS_SOCKET_MSG, ANS_SOCKET_PKG, TransDataType::DATA_TYPE_BYTES, socketId);
74         if (ret != ERR_OK) {
75             return ret;
76         }
77         std::string key = std::to_string(TransDataType::DATA_TYPE_BYTES) + "_" + std::to_string(deviceType);
78         serverSocket_[key] = socketId;
79     }
80     for (auto& item : serverSocket_) {
81         ANS_LOGI("InitServer %{public}s %{public}s %{public}d", StringAnonymous(deviceId).c_str(),
82             item.first.c_str(), item.second);
83     }
84     init.store(true);
85     return ERR_OK;
86 }
87 
OnBind(int32_t socket,PeerSocketInfo info)88 void DistributedServer::OnBind(int32_t socket, PeerSocketInfo info)
89 {
90     std::lock_guard<ffrt::mutex> lock(serverLock_);
91     std::shared_ptr<ConnectedSocketInfo> socketInfo = std::make_shared<ConnectedSocketInfo>();
92     socketInfo->pkgName_ = info.pkgName;
93     socketInfo->peerName_ = info.name;
94     socketInfo->networkId_ = info.networkId;
95     socketInfo->socketId_ = socket;
96     socketInfo->dataType_ = info.dataType;
97     peerSockets_.push_back(socketInfo);
98 }
99 
OnShutdown(int32_t socket,ShutdownReason reason)100 void DistributedServer::OnShutdown(int32_t socket, ShutdownReason reason)
101 {
102     std::lock_guard<ffrt::mutex> lock(serverLock_);
103     for (auto socketInfo = peerSockets_.begin(); socketInfo != peerSockets_.end();
104         socketInfo++) {
105         if ((*socketInfo)->socketId_ == socket) {
106             peerSockets_.erase(socketInfo);
107             break;
108         }
109     }
110 }
111 
OnBytes(int32_t socket,const void * data,uint32_t dataLen)112 void DistributedServer::OnBytes(int32_t socket, const void *data, uint32_t dataLen)
113 {
114     DistributedService::GetInstance().OnReceiveMsg(data, dataLen);
115     ANS_LOGI("Distributed server On bytes %{public}d %{public}d", (int32_t)(dataLen), socket);
116 }
117 
OnMessage(int32_t socket,const void * data,uint32_t dataLen)118 void DistributedServer::OnMessage(int32_t socket, const void *data, uint32_t dataLen)
119 {
120     DistributedService::GetInstance().OnReceiveMsg(data, dataLen);
121     ANS_LOGI("Distributed server On message %{public}d %{public}d", (int32_t)(dataLen), socket);
122 }
123 }
124 }
125