Debian Codegraph @

ruby/lib/core/message.rb

raw
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18
19 module Qpid::Proton
20
21 # Messsage data and headers that can sent or received on a {Link}
22 #
23 # {#body} is the main message content.
24 # {#properties} is a {Hash} of extra properties that can be attached to the message.
25 #
26 # @example Create a message containing a Unicode string
27 # msg = Qpid::Proton::Message.new "this is a string"
28 #
29 # @example Create a message containing binary data
30 # msg = Qpid::Proton::Message.new
31 # msg.body = Qpid::Proton::BinaryString.new(File.binread("/home/qpid/binfile.tar.gz"))
32 #
33 class Message
34 include Util::Deprecation
35
36 # @private
37 PROTON_METHOD_PREFIX = "pn_message"
38 # @private
39 include Util::Wrapper
40
41 # Decodes a message from AMQP binary data.
42 # @param encoded [String] the encoded bytes
43 # @return[Integer] the number of bytes consumed
44 def decode(encoded)
45 check(Cproton.pn_message_decode(@impl, encoded, encoded.length))
46 post_decode
47 end
48
49 # @private
50 def post_decode
51 # decode elements from the message
52 @properties = Codec::Data.to_object(Cproton::pn_message_properties(@impl)) || {}
53 @instructions = Codec:: Data.to_object(Cproton::pn_message_instructions(@impl)) || {}
54 @annotations = Codec::Data.to_object(Cproton::pn_message_annotations(@impl)) || {}
55 @body = Codec::Data.to_object(Cproton::pn_message_body(@impl))
56 end
57
58 # Encodes the message.
59 def encode
60 pre_encode
61 size = 16
62 loop do
63 error, data = Cproton::pn_message_encode(@impl, size)
64 if error == Qpid::Proton::Error::OVERFLOW
65 size *= 2
66 else
67 check(error)
68 return data
69 end
70 end
71 end
72
73 # @private
74 def pre_encode
75 # encode elements from the message
76 Codec::Data.from_object(Cproton::pn_message_properties(@impl), [email protected]? && @properties)
77 Codec::Data.from_object(Cproton::pn_message_instructions(@impl), [email protected]? && @instructions)
78 Codec::Data.from_object(Cproton::pn_message_annotations(@impl), [email protected]? && @annotations)
79 Codec::Data.from_object(Cproton::pn_message_body(@impl), @body)
80 end
81
82 # Creates a new +Message+ instance.
83 # @param body the body of the message, equivalent to calling m.body=(body)
84 # @param opts [Hash] additional options, equivalent to +Message#key=value+ for each +key=>value+
85 def initialize(body = nil, opts={})
86 @impl = Cproton.pn_message
87 ObjectSpace.define_finalizer(self, self.class.finalize!(@impl))
88 @properties = {}
89 @instructions = {}
90 @annotations = {}
91 @body = nil
92 self.body = body unless body.nil?
93 if !opts.nil? then
94 opts.each do |k, v|
95 setter = (k.to_s+"=").to_sym()
96 self.send setter, v
97 end
98 end
99 end
100
101 # Invoked by garbage collection to clean up resources used
102 # by the underlying message implementation.
103 def self.finalize!(impl) # :nodoc:
104 proc {
105 Cproton.pn_message_free(impl)
106 }
107 end
108
109 # Returns the underlying message implementation.
110 def impl # :nodoc:
111 @impl
112 end
113
114 # Clears the state of the +Message+. This allows a single instance of
115 # +Message+ to be reused.
116 #
117 def clear
118 Cproton.pn_message_clear(@impl)
119 @properties.clear unless @properties.nil?
120 @instructions.clear unless @instructions.nil?
121 @annotations.clear unless @annotations.nil?
122 @body = nil
123 end
124
125 # Returns the most recent error number.
126 #
127 def errno
128 Cproton.pn_message_errno(@impl)
129 end
130
131 # Returns the most recent error message.
132 #
133 def error
134 Cproton.pn_error_text(Cproton.pn_message_error(@impl))
135 end
136
137 # Returns whether there is currently an error reported.
138 #
139 def error?
140 !Cproton.pn_message_errno(@impl).zero?
141 end
142
143 # Sets the durable flag.
144 #
145 # See ::durable for more details on message durability.
146 #
147 # ==== Options
148 #
149 # * state - the durable state
150 #
151 def durable=(state)
152 raise TypeError.new("state cannot be nil") if state.nil?
153 Cproton.pn_message_set_durable(@impl, state)
154 end
155
156 # Returns the durable property.
157 #
158 # The durable property indicates that the emessage should be held durably
159 # by any intermediaries taking responsibility for the message.
160 #
161 # ==== Examples
162 #
163 # msg = Qpid::Proton::Message.new
164 # msg.durable = true
165 #
166 def durable
167 Cproton.pn_message_is_durable(@impl)
168 end
169
170 # Sets the priority.
171 #
172 # +NOTE:+ Priority values are limited to the range [0,255].
173 #
174 # ==== Options
175 #
176 # * priority - the priority value
177 #
178 def priority=(priority)
179 raise TypeError.new("invalid priority: #{priority}") if not priority.is_a?(Numeric)
180 raise RangeError.new("priority out of range: #{priority}") if ((priority > 255) || (priority < 0))
181 Cproton.pn_message_set_priority(@impl, priority.floor)
182 end
183
184 # Returns the priority.
185 #
186 def priority
187 Cproton.pn_message_get_priority(@impl)
188 end
189
190 # Sets the time-to-live for the message.
191 #
192 # ==== Options
193 #
194 # * time - the time in milliseconds
195 #
196 def ttl=(time)
197 raise TypeError.new("invalid ttl: #{time}") if not time.is_a?(Numeric)
198 raise RangeError.new("ttl out of range: #{time}") if ((time.to_i < 0))
199 Cproton.pn_message_set_ttl(@impl, time.floor)
200 end
201
202 # Returns the time-to-live, in milliseconds.
203 #
204 def ttl
205 Cproton.pn_message_get_ttl(@impl)
206 end
207
208 # Sets whether this is the first time the message was acquired.
209 #
210 # See ::first_acquirer? for more details.
211 #
212 # ==== Options
213 #
214 # * state - true if claiming the message
215 #
216 def first_acquirer=(state)
217 raise TypeError.new("invalid state: #{state}") if state.nil? || !([TrueClass, FalseClass].include?(state.class))
218 Cproton.pn_message_set_first_acquirer(@impl, state)
219 end
220
221 # Sets the delivery count for the message.
222 #
223 # See ::delivery_count for more details.
224 #
225 # ==== Options
226 #
227 # * count - the delivery count
228 #
229 def delivery_count=(count)
230 raise ::ArgumentError.new("invalid count: #{count}") if not count.is_a?(Numeric)
231 raise RangeError.new("count out of range: #{count}") if count < 0
232 Cproton.pn_message_set_delivery_count(@impl, count.floor)
233 end
234
235 # Returns the delivery count for the message.
236 #
237 # This is the number of delivery attempts for the given message.
238 #
239 def delivery_count
240 Cproton.pn_message_get_delivery_count(@impl)
241 end
242
243 # Returns whether this is the first acquirer.
244 #
245 #
246 def first_acquirer?
247 Cproton.pn_message_is_first_acquirer(@impl)
248 end
249
250 # Sets the message id.
251 #
252 # ==== Options
253 #
254 # * id = the id
255 #
256 def id=(id)
257 Cproton.pn_message_set_id(@impl, id)
258 end
259
260 # Returns the message id.
261 #
262 def id
263 Cproton.pn_message_get_id(@impl)
264 end
265
266 # Sets the user id.
267 #
268 # ==== Options
269 #
270 # * id - the user id
271 #
272 def user_id=(id)
273 Cproton.pn_message_set_user_id(@impl, id)
274 end
275
276 # Returns the user id.
277 #
278 def user_id
279 Cproton.pn_message_get_user_id(@impl)
280 end
281
282 # @param address[String] set the destination address
283 def to=(address)
284 Cproton.pn_message_set_address(@impl, address)
285 end
286 alias address= to=
287
288 # @return [String] get the destination address.
289 def to
290 Cproton.pn_message_get_address(@impl)
291 end
292
293 alias address to
294
295 # Sets the subject.
296 #
297 # ==== Options
298 #
299 # * subject - the subject
300 #
301 def subject=(subject)
302 Cproton.pn_message_set_subject(@impl, subject)
303 end
304
305 # Returns the subject
306 #
307 def subject
308 Cproton.pn_message_get_subject(@impl)
309 end
310
311 # Sets the reply-to address.
312 #
313 # ==== Options
314 #
315 # * address - the reply-to address
316 #
317 def reply_to=(address)
318 Cproton.pn_message_set_reply_to(@impl, address)
319 end
320
321 # Returns the reply-to address
322 #
323 def reply_to
324 Cproton.pn_message_get_reply_to(@impl)
325 end
326
327 # Sets the correlation id.
328 #
329 # ==== Options
330 #
331 # * id - the correlation id
332 #
333 def correlation_id=(id)
334 Cproton.pn_message_set_correlation_id(@impl, id)
335 end
336
337 # Returns the correlation id.
338 #
339 def correlation_id
340 Cproton.pn_message_get_correlation_id(@impl)
341 end
342
343 # Sets the content type.
344 #
345 # ==== Options
346 #
347 # * content_type - the content type
348 #
349 def content_type=(content_type)
350 Cproton.pn_message_set_content_type(@impl, content_type)
351 end
352
353 # Returns the content type
354 #
355 def content_type
356 Cproton.pn_message_get_content_type(@impl)
357 end
358
359 # @deprecated use {#body=}
360 def content=(content)
361 deprecated __method__, "body="
362 Cproton.pn_message_load(@impl, content)
363 end
364
365 # @deprecated use {#body}
366 def content
367 deprecated __method__, "body"
368 size = 16
369 loop do
370 result = Cproton.pn_message_save(@impl, size)
371 error = result[0]
372 data = result[1]
373 if error == Qpid::Proton::Error::OVERFLOW
374 size = size * 2
375 else
376 check(error)
377 return data
378 end
379 end
380 end
381
382 # Sets the content encoding type.
383 #
384 # ==== Options
385 #
386 # * encoding - the content encoding
387 #
388 def content_encoding=(encoding)
389 Cproton.pn_message_set_content_encoding(@impl, encoding)
390 end
391
392 # Returns the content encoding type.
393 #
394 def content_encoding
395 Cproton.pn_message_get_content_encoding(@impl)
396 end
397
398 # Sets the expiration time.
399 #
400 # ==== Options
401 #
402 # * time - the expiry time
403 #
404 def expires=(time)
405 raise TypeError.new("invalid expiry time: #{time}") if time.nil?
406 raise ::ArgumentError.new("expiry time cannot be negative: #{time}") if time < 0
407 Cproton.pn_message_set_expiry_time(@impl, time)
408 end
409
410 # Returns the expiration time.
411 #
412 def expires
413 Cproton.pn_message_get_expiry_time(@impl)
414 end
415
416 # Sets the creation time.
417 #
418 # ==== Options
419 #
420 # * time - the creation time
421 #
422 def creation_time=(time)
423 raise TypeError.new("invalid time: #{time}") if time.nil?
424 raise ::ArgumentError.new("time cannot be negative") if time < 0
425 Cproton.pn_message_set_creation_time(@impl, time)
426 end
427
428 # Returns the creation time.
429 #
430 def creation_time
431 Cproton.pn_message_get_creation_time(@impl)
432 end
433
434 # Sets the group id.
435 #
436 # ==== Options
437 #
438 # * id - the group id
439 #
440 def group_id=(id)
441 Cproton.pn_message_set_group_id(@impl, id)
442 end
443
444 # Returns the group id.
445 #
446 def group_id
447 Cproton.pn_message_get_group_id(@impl)
448 end
449
450 # Sets the group sequence number.
451 #
452 # ==== Options
453 #
454 # * seq - the sequence number
455 #
456 def group_sequence=(seq)
457 raise TypeError.new("invalid seq: #{seq}") if seq.nil?
458 Cproton.pn_message_set_group_sequence(@impl, seq)
459 end
460
461 # Returns the group sequence number.
462 #
463 def group_sequence
464 Cproton.pn_message_get_group_sequence(@impl)
465 end
466
467 # Sets the reply-to group id.
468 #
469 # ==== Options
470 #
471 # * id - the id
472 #
473 def reply_to_group_id=(id)
474 Cproton.pn_message_set_reply_to_group_id(@impl, id)
475 end
476
477 # Returns the reply-to group id.
478 #
479 def reply_to_group_id
480 Cproton.pn_message_get_reply_to_group_id(@impl)
481 end
482
483 # @return [Hash] Application properties for the message
484 attr_accessor :properties
485
486 # Equivalent to +{#properties}[name] = value+
487 def []=(name, value) @properties[name] = value; end
488
489 # Equivalent to +{#properties}[name]+
490 def [](name) @properties[name]; end
491
492 # Equivalent to +{#properties}.delete(name)+
493 def delete_property(name) @properties.delete(name); end
494
495 # @return [Hash] Delivery instructions for this message.
496 attr_accessor :instructions
497
498 # @return [Hash] Delivery annotations for this message.
499 attr_accessor :annotations
500
501 # @return [Object] body of the message.
502 attr_accessor :body
503
504 def inspect() pre_encode; super; end
505
506 private
507
508 def check(err) # :nodoc:
509 if err < 0
510 raise TypeError, "[#{err}]: #{Cproton.pn_message_error(@data)}"
511 else
512 return err
513 end
514 end
515 end
516
517 end