[MERGE] survey fixes
[odoo/odoo.git] / openerp / 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
26 import netsvc
27
28 #.apidoc title: Net-RPC classes
29
30 # Pickle protocol version 2 is optimized compared to default (version 0)
31 PICKLE_PROTOCOL = 2
32
33
34 class Myexception(Exception):
35     """
36     custom exception object store
37     * faultcode
38     * faulestring
39     * args
40     """
41     
42     def __init__(self, faultCode, faultString):
43         self.faultCode = faultCode
44         self.faultString = faultString
45         self.args = (faultCode, faultString)
46
47 class mysocket:
48
49     def __init__(self, sock=None):
50         if sock is None:
51             self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
52         else:
53             self.sock = sock
54         # self.sock.settimeout(120)
55         # prepare this socket for long operations: it may block for infinite
56         # time, but should exit as soon as the net is down
57         self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
58         
59     def connect(self, host, port=False):
60         if not port:
61             protocol, buf = host.split('//')
62             host, port = buf.split(':')
63         self.sock.connect((host, int(port)))
64         
65     def disconnect(self):
66         netsvc.close_socket(self.sock)
67         
68     def mysend(self, msg, exception=False, traceback=None):
69         msg = cPickle.dumps([msg, traceback], PICKLE_PROTOCOL)
70         self.sock.sendall('%8d%d%s' % (len(msg), bool(exception), msg))
71             
72     def myreceive(self):
73         buf=''
74         while len(buf) < 9:
75             chunk = self.sock.recv(9 - len(buf))
76             if not chunk:
77                 raise socket.timeout
78             buf += chunk
79         size = int(buf[:8])
80         if buf[8] != "0":
81             exception = buf[8]
82         else:
83             exception = False
84         msg = ''
85         while len(msg) < size:
86             chunk = self.sock.recv(size-len(msg))
87             if not chunk:
88                 raise socket.timeout
89             msg = msg + chunk
90         msgio = cStringIO.StringIO(msg)
91         unpickler = cPickle.Unpickler(msgio)
92         unpickler.find_global = None
93         res = unpickler.load()
94
95         if isinstance(res[0],Exception):
96             if exception:
97                 raise Myexception(str(res[0]), str(res[1]))
98             raise res[0]
99         else:
100             return res[0]
101
102 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: