Package proton :: Module _delivery
[frames] | no frames]

Source Code for Module proton._delivery

  1  # 
  2  # Licensed to the Apache Software Foundation (ASF) under one 
  3  # or more contributor license agreements.  See the NOTICE file 
  4  # distributed with this work for additional information 
  5  # regarding copyright ownership.  The ASF licenses this file 
  6  # to you under the Apache License, Version 2.0 (the 
  7  # "License"); you may not use this file except in compliance 
  8  # with the License.  You may obtain a copy of the License at 
  9  # 
 10  #   http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, 
 13  # software distributed under the License is distributed on an 
 14  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
 15  # KIND, either express or implied.  See the License for the 
 16  # specific language governing permissions and limitations 
 17  # under the License. 
 18  # 
 19   
 20  from __future__ import absolute_import 
 21   
 22  from cproton import PN_ACCEPTED, PN_MODIFIED, PN_RECEIVED, PN_REJECTED, PN_RELEASED, pn_delivery_abort, \ 
 23      pn_delivery_aborted, pn_delivery_attachments, pn_delivery_link, pn_delivery_local, pn_delivery_local_state, \ 
 24      pn_delivery_partial, pn_delivery_pending, pn_delivery_readable, pn_delivery_remote, pn_delivery_remote_state, \ 
 25      pn_delivery_settle, pn_delivery_settled, pn_delivery_tag, pn_delivery_update, pn_delivery_updated, \ 
 26      pn_delivery_writable, pn_disposition_annotations, pn_disposition_condition, pn_disposition_data, \ 
 27      pn_disposition_get_section_number, pn_disposition_get_section_offset, pn_disposition_is_failed, \ 
 28      pn_disposition_is_undeliverable, pn_disposition_set_failed, pn_disposition_set_section_number, \ 
 29      pn_disposition_set_section_offset, pn_disposition_set_undeliverable, pn_disposition_type, pn_work_next 
 30   
 31  from ._condition import cond2obj, obj2cond 
 32  from ._data import dat2obj, obj2dat 
 33  from ._wrapper import Wrapper 
34 35 36 -class NamedInt(int):
37 values = {} # type: Dict[int, str] 38
39 - def __new__(cls, i, name):
40 ni = super(NamedInt, cls).__new__(cls, i) 41 cls.values[i] = ni 42 return ni
43
44 - def __init__(self, i, name):
45 self.name = name
46
47 - def __repr__(self):
48 return self.name
49
50 - def __str__(self):
51 return self.name
52 53 @classmethod
54 - def get(cls, i):
55 return cls.values.get(i, i)
56
57 58 -class DispositionType(NamedInt):
59 values = {}
60
61 62 -class Disposition(object):
63 RECEIVED = DispositionType(PN_RECEIVED, "RECEIVED") 64 ACCEPTED = DispositionType(PN_ACCEPTED, "ACCEPTED") 65 REJECTED = DispositionType(PN_REJECTED, "REJECTED") 66 RELEASED = DispositionType(PN_RELEASED, "RELEASED") 67 MODIFIED = DispositionType(PN_MODIFIED, "MODIFIED") 68
69 - def __init__(self, impl, local):
70 self._impl = impl 71 self.local = local 72 self._data = None 73 self._condition = None 74 self._annotations = None
75 76 @property
77 - def type(self):
78 return DispositionType.get(pn_disposition_type(self._impl))
79
80 - def _get_section_number(self):
81 return pn_disposition_get_section_number(self._impl)
82
83 - def _set_section_number(self, n):
84 pn_disposition_set_section_number(self._impl, n)
85 86 section_number = property(_get_section_number, _set_section_number) 87
88 - def _get_section_offset(self):
89 return pn_disposition_get_section_offset(self._impl)
90
91 - def _set_section_offset(self, n):
92 pn_disposition_set_section_offset(self._impl, n)
93 94 section_offset = property(_get_section_offset, _set_section_offset) 95
96 - def _get_failed(self):
97 return pn_disposition_is_failed(self._impl)
98
99 - def _set_failed(self, b):
100 pn_disposition_set_failed(self._impl, b)
101 102 failed = property(_get_failed, _set_failed) 103
104 - def _get_undeliverable(self):
105 return pn_disposition_is_undeliverable(self._impl)
106
107 - def _set_undeliverable(self, b):
108 pn_disposition_set_undeliverable(self._impl, b)
109 110 undeliverable = property(_get_undeliverable, _set_undeliverable) 111
112 - def _get_data(self):
113 if self.local: 114 return self._data 115 else: 116 return dat2obj(pn_disposition_data(self._impl))
117
118 - def _set_data(self, obj):
119 if self.local: 120 self._data = obj 121 else: 122 raise AttributeError("data attribute is read-only")
123 124 data = property(_get_data, _set_data) 125
126 - def _get_annotations(self):
127 if self.local: 128 return self._annotations 129 else: 130 return dat2obj(pn_disposition_annotations(self._impl))
131
132 - def _set_annotations(self, obj):
133 if self.local: 134 self._annotations = obj 135 else: 136 raise AttributeError("annotations attribute is read-only")
137 138 annotations = property(_get_annotations, _set_annotations) 139
140 - def _get_condition(self):
141 if self.local: 142 return self._condition 143 else: 144 return cond2obj(pn_disposition_condition(self._impl))
145
146 - def _set_condition(self, obj):
147 if self.local: 148 self._condition = obj 149 else: 150 raise AttributeError("condition attribute is read-only")
151 152 condition = property(_get_condition, _set_condition)
153
154 155 -class Delivery(Wrapper):
156 """ 157 Tracks and/or records the delivery of a message over a link. 158 """ 159 160 RECEIVED = Disposition.RECEIVED 161 ACCEPTED = Disposition.ACCEPTED 162 REJECTED = Disposition.REJECTED 163 RELEASED = Disposition.RELEASED 164 MODIFIED = Disposition.MODIFIED 165 166 @staticmethod
167 - def wrap(impl):
168 if impl is None: 169 return None 170 else: 171 return Delivery(impl)
172
173 - def __init__(self, impl):
174 Wrapper.__init__(self, impl, pn_delivery_attachments)
175
176 - def _init(self):
177 self.local = Disposition(pn_delivery_local(self._impl), True) 178 self.remote = Disposition(pn_delivery_remote(self._impl), False)
179 180 @property
181 - def tag(self):
182 """The identifier for the delivery.""" 183 return pn_delivery_tag(self._impl)
184 185 @property
186 - def writable(self):
187 """Returns true for an outgoing delivery to which data can now be written.""" 188 return pn_delivery_writable(self._impl)
189 190 @property
191 - def readable(self):
192 """Returns true for an incoming delivery that has data to read.""" 193 return pn_delivery_readable(self._impl)
194 195 @property
196 - def updated(self):
197 """Returns true if the state of the delivery has been updated 198 (e.g. it has been settled and/or accepted, rejected etc).""" 199 return pn_delivery_updated(self._impl)
200
201 - def update(self, state):
202 """ 203 Set the local state of the delivery e.g. ACCEPTED, REJECTED, RELEASED. 204 """ 205 obj2dat(self.local._data, pn_disposition_data(self.local._impl)) 206 obj2dat(self.local._annotations, pn_disposition_annotations(self.local._impl)) 207 obj2cond(self.local._condition, pn_disposition_condition(self.local._impl)) 208 pn_delivery_update(self._impl, state)
209 210 @property
211 - def pending(self):
212 return pn_delivery_pending(self._impl)
213 214 @property
215 - def partial(self):
216 """ 217 Returns true for an incoming delivery if not all the data is 218 yet available. 219 """ 220 return pn_delivery_partial(self._impl)
221 222 @property
223 - def local_state(self):
224 """Returns the local state of the delivery.""" 225 return DispositionType.get(pn_delivery_local_state(self._impl))
226 227 @property
228 - def remote_state(self):
229 """ 230 Returns the state of the delivery as indicated by the remote 231 peer. 232 """ 233 return DispositionType.get(pn_delivery_remote_state(self._impl))
234 235 @property
236 - def settled(self):
237 """ 238 Returns true if the delivery has been settled by the remote peer. 239 """ 240 return pn_delivery_settled(self._impl)
241
242 - def settle(self):
243 """ 244 Settles the delivery locally. This indicates the application 245 considers the delivery complete and does not wish to receive any 246 further events about it. Every delivery should be settled locally. 247 """ 248 pn_delivery_settle(self._impl)
249 250 @property
251 - def aborted(self):
252 """Returns true if the delivery has been aborted.""" 253 return pn_delivery_aborted(self._impl)
254
255 - def abort(self):
256 """ 257 Aborts the delivery. This indicates the application wishes to 258 invalidate any data that may have already been sent on this delivery. 259 The delivery cannot be aborted after it has been completely delivered. 260 """ 261 pn_delivery_abort(self._impl)
262 263 @property
264 - def work_next(self):
265 return Delivery.wrap(pn_work_next(self._impl))
266 267 @property 274 275 @property
276 - def session(self):
277 """ 278 Returns the session over which the delivery was sent or received. 279 """ 280 return self.link.session
281 282 @property
283 - def connection(self):
284 """ 285 Returns the connection over which the delivery was sent or received. 286 """ 287 return self.session.connection
288 289 @property
290 - def transport(self):
291 return self.connection.transport
292