Parcourir la source

[1513] refactoring: extract socket send operation into a separate method.

this will make the planned implementation for this ticket simpler, and
will also help further unrelated extensions such as TCP support.
JINMEI Tatuya il y a 13 ans
Parent
commit
93d4730a61
1 fichiers modifiés avec 24 ajouts et 3 suppressions
  1. 24 3
      src/bin/ddns/ddns.py.in

+ 24 - 3
src/bin/ddns/ddns.py.in

@@ -371,11 +371,32 @@ class DDNSServer:
             msg.to_wire(self.__response_renderer, tsig_ctx)
         else:
             msg.to_wire(self.__response_renderer)
+
+        return self.__send_response(sock, self.__response_renderer.get_data(),
+                                    remote_addr)
+
+    def __send_response(self, sock, data, dest):
+        '''Send DDNS response to the client.
+
+        Right now, this is a straightforward subroutine of handle_request(),
+        but is intended to be extended evetually so that it can handle more
+        comlicated operations for TCP (which requires asynchronous write).
+        Further, when we support multiple requests over a single TCP
+        connection, this method may even be shared by multiple methods.
+
+        Parameters:
+        sock: (python socket) the socket to which the response should be sent.
+        data: (binary) the response data
+        dest: (python socket address) the destion address to which the response
+          should be sent.
+
+        Return: True if the send operation succeds; otherwise False.
+
+        '''
         try:
-            sock.sendto(self.__response_renderer.get_data(), remote_addr)
+            sock.sendto(data, dest)
         except socket.error as ex:
-            logger.error(DDNS_RESPONSE_SOCKET_ERROR,
-                         ClientFormatter(remote_addr), ex)
+            logger.error(DDNS_RESPONSE_SOCKET_ERROR, ClientFormatter(dest), ex)
             return False
 
         return True