Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0f242dbad7
Fetching contributors…

Cannot retrieve contributors at this time

71 lines (63 sloc) 2.625 kb
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from protocol import Fragment, Header, Properties, Footer
from connection import Connection
class Message:
def __init__(self, content=None, delivery_tag=None, **kwargs):
self.delivery_tag = delivery_tag
self.header = Header() = Properties()
self.content = content
self.footer = Footer()
for k, v in kwargs.items():
for o in (self.header,, self.footer):
if hasattr(o, k):
setattr(o, k, v)
def __repr__(self):
args = []
for f in ["delivery_tag"] + [ for f in Header.FIELDS] + \
[ for f in Properties.FIELDS] + ["content"] + \
[ for f in Footer.FIELDS]:
for o in (self, self.header,, self.footer):
if hasattr(o, f):
v = getattr(o, f)
if v is not None:
args.append("%s=%r" % (f, v))
return "Message(%s)" % ", ".join(args)
# XXX: encode(message) -> fragments, decode(transfer) -> message
# XXX: frag + defrag
def encode(message, encoder=Connection.type_encoder):
# XXX: constants
head = Fragment(True, True, 0, 0, 0, encoder.encode(message.header))
prop = Fragment(True, True, 1, 1, 0, encoder.encode(
body = Fragment(True, True, 3, 2, 0, message.content)
foot = Fragment(True, True, 2, 3, 0, encoder.encode(message.footer))
return (head, prop, body, foot)
def decode(transfer, decoder):
message = Message()
message.delivery_tag = transfer.delivery_tag
fragments = transfer.fragments
message.header = decoder.decode(fragments[0].payload)[0] = decoder.decode(fragments[1].payload)[0]
message.content = fragments[2].payload
# XXX: constants
if fragments[-1].format_code == 2:
message.footer = decoder.decode(fragments[-1].payload)[0]
return message
Jump to Line
Something went wrong with that request. Please try again.