Commit | Line | Data |
---|---|---|
85dcce24 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 | ||
f3c9a159 | 23 | from bt2 import utils, native_bt |
85dcce24 | 24 | import bt2 |
3d60267b | 25 | import itertools |
3fb99a22 PP |
26 | from bt2 import message_iterator as bt2_message_iterator |
27 | from bt2 import logging as bt2_logging | |
28 | from bt2 import port as bt2_port | |
c1859f69 | 29 | from bt2 import component as bt2_component |
f3c9a159 SM |
30 | from bt2 import value as bt2_value |
31 | from bt2 import plugin as bt2_plugin | |
85dcce24 | 32 | import datetime |
85dcce24 PP |
33 | from collections import namedtuple |
34 | import numbers | |
35 | ||
36 | ||
3d60267b PP |
37 | # a pair of component and ComponentSpec |
38 | _ComponentAndSpec = namedtuple('_ComponentAndSpec', ['comp', 'spec']) | |
85dcce24 PP |
39 | |
40 | ||
f3c9a159 SM |
41 | class _BaseComponentSpec: |
42 | def __init__(self, params, obj, logging_level): | |
43 | if logging_level is not None: | |
44 | utils._check_log_level(logging_level) | |
45 | ||
46 | self._params = bt2.create_value(params) | |
47 | self._obj = obj | |
48 | self._logging_level = logging_level | |
49 | ||
50 | @property | |
51 | def params(self): | |
52 | return self._params | |
53 | ||
54 | @property | |
55 | def obj(self): | |
56 | return self._obj | |
57 | ||
58 | @property | |
59 | def logging_level(self): | |
60 | return self._logging_level | |
61 | ||
62 | ||
63 | class ComponentSpec(_BaseComponentSpec): | |
cfbd7cf3 FD |
64 | def __init__( |
65 | self, | |
66 | plugin_name, | |
67 | class_name, | |
68 | params=None, | |
66964f3f | 69 | obj=None, |
3fb99a22 | 70 | logging_level=bt2_logging.LoggingLevel.NONE, |
cfbd7cf3 | 71 | ): |
f3c9a159 SM |
72 | if type(params) is str: |
73 | params = {'inputs': [params]} | |
74 | ||
75 | super().__init__(params, obj, logging_level) | |
76 | ||
85dcce24 | 77 | utils._check_str(plugin_name) |
e8ac1aae | 78 | utils._check_str(class_name) |
f3c9a159 | 79 | |
85dcce24 | 80 | self._plugin_name = plugin_name |
e8ac1aae | 81 | self._class_name = class_name |
85dcce24 PP |
82 | |
83 | @property | |
84 | def plugin_name(self): | |
85 | return self._plugin_name | |
86 | ||
87 | @property | |
e8ac1aae PP |
88 | def class_name(self): |
89 | return self._class_name | |
85dcce24 | 90 | |
e874da19 | 91 | |
f3c9a159 SM |
92 | class AutoSourceComponentSpec(_BaseComponentSpec): |
93 | _no_obj = object() | |
94 | ||
95 | def __init__(self, input, params=None, obj=_no_obj, logging_level=None): | |
96 | super().__init__(params, obj, logging_level) | |
97 | self._input = input | |
85dcce24 | 98 | |
66964f3f | 99 | @property |
f3c9a159 SM |
100 | def input(self): |
101 | return self._input | |
102 | ||
103 | ||
104 | def _auto_discover_source_component_specs(auto_source_comp_specs, plugin_set): | |
105 | # Transform a list of `AutoSourceComponentSpec` in a list of `ComponentSpec` | |
106 | # using the automatic source discovery mechanism. | |
107 | inputs = bt2.ArrayValue([spec.input for spec in auto_source_comp_specs]) | |
108 | ||
109 | if plugin_set is None: | |
110 | plugin_set = bt2.find_plugins() | |
111 | else: | |
112 | utils._check_type(plugin_set, bt2_plugin._PluginSet) | |
113 | ||
114 | res_ptr = native_bt.bt2_auto_discover_source_components( | |
115 | inputs._ptr, plugin_set._ptr | |
116 | ) | |
117 | ||
118 | if res_ptr is None: | |
119 | raise bt2._MemoryError('cannot auto discover source components') | |
120 | ||
121 | res = bt2_value._create_from_ptr(res_ptr) | |
122 | ||
123 | assert type(res) == bt2.MapValue | |
124 | assert 'status' in res | |
125 | ||
126 | status = res['status'] | |
127 | utils._handle_func_status(status, 'cannot auto-discover source components') | |
128 | ||
129 | comp_specs = [] | |
130 | comp_specs_raw = res['results'] | |
131 | assert type(comp_specs_raw) == bt2.ArrayValue | |
132 | ||
39b351f9 SM |
133 | used_input_indices = set() |
134 | ||
f3c9a159 SM |
135 | for comp_spec_raw in comp_specs_raw: |
136 | assert type(comp_spec_raw) == bt2.ArrayValue | |
137 | assert len(comp_spec_raw) == 4 | |
138 | ||
139 | plugin_name = comp_spec_raw[0] | |
140 | assert type(plugin_name) == bt2.StringValue | |
141 | plugin_name = str(plugin_name) | |
142 | ||
143 | class_name = comp_spec_raw[1] | |
144 | assert type(class_name) == bt2.StringValue | |
145 | class_name = str(class_name) | |
146 | ||
147 | comp_inputs = comp_spec_raw[2] | |
148 | assert type(comp_inputs) == bt2.ArrayValue | |
149 | ||
150 | comp_orig_indices = comp_spec_raw[3] | |
151 | assert type(comp_orig_indices) | |
152 | ||
153 | params = bt2.MapValue() | |
154 | logging_level = bt2.LoggingLevel.NONE | |
155 | obj = None | |
156 | ||
157 | # Compute `params` for this component by piling up params given to all | |
158 | # AutoSourceComponentSpec objects that contributed in the instantiation | |
159 | # of this component. | |
160 | # | |
161 | # The effective log level for a component is the last one specified | |
162 | # across the AutoSourceComponentSpec that contributed in its | |
163 | # instantiation. | |
164 | for idx in comp_orig_indices: | |
165 | orig_spec = auto_source_comp_specs[idx] | |
166 | ||
167 | if orig_spec.params is not None: | |
168 | params.update(orig_spec.params) | |
169 | ||
170 | if orig_spec.logging_level is not None: | |
171 | logging_level = orig_spec.logging_level | |
172 | ||
173 | if orig_spec.obj is not AutoSourceComponentSpec._no_obj: | |
174 | obj = orig_spec.obj | |
175 | ||
39b351f9 SM |
176 | used_input_indices.add(int(idx)) |
177 | ||
f3c9a159 SM |
178 | params['inputs'] = comp_inputs |
179 | ||
180 | comp_specs.append( | |
181 | ComponentSpec( | |
182 | plugin_name, | |
183 | class_name, | |
184 | params=params, | |
185 | obj=obj, | |
186 | logging_level=logging_level, | |
187 | ) | |
188 | ) | |
189 | ||
39b351f9 SM |
190 | if len(used_input_indices) != len(inputs): |
191 | unused_input_indices = set(range(len(inputs))) - used_input_indices | |
192 | unused_input_indices = sorted(unused_input_indices) | |
193 | unused_inputs = [str(inputs[x]) for x in unused_input_indices] | |
194 | ||
195 | msg = ( | |
196 | 'Some auto source component specs did not produce any component: ' | |
197 | + ', '.join(unused_inputs) | |
198 | ) | |
199 | raise RuntimeError(msg) | |
200 | ||
f3c9a159 | 201 | return comp_specs |
66964f3f | 202 | |
85dcce24 PP |
203 | |
204 | # datetime.datetime or integral to nanoseconds | |
205 | def _get_ns(obj): | |
206 | if obj is None: | |
207 | return | |
208 | ||
209 | if isinstance(obj, numbers.Real): | |
210 | # consider that it's already in seconds | |
211 | s = obj | |
212 | elif isinstance(obj, datetime.datetime): | |
213 | # s -> ns | |
214 | s = obj.timestamp() | |
215 | else: | |
cfbd7cf3 FD |
216 | raise TypeError( |
217 | '"{}" is not an integral number or a datetime.datetime object'.format(obj) | |
218 | ) | |
85dcce24 PP |
219 | |
220 | return int(s * 1e9) | |
221 | ||
222 | ||
3d60267b PP |
223 | class _CompClsType: |
224 | SOURCE = 0 | |
225 | FILTER = 1 | |
226 | ||
227 | ||
c1859f69 PP |
228 | class _TraceCollectionMessageIteratorProxySink(bt2_component._UserSinkComponent): |
229 | def __init__(self, params, msg_list): | |
230 | assert type(msg_list) is list | |
231 | self._msg_list = msg_list | |
232 | self._add_input_port('in') | |
233 | ||
234 | def _user_graph_is_configured(self): | |
235 | self._msg_iter = self._create_input_port_message_iterator( | |
236 | self._input_ports['in'] | |
237 | ) | |
238 | ||
239 | def _user_consume(self): | |
240 | assert self._msg_list[0] is None | |
241 | self._msg_list[0] = next(self._msg_iter) | |
242 | ||
243 | ||
3fb99a22 | 244 | class TraceCollectionMessageIterator(bt2_message_iterator._MessageIterator): |
cfbd7cf3 FD |
245 | def __init__( |
246 | self, | |
247 | source_component_specs, | |
248 | filter_component_specs=None, | |
249 | stream_intersection_mode=False, | |
250 | begin=None, | |
251 | end=None, | |
f3c9a159 | 252 | plugin_set=None, |
cfbd7cf3 | 253 | ): |
85dcce24 PP |
254 | utils._check_bool(stream_intersection_mode) |
255 | self._stream_intersection_mode = stream_intersection_mode | |
256 | self._begin_ns = _get_ns(begin) | |
257 | self._end_ns = _get_ns(end) | |
c1859f69 | 258 | self._msg_list = [None] |
3d60267b | 259 | |
f3c9a159 SM |
260 | # If a single item is provided, convert to a list. |
261 | if type(source_component_specs) in ( | |
262 | ComponentSpec, | |
263 | AutoSourceComponentSpec, | |
264 | str, | |
265 | ): | |
3d60267b PP |
266 | source_component_specs = [source_component_specs] |
267 | ||
f3c9a159 SM |
268 | # Convert any string to an AutoSourceComponentSpec. |
269 | def str_to_auto(item): | |
270 | if type(item) is str: | |
271 | item = AutoSourceComponentSpec(item) | |
272 | ||
273 | return item | |
274 | ||
275 | source_component_specs = [str_to_auto(s) for s in source_component_specs] | |
276 | ||
3d60267b PP |
277 | if type(filter_component_specs) is ComponentSpec: |
278 | filter_component_specs = [filter_component_specs] | |
279 | elif filter_component_specs is None: | |
280 | filter_component_specs = [] | |
281 | ||
f3c9a159 SM |
282 | self._validate_source_component_specs(source_component_specs) |
283 | self._validate_filter_component_specs(filter_component_specs) | |
284 | ||
285 | # Pass any `ComponentSpec` instance as-is. | |
286 | self._src_comp_specs = [ | |
287 | spec for spec in source_component_specs if type(spec) is ComponentSpec | |
288 | ] | |
289 | ||
290 | # Convert any `AutoSourceComponentSpec` in concrete `ComponentSpec` instances. | |
291 | auto_src_comp_specs = [ | |
292 | spec | |
293 | for spec in source_component_specs | |
294 | if type(spec) is AutoSourceComponentSpec | |
295 | ] | |
296 | self._src_comp_specs += _auto_discover_source_component_specs( | |
297 | auto_src_comp_specs, plugin_set | |
298 | ) | |
299 | ||
3d60267b | 300 | self._flt_comp_specs = filter_component_specs |
85dcce24 PP |
301 | self._next_suffix = 1 |
302 | self._connect_ports = False | |
303 | ||
3d60267b | 304 | # lists of _ComponentAndSpec |
85dcce24 | 305 | self._src_comps_and_specs = [] |
3d60267b | 306 | self._flt_comps_and_specs = [] |
85dcce24 | 307 | |
85dcce24 PP |
308 | self._build_graph() |
309 | ||
f3c9a159 SM |
310 | def _validate_source_component_specs(self, comp_specs): |
311 | for comp_spec in comp_specs: | |
312 | if ( | |
313 | type(comp_spec) is not ComponentSpec | |
314 | and type(comp_spec) is not AutoSourceComponentSpec | |
315 | ): | |
316 | raise TypeError( | |
317 | '"{}" object is not a ComponentSpec or AutoSourceComponentSpec'.format( | |
318 | type(comp_spec) | |
319 | ) | |
320 | ) | |
321 | ||
322 | def _validate_filter_component_specs(self, comp_specs): | |
3d60267b PP |
323 | for comp_spec in comp_specs: |
324 | if type(comp_spec) is not ComponentSpec: | |
cfbd7cf3 FD |
325 | raise TypeError( |
326 | '"{}" object is not a ComponentSpec'.format(type(comp_spec)) | |
327 | ) | |
85dcce24 PP |
328 | |
329 | def __next__(self): | |
c1859f69 PP |
330 | assert self._msg_list[0] is None |
331 | self._graph.run_once() | |
332 | msg = self._msg_list[0] | |
333 | assert msg is not None | |
334 | self._msg_list[0] = None | |
335 | return msg | |
85dcce24 | 336 | |
907f2b70 | 337 | def _create_stream_intersection_trimmer(self, component, port): |
85dcce24 | 338 | # find the original parameters specified by the user to create |
73760435 | 339 | # this port's component to get the `inputs` parameter |
85dcce24 | 340 | for src_comp_and_spec in self._src_comps_and_specs: |
907f2b70 | 341 | if component == src_comp_and_spec.comp: |
85dcce24 PP |
342 | break |
343 | ||
344 | try: | |
73760435 | 345 | inputs = src_comp_and_spec.spec.params['inputs'] |
907f2b70 | 346 | except Exception as e: |
ce4923b0 | 347 | raise ValueError( |
73760435 | 348 | 'all source components must be created with an "inputs" parameter in stream intersection mode' |
cfbd7cf3 | 349 | ) from e |
85dcce24 | 350 | |
73760435 | 351 | params = {'inputs': inputs} |
85dcce24 | 352 | |
1a29b831 PP |
353 | # query the port's component for the `babeltrace.trace-info` |
354 | # object which contains the stream intersection range for each | |
355 | # exposed trace | |
3c729b9a | 356 | query_exec = bt2.QueryExecutor( |
1a29b831 | 357 | src_comp_and_spec.comp.cls, 'babeltrace.trace-info', params |
cfbd7cf3 | 358 | ) |
3c729b9a | 359 | trace_info_res = query_exec.query() |
85dcce24 PP |
360 | begin = None |
361 | end = None | |
362 | ||
a38d7650 | 363 | # find the trace info for this port's trace |
87b768aa PP |
364 | try: |
365 | for trace_info in trace_info_res: | |
a38d7650 SM |
366 | for stream in trace_info['streams']: |
367 | if stream['port-name'] == port.name: | |
368 | range_ns = trace_info['intersection-range-ns'] | |
369 | begin = range_ns['begin'] | |
370 | end = range_ns['end'] | |
371 | break | |
907f2b70 | 372 | except Exception: |
87b768aa | 373 | pass |
85dcce24 PP |
374 | |
375 | if begin is None or end is None: | |
ce4923b0 | 376 | raise RuntimeError( |
cfbd7cf3 FD |
377 | 'cannot find stream intersection range for port "{}"'.format(port.name) |
378 | ) | |
85dcce24 | 379 | |
907f2b70 | 380 | name = 'trimmer-{}-{}'.format(src_comp_and_spec.comp.name, port.name) |
85dcce24 PP |
381 | return self._create_trimmer(begin, end, name) |
382 | ||
383 | def _create_muxer(self): | |
384 | plugin = bt2.find_plugin('utils') | |
385 | ||
386 | if plugin is None: | |
ce4923b0 | 387 | raise RuntimeError('cannot find "utils" plugin (needed for the muxer)') |
85dcce24 PP |
388 | |
389 | if 'muxer' not in plugin.filter_component_classes: | |
ce4923b0 | 390 | raise RuntimeError( |
cfbd7cf3 FD |
391 | 'cannot find "muxer" filter component class in "utils" plugin' |
392 | ) | |
85dcce24 PP |
393 | |
394 | comp_cls = plugin.filter_component_classes['muxer'] | |
395 | return self._graph.add_component(comp_cls, 'muxer') | |
396 | ||
907f2b70 | 397 | def _create_trimmer(self, begin_ns, end_ns, name): |
85dcce24 PP |
398 | plugin = bt2.find_plugin('utils') |
399 | ||
400 | if plugin is None: | |
ce4923b0 | 401 | raise RuntimeError('cannot find "utils" plugin (needed for the trimmer)') |
85dcce24 PP |
402 | |
403 | if 'trimmer' not in plugin.filter_component_classes: | |
ce4923b0 | 404 | raise RuntimeError( |
cfbd7cf3 FD |
405 | 'cannot find "trimmer" filter component class in "utils" plugin' |
406 | ) | |
85dcce24 PP |
407 | |
408 | params = {} | |
409 | ||
907f2b70 SM |
410 | def ns_to_string(ns): |
411 | s_part = ns // 1000000000 | |
412 | ns_part = ns % 1000000000 | |
413 | return '{}.{:09d}'.format(s_part, ns_part) | |
85dcce24 | 414 | |
907f2b70 SM |
415 | if begin_ns is not None: |
416 | params['begin'] = ns_to_string(begin_ns) | |
417 | ||
418 | if end_ns is not None: | |
419 | params['end'] = ns_to_string(end_ns) | |
85dcce24 PP |
420 | |
421 | comp_cls = plugin.filter_component_classes['trimmer'] | |
422 | return self._graph.add_component(comp_cls, name, params) | |
423 | ||
3d60267b | 424 | def _get_unique_comp_name(self, comp_spec): |
cfbd7cf3 FD |
425 | name = '{}-{}'.format(comp_spec.plugin_name, comp_spec.class_name) |
426 | comps_and_specs = itertools.chain( | |
427 | self._src_comps_and_specs, self._flt_comps_and_specs | |
428 | ) | |
85dcce24 | 429 | |
3d60267b | 430 | if name in [comp_and_spec.comp.name for comp_and_spec in comps_and_specs]: |
85dcce24 PP |
431 | name += '-{}'.format(self._next_suffix) |
432 | self._next_suffix += 1 | |
433 | ||
434 | return name | |
435 | ||
3d60267b | 436 | def _create_comp(self, comp_spec, comp_cls_type): |
85dcce24 PP |
437 | plugin = bt2.find_plugin(comp_spec.plugin_name) |
438 | ||
439 | if plugin is None: | |
ce4923b0 | 440 | raise ValueError('no such plugin: {}'.format(comp_spec.plugin_name)) |
85dcce24 | 441 | |
3d60267b PP |
442 | if comp_cls_type == _CompClsType.SOURCE: |
443 | comp_classes = plugin.source_component_classes | |
444 | else: | |
445 | comp_classes = plugin.filter_component_classes | |
446 | ||
e8ac1aae | 447 | if comp_spec.class_name not in comp_classes: |
3d60267b | 448 | cc_type = 'source' if comp_cls_type == _CompClsType.SOURCE else 'filter' |
ce4923b0 | 449 | raise ValueError( |
cfbd7cf3 FD |
450 | 'no such {} component class in "{}" plugin: {}'.format( |
451 | cc_type, comp_spec.plugin_name, comp_spec.class_name | |
452 | ) | |
453 | ) | |
85dcce24 | 454 | |
e8ac1aae | 455 | comp_cls = comp_classes[comp_spec.class_name] |
3d60267b | 456 | name = self._get_unique_comp_name(comp_spec) |
cfbd7cf3 | 457 | comp = self._graph.add_component( |
66964f3f | 458 | comp_cls, name, comp_spec.params, comp_spec.obj, comp_spec.logging_level |
cfbd7cf3 | 459 | ) |
85dcce24 PP |
460 | return comp |
461 | ||
462 | def _get_free_muxer_input_port(self): | |
463 | for port in self._muxer_comp.input_ports.values(): | |
464 | if not port.is_connected: | |
465 | return port | |
466 | ||
907f2b70 | 467 | def _connect_src_comp_port(self, component, port): |
85dcce24 PP |
468 | # if this trace collection iterator is in stream intersection |
469 | # mode, we need this connection: | |
470 | # | |
471 | # port -> trimmer -> muxer | |
472 | # | |
473 | # otherwise, simply: | |
474 | # | |
475 | # port -> muxer | |
476 | if self._stream_intersection_mode: | |
907f2b70 | 477 | trimmer_comp = self._create_stream_intersection_trimmer(component, port) |
85dcce24 PP |
478 | self._graph.connect_ports(port, trimmer_comp.input_ports['in']) |
479 | port_to_muxer = trimmer_comp.output_ports['out'] | |
480 | else: | |
481 | port_to_muxer = port | |
482 | ||
483 | self._graph.connect_ports(port_to_muxer, self._get_free_muxer_input_port()) | |
484 | ||
907f2b70 | 485 | def _graph_port_added(self, component, port): |
85dcce24 PP |
486 | if not self._connect_ports: |
487 | return | |
488 | ||
3fb99a22 | 489 | if type(port) is bt2_port._InputPort: |
85dcce24 PP |
490 | return |
491 | ||
907f2b70 | 492 | if component not in [comp.comp for comp in self._src_comps_and_specs]: |
85dcce24 PP |
493 | # do not care about non-source components (muxer, trimmer, etc.) |
494 | return | |
495 | ||
907f2b70 | 496 | self._connect_src_comp_port(component, port) |
85dcce24 PP |
497 | |
498 | def _build_graph(self): | |
499 | self._graph = bt2.Graph() | |
907f2b70 | 500 | self._graph.add_port_added_listener(self._graph_port_added) |
85dcce24 PP |
501 | self._muxer_comp = self._create_muxer() |
502 | ||
503 | if self._begin_ns is not None or self._end_ns is not None: | |
cfbd7cf3 FD |
504 | trimmer_comp = self._create_trimmer(self._begin_ns, self._end_ns, 'trimmer') |
505 | self._graph.connect_ports( | |
506 | self._muxer_comp.output_ports['out'], trimmer_comp.input_ports['in'] | |
507 | ) | |
c1859f69 | 508 | last_flt_out_port = trimmer_comp.output_ports['out'] |
85dcce24 | 509 | else: |
c1859f69 | 510 | last_flt_out_port = self._muxer_comp.output_ports['out'] |
85dcce24 | 511 | |
3d60267b PP |
512 | # create extra filter components (chained) |
513 | for comp_spec in self._flt_comp_specs: | |
514 | comp = self._create_comp(comp_spec, _CompClsType.FILTER) | |
515 | self._flt_comps_and_specs.append(_ComponentAndSpec(comp, comp_spec)) | |
516 | ||
517 | # connect the extra filter chain | |
518 | for comp_and_spec in self._flt_comps_and_specs: | |
519 | in_port = list(comp_and_spec.comp.input_ports.values())[0] | |
520 | out_port = list(comp_and_spec.comp.output_ports.values())[0] | |
c1859f69 PP |
521 | self._graph.connect_ports(last_flt_out_port, in_port) |
522 | last_flt_out_port = out_port | |
3d60267b | 523 | |
85dcce24 PP |
524 | # Here we create the components, self._graph_port_added() is |
525 | # called when they add ports, but the callback returns early | |
526 | # because self._connect_ports is False. This is because the | |
527 | # self._graph_port_added() could not find the associated source | |
528 | # component specification in self._src_comps_and_specs because | |
529 | # it does not exist yet (it needs the created component to | |
530 | # exist). | |
531 | for comp_spec in self._src_comp_specs: | |
3d60267b PP |
532 | comp = self._create_comp(comp_spec, _CompClsType.SOURCE) |
533 | self._src_comps_and_specs.append(_ComponentAndSpec(comp, comp_spec)) | |
85dcce24 PP |
534 | |
535 | # Now we connect the ports which exist at this point. We allow | |
536 | # self._graph_port_added() to automatically connect _new_ ports. | |
537 | self._connect_ports = True | |
538 | ||
539 | for comp_and_spec in self._src_comps_and_specs: | |
540 | # Keep a separate list because comp_and_spec.output_ports | |
541 | # could change during the connection of one of its ports. | |
542 | # Any new port is handled by self._graph_port_added(). | |
543 | out_ports = [port for port in comp_and_spec.comp.output_ports.values()] | |
544 | ||
545 | for out_port in out_ports: | |
907f2b70 | 546 | if out_port.is_connected: |
85dcce24 PP |
547 | continue |
548 | ||
907f2b70 | 549 | self._connect_src_comp_port(comp_and_spec.comp, out_port) |
85dcce24 | 550 | |
c1859f69 PP |
551 | # Add the proxy sink, passing our message list to share consumed |
552 | # messages with this trace collection message iterator. | |
553 | sink = self._graph.add_component( | |
554 | _TraceCollectionMessageIteratorProxySink, 'proxy-sink', obj=self._msg_list | |
555 | ) | |
556 | sink_in_port = sink.input_ports['in'] | |
557 | ||
558 | # connect last filter to proxy sink | |
559 | self._graph.connect_ports(last_flt_out_port, sink_in_port) |