removed temporary code of account fiscal year.
[odoo/odoo.git] / bin / tiny_socket.py
1 ##############################################################################
2 #
3 # Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
4 #
5 # $Id$
6 #
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
12 # Service Company
13 #
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 ###############################################################################
28 import socket
29 import cPickle
30 import marshal
31
32 class Myexception(Exception):
33         def __init__(self, faultCode, faultString):
34                 self.faultCode = faultCode
35                 self.faultString = faultString
36                 self.args = (faultCode, faultString)
37
38 class mysocket:
39         def __init__(self, sock=None):
40                 if sock is None:
41                         self.sock = socket.socket(
42                         socket.AF_INET, socket.SOCK_STREAM)
43                 else:
44                         self.sock = sock
45                 self.sock.settimeout(120)
46         def connect(self, host, port=False):
47                 if not port:
48                         protocol, buf = host.split('//')
49                         host, port = buf.split(':')
50                 self.sock.connect((host, int(port)))
51         def disconnect(self):
52                 self.sock.shutdown(socket.SHUT_RDWR)
53                 self.sock.close()
54         def mysend(self, msg, exception=False, traceback=None):
55                 msg = cPickle.dumps([msg,traceback])
56                 size = len(msg)
57                 self.sock.send('%8d' % size)
58                 self.sock.send(exception and "1" or "0")
59                 totalsent = 0
60                 while totalsent < size:
61                         sent = self.sock.send(msg[totalsent:])
62                         if sent == 0:
63                                 raise RuntimeError, "socket connection broken"
64                         totalsent = totalsent + sent
65         def myreceive(self):
66                 buf=''
67                 while len(buf) < 8:
68                         chunk = self.sock.recv(8 - len(buf))
69                         if chunk == '':
70                                 raise RuntimeError, "socket connection broken"
71                         buf += chunk
72                 size = int(buf)
73                 buf = self.sock.recv(1)
74                 if buf != "0":
75                         exception = buf
76                 else:
77                         exception = False
78                 msg = ''
79                 while len(msg) < size:
80                         chunk = self.sock.recv(size-len(msg))
81                         if chunk == '':
82                                 raise RuntimeError, "socket connection broken"
83                         msg = msg + chunk
84                 res = cPickle.loads(msg)
85                 if isinstance(res[0],Exception):
86                         if exception:
87                                 raise Myexception(str(res[0]), str(res[1]))
88                         raise res[0]
89                 else:
90                         return res[0]