document: fix regressions at storage and node_descriptor
[odoo/odoo.git] / bin / tiny_socket.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program 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
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 import socket
23 import cPickle
24 import cStringIO
25 import marshal
26
27 class Myexception(Exception):
28     def __init__(self, faultCode, faultString):
29         self.faultCode = faultCode
30         self.faultString = faultString
31         self.args = (faultCode, faultString)
32
33 class mysocket:
34     def __init__(self, sock=None):
35         if sock is None:
36             self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
37         else:
38             self.sock = sock
39         # self.sock.settimeout(120)
40         # prepare this socket for long operations: it may block for infinite
41         # time, but should exit as soon as the net is down
42         self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
43     def connect(self, host, port=False):
44         if not port:
45             protocol, buf = host.split('//')
46             host, port = buf.split(':')
47         self.sock.connect((host, int(port)))
48     def disconnect(self):
49         self.sock.shutdown(socket.SHUT_RDWR)
50         self.sock.close()
51     def mysend(self, msg, exception=False, traceback=None):
52         msg = cPickle.dumps([msg,traceback])
53         size = len(msg)
54         self.sock.send('%8d' % size)
55         self.sock.send(exception and "1" or "0")
56         totalsent = 0
57         while totalsent < size:
58             sent = self.sock.send(msg[totalsent:])
59             if sent == 0:
60                 raise RuntimeError, "socket connection broken"
61             totalsent = totalsent + sent
62     def myreceive(self):
63         buf=''
64         while len(buf) < 8:
65             chunk = self.sock.recv(8 - len(buf))
66             if not chunk:
67                 raise socket.timeout
68             buf += chunk
69         size = int(buf)
70         buf = self.sock.recv(1)
71         if buf != "0":
72             exception = buf
73         else:
74             exception = False
75         msg = ''
76         while len(msg) < size:
77             chunk = self.sock.recv(size-len(msg))
78             if not chunk:
79                 raise socket.timeout
80             msg = msg + chunk
81         msgio = cStringIO.StringIO(msg)
82         unpickler = cPickle.Unpickler(msgio)
83         unpickler.find_global = None
84         res = unpickler.load()
85
86         if isinstance(res[0],Exception):
87             if exception:
88                 raise Myexception(str(res[0]), str(res[1]))
89             raise res[0]
90         else:
91             return res[0]
92
93
94