File: zeek.cpp

package info (click to toggle)
broker 1.4.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 4,420 kB
  • sloc: cpp: 20,886; python: 1,382; ansic: 1,077; sh: 321; makefile: 103
file content (58 lines) | stat: -rw-r--r-- 1,957 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include <utility>
#include <string>
#include <stdexcept>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#include <pybind11/pybind11.h>
#pragma GCC diagnostic pop

#include "broker/zeek.hh"
#include "broker/data.hh"

namespace py = pybind11;
using namespace pybind11::literals;

void init_zeek(py::module& m) {
  py::class_<broker::zeek::Message>(m, "Message")
    .def("as_data",
         static_cast<const broker::data& (broker::zeek::Message::*)() const>
         (&broker::zeek::Message::as_data));

  py::class_<broker::zeek::Event, broker::zeek::Message>(m, "Event")
    .def(py::init([](broker::data data) {
       return broker::zeek::Event(std::move(data));
       }))
    .def(py::init([](std::string name, broker::data args) {
       return broker::zeek::Event(std::move(name), std::move(caf::get<broker::vector>(args)));
       }))
    .def("valid", [](const broker::zeek::Event& ev) -> bool {
         auto t = broker::zeek::Message::type(ev.as_data());
         if ( t != broker::zeek::Message::Type::Event )
           return false;
         return ev.valid();
         })
    .def("name", [](const broker::zeek::Event& ev) -> const std::string& {
         auto t = broker::zeek::Message::type(ev.as_data());
         if ( t != broker::zeek::Message::Type::Event ) {
           throw std::invalid_argument("invalid Event data/type");
         }
         if ( ! ev.valid() ) {
           throw std::invalid_argument("invalid Event data");
         }
         return ev.name();
         })
    .def("args", [](const broker::zeek::Event& ev) -> const broker::vector& {
         auto t = broker::zeek::Message::type(ev.as_data());
         if ( t != broker::zeek::Message::Type::Event ) {
           throw std::invalid_argument("invalid Event data/type");
         }
         if ( ! ev.valid() ) {
           throw std::invalid_argument("invalid Event data");
         }
         return ev.args();
         })
    ;
}