[FIX]crm : fixed problems regardig email_from/email_to
[odoo/odoo.git] / bin / tiny_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 import marshal
26
27 class Myexception(Exception):
28     """
29     custome exception object store 
30     * faultcode
31     * faulestring
32     * args
33     """
34     
35     def __init__(self, faultCode, faultString):
36         self.faultCode = faultCode
37         self.faultString = faultString
38         self.args = (faultCode, faultString)
39
40 class mysocket:
41
42     def __init__(self, sock=None):
43         if sock is None:
44             self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
45         else:
46             self.sock = sock
47         # self.sock.settimeout(120)
48         # prepare this socket for long operations: it may block for infinite
49         # time, but should exit as soon as the net is down
50         self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
51         
52     def connect(self, host, port=False):
53         if not port:
54             protocol, buf = host.split('//')
55             host, port = buf.split(':')
56         self.sock.connect((host, int(port)))
57         
58     def disconnect(self):
59         self.sock.shutdown(socket.SHUT_RDWR)
60         self.sock.close()
61         
62     def mysend(self, msg, exception=False, traceback=None):
63         msg = cPickle.dumps([msg,traceback])
64         size = len(msg)
65         self.sock.send('%8d' % size)
66         self.sock.send(exception and "1" or "0")
67         totalsent = 0
68         while totalsent < size:
69             sent = self.sock.send(msg[totalsent:])
70             if sent == 0:
71                 raise RuntimeError, "socket connection broken"
72             totalsent = totalsent + sent
73             
74     def myreceive(self):
75         buf=''
76         while len(buf) < 8:
77             chunk = self.sock.recv(8 - len(buf))
78             if not chunk:
79                 raise socket.timeout
80             buf += chunk
81         size = int(buf)
82         buf = self.sock.recv(1)
83         if buf != "0":
84             exception = buf
85         else:
86             exception = False
87         msg = ''
88         while len(msg) < size:
89             chunk = self.sock.recv(size-len(msg))
90             if not chunk:
91                 raise socket.timeout
92             msg = msg + chunk
93         msgio = cStringIO.StringIO(msg)
94         unpickler = cPickle.Unpickler(msgio)
95         unpickler.find_global = None
96         res = unpickler.load()
97
98         if isinstance(res[0],Exception):
99             if exception:
100                 raise Myexception(str(res[0]), str(res[1]))
101             raise res[0]
102         else:
103             return res[0]