Commit a4e86bfd authored by Daniel P. Berrange's avatar Daniel P. Berrange

Add event loop handler callback support

Signed-off-by: default avatarDaniel P. Berrange <berrange@redhat.com>
parent be963314
......@@ -50,13 +50,7 @@ import (
var (
ignoreFuncs = []string{
/* TODO */
"virEventAddHandle",
"virEventAddTimeout",
"virEventRegisterImpl",
"virEventRemoveHandle",
"virEventRemoveTimeout",
"virEventUpdateHandle",
"virEventUpdateTimeout",
/* Wrapped in domain_events_cfuncs.go instead */
"virConnectDomainEventRegister",
......
......@@ -29,7 +29,7 @@ package libvirt
/*
#cgo pkg-config: libvirt
#include <libvirt/libvirt.h>
#include "events_cfuncs.h"
*/
import "C"
......@@ -55,3 +55,69 @@ func EventRunDefaultImpl() error {
}
return nil
}
type EventHandleCallback func(watch int, file int, events EventHandleType)
//export eventHandleCallback
func eventHandleCallback(watch int, fd int, events int, callbackID int) {
callbackFunc := getCallbackId(callbackID)
callback, ok := callbackFunc.(EventHandleCallback)
if !ok {
panic("Incorrect event handle callback data")
}
callback(watch, fd, (EventHandleType)(events))
}
func EventAddHandle(fd int, events EventHandleType, callback EventHandleCallback) (int, error) {
callbackID := registerCallbackId(callback)
ret := C.virEventAddHandle_cgo((C.int)(fd), (C.int)(events), (C.int)(callbackID))
if ret == -1 {
return 0, GetLastError()
}
return int(ret), nil
}
func EventUpdateHandle(watch int, events EventHandleType) {
C.virEventUpdateHandle((C.int)(watch), (C.int)(events))
}
func EventRemoveHandle(watch int) {
C.virEventRemoveHandle((C.int)(watch))
}
type EventTimeoutCallback func(timer int)
//export eventTimeoutCallback
func eventTimeoutCallback(timer int, callbackID int) {
callbackFunc := getCallbackId(callbackID)
callback, ok := callbackFunc.(EventTimeoutCallback)
if !ok {
panic("Incorrect event timeout callback data")
}
callback(timer)
}
func EventAddTimeout(freq int, callback EventTimeoutCallback) (int, error) {
callbackID := registerCallbackId(callback)
ret := C.virEventAddTimeout_cgo((C.int)(freq), (C.int)(callbackID))
if ret == -1 {
return 0, GetLastError()
}
return int(ret), nil
}
func EventUpdateTimeout(timer int, freq int) {
C.virEventUpdateTimeout((C.int)(timer), (C.int)(freq))
}
func EventRemoveTimeout(timer int) {
C.virEventRemoveTimeout((C.int)(timer))
}
/*
* This file is part of the libvirt-go project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Copyright (c) 2013 Alex Zorin
* Copyright (C) 2016 Red Hat, Inc.
*
*/
package libvirt
/*
#cgo pkg-config: libvirt
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include <stdint.h>
#include <stdlib.h>
#include "events_cfuncs.h"
void eventHandleCallback(int watch, int fd, int events, int callbackID);
static void eventAddHandleHelper(int watch, int fd, int events, void *opaque)
{
eventHandleCallback(watch, fd, events, (int)(intptr_t)opaque);
}
int virEventAddHandle_cgo(int fd, int events, int callbackID)
{
return virEventAddHandle(fd, events, eventAddHandleHelper, (void *)(intptr_t)callbackID, NULL);
}
void eventTimeoutCallback(int timer, int callbackID);
static void eventAddTimeoutHelper(int timer, void *opaque)
{
eventTimeoutCallback(timer, (int)(intptr_t)opaque);
}
int virEventAddTimeout_cgo(int freq, int callbackID)
{
return virEventAddTimeout(freq, eventAddTimeoutHelper, (void *)(intptr_t)callbackID, NULL);
}
*/
import "C"
/*
* This file is part of the libvirt-go project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Copyright (c) 2013 Alex Zorin
* Copyright (C) 2016 Red Hat, Inc.
*
*/
#ifndef LIBVIRT_GO_EVENTS_CFUNCS_H__
#define LIBVIRT_GO_EVENTS_CFUNCS_H__
int virEventAddHandle_cgo(int fd, int events, int callbackID);
int virEventAddTimeout_cgo(int freq, int callbackID);
#endif /* LIBVIRT_GO_EVENTS_CFUNCS_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment