server.vala 4.41 KB
Newer Older
Brendan Long's avatar
Brendan Long committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* Copyright (c) 2014, CableLabs, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
26 27 28 29
errordomain RUIError {
    BAD_CONFIG
}

30
public class RUI.RemoteUIServer {
31 32 33 34 35 36 37
    static const string REMOTE_UI_SERVICE_TYPE = "urn:schemas-upnp-org:service:RemoteUIServer:1";

    string root_device_xml;
    string service_directory;

    GUPnP.Context context;
    GUPnP.RootDevice root_device;
Brendan Long's avatar
Brendan Long committed
38
    RemoteUI[] remoteUIs;
39

Brendan Long's avatar
Brendan Long committed
40 41
    public RemoteUIServer(string root_device_xml, string service_directory,
        RemoteUI[] remoteUIs) {
42 43
        this.root_device_xml = root_device_xml;
        this.service_directory = service_directory;
Brendan Long's avatar
Brendan Long committed
44
        this.remoteUIs = remoteUIs;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    }

    public void start() throws Error {
        context = new GUPnP.Context(null, null, 0);
        root_device = new GUPnP.RootDevice(context, root_device_xml,
            service_directory);
        root_device.set_available(true);
        stdout.printf("Running UPnP service on http://%s:%u/%s\n", context.host_ip, context.port, root_device.description_path);

        var service = (GUPnP.Service)root_device.get_service(REMOTE_UI_SERVICE_TYPE);
        if (service == null) {
            throw new RUIError.BAD_CONFIG(
                "Unable to get %s.".printf(REMOTE_UI_SERVICE_TYPE));
        }
        service.action_invoked["GetCompatibleUIs"].connect(on_get_compatible_uis);
        
        MainLoop loop = new MainLoop();
62 63 64 65
        Unix.signal_add(Posix.SIGINT, () => {
            loop.quit();
            return true;
        });
66
        loop.run();
67
        root_device.set_available(false);
68 69 70
    }
    
    void on_get_compatible_uis(GUPnP.ServiceAction action) {
71 72
        XMLBuilder builder = new XMLBuilder();
        builder.open_tag("uilist");
73
        foreach (RemoteUI ui in remoteUIs) {
74 75 76
            builder.open_tag("ui");
            builder.append_node("uiID", ui.id);
            builder.append_node("name", ui.name);
77
            if (ui.description != null) {
78
                builder.append_node("description", ui.description);
79
            }
80
            if (ui.icons != null && ui.icons.length > 0) {
81 82 83 84 85 86 87 88 89 90 91
                builder.open_tag("iconList");
                foreach (Icon icon in ui.icons) {
                    builder.open_tag("icon");
                    builder.append_node("url", icon.url);
                    if (icon.width != null) {
                        builder.append_node("width", icon.width.to_string());
                    }
                    if (icon.height != null) {
                        builder.append_node("height", icon.height.to_string());
                    }
                    builder.close_tag("icon");
92
                }
93
                builder.close_tag("iconList");
94
            }
95 96 97 98 99 100 101 102
            foreach (Protocol protocol in ui.protocols) {
                builder.open_tag("protocol",
                    "shortName=\"%s\"".printf(protocol.shortName));
                foreach (string url in protocol.urls) {
                    builder.append_node("uri", url);
                }
                builder.close_tag("protocol");
            }
103
            builder.close_tag("ui");
104
        }
105 106
        builder.close_tag("uilist");
        action.set_value("UIListing", builder.to_string());
107 108 109
        action.return();
    }
}