|
| 1 | +#------------------------------------------------------------------------------ |
| 2 | +# Copyright (c) 2025, Oracle and/or its affiliates. |
| 3 | +# |
| 4 | +# This software is dual-licensed to you under the Universal Permissive License |
| 5 | +# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License |
| 6 | +# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 7 | +# either license. |
| 8 | +# |
| 9 | +# If you elect to accept the software under the Apache License, Version 2.0, |
| 10 | +# the following applies: |
| 11 | +# |
| 12 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 | +# you may not use this file except in compliance with the License. |
| 14 | +# You may obtain a copy of the License at |
| 15 | +# |
| 16 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +# |
| 18 | +# Unless required by applicable law or agreed to in writing, software |
| 19 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | +# See the License for the specific language governing permissions and |
| 22 | +# limitations under the License. |
| 23 | +#------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +#------------------------------------------------------------------------------ |
| 26 | +# aq_base.pyx |
| 27 | +# |
| 28 | +# Cython file defining the base class for messages that are sent to the |
| 29 | +# database and the responses that are received by the client for enqueing and |
| 30 | +# dequeuing AQ messages (embedded in thin_impl.pyx). |
| 31 | +#------------------------------------------------------------------------------ |
| 32 | + |
| 33 | +cdef class AqBaseMessage(Message): |
| 34 | + cdef: |
| 35 | + BaseThinQueueImpl queue_impl |
| 36 | + ThinDeqOptionsImpl deq_options_impl |
| 37 | + ThinEnqOptionsImpl enq_options_impl |
| 38 | + bint no_msg_found |
| 39 | + |
| 40 | + cdef object _process_date(self, ReadBuffer buf): |
| 41 | + """ |
| 42 | + Processes a date found in the buffer. |
| 43 | + """ |
| 44 | + cdef: |
| 45 | + const char_type *ptr |
| 46 | + uint32_t num_bytes |
| 47 | + OracleData data |
| 48 | + ssize_t length |
| 49 | + buf.read_ub4(&num_bytes) |
| 50 | + if num_bytes > 0: |
| 51 | + buf.read_raw_bytes_and_length(&ptr, &length) |
| 52 | + decode_date(ptr, length, &data.buffer) |
| 53 | + return convert_date_to_python(&data.buffer) |
| 54 | + |
| 55 | + cdef int _process_error_info(self, ReadBuffer buf) except -1: |
| 56 | + """ |
| 57 | + Process error information from the buffer. If the error that indicates |
| 58 | + that no messages were received is detected, the error is cleared and |
| 59 | + the flag set so that the dequeue can handle that case. |
| 60 | + """ |
| 61 | + Message._process_error_info(self, buf) |
| 62 | + if self.error_info.num == TNS_ERR_NO_MESSAGES_FOUND: |
| 63 | + self.error_info.num = 0 |
| 64 | + self.error_occurred = False |
| 65 | + self.no_msg_found = True |
| 66 | + |
| 67 | + cdef int _process_extensions(self, ReadBuffer buf, |
| 68 | + ThinMsgPropsImpl props_impl) except -1: |
| 69 | + """ |
| 70 | + Processes extensions to the message property object returned by the |
| 71 | + database. |
| 72 | + """ |
| 73 | + cdef: |
| 74 | + bytes text_value, binary_value, value |
| 75 | + uint32_t i, num_extensions |
| 76 | + uint16_t keyword |
| 77 | + buf.read_ub4(&num_extensions) |
| 78 | + if num_extensions > 0: |
| 79 | + buf.skip_ub1() |
| 80 | + for i in range(num_extensions): |
| 81 | + text_value = buf.read_bytes_with_length() |
| 82 | + binary_value = buf.read_bytes_with_length() |
| 83 | + value = text_value or binary_value |
| 84 | + buf.read_ub2(&keyword) |
| 85 | + if value is not None: |
| 86 | + if keyword == TNS_AQ_EXT_KEYWORD_AGENT_NAME: |
| 87 | + props_impl.sender_agent_name = value |
| 88 | + elif keyword == TNS_AQ_EXT_KEYWORD_AGENT_ADDRESS: |
| 89 | + props_impl.sender_agent_address = value |
| 90 | + elif keyword == TNS_AQ_EXT_KEYWORD_AGENT_PROTOCOL: |
| 91 | + props_impl.sender_agent_protocol = value |
| 92 | + elif keyword == TNS_AQ_EXT_KEYWORD_ORIGINAL_MSGID: |
| 93 | + props_impl.original_msg_id = value |
| 94 | + |
| 95 | + cdef bytes _process_msg_id(self, ReadBuffer buf): |
| 96 | + """ |
| 97 | + Reads a message id from the buffer and returns it. |
| 98 | + """ |
| 99 | + cdef const char_type *ptr |
| 100 | + ptr = buf.read_raw_bytes(TNS_AQ_MESSAGE_ID_LENGTH) |
| 101 | + return ptr[:TNS_AQ_MESSAGE_ID_LENGTH] |
| 102 | + |
| 103 | + cdef int _process_msg_props(self, ReadBuffer buf, |
| 104 | + ThinMsgPropsImpl props_impl) except -1: |
| 105 | + """ |
| 106 | + Processes a message property object returned by the database. |
| 107 | + """ |
| 108 | + cdef uint32_t temp32 |
| 109 | + buf.read_sb4(&props_impl.priority) |
| 110 | + buf.read_sb4(&props_impl.delay) |
| 111 | + buf.read_sb4(&props_impl.expiration) |
| 112 | + props_impl.correlation = buf.read_str_with_length() |
| 113 | + buf.read_sb4(&props_impl.num_attempts) |
| 114 | + props_impl.exceptionq = buf.read_str_with_length() |
| 115 | + buf.read_sb4(&props_impl.state) |
| 116 | + props_impl.enq_time = self._process_date(buf) |
| 117 | + props_impl.enq_txn_id = buf.read_bytes_with_length() |
| 118 | + self._process_extensions(buf, props_impl) |
| 119 | + buf.read_ub4(&temp32) # user properties |
| 120 | + if temp32 > 0: |
| 121 | + errors._raise_err(errors.ERR_NOT_IMPLEMENTED) |
| 122 | + buf.skip_ub4() # csn |
| 123 | + buf.skip_ub4() # dsn |
| 124 | + buf.skip_ub4() # flags |
| 125 | + if buf._caps.ttc_field_version >= TNS_CCAP_FIELD_VERSION_21_1: |
| 126 | + buf.skip_ub4() # shard number |
| 127 | + |
| 128 | + cdef object _process_payload(self, ReadBuffer buf): |
| 129 | + """ |
| 130 | + Processes the payload for an enqueued message returned by the database. |
| 131 | + """ |
| 132 | + cdef: |
| 133 | + ThinDbObjectImpl obj_impl |
| 134 | + uint32_t image_length |
| 135 | + bytes payload |
| 136 | + if self.queue_impl.payload_type is not None: |
| 137 | + obj_impl = buf.read_dbobject(self.queue_impl.payload_type) |
| 138 | + if obj_impl is None: |
| 139 | + obj_impl = self.queue_impl.payload_type.create_new_object() |
| 140 | + return PY_TYPE_DB_OBJECT._from_impl(obj_impl) |
| 141 | + else: |
| 142 | + buf.read_bytes_with_length() # TOID |
| 143 | + buf.read_bytes_with_length() # OID |
| 144 | + buf.read_bytes_with_length() # snapshot |
| 145 | + buf.skip_ub2() # version no |
| 146 | + buf.read_ub4(&image_length) # image length |
| 147 | + buf.skip_ub2() # flags |
| 148 | + if image_length > 0: |
| 149 | + payload = buf.read_bytes()[4:image_length] |
| 150 | + if self.queue_impl.is_json: |
| 151 | + return self.conn_impl.decode_oson(payload) |
| 152 | + return payload |
| 153 | + elif not self.queue_impl.is_json: |
| 154 | + return b'' |
| 155 | + |
| 156 | + cdef object _process_recipients(self, ReadBuffer buf): |
| 157 | + """ |
| 158 | + Process recipients for a message. Currently this is unsupported. |
| 159 | + """ |
| 160 | + cdef uint32_t temp32 |
| 161 | + buf.read_ub4(&temp32) |
| 162 | + if temp32 > 0: |
| 163 | + errors._raise_err(errors.ERR_NOT_IMPLEMENTED) |
| 164 | + return [] |
| 165 | + |
| 166 | + cdef int _write_msg_props(self, WriteBuffer buf, |
| 167 | + ThinMsgPropsImpl props_impl) except -1: |
| 168 | + """ |
| 169 | + Write a message property object to the buffer. |
| 170 | + """ |
| 171 | + buf.write_ub4(props_impl.priority) |
| 172 | + buf.write_ub4(props_impl.delay) |
| 173 | + buf.write_sb4(props_impl.expiration) |
| 174 | + self._write_value_with_length(buf, props_impl.correlation) |
| 175 | + buf.write_ub4(0) # number of attempts |
| 176 | + self._write_value_with_length(buf, props_impl.exceptionq) |
| 177 | + buf.write_ub4(props_impl.state) |
| 178 | + buf.write_ub4(0) # enqueue time length |
| 179 | + self._write_value_with_length(buf, props_impl.enq_txn_id) |
| 180 | + buf.write_ub4(4) # number of extensions |
| 181 | + buf.write_uint8(0x0e) # unknown extra byte |
| 182 | + buf.write_extension_values(None, None, TNS_AQ_EXT_KEYWORD_AGENT_NAME) |
| 183 | + buf.write_extension_values(None, None, |
| 184 | + TNS_AQ_EXT_KEYWORD_AGENT_ADDRESS) |
| 185 | + buf.write_extension_values(None, b'\x00', |
| 186 | + TNS_AQ_EXT_KEYWORD_AGENT_PROTOCOL) |
| 187 | + buf.write_extension_values(None, None, |
| 188 | + TNS_AQ_EXT_KEYWORD_ORIGINAL_MSGID) |
| 189 | + buf.write_ub4(0) # user property |
| 190 | + buf.write_ub4(0) # cscn |
| 191 | + buf.write_ub4(0) # dscn |
| 192 | + buf.write_ub4(0) # flags |
| 193 | + if buf._caps.ttc_field_version >= TNS_CCAP_FIELD_VERSION_21_1: |
| 194 | + buf.write_ub4(0xffffffffl) # shard id |
| 195 | + |
| 196 | + cdef int _write_payload(self, WriteBuffer buf, |
| 197 | + ThinMsgPropsImpl props_impl) except -1: |
| 198 | + """ |
| 199 | + Writes the payload of the message property object to the buffer. |
| 200 | + """ |
| 201 | + if self.queue_impl.is_json: |
| 202 | + buf.write_oson(props_impl.payload_obj, |
| 203 | + self.conn_impl._oson_max_fname_size, False) |
| 204 | + elif self.queue_impl.payload_type is not None: |
| 205 | + buf.write_dbobject(props_impl.payload_obj) |
| 206 | + else: |
| 207 | + buf.write_bytes(props_impl.payload_obj) |
| 208 | + |
| 209 | + cdef int _write_value_with_length(self, WriteBuffer buf, |
| 210 | + object value) except -1: |
| 211 | + """ |
| 212 | + Write a string to the buffer, prefixed by a length. |
| 213 | + """ |
| 214 | + cdef bytes value_bytes |
| 215 | + if value is None: |
| 216 | + buf.write_ub4(0) |
| 217 | + else: |
| 218 | + if isinstance(value, str): |
| 219 | + value_bytes = value.encode() |
| 220 | + else: |
| 221 | + value_bytes = value |
| 222 | + buf.write_ub4(len(value_bytes)) |
| 223 | + buf.write_bytes_with_length(value_bytes) |
0 commit comments