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