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