storage_event.c 9.9 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/*
 * storage_event.c: storage event queue processing helpers
 *
 * Copyright (C) 2010-2014 Red Hat, Inc.
 * Copyright (C) 2008 VirtualIron
 * 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/>.
*/

#include <config.h>

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

VIR_LOG_INIT("conf.storage_event");

struct _virStoragePoolEvent {
    virObjectEvent parent;

    /* Unused attribute to allow for subclass creation */
    bool dummy;
};
typedef struct _virStoragePoolEvent virStoragePoolEvent;
typedef virStoragePoolEvent *virStoragePoolEventPtr;

struct _virStoragePoolEventLifecycle {
    virStoragePoolEvent parent;

    int type;
    int detail;
};
typedef struct _virStoragePoolEventLifecycle virStoragePoolEventLifecycle;
typedef virStoragePoolEventLifecycle *virStoragePoolEventLifecyclePtr;

51 52 53 54 55 56 57 58
struct _virStoragePoolEventRefresh {
    virStoragePoolEvent parent;

    bool dummy;
};
typedef struct _virStoragePoolEventRefresh virStoragePoolEventRefresh;
typedef virStoragePoolEventRefresh *virStoragePoolEventRefreshPtr;

59 60
static virClassPtr virStoragePoolEventClass;
static virClassPtr virStoragePoolEventLifecycleClass;
61
static virClassPtr virStoragePoolEventRefreshClass;
62 63
static void virStoragePoolEventDispose(void *obj);
static void virStoragePoolEventLifecycleDispose(void *obj);
64
static void virStoragePoolEventRefreshDispose(void *obj);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

static int
virStoragePoolEventsOnceInit(void)
{
    if (!(virStoragePoolEventClass =
          virClassNew(virClassForObjectEvent(),
                      "virStoragePoolEvent",
                      sizeof(virStoragePoolEvent),
                      virStoragePoolEventDispose)))
        return -1;
    if (!(virStoragePoolEventLifecycleClass =
          virClassNew(virStoragePoolEventClass,
                      "virStoragePoolEventLifecycle",
                      sizeof(virStoragePoolEventLifecycle),
                      virStoragePoolEventLifecycleDispose)))
        return -1;
81 82 83 84 85 86
    if (!(virStoragePoolEventRefreshClass =
          virClassNew(virStoragePoolEventClass,
                      "virStoragePoolEventRefresh",
                      sizeof(virStoragePoolEventRefresh),
                      virStoragePoolEventRefreshDispose)))
        return -1;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    return 0;
}

VIR_ONCE_GLOBAL_INIT(virStoragePoolEvents)

static void
virStoragePoolEventDispose(void *obj)
{
    virStoragePoolEventPtr event = obj;
    VIR_DEBUG("obj=%p", event);
}


static void
virStoragePoolEventLifecycleDispose(void *obj)
{
    virStoragePoolEventLifecyclePtr event = obj;
    VIR_DEBUG("obj=%p", event);
}


108 109 110 111 112 113 114 115
static void
virStoragePoolEventRefreshDispose(void *obj)
{
    virStoragePoolEventRefreshPtr event = obj;
    VIR_DEBUG("obj=%p", event);
}


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
static void
virStoragePoolEventDispatchDefaultFunc(virConnectPtr conn,
                                       virObjectEventPtr event,
                                       virConnectObjectEventGenericCallback cb,
                                       void *cbopaque)
{
    virStoragePoolPtr pool = virGetStoragePool(conn,
                                               event->meta.name,
                                               event->meta.uuid,
                                               NULL, NULL);
    if (!pool)
        return;

    switch ((virStoragePoolEventID)event->eventID) {
    case VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE:
        {
            virStoragePoolEventLifecyclePtr storagePoolLifecycleEvent;

            storagePoolLifecycleEvent = (virStoragePoolEventLifecyclePtr)event;
            ((virConnectStoragePoolEventLifecycleCallback)cb)(conn, pool,
                                                              storagePoolLifecycleEvent->type,
                                                              storagePoolLifecycleEvent->detail,
                                                              cbopaque);
            goto cleanup;
        }

142 143 144 145 146 147 148
    case VIR_STORAGE_POOL_EVENT_ID_REFRESH:
        {
            ((virConnectStoragePoolEventGenericCallback)cb)(conn, pool,
                                                            cbopaque);
            goto cleanup;
        }

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    case VIR_STORAGE_POOL_EVENT_ID_LAST:
        break;
    }
    VIR_WARN("Unexpected event ID %d", event->eventID);

 cleanup:
    virObjectUnref(pool);
}


/**
 * virStoragePoolEventStateRegisterID:
 * @conn: connection to associate with callback
 * @state: object event state
 * @pool: storage pool to filter on or NULL for all storage pools
 * @eventID: ID of the event type to register for
 * @cb: function to invoke when event occurs
 * @opaque: data blob to pass to @callback
 * @freecb: callback to free @opaque
 * @callbackID: filled with callback ID
 *
 * Register the function @cb with connection @conn, from @state, for
 * events of type @eventID, and return the registration handle in
 * @callbackID.
 *
 * Returns: the number of callbacks now registered, or -1 on error
 */
int
virStoragePoolEventStateRegisterID(virConnectPtr conn,
                                   virObjectEventStatePtr state,
                                   virStoragePoolPtr pool,
                                   int eventID,
                                   virConnectStoragePoolEventGenericCallback cb,
                                   void *opaque,
                                   virFreeCallback freecb,
                                   int *callbackID)
{
    if (virStoragePoolEventsInitialize() < 0)
        return -1;

    return virObjectEventStateRegisterID(conn, state, pool ? pool->uuid : NULL,
                                         NULL, NULL,
                                         virStoragePoolEventClass, eventID,
                                         VIR_OBJECT_EVENT_CALLBACK(cb),
                                         opaque, freecb,
                                         false, callbackID, false);
}


/**
 * virStoragePoolEventStateRegisterClient:
 * @conn: connection to associate with callback
 * @state: object event state
 * @pool: storage pool to filter on or NULL for all storage pools
 * @eventID: ID of the event type to register for
 * @cb: function to invoke when event occurs
 * @opaque: data blob to pass to @callback
 * @freecb: callback to free @opaque
 * @callbackID: filled with callback ID
 *
 * Register the function @cb with connection @conn, from @state, for
 * events of type @eventID, and return the registration handle in
 * @callbackID.  This version is intended for use on the client side
 * of RPC.
 *
 * Returns: the number of callbacks now registered, or -1 on error
 */
int
virStoragePoolEventStateRegisterClient(virConnectPtr conn,
                                       virObjectEventStatePtr state,
                                       virStoragePoolPtr pool,
                                       int eventID,
                                       virConnectStoragePoolEventGenericCallback cb,
                                       void *opaque,
                                       virFreeCallback freecb,
                                       int *callbackID)
{
    if (virStoragePoolEventsInitialize() < 0)
        return -1;

    return virObjectEventStateRegisterID(conn, state,  pool ? pool->uuid : NULL,
                                         NULL, NULL,
                                         virStoragePoolEventClass, eventID,
                                         VIR_OBJECT_EVENT_CALLBACK(cb),
                                         opaque, freecb,
                                         false, callbackID, true);
}


/**
 * virStoragePoolEventLifecycleNew:
 * @name: name of the storage pool object the event describes
 * @uuid: uuid of the storage pool object the event describes
 * @type: type of lifecycle event
 * @detail: more details about @type
 *
 * Create a new storage pool lifecycle event.
 */
virObjectEventPtr
virStoragePoolEventLifecycleNew(const char *name,
                                const unsigned char *uuid,
                                int type,
                                int detail)
{
    virStoragePoolEventLifecyclePtr event;

    if (virStoragePoolEventsInitialize() < 0)
        return NULL;

    if (!(event = virObjectEventNew(virStoragePoolEventLifecycleClass,
                                    virStoragePoolEventDispatchDefaultFunc,
                                    VIR_STORAGE_POOL_EVENT_ID_LIFECYCLE,
                                    0, name, uuid)))
        return NULL;

    event->type = type;
    event->detail = detail;

    return (virObjectEventPtr)event;
}
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294


/**
 * virStoragePoolEventRefreshNew:
 * @name: name of the storage pool object the event describes
 * @uuid: uuid of the storage pool object the event describes
 *
 * Create a new storage pool refresh event.
 */
virObjectEventPtr
virStoragePoolEventRefreshNew(const char *name,
                              const unsigned char *uuid)
{
    virStoragePoolEventRefreshPtr event;

    if (virStoragePoolEventsInitialize() < 0)
        return NULL;

    if (!(event = virObjectEventNew(virStoragePoolEventRefreshClass,
                                    virStoragePoolEventDispatchDefaultFunc,
                                    VIR_STORAGE_POOL_EVENT_ID_REFRESH,
                                    0, name, uuid)))
        return NULL;

    return (virObjectEventPtr)event;
}