1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "base_producer.h"
17 #include "common/common_macro.h"
18 
19 namespace OHOS {
20 namespace Sharing {
21 
BaseProducer()22 BaseProducer::BaseProducer()
23 {
24     SHARING_LOGD("producerId: %{public}u.", GetId());
25 }
26 
~BaseProducer()27 BaseProducer::~BaseProducer()
28 {
29     SHARING_LOGD("producerId: %{public}u.", GetId());
30 }
31 
SetProducerListener(std::weak_ptr<IProducerListener> listener)32 void BaseProducer::SetProducerListener(std::weak_ptr<IProducerListener> listener)
33 {
34     SHARING_LOGD("trace.");
35     listener_ = listener;
36 }
37 
IsRunning()38 bool BaseProducer::IsRunning()
39 {
40     SHARING_LOGD("trace.");
41     return isRunning_;
42 }
43 
SetSrcAgentId(uint32_t agentId)44 void BaseProducer::SetSrcAgentId(uint32_t agentId)
45 {
46     SHARING_LOGD("trace.");
47     srcAgentId_ = agentId;
48 }
49 
GetSrcAgentId()50 uint32_t BaseProducer::GetSrcAgentId()
51 {
52     SHARING_LOGD("trace.");
53     return srcAgentId_;
54 }
55 
Notify(ProsumerStatusMsg::Ptr &statusMsg)56 void BaseProducer::Notify(ProsumerStatusMsg::Ptr &statusMsg)
57 {
58     SHARING_LOGD("trace.");
59     RETURN_IF_NULL(statusMsg);
60     statusMsg->prosumerId = GetId();
61     statusMsg->agentId = GetSrcAgentId();
62 
63     auto listener = listener_.lock();
64     if (listener) {
65         listener->OnProducerNotify(statusMsg);
66     }
67 }
68 
NotifyPrivateEvent(EventMsg::Ptr eventMsg)69 void BaseProducer::NotifyPrivateEvent(EventMsg::Ptr eventMsg)
70 {
71     SHARING_LOGD("trace.");
72     RETURN_IF_NULL(eventMsg);
73     auto pMsg = std::make_shared<ProsumerStatusMsg>();
74     pMsg->prosumerId = GetId();
75     pMsg->agentId = GetSrcAgentId();
76     pMsg->status = PROSUMER_NOTIFY_PRIVATE_EVENT;
77     pMsg->eventMsg = std::move(eventMsg);
78 
79     auto listener = listener_.lock();
80     if (listener) {
81         listener->OnProducerNotify(pMsg);
82     }
83 }
84 
85 } // namespace Sharing
86 } // namespace OHOS