• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef HIVIEW_BASE_EVENT_H
16 #define HIVIEW_BASE_EVENT_H
17 #include <iostream>
18 #include <list>
19 #include <map>
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <string>
24 #include <sys/types.h>
25 #include "defines.h"
26 
27 #include "base/raw_data.h"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 class DllExport Event : public std::enable_shared_from_this<Event> {
32 public:
Event(const std::string & sender)33     explicit Event(const std::string &sender)
34         : messageType_(MessageType::NONE),
35           processType_(ManageType::UNORDERED),
36           what_(0),
37           eventId_(0),
38           happenTime_(0),
39           targetDispatchTime_(0),
40           createTime_(0),
41           realtime_(0),
42           processTime_(0),
43           sender_(sender),
44           domain_(""),
45           eventName_(""),
46           isPipeline_(false),
47           hasFinish_(false),
48           hasPending_(false),
49           traceId_(""),
50           spanId_(""),
51           parentSpanId_(""),
52           traceFlag_(""),
53           normalExtraInfo_(""),
54           rawData_(nullptr)
55     {
56         ResetTimestamp();
57     };
58 
Event(const std::string & sender,const std::string & name)59     explicit Event(const std::string &sender, const std::string &name)
60         : messageType_(MessageType::NONE),
61           processType_(ManageType::UNORDERED),
62           what_(0),
63           eventId_(0),
64           happenTime_(0),
65           targetDispatchTime_(0),
66           createTime_(0),
67           realtime_(0),
68           processTime_(0),
69           sender_(sender),
70           domain_(""),
71           eventName_(name),
72           isPipeline_(false),
73           hasFinish_(false),
74           hasPending_(false),
75           traceId_(""),
76           spanId_(""),
77           parentSpanId_(""),
78           traceFlag_(""),
79           normalExtraInfo_(""),
80           rawData_(nullptr)
81     {
82         ResetTimestamp();
83     };
84 
~Event()85     virtual ~Event() {};
86 
OnContinue()87     virtual bool OnContinue()
88     {
89         return true;
90     };
91 
OnFinish()92     virtual bool OnFinish()
93     {
94         hasFinish_ = true;
95         return true;
96     };
97 
OnRepack()98     virtual void OnRepack() {};
99 
OnPending()100     virtual void OnPending() {};
101 
GetPendingProcessorSize()102     virtual uint32_t GetPendingProcessorSize()
103     {
104         return 0;
105     }
106 
107     // call from audit module
108     // if you want to override this function
109     // you should call parent function first and append to your result
GetEventInfo()110     virtual std::string GetEventInfo()
111     {
112         return std::to_string(eventId_);
113     };
114 
115     // use for broadcasting events
116     enum MessageType {
117         NONE = 0,
118         PLUGIN_MAINTENANCE, // Reserved
119         FAULT_EVENT,
120         STATISTICS_EVENT,
121         RAW_EVENT,
122         SYS_EVENT,
123         UE_EVENT,
124         EXTERNAL_EVENT,
125         EXTERNAL_REMOTE_EVENT,
126         CROSS_PLATFORM,
127         PRIVATE_MESSAGE_TYPE // Expand macro from defines.h
128     };
129 
130     enum ManageType {
131         ORDERED,
132         UNORDERED,
133         PROCESS_TYPE_NUM,
134     };
135 
136     enum EventId {
137         PLUGIN_LOADED,
138     };
139 
140     MessageType messageType_;
141     ManageType processType_;
142     uint16_t what_;
143     uint32_t eventId_;
144     uint64_t happenTime_;
145     uint64_t targetDispatchTime_;
146     uint64_t createTime_;
147     uint64_t realtime_;
148     uint64_t processTime_;
149     std::string sender_;
150     std::string domain_;
151     std::string eventName_;
152     bool isPipeline_;
153     bool hasFinish_;
154     bool hasPending_;
155     // dft fault listener params
156     std::string traceId_;
157     std::string spanId_;
158     std::string parentSpanId_;
159     std::string traceFlag_;
160     std::string normalExtraInfo_;
161     std::shared_ptr<EventRaw::RawData> rawData_ = nullptr;
162     void SetValue(const std::string &name, const std::string &value);
163     void SetValue(const std::string &name, int32_t value);
164     void SetKeyValuePairs(std::map<std::string, std::string> keyValuePairs);
165     const std::string GetValue(const std::string &name) const;
166     int32_t GetIntValue(const std::string &name) const;
167     int64_t GetInt64Value(const std::string &name) const;
168     std::map<std::string, std::string> GetKeyValuePairs() const;
169     void ResetTimestamp();
ResetPendingStatus()170     void ResetPendingStatus()
171     {
172         hasPending_ = false;
173     };
174 
IsPipelineEvent()175     bool IsPipelineEvent() const
176     {
177         return isPipeline_;
178     };
179 
HasFinish()180     bool HasFinish() const
181     {
182         return hasFinish_;
183     };
184 
HasPending()185     bool HasPending() const
186     {
187         return hasPending_;
188     };
189 
GetEventName()190     std::string GetEventName() const
191     {
192         return eventName_;
193     }
194 
SetEventName(const std::string & name)195     void SetEventName(const std::string &name)
196     {
197         eventName_ = name;
198     }
199 
200     template <typename Derived>
DownCastTo(std::shared_ptr<Event> event)201     static std::shared_ptr<Derived> DownCastTo(std::shared_ptr<Event> event)
202     {
203         return std::static_pointer_cast<Derived>(event);
204     };
205 
206     // Ensure the copy constructor of Base and Derived classes are carefully arranged
207     template <typename Base, typename Derived>
208     static std::shared_ptr<Derived> Repack(std::shared_ptr<Event>& event, bool replace = true)
209     {
210         auto base = std::static_pointer_cast<Base>(event);
211         if (replace) {
212             auto derived = new Derived(*(base.get()));
213             event.reset(derived);
214             return std::static_pointer_cast<Derived>(event);
215         }
216 
217         auto newEvent = std::make_shared<Derived>(*(base.get()));
218         newEvent->OnRepack();
219         return newEvent;
220     };
221 
222 protected:
223     std::map<std::string, std::string> bundle_;
224 };
225 class DllExport EventHandler {
226 public:
~EventHandler()227     virtual ~EventHandler(){};
228     virtual bool OnEvent(std::shared_ptr<Event>& event) = 0;
OnEventProxy(std::shared_ptr<Event> event)229     virtual bool OnEventProxy(std::shared_ptr<Event> event)
230     {
231         return OnEvent(event);
232     };
233 
CanProcessEvent(std::shared_ptr<Event> event __UNUSED)234     virtual bool CanProcessEvent(std::shared_ptr<Event> event __UNUSED)
235     {
236         return true;
237     };
238 
IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)239     virtual bool IsInterestedPipelineEvent(std::shared_ptr<Event> event __UNUSED)
240     {
241         return true;
242     };
243 
CanProcessMoreEvents()244     virtual bool CanProcessMoreEvents()
245     {
246         return true;
247     };
248 
GetHandlerInfo()249     virtual std::string GetHandlerInfo()
250     {
251         return "";
252     };
253 };
254 class EventListener {
255 public:
EventListener()256     EventListener() {};
~EventListener()257     virtual ~EventListener(){};
258 
OnOrderedEvent(const Event & msg)259     virtual bool OnOrderedEvent(const Event &msg)
260     {
261         return false;
262     }
263     virtual void OnUnorderedEvent(const Event &msg) = 0;
264     virtual std::string GetListenerName() = 0;
265     void AddListenerInfo(uint32_t type);
266     void AddListenerInfo(uint32_t type, const std::map<std::string, DomainRule>& domainRulesMap);
267     void AddListenerInfo(uint32_t type, const std::set<std::string> &eventNames,
268         const std::map<std::string, DomainRule>& domainRulesMap = {});
269 };
270 } // namespace HiviewDFX
271 } // namespace OHOS
272 #endif // HIVIEW_BASE_EVENT_H
273