From 8496d93c761fd77d5e996c0faa266fb4a601458b Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Mon, 15 Mar 2010 11:40:54 +0000
Subject: [PATCH] ConfdClient: add synchronous features

By sending requests with async=False, and receiving replies with
ReceiveReply we can more easily use confd from a synchronous client.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/confd/client.py | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/lib/confd/client.py b/lib/confd/client.py
index 1b3bb9ed3..b9026760c 100644
--- a/lib/confd/client.py
+++ b/lib/confd/client.py
@@ -185,6 +185,8 @@ class ConfdClient:
     @param args: additional callback arguments
     @type coverage: integer
     @param coverage: number of remote nodes to contact
+    @type async: boolean
+    @param async: handle the write asynchronously
 
     """
     if coverage is None:
@@ -220,6 +222,9 @@ class ConfdClient:
     expire_time = now + constants.CONFD_CLIENT_EXPIRE_TIMEOUT
     self._expire_requests.append((expire_time, request.rsalt))
 
+    if not async:
+      self.FlushSendQueue()
+
   def HandleResponse(self, payload, ip, port):
     """Asynchronous handler for a confd reply
 
@@ -255,6 +260,26 @@ class ConfdClient:
     finally:
       self.ExpireRequests()
 
+  def FlushSendQueue(self):
+    """Send out all pending requests.
+
+    Can be used for synchronous client use.
+
+    """
+    while self._socket.writable():
+      self._socket.handle_write()
+
+  def ReceiveReply(self, timeout=1):
+    """Receive one reply.
+
+    @type timeout: float
+    @param timeout: how long to wait for the reply
+    @rtype: boolean
+    @return: True if some data has been handled, False otherwise
+
+    """
+    return self._socket.process_next_packet(timeout=timeout)
+
 
 # UPCALL_REPLY: server reply upcall
 # has all ConfdUpcallPayload fields populated
-- 
GitLab