Commit | Line | Data |
---|---|---|
81447b5b 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 | |
5602ef81 | 24 | import bt2.message |
81447b5b PP |
25 | import collections.abc |
26 | import bt2.component | |
27 | import bt2 | |
28 | ||
29 | ||
5602ef81 | 30 | class _MessageIterator(collections.abc.Iterator): |
81447b5b | 31 | def _handle_status(self, status, gen_error_msg): |
5602ef81 SM |
32 | if status == native_bt.MESSAGE_ITERATOR_STATUS_CANCELED: |
33 | raise bt2.MessageIteratorCanceled | |
34 | elif status == native_bt.MESSAGE_ITERATOR_STATUS_AGAIN: | |
811644b8 | 35 | raise bt2.TryAgain |
5602ef81 | 36 | elif status == native_bt.MESSAGE_ITERATOR_STATUS_END: |
81447b5b | 37 | raise bt2.Stop |
5602ef81 | 38 | elif status == native_bt.MESSAGE_ITERATOR_STATUS_UNSUPPORTED: |
81447b5b PP |
39 | raise bt2.UnsupportedFeature |
40 | elif status < 0: | |
41 | raise bt2.Error(gen_error_msg) | |
42 | ||
81447b5b | 43 | def __next__(self): |
811644b8 | 44 | raise NotImplementedError |
81447b5b PP |
45 | |
46 | ||
78288f58 | 47 | class _GenericMessageIterator(object._SharedObject, _MessageIterator): |
5602ef81 SM |
48 | def _get_msg(self): |
49 | msg_ptr = native_bt.message_iterator_get_message(self._ptr) | |
50 | utils._handle_ptr(msg_ptr, "cannot get message iterator object's current message object") | |
51 | return bt2.message._create_from_ptr(msg_ptr) | |
811644b8 PP |
52 | |
53 | def _next(self): | |
5602ef81 | 54 | status = native_bt.message_iterator_next(self._ptr) |
811644b8 | 55 | self._handle_status(status, |
5602ef81 | 56 | 'unexpected error: cannot advance the message iterator') |
811644b8 PP |
57 | |
58 | def __next__(self): | |
59 | self._next() | |
5602ef81 | 60 | return self._get_msg() |
81447b5b | 61 | |
811644b8 | 62 | |
5602ef81 | 63 | class _PrivateConnectionMessageIterator(_GenericMessageIterator): |
90157d89 PP |
64 | @property |
65 | def component(self): | |
5602ef81 | 66 | comp_ptr = native_bt.private_connection_message_iterator_get_component(self._ptr) |
90157d89 PP |
67 | assert(comp_ptr) |
68 | return bt2.component._create_generic_component_from_ptr(comp_ptr) | |
69 | ||
70 | ||
5602ef81 | 71 | class _OutputPortMessageIterator(_GenericMessageIterator): |
dc43190b PP |
72 | pass |
73 | ||
74 | ||
5602ef81 | 75 | class _UserMessageIterator(_MessageIterator): |
81447b5b | 76 | def __new__(cls, ptr): |
811644b8 | 77 | # User iterator objects are always created by the native side, |
81447b5b PP |
78 | # that is, never instantiated directly by Python code. |
79 | # | |
811644b8 PP |
80 | # The native code calls this, then manually calls |
81 | # self.__init__() without the `ptr` argument. The user has | |
82 | # access to self.component during this call, thanks to this | |
83 | # self._ptr argument being set. | |
81447b5b PP |
84 | # |
85 | # self._ptr is NOT owned by this object here, so there's nothing | |
86 | # to do in __del__(). | |
87 | self = super().__new__(cls) | |
88 | self._ptr = ptr | |
89 | return self | |
90 | ||
91 | def __init__(self): | |
92 | pass | |
93 | ||
94 | @property | |
811644b8 | 95 | def _component(self): |
5602ef81 | 96 | return native_bt.py3_get_user_component_from_user_msg_iter(self._ptr) |
81447b5b PP |
97 | |
98 | @property | |
99 | def addr(self): | |
100 | return int(self._ptr) | |
101 | ||
811644b8 | 102 | def _finalize(self): |
81447b5b PP |
103 | pass |
104 | ||
811644b8 PP |
105 | def __next__(self): |
106 | raise bt2.Stop | |
107 | ||
108 | def _next_from_native(self): | |
109 | # this can raise anything: it's catched by the native part | |
110 | try: | |
5602ef81 | 111 | msg = next(self) |
811644b8 PP |
112 | except StopIteration: |
113 | raise bt2.Stop | |
114 | except: | |
115 | raise | |
116 | ||
5602ef81 | 117 | utils._check_type(msg, bt2.message._Message) |
81447b5b | 118 | |
811644b8 | 119 | # take a new reference for the native part |
5602ef81 SM |
120 | msg._get() |
121 | return int(msg._ptr) |