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 | ||
bc5c9924 | 23 | from bt2 import native_bt, object |
f6a5e476 PP |
24 | import bt2.component |
25 | import bt2.connection | |
fa4c33e3 SM |
26 | import bt2.message_iterator |
27 | import bt2.message | |
f6a5e476 PP |
28 | import bt2 |
29 | ||
30 | ||
824ce8b6 SM |
31 | def _create_from_ptr_and_get_ref(ptr, port_type): |
32 | cls = _PORT_TYPE_TO_PYCLS.get(port_type, None) | |
33 | ||
34 | if cls is None: | |
fb25b9e3 | 35 | raise TypeError('unknown port type: {}'.format(port_type)) |
824ce8b6 SM |
36 | |
37 | return cls._create_from_ptr_and_get_ref(ptr) | |
38 | ||
39 | ||
40 | def _create_self_from_ptr_and_get_ref(ptr, port_type): | |
41 | cls = _PORT_TYPE_TO_USER_PYCLS.get(port_type, None) | |
42 | ||
43 | if cls is None: | |
fb25b9e3 | 44 | raise TypeError('unknown port type: {}'.format(port_type)) |
824ce8b6 SM |
45 | |
46 | return cls._create_from_ptr_and_get_ref(ptr) | |
47 | ||
48 | ||
c3044a97 | 49 | class _Port(object._SharedObject): |
bc5c9924 SM |
50 | @classmethod |
51 | def _get_ref(cls, ptr): | |
52 | ptr = cls._as_port_ptr(ptr) | |
53 | return native_bt.port_get_ref(ptr) | |
f6a5e476 | 54 | |
bc5c9924 SM |
55 | @classmethod |
56 | def _put_ref(cls, ptr): | |
57 | ptr = cls._as_port_ptr(ptr) | |
58 | return native_bt.port_put_ref(ptr) | |
f6a5e476 PP |
59 | |
60 | @property | |
61 | def name(self): | |
bc5c9924 SM |
62 | ptr = self._as_port_ptr(self._ptr) |
63 | name = native_bt.port_get_name(ptr) | |
64 | assert name is not None | |
65 | return name | |
f6a5e476 PP |
66 | |
67 | @property | |
68 | def connection(self): | |
bc5c9924 SM |
69 | ptr = self._as_port_ptr(self._ptr) |
70 | conn_ptr = native_bt.port_borrow_connection_const(ptr) | |
f6a5e476 PP |
71 | |
72 | if conn_ptr is None: | |
73 | return | |
74 | ||
bc5c9924 | 75 | return bt2.connection._Connection._create_from_ptr_and_get_ref(conn_ptr) |
f6a5e476 PP |
76 | |
77 | @property | |
78 | def is_connected(self): | |
79 | return self.connection is not None | |
80 | ||
f6a5e476 PP |
81 | |
82 | class _InputPort(_Port): | |
bc5c9924 | 83 | _as_port_ptr = staticmethod(native_bt.port_input_as_port_const) |
f6a5e476 PP |
84 | |
85 | ||
86 | class _OutputPort(_Port): | |
bc5c9924 | 87 | _as_port_ptr = staticmethod(native_bt.port_output_as_port_const) |
26c5273a | 88 | |
26c5273a | 89 | |
bc5c9924 SM |
90 | class _UserComponentPort(_Port): |
91 | @classmethod | |
92 | def _as_port_ptr(cls, ptr): | |
93 | ptr = cls._as_self_port_ptr(ptr) | |
94 | return native_bt.self_component_port_as_port(ptr) | |
f6a5e476 PP |
95 | |
96 | @property | |
97 | def connection(self): | |
bc5c9924 SM |
98 | ptr = self._as_port_ptr(self._ptr) |
99 | conn_ptr = native_bt.port_borrow_connection_const(ptr) | |
f6a5e476 PP |
100 | |
101 | if conn_ptr is None: | |
102 | return | |
103 | ||
bc5c9924 | 104 | return bt2.connection._Connection._create_from_ptr_and_get_ref(conn_ptr) |
f6a5e476 | 105 | |
03ec9ebd SM |
106 | @property |
107 | def user_data(self): | |
108 | ptr = self._as_self_port_ptr(self._ptr) | |
109 | return native_bt.self_component_port_get_data(ptr) | |
110 | ||
f6a5e476 | 111 | |
bc5c9924 | 112 | class _UserComponentInputPort(_UserComponentPort, _InputPort): |
61d96b89 FD |
113 | _as_self_port_ptr = staticmethod( |
114 | native_bt.self_component_port_input_as_self_component_port | |
115 | ) | |
f6a5e476 | 116 | |
871a292a | 117 | def create_message_iterator(self): |
61d96b89 FD |
118 | msg_iter_ptr = native_bt.self_component_port_input_message_iterator_create( |
119 | self._ptr | |
120 | ) | |
871a292a | 121 | if msg_iter_ptr is None: |
614743a5 | 122 | raise bt2._MemoryError('cannot create message iterator object') |
871a292a SM |
123 | |
124 | return bt2.message_iterator._UserComponentInputPortMessageIterator(msg_iter_ptr) | |
125 | ||
f6a5e476 | 126 | |
bc5c9924 | 127 | class _UserComponentOutputPort(_UserComponentPort, _OutputPort): |
61d96b89 FD |
128 | _as_self_port_ptr = staticmethod( |
129 | native_bt.self_component_port_output_as_self_component_port | |
130 | ) | |
824ce8b6 SM |
131 | |
132 | ||
133 | _PORT_TYPE_TO_PYCLS = { | |
134 | native_bt.PORT_TYPE_INPUT: _InputPort, | |
135 | native_bt.PORT_TYPE_OUTPUT: _OutputPort, | |
136 | } | |
137 | ||
138 | ||
139 | _PORT_TYPE_TO_USER_PYCLS = { | |
140 | native_bt.PORT_TYPE_INPUT: _UserComponentInputPort, | |
141 | native_bt.PORT_TYPE_OUTPUT: _UserComponentOutputPort, | |
142 | } |