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