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