[libvirt] [dbus PATCH v4 4/5] events: Introduce virtDBusEventsDomainEventToString function

Pavel Hrdina phrdina at redhat.com
Tue Apr 3 08:43:40 UTC 2018


On Tue, Apr 03, 2018 at 10:29:59AM +0200, Katerina Koukiou wrote:
> This function replaces the manual translation of Event ENUMS.
> 
> Signed-off-by: Katerina Koukiou <kkoukiou at redhat.com>
> ---
>  src/events.c | 55 +++++++++++++++++++++----------------------------------
>  1 file changed, 21 insertions(+), 34 deletions(-)
> 
> diff --git a/src/events.c b/src/events.c
> index dada55f..1877115 100644
> --- a/src/events.c
> +++ b/src/events.c
> @@ -4,6 +4,26 @@
>  
>  #include <libvirt/libvirt.h>
>  
> +VIRT_DBUS_ENUM_DECL(virtDBusEventsDomainEvent)
> +VIRT_DBUS_ENUM_IMPL(virtDBusEventsDomainEvent,
> +                    VIR_DOMAIN_EVENT_LAST,
> +                    "DomainDefined",
> +                    "DomainUndefined",
> +                    "DomainStarted",
> +                    "DomainSuspended",
> +                    "DomainResumed",
> +                    "DomainStopped",
> +                    "DomainShutdown",
> +                    "DomainPMSuspended",
> +                    "DomainCrashed")
> +
> +static const gchar *
> +virtDBusEventsDomainEventToString(gint event)
> +{
> +    const gchar *str = virtDBusEventsDomainEventTypeToString(event);
> +    return str ? str : "unknown";
> +}
> +
>  static gint
>  virtDBusEventsDomainLifecycle(virConnectPtr connection G_GNUC_UNUSED,
>                                virDomainPtr domain,
> @@ -12,42 +32,9 @@ virtDBusEventsDomainLifecycle(virConnectPtr connection G_GNUC_UNUSED,
>                                gpointer opaque)
>  {
>      virtDBusConnect *connect = opaque;
> -    const gchar *signal = NULL;
>      const gchar *name;
>      g_autofree gchar *path = NULL;
>  
> -    switch (event) {
> -    case VIR_DOMAIN_EVENT_DEFINED:
> -        signal = "DomainDefined";
> -        break;
> -    case VIR_DOMAIN_EVENT_UNDEFINED:
> -        signal = "DomainUndefined";
> -        break;
> -    case VIR_DOMAIN_EVENT_STARTED:
> -        signal = "DomainStarted";
> -        break;
> -    case VIR_DOMAIN_EVENT_SUSPENDED:
> -        signal = "DomainSuspended";
> -        break;
> -    case VIR_DOMAIN_EVENT_RESUMED:
> -        signal = "DomainResumed";
> -        break;
> -    case VIR_DOMAIN_EVENT_STOPPED:
> -        signal = "DomainStopped";
> -        break;
> -    case VIR_DOMAIN_EVENT_SHUTDOWN:
> -        signal = "DomainShutdown";
> -        break;
> -    case VIR_DOMAIN_EVENT_PMSUSPENDED:
> -        signal = "DomainPMSuspended";
> -        break;
> -    case VIR_DOMAIN_EVENT_CRASHED:
> -        signal = "DomainCrashed";
> -        break;
> -    default:
> -        return 0;
> -    }
> -
>      name = virDomainGetName(domain);
>      path = virtDBusUtilBusPathForVirDomain(domain, connect->domainPath);
>  
> @@ -55,7 +42,7 @@ virtDBusEventsDomainLifecycle(virConnectPtr connection G_GNUC_UNUSED,
>                                    NULL,
>                                    connect->connectPath,
>                                    VIRT_DBUS_CONNECT_INTERFACE,
> -                                  signal,
> +                                  virtDBusEventsDomainEventToString(event),

I've just noticed that we should check the return value of
virtDBusEventsDomainEventToString() and if it's NULL simply "return 0"
without emitting any signal.  So this will require to use temporary
variable.  If you add something like this:

const gchar *eventStr = virtDBusEventsDomainEventToString(event);

if (!eventStr)
    return 0;

you don't have to resend it and you can push it.

Pavel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20180403/9d3700f6/attachment-0001.sig>


More information about the libvir-list mailing list