| 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 |
| 24 | import bt2.component |
| 25 | import bt2.connection |
| 26 | import bt2.message_iterator |
| 27 | import bt2.message |
| 28 | import bt2 |
| 29 | |
| 30 | |
| 31 | class _Port(object._SharedObject): |
| 32 | @classmethod |
| 33 | def _get_ref(cls, ptr): |
| 34 | ptr = cls._as_port_ptr(ptr) |
| 35 | return native_bt.port_get_ref(ptr) |
| 36 | |
| 37 | @classmethod |
| 38 | def _put_ref(cls, ptr): |
| 39 | ptr = cls._as_port_ptr(ptr) |
| 40 | return native_bt.port_put_ref(ptr) |
| 41 | |
| 42 | @property |
| 43 | def name(self): |
| 44 | ptr = self._as_port_ptr(self._ptr) |
| 45 | name = native_bt.port_get_name(ptr) |
| 46 | assert name is not None |
| 47 | return name |
| 48 | |
| 49 | @property |
| 50 | def connection(self): |
| 51 | ptr = self._as_port_ptr(self._ptr) |
| 52 | conn_ptr = native_bt.port_borrow_connection_const(ptr) |
| 53 | |
| 54 | if conn_ptr is None: |
| 55 | return |
| 56 | |
| 57 | return bt2.connection._Connection._create_from_ptr_and_get_ref(conn_ptr) |
| 58 | |
| 59 | @property |
| 60 | def is_connected(self): |
| 61 | return self.connection is not None |
| 62 | |
| 63 | |
| 64 | class _InputPort(_Port): |
| 65 | _as_port_ptr = staticmethod(native_bt.port_input_as_port_const) |
| 66 | |
| 67 | |
| 68 | class _OutputPort(_Port): |
| 69 | _as_port_ptr = staticmethod(native_bt.port_output_as_port_const) |
| 70 | |
| 71 | |
| 72 | class _UserComponentPort(_Port): |
| 73 | @classmethod |
| 74 | def _as_port_ptr(cls, ptr): |
| 75 | ptr = cls._as_self_port_ptr(ptr) |
| 76 | return native_bt.self_component_port_as_port(ptr) |
| 77 | |
| 78 | @property |
| 79 | def connection(self): |
| 80 | ptr = self._as_port_ptr(self._ptr) |
| 81 | conn_ptr = native_bt.port_borrow_connection_const(ptr) |
| 82 | |
| 83 | if conn_ptr is None: |
| 84 | return |
| 85 | |
| 86 | return bt2.connection._Connection._create_from_ptr_and_get_ref(conn_ptr) |
| 87 | |
| 88 | |
| 89 | class _UserComponentInputPort(_UserComponentPort, _InputPort): |
| 90 | _as_self_port_ptr = staticmethod(native_bt.self_component_port_input_as_self_component_port) |
| 91 | |
| 92 | |
| 93 | class _UserComponentOutputPort(_UserComponentPort, _OutputPort): |
| 94 | _as_self_port_ptr = staticmethod(native_bt.self_component_port_output_as_self_component_port) |