[IMP] Added YAML for demo data.
[odoo/odoo.git] / bin / tiny_socket.py
index 8f89fe9..cb51aef 100644 (file)
@@ -1,31 +1,24 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
+#    
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 #
-# Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
+#    This program is free software: you can redistribute it and/or modify
+#    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.
 #
-# $Id$
+#    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
+#    GNU Affero General Public License for more details.
 #
-# WARNING: This program as such is intended to be used by professional
-# programmers who take the whole responsability of assessing all potential
-# consequences resulting from its eventual inadequacies and bugs
-# End users who are looking for a ready-to-use solution with commercial
-# garantees and support are strongly adviced to contract a Free Software
-# Service Company
+#    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/>.     
 #
-# This program is Free Software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# 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
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-###############################################################################
+##############################################################################
+
 import socket
 import cPickle
 import cStringIO
@@ -40,11 +33,13 @@ class Myexception(Exception):
 class mysocket:
     def __init__(self, sock=None):
         if sock is None:
-            self.sock = socket.socket(
-            socket.AF_INET, socket.SOCK_STREAM)
+            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         else:
             self.sock = sock
-        self.sock.settimeout(120)
+        # 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)
     def connect(self, host, port=False):
         if not port:
             protocol, buf = host.split('//')
@@ -68,8 +63,8 @@ class mysocket:
         buf=''
         while len(buf) < 8:
             chunk = self.sock.recv(8 - len(buf))
-            if chunk == '':
-                raise RuntimeError, "socket connection broken"
+            if not chunk:
+                raise socket.timeout
             buf += chunk
         size = int(buf)
         buf = self.sock.recv(1)
@@ -80,8 +75,8 @@ class mysocket:
         msg = ''
         while len(msg) < size:
             chunk = self.sock.recv(size-len(msg))
-            if chunk == '':
-                raise RuntimeError, "socket connection broken"
+            if not chunk:
+                raise socket.timeout
             msg = msg + chunk
         msgio = cStringIO.StringIO(msg)
         unpickler = cPickle.Unpickler(msgio)
@@ -95,5 +90,5 @@ class mysocket:
         else:
             return res[0]
 
-# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
+