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