1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
28 #.apidoc title: Net-RPC classes
30 # Pickle protocol version 2 is optimized compared to default (version 0)
34 class Myexception(Exception):
36 custom exception object store
42 def __init__(self, faultCode, faultString):
43 self.faultCode = faultCode
44 self.faultString = faultString
45 self.args = (faultCode, faultString)
49 def __init__(self, sock=None):
51 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
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)
59 def connect(self, host, port=False):
61 protocol, buf = host.split('//')
62 host, port = buf.split(':')
63 self.sock.connect((host, int(port)))
66 netsvc.close_socket(self.sock)
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))
75 chunk = self.sock.recv(9 - len(buf))
85 while len(msg) < size:
86 chunk = self.sock.recv(size-len(msg))
90 msgio = cStringIO.StringIO(msg)
91 unpickler = cPickle.Unpickler(msgio)
92 unpickler.find_global = None
93 res = unpickler.load()
95 if isinstance(res[0],Exception):
97 raise Myexception(str(res[0]), str(res[1]))
102 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: