commit dcd511c8c4e847de08a202f12f5f47abc3bed72d
Author: Guido Trotter <ultrotter@google.com>
Date:   Tue Mar 16 13:59:25 2010 +0000

    move http.WaitForSocketCondition to utils
    
    Signed-off-by: Guido Trotter <ultrotter@google.com>
    Reviewed-by: Michael Hanselmann <hansmi@google.com>

diff --git a/lib/http/__init__.py b/lib/http/__init__.py
index 1d32e64..d0db1ce 100644
--- a/lib/http/__init__.py
+++ b/lib/http/__init__.py
@@ -311,44 +311,6 @@ class HttpJsonConverter: # pylint: disable-msg=W0232
     return serializer.LoadJson(data)
 
 
-def WaitForSocketCondition(sock, event, timeout):
-  """Waits for a condition to occur on the socket.
-
-  @type sock: socket
-  @param sock: Wait for events on this socket
-  @type event: int
-  @param event: ORed condition (see select module)
-  @type timeout: float or None
-  @param timeout: Timeout in seconds
-  @rtype: int or None
-  @return: None for timeout, otherwise occured conditions
-
-  """
-  check = (event | select.POLLPRI |
-           select.POLLNVAL | select.POLLHUP | select.POLLERR)
-
-  if timeout is not None:
-    # Poller object expects milliseconds
-    timeout *= 1000
-
-  poller = select.poll()
-  poller.register(sock, event)
-  try:
-    while True:
-      # TODO: If the main thread receives a signal and we have no timeout, we
-      # could wait forever. This should check a global "quit" flag or
-      # something every so often.
-      io_events = poller.poll(timeout)
-      if not io_events:
-        # Timeout
-        return None
-      for (_, evcond) in io_events:
-        if evcond & check:
-          return evcond
-  finally:
-    poller.unregister(sock)
-
-
 def SocketOperation(sock, op, arg1, timeout):
   """Wrapper around socket functions.
 
@@ -399,7 +361,7 @@ def SocketOperation(sock, op, arg1, timeout):
       else:
         wait_for_event = event_poll
 
-      event = WaitForSocketCondition(sock, wait_for_event, timeout)
+      event = utils.WaitForSocketCondition(sock, wait_for_event, timeout)
       if event is None:
         raise HttpSocketTimeout()
 
diff --git a/lib/http/client.py b/lib/http/client.py
index ba27d1a..6b70a04 100644
--- a/lib/http/client.py
+++ b/lib/http/client.py
@@ -37,6 +37,7 @@ import threading
 
 from ganeti import workerpool
 from ganeti import http
+from ganeti import utils
 
 
 HTTP_CLIENT_THREADS = 10
@@ -249,8 +250,8 @@ class HttpClientRequestExecutor(http.HttpBase):
 
     if not connected:
       # Wait for connection
-      event = http.WaitForSocketCondition(self.sock, select.POLLOUT,
-                                          self.CONNECT_TIMEOUT)
+      event = utils.WaitForSocketCondition(self.sock, select.POLLOUT,
+                                           self.CONNECT_TIMEOUT)
       if event is None:
         raise http.HttpError("Timeout while connecting to server")
 
diff --git a/lib/utils.py b/lib/utils.py
index edc92b4..7f2c861 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -1497,6 +1497,44 @@ except NameError:
     return False
 
 
+def WaitForSocketCondition(sock, event, timeout):
+  """Waits for a condition to occur on the socket.
+
+  @type sock: socket
+  @param sock: Wait for events on this socket
+  @type event: int
+  @param event: ORed condition (see select module)
+  @type timeout: float or None
+  @param timeout: Timeout in seconds
+  @rtype: int or None
+  @return: None for timeout, otherwise occured conditions
+
+  """
+  check = (event | select.POLLPRI |
+           select.POLLNVAL | select.POLLHUP | select.POLLERR)
+
+  if timeout is not None:
+    # Poller object expects milliseconds
+    timeout *= 1000
+
+  poller = select.poll()
+  poller.register(sock, event)
+  try:
+    while True:
+      # TODO: If the main thread receives a signal and we have no timeout, we
+      # could wait forever. This should check a global "quit" flag or
+      # something every so often.
+      io_events = poller.poll(timeout)
+      if not io_events:
+        # Timeout
+        return None
+      for (_, evcond) in io_events:
+        if evcond & check:
+          return evcond
+  finally:
+    poller.unregister(sock)
+
+
 def partition(seq, pred=bool): # # pylint: disable-msg=W0622
   "Partition a list in two, based on the given predicate"
   return (list(itertools.ifilter(pred, seq)),
