2009-10-20 10:52:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2008-06-16 07:24:04 +00:00
|
|
|
##############################################################################
|
2009-10-14 12:32:15 +00:00
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
2008-06-16 07:24:04 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 12:32:15 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2008-06-16 07:24:04 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 12:32:15 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2008-06-16 07:24:04 +00:00
|
|
|
#
|
2009-10-14 12:32:15 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-06-16 07:24:04 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
##############################################################################
|
2009-10-14 12:32:15 +00:00
|
|
|
|
2007-01-23 07:49:58 +00:00
|
|
|
import socket
|
|
|
|
import cPickle
|
2008-08-18 22:31:40 +00:00
|
|
|
import cStringIO
|
2007-01-23 07:49:58 +00:00
|
|
|
|
2012-09-22 10:51:07 +00:00
|
|
|
import openerp.netsvc as netsvc
|
2011-06-23 09:03:57 +00:00
|
|
|
|
2012-01-23 14:29:32 +00:00
|
|
|
# Pickle protocol version 2 is optimized compared to default (version 0)
|
|
|
|
PICKLE_PROTOCOL = 2
|
|
|
|
|
2007-03-13 15:11:29 +00:00
|
|
|
class Myexception(Exception):
|
2010-03-11 05:51:33 +00:00
|
|
|
"""
|
2011-06-23 09:03:57 +00:00
|
|
|
custom exception object store
|
2010-03-11 05:51:33 +00:00
|
|
|
* faultcode
|
|
|
|
* faulestring
|
|
|
|
* args
|
|
|
|
"""
|
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def __init__(self, faultCode, faultString):
|
|
|
|
self.faultCode = faultCode
|
|
|
|
self.faultString = faultString
|
|
|
|
self.args = (faultCode, faultString)
|
2007-03-13 15:11:29 +00:00
|
|
|
|
2007-01-23 07:49:58 +00:00
|
|
|
class mysocket:
|
2010-03-11 05:51:33 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def __init__(self, sock=None):
|
|
|
|
if sock is None:
|
2009-10-28 00:40:02 +00:00
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2008-07-22 14:24:36 +00:00
|
|
|
else:
|
|
|
|
self.sock = sock
|
2009-10-28 00:40:02 +00:00
|
|
|
# self.sock.settimeout(120)
|
|
|
|
# prepare this socket for long operations: it may block for infinite
|
|
|
|
# time, but should exit as soon as the net is down
|
|
|
|
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
2010-03-11 05:51:33 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def connect(self, host, port=False):
|
|
|
|
if not port:
|
|
|
|
protocol, buf = host.split('//')
|
|
|
|
host, port = buf.split(':')
|
|
|
|
self.sock.connect((host, int(port)))
|
2010-03-11 05:51:33 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def disconnect(self):
|
2011-06-16 10:26:24 +00:00
|
|
|
netsvc.close_socket(self.sock)
|
2010-03-11 05:51:33 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def mysend(self, msg, exception=False, traceback=None):
|
2012-01-23 14:29:32 +00:00
|
|
|
msg = cPickle.dumps([msg, traceback], PICKLE_PROTOCOL)
|
|
|
|
self.sock.sendall('%8d%d%s' % (len(msg), bool(exception), msg))
|
2010-03-11 05:51:33 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def myreceive(self):
|
|
|
|
buf=''
|
2012-01-23 14:30:15 +00:00
|
|
|
while len(buf) < 9:
|
|
|
|
chunk = self.sock.recv(9 - len(buf))
|
2009-10-28 00:40:02 +00:00
|
|
|
if not chunk:
|
|
|
|
raise socket.timeout
|
2008-07-22 14:24:36 +00:00
|
|
|
buf += chunk
|
2012-01-23 14:30:15 +00:00
|
|
|
size = int(buf[:8])
|
|
|
|
if buf[8] != "0":
|
|
|
|
exception = buf[8]
|
2008-07-22 14:24:36 +00:00
|
|
|
else:
|
|
|
|
exception = False
|
|
|
|
msg = ''
|
|
|
|
while len(msg) < size:
|
|
|
|
chunk = self.sock.recv(size-len(msg))
|
2009-10-28 00:40:02 +00:00
|
|
|
if not chunk:
|
|
|
|
raise socket.timeout
|
2008-07-22 14:24:36 +00:00
|
|
|
msg = msg + chunk
|
2008-08-18 22:31:40 +00:00
|
|
|
msgio = cStringIO.StringIO(msg)
|
|
|
|
unpickler = cPickle.Unpickler(msgio)
|
|
|
|
unpickler.find_global = None
|
|
|
|
res = unpickler.load()
|
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
if isinstance(res[0],Exception):
|
|
|
|
if exception:
|
|
|
|
raise Myexception(str(res[0]), str(res[1]))
|
|
|
|
raise res[0]
|
|
|
|
else:
|
|
|
|
return res[0]
|
2011-11-22 08:58:48 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|