1 /*
2  * Copyright (c) 2021 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 "event_processor_example2.h"
17 
18 #include "event_source_example.h"
19 #include "pipeline.h"
20 #include "plugin_factory.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 REGISTER(EventProcessorExample2);
EventProcessorExample2()25 EventProcessorExample2::EventProcessorExample2()
26 {
27     std::unique_lock<std::mutex> lock(EventSourceExample::mutex_);
28     printf("EventProcessorExample2::EventProcessorExample2()\n");
29     EventSourceExample::count.insert("EventProcessorExample2");
30 }
31 
~EventProcessorExample2()32 EventProcessorExample2::~EventProcessorExample2()
33 {
34     std::unique_lock<std::mutex> lock(EventSourceExample::mutex_);
35     printf("EventProcessorExample2::~EventProcessorExample2()\n");
36     EventSourceExample::count.erase("EventProcessorExample2");
37 }
38 
CanProcessEvent(std::shared_ptr<Event> event)39 bool EventProcessorExample2::CanProcessEvent(std::shared_ptr<Event> event)
40 {
41     return false;
42 }
43 
OnEvent(std::shared_ptr<Event> & event)44 bool EventProcessorExample2::OnEvent(std::shared_ptr<Event>& event)
45 {
46     printf("EventProcessorExample2 OnEvent \n");
47     if (event->eventId_ == EventSourceExample::PIPELINE_EVENT_ID_BBB) {
48         printf("EventProcessorExample2 process bbb event.\n");
49     }
50 
51     if (event->eventId_ == EventSourceExample::PIPELINE_EVENT_ID_AAA) {
52         printf("EventProcessorExample2 process aaa event.\n");
53     }
54 
55     if (event->GetValue("Finish") == "EventProcessorExample2") {
56         printf("EventProcessorExample2 OnFinish.\n");
57         event->OnFinish();
58     }
59 
60     if (event->eventId_ == EventSourceExample::PIPELINE_EVENT_ID_CCC) {
61         auto repackEvent = PipelineEvent::RepackPipelineEvent<Event, PipelineEvent>(shared_from_this(),
62                                                                                     "NormalPipeline", event, false);
63         repackEvent->eventId_ = EventSourceExample::PIPELINE_EVENT_ID_AAA;
64         repackEvent->SetValue("Unload", "True");
65         printf("EventProcessorExample2 repack ccc event.\n");
66         repackEvent->OnContinue();
67         return true;
68     }
69     if (GetHiviewContext() != nullptr) {
70         GetHiviewContext()->SetHiviewProperty("EPE2_OnEvent", "received : " + event->eventName_, true);
71     }
72     event->SetValue("EventProcessorExample2", "Done");
73     return true;
74 }
OnLoad()75 void EventProcessorExample2::OnLoad()
76 {
77     SetVersion("EventProcessorExample2.0");
78     printf("EventProcessorExample2 OnLoad \n");
79     auto ptr = std::static_pointer_cast<EventProcessorExample2>(shared_from_this());
80     printf("register event listener %p \n", ptr.get());
81     AddListenerInfo(OHOS::HiviewDFX::Event::MessageType::RAW_EVENT);
82     GetHiviewContext()->RegisterUnorderedEventListener(ptr);
83 }
84 
OnUnload()85 void EventProcessorExample2::OnUnload()
86 {
87     printf("EventProcessorExample2 OnUnload \n");
88 }
89 
OnUnorderedEvent(const Event & msg)90 void EventProcessorExample2::OnUnorderedEvent(const Event &msg)
91 {
92     printf("EventProcessorExample2 OnUnorderedEvent.\n");
93     GetHiviewContext()->SetHiviewProperty("EPE2_Listening", "received : " + msg.eventName_, true);
94 }
95 
GetListenerName()96 std::string EventProcessorExample2::GetListenerName()
97 {
98     printf("EventProcessorExample2 GetListenerName \n");
99     return "EventProcessorExample2";
100 }
101 } // namespace HiviewDFX
102 } // namespace OHOS