diff --git a/interfaces/innerkits/native/BUILD.gn b/interfaces/innerkits/native/BUILD.gn index 42a2f13efad13aaea039b331e18598f3dc6b95dd..38da81cb9dae9bebf3e9fe44d9a4e50cb9c2ef1b 100644 --- a/interfaces/innerkits/native/BUILD.gn +++ b/interfaces/innerkits/native/BUILD.gn @@ -29,6 +29,12 @@ ohos_shared_library("libdistributedfile_innerkits") { "${distributedfile_path}/frameworks/native/src/service_proxy.cpp", ] + defines = [ + "LOG_TAG=\"distributedfile_innerkits\"", + "LOG_DOMAIN=0xD001600", + "LOG_LEVEL=INFO", + ] + configs = [ ":private_config" ] public_configs = [ ":public_config" ] diff --git a/services/distributedfileservice/include/device/device_manager_agent.h b/services/distributedfileservice/include/device/device_manager_agent.h index 82f024813e8a6fdcc7d849ceefc12f447b07b640..661bb7df8d3a55a6ed770a66e0e7672315892068 100644 --- a/services/distributedfileservice/include/device/device_manager_agent.h +++ b/services/distributedfileservice/include/device/device_manager_agent.h @@ -24,7 +24,9 @@ #include #include "device_manager.h" +#include "dfsu_actor.h" #include "dfsu_singleton.h" +#include "dfsu_startable.h" namespace OHOS { namespace Storage { @@ -32,10 +34,14 @@ namespace DistributedFile { class DeviceManagerAgent : public DistributedHardware::DmInitCallback, public DistributedHardware::DeviceStateCallback, public std::enable_shared_from_this, + public DfsuStartable, + public DfsuActor, public Utils::DfsuSingleton { DECLARE_SINGLETON(DeviceManagerAgent); public: + void Start() override; + void Stop() override; void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override {} @@ -46,7 +52,7 @@ public: { return alreadyOnlineDev_; } - std::vector GetRemoteDevicesInfo(); + std::vector GetRemoteDevicesInfo(); private: void StartInstance() override; diff --git a/services/distributedfileservice/include/network/softbus_agent.h b/services/distributedfileservice/include/network/softbus_agent.h index bde01d7c7fac63ba7a87f9e3024866a4d1d03c3e..6b816ee5c5b907f6f0d46d7210a641ac216f261d 100644 --- a/services/distributedfileservice/include/network/softbus_agent.h +++ b/services/distributedfileservice/include/network/softbus_agent.h @@ -20,16 +20,23 @@ #include #include +#include "dfsu_actor.h" #include "dfsu_singleton.h" +#include "dfsu_startable.h" #include "i_filetransfer_callback.h" namespace OHOS { namespace Storage { namespace DistributedFile { -class SoftbusAgent : public std::enable_shared_from_this, public Utils::DfsuSingleton { +class SoftbusAgent : public std::enable_shared_from_this, + public Utils::DfsuSingleton, + public DfsuStartable, + public DfsuActor { DECLARE_SINGLETON(SoftbusAgent); public: + void Start() override; + void Stop() override; void RegisterSessionListener(); void RegisterFileListener(); void UnRegisterSessionListener(); @@ -39,6 +46,7 @@ public: void OnSessionOpened(const int sessionId, const int result); void OnSessionClosed(int sessionId); int SendFile(const std::string &cid, const char *sFileList[], const char *dFileList[], uint32_t fileCnt); + void CallFileSend(const int sessionId, const char *sFileList[], const char *dFileList[], uint32_t fileCnt); void OnSendFileFinished(const int sessionId, const std::string firstFile); void OnFileTransError(const int sessionId); void OnReceiveFileFinished(const int sessionId, const std::string files, int fileCnt); @@ -48,7 +56,7 @@ public: protected: void StartInstance() override; void StopInstance() override; - void OpenSession(const std::string &cid); + void OpenSession(const std::string cid); void CloseSession(const std::string &cid); std::string GetPeerDevId(const int sessionId); diff --git a/services/distributedfileservice/src/device/device_manager_agent.cpp b/services/distributedfileservice/src/device/device_manager_agent.cpp index 9dd1b4f7fb346662699673348e21c8e6b7cd9d0e..f09fb800c5934d7bf7c164651f8adf4b3ae5556c 100644 --- a/services/distributedfileservice/src/device/device_manager_agent.cpp +++ b/services/distributedfileservice/src/device/device_manager_agent.cpp @@ -22,18 +22,35 @@ namespace OHOS { namespace Storage { namespace DistributedFile { -DeviceManagerAgent::DeviceManagerAgent() {} +// constexpr int MAX_RETRY_COUNT = 7; +DeviceManagerAgent::DeviceManagerAgent() : DfsuActor(this, std::numeric_limits::max()) {} DeviceManagerAgent::~DeviceManagerAgent() { StopInstance(); } + void DeviceManagerAgent::StartInstance() +{ + StartActor(); +} + +void DeviceManagerAgent::StopInstance() +{ + StopActor(); +} + +void DeviceManagerAgent::Start() { // the time sequence can ensure there is no resource competition alreadyOnlineDev_.clear(); try { RegisterToExternalDm(); + auto infos = GetRemoteDevicesInfo(); + LOGI("Have %{public}d devices Online", infos.size()); + for (const auto &info : infos) { + OnDeviceOnline(info); + } } catch (const DfsuException &e) { // do not throw exception } catch (const std::exception &e) { @@ -41,7 +58,7 @@ void DeviceManagerAgent::StartInstance() } } -void DeviceManagerAgent::StopInstance() +void DeviceManagerAgent::Stop() { try { UnregisterFromExternalDm(); @@ -52,6 +69,19 @@ void DeviceManagerAgent::StopInstance() } } +std::vector DeviceManagerAgent::GetRemoteDevicesInfo() +{ + std::string extra = ""; + std::vector deviceList; + + auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); + int errCode = deviceManager.GetTrustedDeviceList(DistributedFileService::pkgName_, extra, deviceList); + if (errCode) { + ThrowException(errCode, "Failed to get info of remote devices"); + } + return deviceList; +} + void DeviceManagerAgent::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) { std::unique_lock lock(devsRecordMutex_); diff --git a/services/distributedfileservice/src/network/softbus_agent.cpp b/services/distributedfileservice/src/network/softbus_agent.cpp index 55f0fa95ee57e9a7f6a7abe490405f608303eb07..61078f5556bcf0c93d71d263e640608dfa18f85e 100644 --- a/services/distributedfileservice/src/network/softbus_agent.cpp +++ b/services/distributedfileservice/src/network/softbus_agent.cpp @@ -37,11 +37,12 @@ namespace DistributedFile { namespace { constexpr int32_t SOFTBUS_OK = 0; constexpr int32_t DEVICE_ID_SIZE_MAX = 65; + constexpr int MAX_RETRY_COUNT = 7; constexpr int32_t IS_CLIENT = 1; const std::string DEFAULT_ROOT_PATH = "/data/service/el2/100/hmdfs/non_account/data/"; } -SoftbusAgent::SoftbusAgent() {} +SoftbusAgent::SoftbusAgent() : DfsuActor(this, std::numeric_limits::max()) {} SoftbusAgent::~SoftbusAgent() { @@ -50,11 +51,27 @@ SoftbusAgent::~SoftbusAgent() void SoftbusAgent::StartInstance() { - RegisterSessionListener(); - RegisterFileListener(); + StartActor(); } void SoftbusAgent::StopInstance() +{ + StopActor(); +} + +void SoftbusAgent::Start() +{ + try { + RegisterSessionListener(); + RegisterFileListener(); + } catch (const DfsuException &e) { + // do not throw exception + } catch (const std::exception &e) { + // do not throw exception + } +} + +void SoftbusAgent::Stop() { { std::unique_lock lock(sessionMapMux_); @@ -64,7 +81,13 @@ void SoftbusAgent::StopInstance() } } getSessionCV_.notify_all(); - UnRegisterSessionListener(); + try { + UnRegisterSessionListener(); + } catch (const DfsuException &e) { + // do not throw exception + } catch (const std::exception &e) { + // do not throw exception + } } void SoftbusAgent::OnDeviceOnline(const std::string &cid) @@ -89,33 +112,54 @@ int SoftbusAgent::SendFile(const std::string &cid, const char *sFileList[], cons // build socket synchronously if (sessionId == -1) { - OpenSession(cid); - // wait for get sessionId - LOGD("openSession, wait"); - std::unique_lock lock(getSessionCVMut_); - getSessionCV_.wait(lock, [this, cid]() { return !cidToSessionID_[cid].empty(); }); - LOGD("openSession success, wakeup"); - if (cidToSessionID_[cid].empty()) { // wakeup check - LOGE("there is no sessionId of cid:%{public}s", cid.c_str()); - return -1; - } - sessionId = cidToSessionID_[cid].front(); + auto cmd = std::make_unique>(&SoftbusAgent::OpenSession, cid); + cmd->UpdateOption({ + .tryTimes_ = MAX_RETRY_COUNT, + }); + Recv(move(cmd)); } - int ret = ::SendFile(sessionId, sFileList, dFileList, fileCnt); - if (ret != 0) { + std::unique_lock lock(getSessionCVMut_); + getSessionCV_.wait(lock, [this, cid]() { return !cidToSessionID_[cid].empty(); }); + + if (cidToSessionID_[cid].empty()) { + LOGE("there is no sessionId of cid:%{public}s", cid.c_str()); return -1; } - return 0; + sessionId = cidToSessionID_[cid].front(); + + int result = ::SendFile(sessionId, sFileList, dFileList, fileCnt); + LOGD("SoftbusAgent::SendFile, result %{public}d", result); + if (result != 0) { + auto cmd = std::make_unique> + (&SoftbusAgent::CallFileSend, sessionId, sFileList, dFileList, fileCnt); + cmd->UpdateOption({ + .tryTimes_ = MAX_RETRY_COUNT, + }); + Recv(move(cmd)); + return result; + } + + return result; +} + +void SoftbusAgent::CallFileSend(const int sessionId, const char *sFileList[], const char *dFileList[], uint32_t fileCnt) +{ + int result = ::SendFile(sessionId, sFileList, dFileList, fileCnt); + if (result != 0) { + ThrowException(ERR_SOFTBUS_AGENT_ON_SESSION_OPENED_FAIL, "softbus sendfile failed, retry..."); + } } -void SoftbusAgent::OpenSession(const std::string &cid) +void SoftbusAgent::OpenSession(const std::string cid) { + LOGD("SoftbusAgent::OpenSession, deviceId %{public}s", cid.c_str()); SessionAttribute attr; attr.dataType = TYPE_FILE; // files use UDP, CHANNEL_TYPE_UDP int sessionId = ::OpenSession(sessionName_.c_str(), sessionName_.c_str(), cid.c_str(), "DFS_wifiGroup", &attr); if (sessionId < 0) { - return; + LOGE("Failed to open session, cid:%{public}s", cid.c_str()); + ThrowException(ERR_SOFTBUS_AGENT_ON_SESSION_OPENED_FAIL, "Open Session failed"); } } @@ -154,6 +198,7 @@ void SoftbusAgent::OnSendFileFinished(const int sessionId, const std::string fir LOGE("OnSendFileFinished Nofify pointer is empty"); return; } + LOGI("SoftbusAgent::OnSendFileFinished sessionId = %{public}d, %{public}s", sessionId, firstFile.c_str()); std::string cid = GetPeerDevId(sessionId); notifyCallback_->SendFinished(cid, firstFile); } @@ -164,6 +209,7 @@ void SoftbusAgent::OnFileTransError(const int sessionId) LOGE("OnFileTransError Nofify pointer is empty"); return; } + LOGI("SoftbusAgent::OnFileTransError %{public}d", sessionId); std::string cid = GetPeerDevId(sessionId); if (::GetSessionSide(sessionId) == IS_CLIENT) { notifyCallback_->SendError(cid); @@ -288,7 +334,7 @@ void SoftbusAgent::RegisterFileListener() throw std::runtime_error(ss.str()); } LOGD("Succeed to SetFileSendListener, pkgName %{public}s, sbusName:%{public}s", pkgName.c_str(), - sessionName_.c_str()); + sessionName_.c_str()); IFileReceiveListener fileRecvListener = { .OnReceiveFileFinished = SoftbusDispatcher::OnReceiveFileFinished, @@ -303,7 +349,7 @@ void SoftbusAgent::RegisterFileListener() throw std::runtime_error(ss.str()); } LOGD("Succeed to SetFileReceiveListener, pkgName %{public}s, sbusName:%{public}s", pkgName.c_str(), - sessionName_.c_str()); + sessionName_.c_str()); } void SoftbusAgent::UnRegisterSessionListener()