5
# Copyright (C) 2007-2008 Barry Pederson <bp@barryp.org>
7
# This library is free software; you can redistribute it and/or
8
# modify it under the terms of the GNU Lesser General Public
9
# License as published by the Free Software Foundation; either
10
# version 2.1 of the License, or (at your option) any later version.
12
# This library is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
# Lesser General Public License for more details.
17
# You should have received a copy of the GNU Lesser General Public
18
# License along with this library; if not, write to the Free Software
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
22
from serialization import GenericContent
29
class Message(GenericContent):
31
A Message for use with the Channnel.basic_* methods.
35
# Instances of this class have these attributes, which
36
# are passed back and forth as message properties between
40
('content_type', 'shortstr'),
41
('content_encoding', 'shortstr'),
42
('application_headers', 'table'),
43
('delivery_mode', 'octet'),
44
('priority', 'octet'),
45
('correlation_id', 'shortstr'),
46
('reply_to', 'shortstr'),
47
('expiration', 'shortstr'),
48
('message_id', 'shortstr'),
49
('timestamp', 'timestamp'),
51
('user_id', 'shortstr'),
52
('app_id', 'shortstr'),
53
('cluster_id', 'shortstr')
56
def __init__(self, body='', children=None, **properties):
61
children: (not supported)
63
Keyword properties may include:
65
content_type: shortstr
68
content_encoding: shortstr
71
application_headers: table
72
Message header field table, a dict with string keys,
73
and string | int | Decimal | datetime | dict values.
76
Non-persistent (1) or persistent (2)
79
The message priority, 0 to 9
81
correlation_id: shortstr
82
The application correlation identifier
85
The destination to reply to
88
Message expiration specification
91
The application message identifier
93
timestamp: datetime.datetime
103
The creating application id
106
Intra-cluster routing identifier
108
Unicode bodies are encoded according to the 'content_encoding'
109
argument. If that's None, it's set to 'UTF-8' automatically.
113
msg = Message('hello world',
114
content_type='text/plain',
115
application_headers={'foo': 7})
118
if isinstance(body, unicode):
119
if properties.get('content_encoding', None) is None:
120
properties['content_encoding'] = 'UTF-8'
121
self.body = body.encode(properties['content_encoding'])
125
super(Message, self).__init__(**properties)
128
def __eq__(self, other):
130
Check if the properties and bodies of this Message and another
131
Message are the same.
133
Received messages may contain a 'delivery_info' attribute,
134
which isn't compared.
137
return super(Message, self).__eq__(other) and (self.body == other.body)