Tizen 2.1 base
[platform/upstream/hplip.git] / base / dime.py
1 # -*- coding: utf-8 -*-
2 #
3 # (c) Copyright 2003-2008 Hewlett-Packard Development Company, L.P.
4 #
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.
9 #
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.
14 #
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
18 #
19 # Author: Don Welch
20 #
21
22 # Std Lib
23 import struct
24
25 # Local
26 from g import *
27
28 # DIME constants
29 TYPE_T_MIME = 0x01
30 TYPE_T_URI = 0x02
31 DIME_VERSION = 1
32 PAD_SIZE = 4
33
34
35 class Record(object):
36     def __init__(self, id, typ, typ_code, payload):
37         self.id = id
38         self.typ = typ
39         self.typ_code = typ_code
40         self.payload = payload
41         
42
43 class Message(object):
44     def __init__(self):
45         self.records = []
46
47     def add_record(self, rec):
48         self.records.append(rec)
49         
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))
53             mb = me = cf = 0
54             if i == 0: mb = 1
55             if i == len(self.records)-1: me = 1
56                 
57             output.write(struct.pack("!B", ((DIME_VERSION & 0x1f) << 3 |
58                                             (mb & 0x01) << 2 |
59                                             (me & 0x01) << 1 |
60                                             (cf & 0x01))))
61                    
62             output.write(struct.pack("!B", ((r.typ_code & 0xf) << 4) & 0xf0))
63     
64             output.write(struct.pack("!H", 0)) # Options length
65             
66             id_len = self.bytes_needed(len(r.id))
67             output.write(struct.pack("!H", len(r.id))) # ID length
68             
69             typ_len = self.bytes_needed(len(r.typ))
70             output.write(struct.pack("!H", len(r.typ))) # Type length
71             
72             data_len = self.bytes_needed(len(r.payload))
73             output.write(struct.pack("!I", len(r.payload))) # Data length
74             
75             if id_len:
76                 output.write(struct.pack("%ds" % id_len, r.id))
77                 
78             if typ_len:
79                 output.write(struct.pack("%ds" % typ_len, r.typ))
80             
81             if data_len:
82                 output.write(struct.pack("%ds" % data_len, r.payload))
83         
84     
85     def bytes_needed(self, data_len, block_size=PAD_SIZE):
86         if data_len % block_size == 0:
87             return data_len
88         else:
89             return (data_len/block_size+1)*block_size
90             
91             
92
93
94 if __name__ == "__main__":
95     log.set_level("debug")
96     import cStringIO
97     m = Message()
98     m.add_record(Record("cid:id0", "http://schemas.xmlsoap.org/soap/envelope/", 
99                         TYPE_T_URI, "<test>test</test>"))
100     
101     m.add_record(Record("test2", "text/xml", TYPE_T_MIME, "<test>test2</test>"))
102     
103     output = cStringIO.StringIO()
104     
105     m.generate(output)
106     
107     log.log_data(output.getvalue())
108
109
110