1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
23 from bt2
import native_bt
, object, utils
24 import collections
.abc
27 import bt2
.message_iterator
33 def _create_from_ptr(ptr
):
34 port_type
= native_bt
.port_get_type(ptr
)
36 if port_type
== native_bt
.PORT_TYPE_INPUT
:
38 elif port_type
== native_bt
.PORT_TYPE_OUTPUT
:
41 raise bt2
.Error('unknown port type: {}'.format(port_type
))
43 return cls
._create
_from
_ptr
(ptr
)
46 def _create_private_from_ptr(ptr
):
47 pub_ptr
= native_bt
.port_from_private(ptr
)
48 utils
._handle
_ptr
(pub_ptr
, 'cannot get port object from private port object')
49 port_type
= native_bt
.port_get_type(pub_ptr
)
50 assert(port_type
== native_bt
.PORT_TYPE_INPUT
or port_type
== native_bt
.PORT_TYPE_OUTPUT
)
52 if port_type
== native_bt
.PORT_TYPE_INPUT
:
53 cls
= _PrivateInputPort
54 elif port_type
== native_bt
.PORT_TYPE_OUTPUT
:
55 cls
= _PrivateOutputPort
57 obj
= cls
._create
_from
_ptr
(ptr
)
58 obj
._pub
_ptr
= pub_ptr
62 class _Port(object._SharedObject
):
65 name
= native_bt
.port_get_name(ptr
)
66 assert(name
is not None)
71 status
= native_bt
.port_disconnect(ptr
)
74 raise bt2
.Error('cannot disconnect port')
78 return self
._name
(self
._ptr
)
82 comp_ptr
= native_bt
.port_get_component(self
._ptr
)
87 return bt2
.component
._create
_generic
_component
_from
_ptr
(comp_ptr
)
91 conn_ptr
= native_bt
.port_get_connection(self
._ptr
)
96 return bt2
.connection
._Connection
._create
_from
_ptr
(conn_ptr
)
99 def is_connected(self
):
100 return self
.connection
is not None
102 def disconnect(self
):
103 self
._disconnect
(self
._ptr
)
105 def __eq__(self
, other
):
106 if type(other
) is not type(self
):
109 return self
.addr
== other
.addr
112 class _InputPort(_Port
):
116 class _OutputPort(_Port
):
117 def create_message_iterator(self
, message_types
=None,
118 colander_component_name
=None):
119 msg_types
= bt2
.message
._msg
_types
_from
_msg
_classes
(message_types
)
121 if colander_component_name
is not None:
122 utils
._check
_str
(colander_component_name
)
124 msg_iter_ptr
= native_bt
.py3_create_output_port_msg_iter(int(self
._ptr
),
125 colander_component_name
,
128 if msg_iter_ptr
is None:
129 raise bt2
.CreationError('cannot create output port message iterator')
131 return bt2
.message_iterator
._OutputPortMessageIterator
._create
_from
_ptr
(msg_iter_ptr
)
134 class _PrivatePort(_Port
):
137 return self
._name
(self
._pub
_ptr
)
141 comp_ptr
= native_bt
.private_port_get_private_component(self
._ptr
)
146 pub_comp_ptr
= native_bt
.component_from_private(comp_ptr
)
148 comp
= bt2
.component
._create
_generic
_component
_from
_ptr
(pub_comp_ptr
)
149 native_bt
.put(comp_ptr
)
153 def connection(self
):
154 conn_ptr
= native_bt
.private_port_get_private_connection(self
._ptr
)
159 return bt2
.connection
._create
_private
_from
_ptr
(conn_ptr
)
161 def remove_from_component(self
):
162 status
= native_bt
.private_port_remove_from_component(self
._ptr
)
165 raise bt2
.Error("cannot remove port from component")
167 def disconnect(self
):
168 self
._disconnect
(self
._pub
_ptr
)
171 class _PrivateInputPort(_PrivatePort
, _InputPort
):
175 class _PrivateOutputPort(_PrivatePort
, _OutputPort
):