summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/lib/block_gateway_impl.h
blob: e03d45518c7698286d646cf0c7953fb5f2ba1ee4 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* -*- c++ -*- */
/*
 * Copyright 2013,2020 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#ifndef INCLUDED_RUNTIME_BLOCK_GATEWAY_IMPL_H
#define INCLUDED_RUNTIME_BLOCK_GATEWAY_IMPL_H

#include <gnuradio/block_gateway.h>

namespace gr {

/***********************************************************************
 * The gr::block gateway implementation class
 **********************************************************************/
class block_gateway_impl : public block_gateway
{
public:
    block_gateway_impl(const py::handle& p,
                       const std::string& name,
                       gr::io_signature::sptr in_sig,
                       gr::io_signature::sptr out_sig);

    /*******************************************************************
     * Overloads for various scheduler-called functions
     ******************************************************************/
    void forecast(int noutput_items, gr_vector_int& ninput_items_required) override;

    int general_work(int noutput_items,
                     gr_vector_int& ninput_items,
                     gr_vector_const_void_star& input_items,
                     gr_vector_void_star& output_items) override;

    bool start(void) override;
    bool stop(void) override;
    void set_msg_handler_pybind(pmt::pmt_t which_port, std::string& handler_name) override
    {
        if (msg_queue.find(which_port) == msg_queue.end()) {
            throw std::runtime_error(
                "attempt to set_msg_handler_pybind() on invalid input message port!");
        }
        d_msg_handlers_pybind[which_port] = handler_name;
    }

protected:
    // Message handlers back into python using pybind API
    typedef std::map<pmt::pmt_t, std::string, pmt::comparator> msg_handlers_pybind_t;
    msg_handlers_pybind_t d_msg_handlers_pybind;

    bool has_msg_handler(pmt::pmt_t which_port) override
    {
        if (d_msg_handlers_pybind.find(which_port) != d_msg_handlers_pybind.end()) {
            return true;
        } else {
            return gr::basic_block::has_msg_handler(which_port);
        }
    }

    void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg) override
    {
        // Is there a handler?
        if (d_msg_handlers_pybind.find(which_port) != d_msg_handlers_pybind.end()) {
            // d_msg_handlers_pybind[which_port]->calleval(msg); // Yes, invoke it.
            py::gil_scoped_acquire acquire;
            // std::string handler_name(d_msg_handlers_pybind[which_port]);
            py::object ret =
                _py_handle.attr(d_msg_handlers_pybind[which_port].c_str())(msg);
        } else {
            // Pass to generic dispatcher if not found
            gr::basic_block::dispatch_msg(which_port, msg);
        }
    }

private:
    py::handle _py_handle;
};

} /* namespace gr */

#endif /* INCLUDED_RUNTIME_BLOCK_GATEWAY_H */