package events

import (
	"log"
	"os"

	"libvirt.org/go/libvirt"
)

func Server() {
	err := libvirt.EventRegisterDefaultImpl()
	if err != nil {
		log.Fatalf("Échec d'EventRegisterDefaultImpl: %v", err)
		os.Exit(0)
	}

	conn, err := libvirt.NewConnect("qemu:///system")
	if err != nil {
		log.Println("Connexion Error")
	}
	defer conn.Close()

	conn.SetKeepAlive(5, 3)

	conn.DomainEventAgentLifecycleRegister(nil, AgentLifecycle)
	conn.DomainEventGraphicsRegister(nil, Graphics)
	conn.DomainEventJobCompletedRegister(nil, JobCompleted)
	conn.DomainEventMigrationIterationRegister(nil, MigrationIteration)
	conn.DomainEventLifecycleRegister(nil, Lifecyle)
	conn.DomainEventRebootRegister(nil, Reboot)
	conn.DomainEventWatchdogRegister(nil, Watchdog)

	for {
		libvirt.EventRunDefaultImpl()
	}
}
