summaryrefslogtreecommitdiff
path: root/src/librygel-core/rygel-connection-manager.vala
blob: c47a1be1f77769e4df43ebd4c863acb2cc9f9a79 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright (C) 2008 OpenedHand Ltd.
 * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
 *
 * Author: Jorn Baayen <jorn@openedhand.com>
 *         Zeeshan Ali <zeenix@gmail.com>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using GLib;
using GUPnP;

/**
 * Basic implementation of UPnP ConnectionManager service version 2.
 */
public class Rygel.ConnectionManager : Service {
    public const string UPNP_ID = "urn:upnp-org:serviceId:ConnectionManager";
    public const string UPNP_TYPE =
                    "urn:schemas-upnp-org:service:ConnectionManager:2";
    public const string DESCRIPTION_PATH = "xml/ConnectionManager.xml";

    protected string sink_protocol_info;
    protected string connection_ids;
    protected string source_protocol_info;

    protected int rcs_id;
    protected int av_transport_id;
    protected string direction;

    public override void constructed () {
        base.constructed ();

        this.sink_protocol_info   = "";
        this.source_protocol_info = "";
        this.connection_ids       = "0";

        this.query_variable["SourceProtocolInfo"].connect
                                        (this.query_source_protocol_info_cb);
        this.query_variable["SinkProtocolInfo"].connect
                                        (this.query_sink_protocol_info_cb);
        this.query_variable["CurrentConnectionIDs"].connect
                                        (this.query_current_connection_ids_cb);

        this.action_invoked["GetProtocolInfo"].connect
                                        (this.get_protocol_info_cb);
        this.action_invoked["GetCurrentConnectionIDs"].connect
                                        (this.get_current_connection_ids_cb);
        this.action_invoked["GetCurrentConnectionInfo"].connect
                                        (this.get_current_connection_info_cb);
    }

    public virtual string get_current_protocol_info () {
        return "";
    }

    private void query_source_protocol_info_cb (Service   cm,
                                                string    var,
                                                ref Value val) {
        val.init (typeof (string));
        val.set_string (source_protocol_info);
    }

    private void query_sink_protocol_info_cb (Service   cm,
                                              string    var,
                                              ref Value val) {
        val.init (typeof (string));
        val.set_string (sink_protocol_info);
    }

    private void query_current_connection_ids_cb (Service   cm,
                                                  string    var,
                                                  ref Value val) {
        val.init (typeof (string));
        val.set_string (connection_ids);
    }

    private void get_protocol_info_cb (Service             cm,
                                       ServiceAction action) {
        if (action.get_argument_count () != 0) {
            action.return_error (402, _("Invalid argument"));

            return;
        }

        action.set ("Source", typeof (string), source_protocol_info,
                    "Sink",   typeof (string), sink_protocol_info);

        action.return_success ();
    }

    private void get_current_connection_ids_cb (Service             cm,
                                                ServiceAction action) {
        if (action.get_argument_count () != 0) {
            action.return_error (402, _("Invalid argument"));

            return;
        }

        action.set ("ConnectionIDs", typeof (string), connection_ids);

        action.return_success ();
    }

    private void get_current_connection_info_cb (Service             cm,
                                                 ServiceAction action) {
        string connection_id;

        action.get ("ConnectionID", typeof (string), out connection_id);
        if (connection_id == null || action.get_argument_count () != 1 ||
            (connection_id != "0" && int.parse (connection_id) == 0)) {
            action.return_error (402, _("Invalid argument"));

            return;
        }

        if (connection_id != "0") {
            action.return_error (706, _("Invalid connection reference"));

            return;
        }

        action.set ("RcsID",
                        typeof (int),
                        this.rcs_id,
                    "AVTransportID",
                        typeof (int),
                        this.av_transport_id,
                    "ProtocolInfo",
                        typeof (string),
                        this.get_current_protocol_info (),
                    "PeerConnectionManager",
                        typeof (string),
                        "",
                    "PeerConnectionID",
                        typeof (int),
                        -1,
                    "Direction",
                        typeof (string),
                        this.direction,
                    "Status",
                        typeof (string),
                        "OK");

        action.return_success ();
    }
}