1 # -*- coding: utf-8 -*-
3 # (c) Copyright 2003-2008 Hewlett-Packard Development Company, L.P.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 def __init__(self, id, typ, typ_code, payload):
39 self.typ_code = typ_code
40 self.payload = payload
43 class Message(object):
47 def add_record(self, rec):
48 self.records.append(rec)
50 def generate(self, output): # output is a stream type
51 for i, r in enumerate(self.records):
52 log.debug("Processing record %d (%s)" % (i, r.id))
55 if i == len(self.records)-1: me = 1
57 output.write(struct.pack("!B", ((DIME_VERSION & 0x1f) << 3 |
62 output.write(struct.pack("!B", ((r.typ_code & 0xf) << 4) & 0xf0))
64 output.write(struct.pack("!H", 0)) # Options length
66 id_len = self.bytes_needed(len(r.id))
67 output.write(struct.pack("!H", len(r.id))) # ID length
69 typ_len = self.bytes_needed(len(r.typ))
70 output.write(struct.pack("!H", len(r.typ))) # Type length
72 data_len = self.bytes_needed(len(r.payload))
73 output.write(struct.pack("!I", len(r.payload))) # Data length
76 output.write(struct.pack("%ds" % id_len, r.id))
79 output.write(struct.pack("%ds" % typ_len, r.typ))
82 output.write(struct.pack("%ds" % data_len, r.payload))
85 def bytes_needed(self, data_len, block_size=PAD_SIZE):
86 if data_len % block_size == 0:
89 return (data_len/block_size+1)*block_size
94 if __name__ == "__main__":
95 log.set_level("debug")
98 m.add_record(Record("cid:id0", "http://schemas.xmlsoap.org/soap/envelope/",
99 TYPE_T_URI, "<test>test</test>"))
101 m.add_record(Record("test2", "text/xml", TYPE_T_MIME, "<test>test2</test>"))
103 output = cStringIO.StringIO()
107 log.log_data(output.getvalue())