1 /*
2 * Copyright (c) 2022 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 #include "distributed_object_adapter.h"
17 #include "avsession_log.h"
18 #include "avsession_errors.h"
19 #include "objectstore_errors.h"
20
21 namespace OHOS::AVSession {
AVSessionObjectWatcher(const ObjectCallbackType & callback)22 DistributedObjectAdapter::AVSessionObjectWatcher::AVSessionObjectWatcher(const ObjectCallbackType &callback)
23 {
24 callback_ = callback;
25 }
26
OnChanged(const std::string & name,const std::vector<std::string> & keys)27 void DistributedObjectAdapter::AVSessionObjectWatcher::OnChanged(const std::string &name,
28 const std::vector<std::string> &keys)
29 {
30 if (callback_) {
31 callback_(keys);
32 }
33 }
34
AVSessionStatusNotifier(const ObjectDisconnectNotifier & notifier)35 DistributedObjectAdapter::AVSessionStatusNotifier::AVSessionStatusNotifier(const ObjectDisconnectNotifier ¬ifier)
36 {
37 notifier_ = notifier;
38 }
39
OnChanged(const std::string & name,const std::string & networkId,const std::string & onlineStatus)40 void DistributedObjectAdapter::AVSessionStatusNotifier::OnChanged(const std::string &name,
41 const std::string &networkId,
42 const std::string &onlineStatus)
43 {
44 SLOGI("%{public}.3s %{public}s", networkId.c_str(), onlineStatus.c_str());
45 if (onlineStatus == "offline" && notifier_) {
46 notifier_(networkId);
47 }
48 }
49
DistributedObjectAdapter(const std::string & name)50 DistributedObjectAdapter::DistributedObjectAdapter(const std::string& name)
51 : name_(name)
52 {
53 SLOGI("construct");
54 }
55
~DistributedObjectAdapter()56 DistributedObjectAdapter::~DistributedObjectAdapter()
57 {
58 SLOGI("destroy");
59 if (object_ != nullptr) {
60 ObjectStore::DistributedObjectStore::GetInstance(AVSESSION_NAME)->DeleteObject(name_);
61 }
62 }
63
CreateObject()64 int32_t DistributedObjectAdapter::CreateObject()
65 {
66 object_ = ObjectStore::DistributedObjectStore::GetInstance(AVSESSION_NAME)->CreateObject(name_);
67 if (object_ == nullptr) {
68 SLOGE("failed");
69 return AVSESSION_ERROR;
70 }
71 SLOGI("success");
72 return AVSESSION_SUCCESS;
73 }
74
PutData(const std::string & key,std::vector<uint8_t> & data)75 int32_t DistributedObjectAdapter::PutData(const std::string &key, std::vector<uint8_t> &data)
76 {
77 if (object_ == nullptr) {
78 SLOGE("object is nullptr");
79 return AVSESSION_ERROR;
80 }
81
82 return object_->PutComplex(key, data) == ObjectStore::SUCCESS ? AVSESSION_SUCCESS : AVSESSION_ERROR;
83 }
84
GetData(const std::string & key,std::vector<uint8_t> & data)85 int32_t DistributedObjectAdapter::GetData(const std::string &key, std::vector<uint8_t> &data)
86 {
87 if (object_ == nullptr) {
88 SLOGE("object is nullptr");
89 return AVSESSION_ERROR;
90 }
91
92 return object_->GetComplex(key, data) == ObjectStore::SUCCESS ? AVSESSION_SUCCESS : AVSESSION_ERROR;
93 }
94
SetCallback(const ObjectCallbackType & callback)95 void DistributedObjectAdapter::SetCallback(const ObjectCallbackType &callback)
96 {
97 if (object_ == nullptr) {
98 SLOGE("object is nullptr");
99 return;
100 }
101 ObjectStore::DistributedObjectStore::GetInstance()->Watch(
102 object_, std::make_shared<AVSessionObjectWatcher>(callback));
103 }
104
SetDisconnectNotifier(const ObjectDisconnectNotifier & notifer)105 void DistributedObjectAdapter::SetDisconnectNotifier(const ObjectDisconnectNotifier ¬ifer)
106 {
107 if (object_ == nullptr) {
108 SLOGE("object is nullptr");
109 return;
110 }
111 ObjectStore::DistributedObjectStore::GetInstance()->SetStatusNotifier(
112 std::make_shared<AVSessionStatusNotifier>(notifer));
113 }
114 }