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_example4.h"
17 
18 #include "event.h"
19 #include "event_source_example.h"
20 #include "plugin_factory.h"
21 
22 namespace OHOS {
23 namespace HiviewDFX {
24 REGISTER_PROXY_WITH_LOADED(EventProcessorExample4);
EventProcessorExample4()25 EventProcessorExample4::EventProcessorExample4()
26 {
27     std::unique_lock<std::mutex> lock(EventSourceExample::mutex_);
28     printf("EventProcessorExample4::EventProcessorExample4()\n");
29     EventSourceExample::count.insert("EventProcessorExample4");
30 }
31 
~EventProcessorExample4()32 EventProcessorExample4::~EventProcessorExample4()
33 {
34     std::unique_lock<std::mutex> lock(EventSourceExample::mutex_);
35     printf("EventProcessorExample4::~EventProcessorExample4()\n");
36     EventSourceExample::count.erase("EventProcessorExample4");
37 }
38 
CanProcessEvent(std::shared_ptr<Event> event)39 bool EventProcessorExample4::CanProcessEvent(std::shared_ptr<Event> event)
40 {
41     if (event->messageType_ == Event::MessageType::FAULT_EVENT &&
42         event->eventId_ == EventSourceExample::PIPELINE_EVENT_ID_BBB) {
43         printf("EventProcessorExample4 CanProcessEvent true.\n");
44         return true;
45     }
46     if (event->messageType_ == Event::MessageType::FAULT_EVENT &&
47         event->eventName_ == "testbb") {
48         printf("EventProcessorExample4 CanProcessEvent true.\n");
49         return true;
50     }
51     return false;
52 }
53 
OnEvent(std::shared_ptr<Event> & event)54 bool EventProcessorExample4::OnEvent(std::shared_ptr<Event>& event)
55 {
56     printf("EventProcessorExample4 cur ref:%ld \n", shared_from_this().use_count());
57     printf("EventProcessorExample4 OnEvent 0 tid:%d sender:%s.\n", gettid(), event->sender_.c_str());
58     if (event->eventId_ == EventSourceExample::PIPELINE_EVENT_ID_BBB) {
59         printf("EventProcessorExample4 process bbb event.\n");
60     }
61 
62     if (event->eventId_ == EventSourceExample::PIPELINE_EVENT_ID_AAA) {
63         printf("EventProcessorExample4 process aaa event.\n");
64         event->SetValue("Done", GetName());
65         if (GetHiviewContext() != nullptr) {
66             printf("EventProcessorExample4 PostUnorderedEvent aaa\n ");
67             GetHiviewContext()->PostUnorderedEvent(shared_from_this(), event);
68             if (!event->GetValue("Unload").empty()) {
69                 GetHiviewContext()->RequestUnloadPlugin(shared_from_this());
70                 printf("EventProcessorExample4 RequestUnloadPlugin aaa\n ");
71             }
72         }
73     }
74 
75     if (event->eventId_ == EventSourceExample::PIPELINE_EVENT_ID_CCC) {
76         printf("EventProcessorExample4 process ccc event.\n");
77         event->SetValue("Done", GetName());
78         if (GetHiviewContext() != nullptr) {
79             printf("EventProcessorExample4 PostUnorderedEvent ccc\n ");
80             GetHiviewContext()->PostUnorderedEvent(shared_from_this(), Event::Repack<Event, Event>(event, false));
81         }
82     }
83     if (GetHiviewContext() != nullptr) {
84         GetHiviewContext()->SetHiviewProperty("EPE4_OnEvent", "received : " + event->eventName_, true);
85     }
86     event->SetValue("EventProcessorExample4", "Done");
87     return true;
88 }
OnLoad()89 void EventProcessorExample4::OnLoad()
90 {
91     SetVersion("EventProcessorExample4.0");
92     printf("EventProcessorExample4 OnLoad \n");
93 }
94 
OnUnload()95 void EventProcessorExample4::OnUnload()
96 {
97     printf("EventProcessorExample4 OnUnload \n.");
98 }
99 
OnEventListeningCallback(const Event & msg)100 void EventProcessorExample4::OnEventListeningCallback(const Event &msg)
101 {
102     printf("EventProcessorExample4 OnEventListeningCallback.\n");
103     GetHiviewContext()->SetHiviewProperty("EPE4_Listening", "received : " + msg.eventName_, true);
104 }
105 } // namespace HiviewDFX
106 } // namespace OHOS