VM process stucks on every event using an active listener

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
nanodario
Posts: 3
Joined: 20. Aug 2015, 20:12

VM process stucks on every event using an active listener

Post by nanodario »

I have written some code to handle events generated from an IConsole object using an active listener. For some reason on every generated Event, the VM process hang (totally) and there is no way to proceed with execution.
How I should handle events using active listener? I can not find any working sample.

This code should register the listener using active mode:

Code: Select all

bool MachineBridge::registerListener()
{
	nsresult rc;

	if(session == nsnull)
		return false;
	
	rc = session->GetConsole(getter_AddRefs(console));
	if(NS_FAILED(rc))
		return false;

	rc = console->GetEventSource(getter_AddRefs(eventSource));
	if(NS_FAILED(rc))
		return false;
	
	uint32_t events[1];
	events[0] = VBoxEventType::Any; //FIXME Quick and dirty code

	rc = eventSource->RegisterListener(eventListener, 1, events, (PRBool) true);
	if(NS_FAILED(rc))
		return false;

	return true;
}
This is the handler procedure (I took a look on VirtualBox source and I get some ideas from UIMainEventListener class)

Code: Select all

STDMETHODIMP UIMainEventListener::HandleEvent(uint32_t aType, IEvent *pEvent)
{
	std::cout << "Event type: " << aType << std::endl;
	switch (aType)
	{
		case VBoxEventType::OnStateChanged:
		{
			uint32_t machineState;
			
			IStateChangedEvent *es;
			nsresult rc = CallQueryInterface<IEvent, IStateChangedEvent>(pEvent, &es);
			if(NS_SUCCEEDED(rc))
			{
				es->GetState(&machineState);
				std::cout << "machineState: " << machineState << std::endl;
			}
			
			emit sigStateChange(machineState);
			break;
		}

		case VBoxEventType::OnMachineStateChanged:
		{
			uint32_t machineState;

			IMachineStateChangedEvent *es;
			nsresult rc = CallQueryInterface<IEvent, IMachineStateChangedEvent>(pEvent, &es);
			if(NS_SUCCEEDED(rc))
			{
				es->GetState(&machineState);
				std::cout << "machineState: " << machineState << std::endl;
			}
			
			emit sigMachineStateChange(machineState);
			break;
		}
		
		/* Other cases... */
		
		default: break;
	}

	return NS_OK;
}
Is this a wrong way?
Thank you.

Virtual Box version: 4.3.10_Ubuntu
Virtual Box API Version: 4_3
Connection Type: XPCOM
Language Used: C++
OS: Kubuntu 14.04.3
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: VM process stucks on every event using an active listene

Post by noteirak »

Recommended way to use events is with a passive listener. As far as I know, this is how it's done in all implementations of VirtualBox. I can't help with the active implementation, having no experience with it. Maybe one of the devs will give some insight.
Have you tried with a passive implementation?
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
nanodario
Posts: 3
Joined: 20. Aug 2015, 20:12

Re: VM process stucks on every event using an active listene

Post by nanodario »

I trusted the VirtualBox docs which says "Passive listeners are somewhat trickier to implement [...]", but now I think it's a joke :?
Anyway, I did a little try (last night, nothing relevant) and I failed. Is it easier to implement passive listeners? Can you help me please?
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: VM process stucks on every event using an active listene

Post by noteirak »

Passive implementation is easier AFAIK. I can help you yes, but not in C++ :) I can give you Java examples which you can relate to. If that's fine with you, I'll write a sample code that listen for a VM state.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
nanodario
Posts: 3
Joined: 20. Aug 2015, 20:12

Re: VM process stucks on every event using an active listene

Post by nanodario »

I did some other experiment (this night was veeery long :shock: ) using active listeners and I found out this: once the event is generated, VM process freezes (without handling the event) and I have to interact with API (any interaction) to wake up VM process, so the event will be processed. This occur on every generated event.
noteirak wrote:Passive implementation is easier AFAIK. I can help you yes, but not in C++ :) I can give you Java examples which you can relate to. If that's fine with you, I'll write a sample code that listen for a VM state.
Anyway, if you had some sample about using passive listener, I will appreciate it :)
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: VM process stucks on every event using an active listene

Post by noteirak »

Finally got around to do it, I wanted to make a good sample code for everyone to use. Here it is.

This is a full sample code which does two things:
- Run in a dedicated thread the code managing the events
- Do some actions in the main thread that produce events to illustrate.

Expected output (order and UUID will change) which will show you what is supposed to happen:

Code: Select all

Creating VirtualBox client instance
Connecting to VirtualBox using Web Services
Connected to VirtualBox using Web Services
EventWorker started
Got event of type OnMachineRegistered
Machine b4fb5bf8-96e1-4a27-b415-03faeb3ec428 has been registered
Got event of type OnSessionStateChanged
Session state changed to Spawning for machine b4fb5bf8-96e1-4a27-b415-03faeb3ec428
Got event of type OnSessionStateChanged
Session state changed to Locked for machine b4fb5bf8-96e1-4a27-b415-03faeb3ec428
Got event of type OnMachineStateChanged
Machine b4fb5bf8-96e1-4a27-b415-03faeb3ec428 state changed to Starting
Got event of type OnMachineStateChanged
Machine b4fb5bf8-96e1-4a27-b415-03faeb3ec428 state changed to Running
Got event of type OnMachineStateChanged
Machine b4fb5bf8-96e1-4a27-b415-03faeb3ec428 state changed to Stopping
Got event of type OnMachineStateChanged
Machine b4fb5bf8-96e1-4a27-b415-03faeb3ec428 state changed to PoweredOff
Waiting for session unlock...
Got event of type OnSessionStateChanged
Session state changed to Unlocked for machine b4fb5bf8-96e1-4a27-b415-03faeb3ec428
Deleting machine
Got event of type OnMachineRegistered
Machine b4fb5bf8-96e1-4a27-b415-03faeb3ec428 has been unregistered
EventWorker finished
Event worked stopped
Disconnected from VirtualBox - bye bye!
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply