Commit | Line | Data |
---|---|---|
f6a5e476 PP |
1 | # The MIT License (MIT) |
2 | # | |
3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
4 | # | |
5 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | # of this software and associated documentation files (the "Software"), to deal | |
7 | # in the Software without restriction, including without limitation the rights | |
8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | # copies of the Software, and to permit persons to whom the Software is | |
10 | # furnished to do so, subject to the following conditions: | |
11 | # | |
12 | # The above copyright notice and this permission notice shall be included in | |
13 | # all copies or substantial portions of the Software. | |
14 | # | |
15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
21 | # THE SOFTWARE. | |
22 | ||
23 | from bt2 import native_bt, object, utils | |
d73bb381 | 24 | import bt2.interrupter |
f6a5e476 PP |
25 | import bt2.connection |
26 | import bt2.component | |
27 | import functools | |
28 | import bt2.port | |
cc81b5ab | 29 | import bt2.logging |
f6a5e476 PP |
30 | import bt2 |
31 | ||
32 | ||
61d96b89 FD |
33 | def _graph_port_added_listener_from_native( |
34 | user_listener, component_ptr, component_type, port_ptr, port_type | |
35 | ): | |
36 | component = bt2.component._create_component_from_ptr_and_get_ref( | |
37 | component_ptr, component_type | |
38 | ) | |
871a292a SM |
39 | port = bt2.port._create_from_ptr_and_get_ref(port_ptr, port_type) |
40 | user_listener(component, port) | |
f6a5e476 PP |
41 | |
42 | ||
61d96b89 FD |
43 | def _graph_ports_connected_listener_from_native( |
44 | user_listener, | |
45 | upstream_component_ptr, | |
46 | upstream_component_type, | |
47 | upstream_port_ptr, | |
48 | downstream_component_ptr, | |
49 | downstream_component_type, | |
50 | downstream_port_ptr, | |
51 | ): | |
871a292a | 52 | upstream_component = bt2.component._create_component_from_ptr_and_get_ref( |
61d96b89 FD |
53 | upstream_component_ptr, upstream_component_type |
54 | ) | |
871a292a | 55 | upstream_port = bt2.port._create_from_ptr_and_get_ref( |
61d96b89 FD |
56 | upstream_port_ptr, native_bt.PORT_TYPE_OUTPUT |
57 | ) | |
871a292a | 58 | downstream_component = bt2.component._create_component_from_ptr_and_get_ref( |
61d96b89 FD |
59 | downstream_component_ptr, downstream_component_type |
60 | ) | |
871a292a | 61 | downstream_port = bt2.port._create_from_ptr_and_get_ref( |
61d96b89 FD |
62 | downstream_port_ptr, native_bt.PORT_TYPE_INPUT |
63 | ) | |
64 | user_listener( | |
65 | upstream_component, upstream_port, downstream_component, downstream_port | |
66 | ) | |
f6a5e476 PP |
67 | |
68 | ||
c3044a97 | 69 | class Graph(object._SharedObject): |
a49e2cc3 PP |
70 | _get_ref = staticmethod(native_bt.graph_get_ref) |
71 | _put_ref = staticmethod(native_bt.graph_put_ref) | |
bbb3650f | 72 | |
f6a5e476 PP |
73 | def __init__(self): |
74 | ptr = native_bt.graph_create() | |
75 | ||
76 | if ptr is None: | |
614743a5 | 77 | raise bt2._MemoryError('cannot create graph object') |
f6a5e476 PP |
78 | |
79 | super().__init__(ptr) | |
80 | ||
61d96b89 FD |
81 | def add_component( |
82 | self, | |
83 | component_class, | |
84 | name, | |
85 | params=None, | |
86 | logging_level=bt2.logging.LoggingLevel.NONE, | |
87 | ): | |
ede525fd | 88 | if isinstance(component_class, bt2.component._SourceComponentClass): |
871a292a | 89 | cc_ptr = component_class._ptr |
bc5c9924 SM |
90 | add_fn = native_bt.graph_add_source_component |
91 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE | |
ede525fd | 92 | elif isinstance(component_class, bt2.component._FilterComponentClass): |
871a292a | 93 | cc_ptr = component_class._ptr |
bc5c9924 SM |
94 | add_fn = native_bt.graph_add_filter_component |
95 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER | |
ede525fd | 96 | elif isinstance(component_class, bt2.component._SinkComponentClass): |
871a292a SM |
97 | cc_ptr = component_class._ptr |
98 | add_fn = native_bt.graph_add_sink_component | |
99 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK | |
100 | elif issubclass(component_class, bt2.component._UserSourceComponent): | |
deec48a6 | 101 | cc_ptr = component_class._bt_cc_ptr |
871a292a SM |
102 | add_fn = native_bt.graph_add_source_component |
103 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SOURCE | |
bc5c9924 | 104 | elif issubclass(component_class, bt2.component._UserSinkComponent): |
deec48a6 | 105 | cc_ptr = component_class._bt_cc_ptr |
bc5c9924 SM |
106 | add_fn = native_bt.graph_add_sink_component |
107 | cc_type = native_bt.COMPONENT_CLASS_TYPE_SINK | |
871a292a | 108 | elif issubclass(component_class, bt2.component._UserFilterComponent): |
deec48a6 | 109 | cc_ptr = component_class._bt_cc_ptr |
871a292a SM |
110 | add_fn = native_bt.graph_add_filter_component |
111 | cc_type = native_bt.COMPONENT_CLASS_TYPE_FILTER | |
f6a5e476 | 112 | else: |
61d96b89 FD |
113 | raise TypeError( |
114 | "'{}' is not a component class".format( | |
115 | component_class.__class__.__name__ | |
116 | ) | |
117 | ) | |
f6a5e476 PP |
118 | |
119 | utils._check_str(name) | |
cc81b5ab | 120 | utils._check_log_level(logging_level) |
f6a5e476 PP |
121 | params = bt2.create_value(params) |
122 | ||
bbb3650f | 123 | params_ptr = params._ptr if params is not None else None |
f6a5e476 | 124 | |
61d96b89 | 125 | status, comp_ptr = add_fn(self._ptr, cc_ptr, name, params_ptr, logging_level) |
fb25b9e3 | 126 | utils._handle_func_status(status, 'cannot add component to graph') |
bc5c9924 SM |
127 | assert comp_ptr |
128 | return bt2.component._create_component_from_ptr(comp_ptr, cc_type) | |
f6a5e476 PP |
129 | |
130 | def connect_ports(self, upstream_port, downstream_port): | |
131 | utils._check_type(upstream_port, bt2.port._OutputPort) | |
132 | utils._check_type(downstream_port, bt2.port._InputPort) | |
61d96b89 FD |
133 | status, conn_ptr = native_bt.graph_connect_ports( |
134 | self._ptr, upstream_port._ptr, downstream_port._ptr | |
135 | ) | |
136 | utils._handle_func_status(status, 'cannot connect component ports within graph') | |
137 | assert conn_ptr | |
f6a5e476 PP |
138 | return bt2.connection._Connection._create_from_ptr(conn_ptr) |
139 | ||
871a292a SM |
140 | def add_port_added_listener(self, listener): |
141 | if not callable(listener): | |
f6a5e476 PP |
142 | raise TypeError("'listener' parameter is not callable") |
143 | ||
fb25b9e3 | 144 | fn = native_bt.bt2_graph_add_port_added_listener |
61d96b89 FD |
145 | listener_from_native = functools.partial( |
146 | _graph_port_added_listener_from_native, listener | |
147 | ) | |
f6a5e476 | 148 | |
871a292a SM |
149 | listener_ids = fn(self._ptr, listener_from_native) |
150 | if listener_ids is None: | |
614743a5 | 151 | raise bt2._Error('cannot add listener to graph object') |
d09c69f7 | 152 | |
76f5df43 | 153 | return bt2.utils._ListenerHandle(listener_ids, self) |
871a292a SM |
154 | |
155 | def add_ports_connected_listener(self, listener): | |
156 | if not callable(listener): | |
157 | raise TypeError("'listener' parameter is not callable") | |
158 | ||
fb25b9e3 | 159 | fn = native_bt.bt2_graph_add_ports_connected_listener |
61d96b89 FD |
160 | listener_from_native = functools.partial( |
161 | _graph_ports_connected_listener_from_native, listener | |
162 | ) | |
871a292a SM |
163 | |
164 | listener_ids = fn(self._ptr, listener_from_native) | |
165 | if listener_ids is None: | |
614743a5 | 166 | raise bt2._Error('cannot add listener to graph object') |
d09c69f7 | 167 | |
76f5df43 | 168 | return bt2.utils._ListenerHandle(listener_ids, self) |
f6a5e476 PP |
169 | |
170 | def run(self): | |
171 | status = native_bt.graph_run(self._ptr) | |
172 | ||
fb25b9e3 | 173 | try: |
d73bb381 | 174 | utils._handle_func_status(status, 'graph object stopped running') |
fb25b9e3 PP |
175 | except bt2.Stop: |
176 | # done | |
f6a5e476 | 177 | return |
fb25b9e3 PP |
178 | except Exception: |
179 | raise | |
f6a5e476 | 180 | |
d73bb381 PP |
181 | def add_interrupter(self, interrupter): |
182 | utils._check_type(interrupter, bt2.interrupter.Interrupter) | |
183 | native_bt.graph_add_interrupter(self._ptr, interrupter._ptr) | |
f6a5e476 | 184 | |
d73bb381 PP |
185 | def interrupt(self): |
186 | native_bt.graph_interrupt(self._ptr) | |
f6a5e476 | 187 | |
27d97a3f SM |
188 | def create_output_port_message_iterator(self, output_port): |
189 | utils._check_type(output_port, bt2.port._OutputPort) | |
61d96b89 FD |
190 | msg_iter_ptr = native_bt.port_output_message_iterator_create( |
191 | self._ptr, output_port._ptr | |
192 | ) | |
27d97a3f SM |
193 | |
194 | if msg_iter_ptr is None: | |
614743a5 | 195 | raise bt2._MemoryError('cannot create output port message iterator') |
27d97a3f SM |
196 | |
197 | return bt2.message_iterator._OutputPortMessageIterator(msg_iter_ptr) |