On Wed, Sep 16, 2009 at 09:45:01AM +0100, Guido Trotter wrote:
> 
> This way it can be used by the client as well
> 
> Signed-off-by: Guido Trotter <[email protected]>
> ---
>  daemons/ganeti-confd  |   18 ++++++------------
>  lib/confd/__init__.py |   27 ++++++++++++++++++++++++++-
>  lib/errors.py         |    8 ++++++++
>  3 files changed, 40 insertions(+), 13 deletions(-)
> 
> diff --git a/daemons/ganeti-confd b/daemons/ganeti-confd
> index f64c796..22e1637 100755
> --- a/daemons/ganeti-confd
> +++ b/daemons/ganeti-confd
> @@ -43,6 +43,7 @@ from ganeti import daemon
>  from ganeti import ssconf
>  from ganeti.asyncnotifier import AsyncNotifier
>  from ganeti.confd.server import ConfdProcessor
> +from ganeti.confd import PackMagic, UnpackMagic
>  
>  
>  class ConfdAsyncUDPServer(daemon.AsyncUDPSocket):
> @@ -69,23 +70,16 @@ class ConfdAsyncUDPServer(daemon.AsyncUDPSocket):
>  
>    # this method is overriding a daemon.AsyncUDPSocket method
>    def handle_datagram(self, payload_in, ip, port):
> -
> -    if len(payload_in) < len(constants.CONFD_MAGIC_FOURCC):
> -      logging.debug("Received a query which is too short to be true")
> -      return
> -
> -    magic_number = payload_in[:4]
> -    query = payload_in[4:]
> -
> -    if magic_number != constants.CONFD_MAGIC_FOURCC:
> -      logging.debug("Received a query with an unknown magic number")
> +    try:
> +      query = UnpackMagic(payload_in)
> +    except errors.ConfdMagicError, err:
> +      logging.debug(err)
>        return
>  
>      answer =  self.processor.ExecQuery(query, ip, port)
>      if answer is not None:
> -      payload_out = ''.join([constants.CONFD_MAGIC_FOURCC, answer])
>        try:
> -        self.enqueue_send(ip, port, payload_out)
> +        self.enqueue_send(ip, port, PackMagic(answer))
>        except errors.UdpDataSizeError:
>          logging.error("Reply too big to fit in an udp packet.")
>  
> diff --git a/lib/confd/__init__.py b/lib/confd/__init__.py
> index 786de1b..2c42e9e 100644
> --- a/lib/confd/__init__.py
> +++ b/lib/confd/__init__.py
> @@ -19,6 +19,31 @@
>  # 02110-1301, USA.
>  
>  
> -"""Ganeti confd library
> +"""Ganeti confd client/server library
>  
>  """
> +
> +from ganeti import constants
> +
> +
> +def PackMagic(payload):
> +  """Prepend the confd magic fourcc to a payload.
> +
> +  """
> +  return ''.join([constants.CONFD_MAGIC_FOURCC, payload])
> +
> +
> +def UnpackMagic(payload):
> +  """Unpack and check the confd magic fourcc from a payload.
> +
> +  """
> +  if len(payload) < 4:
> +    raise errors.ConfdMagicError("UDP payload too short to contain the"
> +                                 " fourcc code")
> +
> +  magic_number = payload[:4]
> +  if magic_number != constants.CONFD_MAGIC_FOURCC:
> +    raise errors.ConfdMagicError("UDP payload contains an unkown fourcc")
> +
> +  return payload[4:]

Uh uh uh "4" "4" "4". Please don't use this. Please say plen =
lentgth(constants.CONFD_MAGIC_FOURCC) and use that in all places.

iustin

Reply via email to