Eunomia  0.1.0
A lightweight eBPF-based CloudNative Monitor tool for Container Security and Observability
event_handler.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2  *
3  * Copyright (c) 2022, 郑昱笙,濮雯旭,张典典(牛校牛子队)
4  * All rights reserved.
5  */
6 
7 #ifndef EVENT_HANDLER_H
8 #define EVENT_HANDLER_H
9 
10 #include <mutex>
11 #include <iostream>
12 #include <string>
13 #include <thread>
14 #include <memory>
15 #include "container_info.h"
16 
18 
20 template<typename EVENT>
21 concept event_concept = requires
22 {
24  typename EVENT::pid;
25 };
26 
28 
30 template <typename T>
32 {
33  T data;
35  // TODO: add more data options here?
36 };
37 
39 template <typename T>
41 {
42 public:
43  virtual ~event_handler_base() = default;
44  virtual void handle(tracker_event<T> &e) = 0;
45  virtual void do_handle_event(tracker_event<T> &e) = 0;
46 };
47 
49 
51 template <typename T>
53 {
54 std::shared_ptr<event_handler_base<T>> next_handler = nullptr;
55 public:
56  virtual ~event_handler() = default;
57 
59  virtual void handle(tracker_event<T> &e) = 0;
60 
62  std::shared_ptr<event_handler<T>> add_handler(std::shared_ptr<event_handler<T>> handler)
63  {
64  next_handler = handler;
65  return handler;
66  }
70  {
71  bool is_catched = false;
72  try {
73  handle(e);
74  } catch (const std::exception& error) {
75  // std::cerr << "exception: " << error.what() << std::endl;
76  is_catched = true;
77  }
78  if (!is_catched && next_handler)
79  next_handler->do_handle_event(e);
80  return;
81  }
82 };
83 
85 
87 template <typename T1, typename T2>
89 {
90 std::shared_ptr<event_handler_base<T2>> next_handler = nullptr;
91 public:
92  virtual ~event_handler_adapter() = default;
94  std::shared_ptr<event_handler<T1>> add_handler(std::shared_ptr<event_handler<T1>> handler)
95  {
96  next_handler = handler;
97  return handler;
98  }
100  {
101  auto event1 = adapt(e);
102  if (next_handler)
103  next_handler->do_handle_event(event1);
104  return;
105  }
106 };
107 
108 #endif
event_handler_adapter::adapt
virtual tracker_event< T1 > adapt(tracker_event< T2 > &e)=0
event_handler_adapter::next_handler
std::shared_ptr< event_handler_base< T2 > > next_handler
Definition: event_handler.h:90
event_handler_base::do_handle_event
virtual void do_handle_event(tracker_event< T > &e)=0
event_handler
the event handler for single type
Definition: event_handler.h:52
event_handler_base
the event handler for share_ptr
Definition: event_handler.h:40
container_info
container info
Definition: container_info.h:32
event_handler::~event_handler
virtual ~event_handler()=default
event_handler_adapter::do_handle_event
void do_handle_event(tracker_event< T2 > &e)
Definition: event_handler.h:99
tracker_event::ct_info
container_info ct_info
Definition: event_handler.h:34
event_handler_adapter
Event handler type switcher.
Definition: event_handler.h:88
event_handler::handle
virtual void handle(tracker_event< T > &e)=0
implement this function to handle the event
event_handler_base::handle
virtual void handle(tracker_event< T > &e)=0
event_handler_adapter::~event_handler_adapter
virtual ~event_handler_adapter()=default
tracker_event
the basic event type
Definition: event_handler.h:31
container_info.h
event_handler_adapter::add_handler
std::shared_ptr< event_handler< T1 > > add_handler(std::shared_ptr< event_handler< T1 >> handler)
Definition: event_handler.h:94
event_handler::do_handle_event
void do_handle_event(tracker_event< T > &e)
Definition: event_handler.h:69
event_handler_base::~event_handler_base
virtual ~event_handler_base()=default
event_handler::next_handler
std::shared_ptr< event_handler_base< T > > next_handler
Definition: event_handler.h:54
event_concept
concept event_concept
concept for a eunomia event
Definition: event_handler.h:21
tracker_event::data
T data
Definition: event_handler.h:33
event_handler::add_handler
std::shared_ptr< event_handler< T > > add_handler(std::shared_ptr< event_handler< T >> handler)
add a next handler after this handler
Definition: event_handler.h:62