SPICE port feature

During my stance at Brno I had the pleasure of working with some of the guys at the SPICE team. The goal was to know more in depth how SPICE works and to know how to use the SPICE channels for communicating information in Fleet Commander instead of using our own service for that.

The first days there I went to lunch with Carlos Soriano, the maintainer of Nautilus, and he introduced me Pavel Grunt from SPICE team. We were talking about the possibility of using SPICE port in spice-html5, and he told me that it have to be implemented because it was not supported by that time in spice-html5.

So I asked him and Victor Toso for some directions on how spice works, message structures, and some information about the SPICE client, and after some time I submitted a patch for including the feature in spice-html5.

I thought the development would be harsher but it went very good. I have to say the SPICE project is a very interesting project to contribute to, and they have a very good team working on it, always willing to share they knowledge and some chilis :P

For now, there is only server->browser communication, but my idea is at some point implement the browser->server part.

So, after this explaination of how this came alive, I will try to explain the way you can use this in your projects.

The first thing is to create the SPICE port channel in your virtual machine. For that you can use virtmanager, or you can just edit the XML for the machine and include this code under the "devices" element:

<channel type="spiceport">
    <source channel="org.spice.myport.0" />
    <target name="org.spice.myport.0" state="connected" type="virtio" />
    <alias name="myport" />
</channel>

After that, your machine will create a character device at path /dev/virtio-ports/org.spice.myport.0

That device can be used to write or read from and receive/send information from the browser. Now, we need to modify the browser code for specifying the event listeners that will be executed when we receive data from the server. If you want to just test this, the best option is to modify the file index.html from the spice-html5 sources and uncomment the following lines:

/* SPICE port event listeners */
window.addEventListener('spice-port-data', function(event) {
    // Here we convert data to text, but really we can obtain binary data also
    var msg_text = arraybuffer_to_str(new Uint8Array(event.detail.data));
    DEBUG > 0 && console.log('SPICE port', event.detail.channel.portName, 'message text:', msg_text);
});

window.addEventListener('spice-port-event', function(event) {
    DEBUG > 0 && console.log('SPICE port', event.detail.channel.portName, 'event data:', event.detail.spiceEvent);
});

And for actually see the text messages in the console, you can just add the following right after the uncommented code:

DEBUG = 1;

Now you can just write some text to the spice port:

echo "DONDE ESTA LA BIBLIOTECA" > /dev/virtio-ports/org.spice.myport.0

And you can see the following text appearing in your browser console:

SPICE port org.spice.myport.0 message text: DONDE ESTA LA BIBLIOTECA

If there are a lot of lines in the console you can just filter using SPICE port string.

And that's it. Hope this feature will help you with your SPICE projects, and see you in next post.