network_event.c 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * network_event.c: network event queue processing helpers
 *
 * Copyright (C) 2013 SUSE LINUX Products GmbH, Nuernberg, Germany.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Cedric Bosdonnat
 */

#include <config.h>

#include "network_event.h"
#include "object_event.h"
#include "object_event_private.h"
#include "datatypes.h"
#include "virlog.h"

31
struct _virNetworkEvent {
32 33
    virObjectEvent parent;

34 35 36 37 38 39 40 41 42
    /* Unused attribute to get virNetworkEvent class being created */
    bool dummy;
};
typedef struct _virNetworkEvent virNetworkEvent;
typedef virNetworkEvent *virNetworkEventPtr;

struct _virNetworkEventLifecycle {
    virNetworkEvent parent;

43
    int type;
44
    int detail;
45 46 47 48
};
typedef struct _virNetworkEventLifecycle virNetworkEventLifecycle;
typedef virNetworkEventLifecycle *virNetworkEventLifecyclePtr;

49
static virClassPtr virNetworkEventClass;
50
static virClassPtr virNetworkEventLifecycleClass;
51
static void virNetworkEventDispose(void *obj);
52 53 54 55 56
static void virNetworkEventLifecycleDispose(void *obj);

static int
virNetworkEventsOnceInit(void)
{
57
    if (!(virNetworkEventClass =
58
          virClassNew(virClassForObjectEvent(),
59 60 61 62 63 64
                      "virNetworkEvent",
                      sizeof(virNetworkEvent),
                      virNetworkEventDispose)))
        return -1;
    if (!(virNetworkEventLifecycleClass =
          virClassNew(virNetworkEventClass,
65 66 67 68 69 70 71 72 73
                      "virNetworkEventLifecycle",
                      sizeof(virNetworkEventLifecycle),
                      virNetworkEventLifecycleDispose)))
        return -1;
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virNetworkEvents)

74 75 76 77 78 79 80 81
void
virNetworkEventDispose(void *obj)
{
    virNetworkEventPtr event = obj;
    VIR_DEBUG("obj=%p", event);
}


82 83 84 85 86 87 88 89
void
virNetworkEventLifecycleDispose(void *obj)
{
    virNetworkEventLifecyclePtr event = obj;
    VIR_DEBUG("obj=%p", event);
}


90
static void
91 92
virNetworkEventDispatchDefaultFunc(virConnectPtr conn,
                                   virObjectEventPtr event,
93 94
                                   virConnectObjectEventGenericCallback cb,
                                   void *cbopaque)
95 96 97 98 99
{
    virNetworkPtr net = virGetNetwork(conn, event->meta.name, event->meta.uuid);
    if (!net)
        return;

100
    switch ((virNetworkEventID)event->eventID) {
101 102 103 104 105 106 107
    case VIR_NETWORK_EVENT_ID_LIFECYCLE:
        {
            virNetworkEventLifecyclePtr networkLifecycleEvent;

            networkLifecycleEvent = (virNetworkEventLifecyclePtr)event;
            ((virConnectNetworkEventLifecycleCallback)cb)(conn, net,
                                                          networkLifecycleEvent->type,
108
                                                          networkLifecycleEvent->detail,
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
                                                          cbopaque);
            goto cleanup;
        }

    case VIR_NETWORK_EVENT_ID_LAST:
        break;
    }
    VIR_WARN("Unexpected event ID %d", event->eventID);

cleanup:
    virNetworkFree(net);
}


/**
 * virNetworkEventStateRegisterID:
 * @conn: connection to associate with callback
 * @state: object event state
 * @net: network to filter on or NULL for all networks
 * @eventID: ID of the event type to register for
 * @cb: function to add to event
 * @opaque: data blob to pass to callback
 * @freecb: callback to free @opaque
 * @callbackID: filled with callback ID
 *
 * Register the function @callbackID with connection @conn,
 * from @state, for events of type @eventID.
 *
 * Returns: the number of callbacks now registered, or -1 on error
 */
int
virNetworkEventStateRegisterID(virConnectPtr conn,
                               virObjectEventStatePtr state,
                               virNetworkPtr net,
                               int eventID,
                               virConnectObjectEventGenericCallback cb,
                               void *opaque,
                               virFreeCallback freecb,
                               int *callbackID)
{
149 150 151
    if (virNetworkEventsInitialize() < 0)
        return -1;

152 153
    if (net)
        return virObjectEventStateRegisterID(conn, state,
154 155
                                             net->uuid, net->name, 0,
                                             virNetworkEventClass, eventID,
156 157 158
                                             cb, opaque, freecb, callbackID);
    else
        return virObjectEventStateRegisterID(conn, state,
159 160
                                             NULL, NULL, 0,
                                             virNetworkEventClass, eventID,
161 162 163 164 165 166
                                             cb, opaque, freecb, callbackID);
}

virObjectEventPtr
virNetworkEventLifecycleNew(const char *name,
                            const unsigned char *uuid,
167 168
                            int type,
                            int detail)
169 170 171 172 173 174 175
{
    virNetworkEventLifecyclePtr event;

    if (virNetworkEventsInitialize() < 0)
        return NULL;

    if (!(event = virObjectEventNew(virNetworkEventLifecycleClass,
176
                                    virNetworkEventDispatchDefaultFunc,
177
                                    VIR_NETWORK_EVENT_ID_LIFECYCLE,
178 179 180 181
                                    0, name, uuid)))
        return NULL;

    event->type = type;
182
    event->detail = detail;
183 184 185

    return (virObjectEventPtr)event;
}