Debian Codegraph @

src/connection.pyx

raw
1 #-------------------------------------------------------------------------
2 # Copyright (c) Microsoft Corporation. All rights reserved.
3 # Licensed under the MIT License. See License.txt in the project root for
4 # license information.
5 #--------------------------------------------------------------------------
6
7 # Python imports
8 import logging
9 from enum import Enum
10
11 # C imports
12 from libc cimport stdint
13 cimport c_amqp_definitions
14 cimport c_connection
15 cimport c_xio
16
17
18 _logger = logging.getLogger(__name__)
19
20
21 cpdef create_connection(XIO sasl_client, const char* hostname, const char* container_id, callback_context):
22 conn = Connection()
23 conn.create(sasl_client, hostname, container_id, on_connection_state_changed, on_io_error, <void*>callback_context)
24 return conn
25
26
27 class ConnectionState(Enum):
28 START = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_START
29 HDR_RCVD = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_HDR_RCVD
30 HDR_SENT = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_HDR_SENT
31 HDR_EXCH = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_HDR_EXCH
32 OPEN_PIPE = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_OPEN_PIPE
33 OC_PIPE = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_OC_PIPE
34 OPEN_RCVD = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_OPEN_RCVD
35 OPEN_SENT = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_OPEN_SENT
36 CLOSE_PIPE = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_CLOSE_PIPE
37 OPENED = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_OPENED
38 CLOSE_RCVD = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_CLOSE_RCVD
39 CLOSE_SENT = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_CLOSE_SENT
40 DISCARDING = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_DISCARDING
41 END = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_END
42 ERROR = c_connection.CONNECTION_STATE_TAG.CONNECTION_STATE_ERROR
43 UNKNOWN = 999
44
45
46 cdef class Connection(StructBase):
47
48 cdef c_connection.CONNECTION_HANDLE _c_value
49 cdef c_connection.ON_CONNECTION_CLOSED_EVENT_SUBSCRIPTION_HANDLE _close_event
50 cdef XIO _sasl_client
51
52 def __cinit__(self):
53 pass
54
55 def __dealloc__(self):
56 _logger.debug("Deallocating Connection")
57 self.destroy()
58
59 def __enter__(self):
60 return self
61
62 def __exit__(self, *args):
63 self.destroy()
64
65 cdef _create(self):
66 if <void*>self._c_value is NULL:
67 self._memory_error()
68
69 cpdef destroy(self):
70 if <void*>self._c_value is not NULL:
71 _logger.debug("Destroying Connection")
72 c_connection.connection_destroy(self._c_value)
73 self._c_value = <c_connection.CONNECTION_HANDLE>NULL
74 self._sasl_client = None
75
76 cdef wrap(self, Connection value):
77 self.destroy()
78 self._sasl_client = value._sasl_client
79 self._c_value = value._c_value
80 self._create()
81
82 cdef create(self, XIO sasl_client, const char* hostname, const char* container_id, c_connection.ON_CONNECTION_STATE_CHANGED on_connection_state_changed, c_xio.ON_IO_ERROR on_io_error, void* callback_context):
83 self.destroy()
84 self._sasl_client = sasl_client
85 self._c_value = c_connection.connection_create2(sasl_client._c_value, hostname, container_id, NULL, NULL, on_connection_state_changed, callback_context, on_io_error, callback_context)
86 self._create()
87
88 cpdef open(self):
89 if c_connection.connection_open(self._c_value) != 0:
90 self._value_error()
91
92 cpdef close(self, const char* condition_value, const char* description):
93 if c_connection.connection_close(self._c_value, condition_value, description, <c_amqpvalue.AMQP_VALUE>NULL) != 0:
94 self._value_error()
95
96 cpdef set_trace(self, bint value):
97 c_connection.connection_set_trace(self._c_value, value)
98
99 cpdef do_work(self):
100 c_connection.connection_dowork(self._c_value)
101
102 cpdef subscribe_to_close_event(self, on_close_received):
103 self._close_event = c_connection.connection_subscribe_on_connection_close_received(
104 self._c_value,
105 <c_connection.ON_CONNECTION_CLOSE_RECEIVED>on_connection_close_received,
106 <void*>on_close_received)
107 if <void*>self._close_event == NULL:
108 self._value_error("Unable to register CLOSE event handler.")
109
110 cpdef unsubscribe_to_close_event(self):
111 c_connection.connection_unsubscribe_on_connection_close_received(self._close_event)
112
113 @property
114 def max_frame_size(self):
115 cdef stdint.uint32_t _value
116 if c_connection.connection_get_max_frame_size(self._c_value, &_value) == 0:
117 if <void*>_value == NULL:
118 return None
119 return _value
120 else:
121 self._value_error()
122
123 @max_frame_size.setter
124 def max_frame_size(self, stdint.uint32_t value):
125 if c_connection.connection_set_max_frame_size(self._c_value, value) != 0:
126 self._value_error()
127
128 @property
129 def channel_max(self):
130 cdef stdint.uint16_t _value
131 if c_connection.connection_get_channel_max(self._c_value, &_value) == 0:
132 if <void*>_value == NULL:
133 return None
134 return _value
135 else:
136 self._value_error()
137
138 @channel_max.setter
139 def channel_max(self, stdint.uint16_t value):
140 if c_connection.connection_set_channel_max(self._c_value, value) != 0:
141 self._value_error()
142
143 @property
144 def idle_timeout(self):
145 cdef c_amqp_definitions.milliseconds _value
146 if c_connection.connection_get_idle_timeout(self._c_value, &_value) == 0:
147 if <void*>_value == NULL:
148 return None
149 return _value
150 else:
151 self._value_error()
152
153 @idle_timeout.setter
154 def idle_timeout(self, c_amqp_definitions.milliseconds value):
155 if c_connection.connection_set_idle_timeout(self._c_value, value) != 0:
156 self._value_error()
157
158 @property
159 def properties(self):
160 cdef c_amqp_definitions.fields _value
161 if c_connection.connection_get_properties(self._c_value, &_value) == 0:
162 if <void*>_value == NULL:
163 return None
164 return value_factory(_value)
165 else:
166 self._value_error()
167
168 @properties.setter
169 def properties(self, AMQPValue value):
170 if c_connection.connection_set_properties(self._c_value, <c_amqp_definitions.fields>value._c_value) != 0:
171 self._value_error()
172
173 @property
174 def remote_max_frame_size(self):
175 cdef stdint.uint32_t _value
176 if c_connection.connection_get_remote_max_frame_size(self._c_value, &_value) == 0:
177 if <void*>_value == NULL:
178 return None
179 return _value
180 else:
181 self._value_error()
182
183
184 #### Callback
185
186 cdef void on_connection_state_changed(void* context, c_connection.CONNECTION_STATE_TAG new_connection_state, c_connection.CONNECTION_STATE_TAG previous_connection_state) noexcept:
187 if <void*>context != NULL:
188 context_pyobj = <PyObject*>context
189 if context_pyobj.ob_refcnt == 0: # context is being garbage collected, skip the callback
190 _logger.warning("Can't call on_connection_state_changed during garbage collection")
191 return
192 context_obj = <object>context
193 try:
194 context_obj._state_changed(previous_connection_state, new_connection_state)
195 except AttributeError:
196 _logger.info("Unknown connection state changed: %r to %r", previous_connection_state, new_connection_state)
197
198
199 cdef void on_io_error(void* context) noexcept:
200 if <void*>context != NULL:
201 context_obj = <object>context
202 if hasattr(context_obj, '_io_error'):
203 context_obj._io_error()
204
205
206 cdef void on_connection_close_received(void* context, c_amqp_definitions.ERROR_HANDLE error):
207 cdef c_amqp_definitions.ERROR_HANDLE cloned
208 context_obj = <object>context
209 if <void*> error != NULL:
210 cloned = c_amqp_definitions.error_clone(error)
211 wrapped_error = error_factory(cloned)
212 else:
213 wrapped_error = None
214 if hasattr(context_obj, '_close_received'):
215 context_obj._close_received(wrapped_error)