~ubuntu-branches/ubuntu/maverick/libvirt/maverick

« back to all changes in this revision

Viewing changes to examples/domain-events/events-c/event-test.c

Tags: upstream-0.6.4
Import upstream version 0.6.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
116
116
            break;
117
117
        case VIR_DOMAIN_EVENT_SUSPENDED:
118
118
            if (detail == VIR_DOMAIN_EVENT_SUSPENDED_PAUSED)
119
 
                ret = "Unpaused";
 
119
                ret = "Paused";
120
120
            else if (detail == VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED)
121
121
                ret = "Migrated";
122
122
            break;
291
291
int main(int argc, char **argv)
292
292
{
293
293
    int sts;
 
294
    int callback1ret = -1;
 
295
    int callback2ret = -1;
294
296
    struct sigaction action_stop = {
295
297
        .sa_handler = stop
296
298
    };
320
322
    DEBUG0("Registering domain event cbs");
321
323
 
322
324
    /* Add 2 callbacks to prove this works with more than just one */
323
 
    virConnectDomainEventRegister(dconn, myDomainEventCallback1,
324
 
                                  strdup("callback 1"), myFreeFunc);
325
 
    virConnectDomainEventRegister(dconn, myDomainEventCallback2,
326
 
                                  strdup("callback 2"), myFreeFunc);
327
 
 
328
 
    while(run) {
329
 
        struct pollfd pfd = { .fd = h_fd,
330
 
                          .events = h_event,
331
 
                          .revents = 0};
332
 
 
333
 
        sts = poll(&pfd, 1, TIMEOUT_MS);
334
 
 
335
 
        /* We are assuming timeout of 0 here - so execute every time */
336
 
        if(t_cb && t_active)
337
 
            t_cb(t_timeout,t_opaque);
338
 
 
339
 
        if (sts == 0) {
340
 
            /* DEBUG0("Poll timeout"); */
341
 
            continue;
342
 
        }
343
 
        if (sts < 0 ) {
344
 
            DEBUG0("Poll failed");
345
 
            continue;
346
 
        }
347
 
        if ( pfd.revents & POLLHUP ) {
348
 
            DEBUG0("Reset by peer");
349
 
            return -1;
350
 
        }
351
 
 
352
 
        if(h_cb) {
353
 
            h_cb(0,
354
 
                 h_fd,
355
 
                 myPollEventToEventHandleType(pfd.revents & h_event),
356
 
                 h_opaque);
357
 
        }
 
325
    callback1ret = virConnectDomainEventRegister(dconn, myDomainEventCallback1,
 
326
                                                 strdup("callback 1"), myFreeFunc);
 
327
    callback2ret = virConnectDomainEventRegister(dconn, myDomainEventCallback2,
 
328
                                                 strdup("callback 2"), myFreeFunc);
 
329
 
 
330
    if ((callback1ret == 0) && (callback2ret == 0) ) {
 
331
        while(run) {
 
332
            struct pollfd pfd = { .fd = h_fd,
 
333
                              .events = h_event,
 
334
                              .revents = 0};
 
335
 
 
336
            sts = poll(&pfd, 1, TIMEOUT_MS);
 
337
 
 
338
            /* We are assuming timeout of 0 here - so execute every time */
 
339
            if(t_cb && t_active)
 
340
                t_cb(t_timeout,t_opaque);
 
341
 
 
342
            if (sts == 0) {
 
343
                /* DEBUG0("Poll timeout"); */
 
344
                continue;
 
345
            }
 
346
            if (sts < 0 ) {
 
347
                DEBUG0("Poll failed");
 
348
                continue;
 
349
            }
 
350
            if ( pfd.revents & POLLHUP ) {
 
351
                DEBUG0("Reset by peer");
 
352
                return -1;
 
353
            }
 
354
 
 
355
            if(h_cb) {
 
356
                h_cb(0,
 
357
                     h_fd,
 
358
                     myPollEventToEventHandleType(pfd.revents & h_event),
 
359
                     h_opaque);
 
360
            }
 
361
 
 
362
        }
 
363
 
 
364
        DEBUG0("Deregistering event handlers");
 
365
        virConnectDomainEventDeregister(dconn, myDomainEventCallback1);
 
366
        virConnectDomainEventDeregister(dconn, myDomainEventCallback2);
358
367
 
359
368
    }
360
369
 
361
 
    DEBUG0("Deregistering event handlers");
362
 
    virConnectDomainEventDeregister(dconn, myDomainEventCallback1);
363
 
    virConnectDomainEventDeregister(dconn, myDomainEventCallback2);
364
 
 
365
370
    DEBUG0("Closing connection");
366
371
    if( dconn && virConnectClose(dconn)<0 ) {
367
372
        printf("error closing\n");