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 ##############################################################################
29 #.apidoc title: Net-RPC classes
31 class Myexception(Exception):
33 custom exception object store
39 def __init__(self, faultCode, faultString):
40 self.faultCode = faultCode
41 self.faultString = faultString
42 self.args = (faultCode, faultString)
46 def __init__(self, sock=None):
48 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
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)
56 def connect(self, host, port=False):
58 protocol, buf = host.split('//')
59 host, port = buf.split(':')
60 self.sock.connect((host, int(port)))
63 netsvc.close_socket(self.sock)
65 def mysend(self, msg, exception=False, traceback=None):
66 msg = cPickle.dumps([msg,traceback])
67 self.sock.sendall('%8d%s%s' % (len(msg), exception and "1" or "0", msg))
72 chunk = self.sock.recv(8 - len(buf))
77 buf = self.sock.recv(1)
83 while len(msg) < size:
84 chunk = self.sock.recv(size-len(msg))
88 msgio = cStringIO.StringIO(msg)
89 unpickler = cPickle.Unpickler(msgio)
90 unpickler.find_global = None
91 res = unpickler.load()
93 if isinstance(res[0],Exception):
95 raise Myexception(str(res[0]), str(res[1]))
100 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: