Avahi publishing example in Vala

I should first of all say that I have seen a handful of Python examples for publishing services, some C ones, but none written in Vala, so I’m guessing this could be useful for some of you.

It is quite intriguing the fact that the API for publishing services is significantly different in Python compared to Vala, so it’s not straightforward at all translating the code from one language to the other.

Without further ado, the mighty code:

using Avahi;
public class AvahiPublisher {
    private string domain = "";
    private string host = "";
    private EntryGroup group = null;
    private Client client;
    private MainLoop main_loop;
    private List<EntryGroupService> services;

    public AvahiPublisher () {
        main_loop  = new MainLoop ();
    try {
        client = new Client ();
        client.state_changed.connect (on_server_state_changed);

        client.start ();

        group = new EntryGroup ();
        group.state_changed.connect (entry_group_state_changed);

        group.attach (client);

        } catch (Avahi.Error e) {
            warning (e.message);
        }
    }

    public void add_service (string service_name, string service_type, uint16 service_port, string? service_text) {
        try {
            var service = group.add_service_full (Interface.UNSPEC, Protocol.UNSPEC, (PublishFlags) 0,
                                                  service_name, service_type, domain, host, service_port,
                                                  service_text);
            services.append (service);
        } catch (Avahi.Error e) {
            warning (e.message);
        }
    }

    public void publish () {
        try {
            group.commit ();
            main_loop.run ();
        } catch (Avahi.Error e) {
            warning (e.message);
        }
    }

    public void unpublish () {
        try {
            group.reset ();
        } catch (Avahi.Error e) {
            warning (e.message);
        }
    }

    public void entry_group_state_changed (EntryGroupState state) {
        if (state == EntryGroupState.ESTABLISHED) {
            print ("Services established\n");
        } else if (state == EntryGroupState.FAILURE)
            print ("Services failed to establish\n");
        }

    public void on_server_state_changed (ClientState state) {
        if (state == ClientState.S_COLLISION)
            print ("Server collision\n");
        else if (state == ClientState.S_RUNNING)
            print ("Server running\n");
    }

    static int main (string[] args) {
        AvahiPublisher publisher = new AvahiPublisher ();
        string service_name = "Demo Service";
        // See http://www.dns-sd.org/ServiceTypes.html
        string service_type = "_demo._tcp";
        uint16 service_port = 1234;
        string service_text = "Something crazy and sassy.";

        publisher.add_service (service_name, service_type, service_port, service_text);
        publisher.publish ();

        return 0;
    }
}

I am currently using Avahi for properly exposing SPICE connections of local libvirt-machines in Boxes on the network, so there certainly will be a follow-up post regarding my progress.

For any questions or for any suggestions on improving this example, feel free to contact me at viorel.visarion@gmail.com.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s