summaryrefslogtreecommitdiff
path: root/libgupnp/gupnp-simple-context-manager.c
blob: f070735ea7381f15498baca81da8f50e88eaab9b (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (C) 2009,2011 Nokia Corporation.
 * Copyright (C) 2006, 2007, 2008 OpenedHand Ltd.
 *
 * Author: Jens Georg <mail@jensge.org>
 *         Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *         Jorn Baayen <jorn@openedhand.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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.
 */

/**
 * SECTION:gupnp-simple-context-manager
 * @short_description: Abstract implementation of basic #GUPnPContextManager.
 *
 */

#include <config.h>
#include <errno.h>
#include <gio/gio.h>
#include <libgssdp/gssdp-error.h>

#include "gupnp-simple-context-manager.h"
#include "gupnp-context.h"

struct _GUPnPSimpleContextManagerPrivate {
        GList *contexts; /* List of GUPnPContext instances */

        GSource *idle_context_creation_src;
};
typedef struct _GUPnPSimpleContextManagerPrivate GUPnPSimpleContextManagerPrivate;

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GUPnPSimpleContextManager,
                                     gupnp_simple_context_manager,
                                     GUPNP_TYPE_CONTEXT_MANAGER)

static GList*
gupnp_simple_context_manager_get_interfaces (GUPnPSimpleContextManager *manager)
{
        GUPnPSimpleContextManagerClass *object_class =
                              GUPNP_SIMPLE_CONTEXT_MANAGER_GET_CLASS (manager);

        return object_class->get_interfaces (manager);
}

static void
create_and_signal_context (const char                *interface,
                           GUPnPSimpleContextManager *manager)
{
        GUPnPContext *context;
        GUPnPSimpleContextManagerPrivate *priv;
        guint port;

        GError *error;

        priv = gupnp_simple_context_manager_get_instance_private (manager);
        g_object_get (manager,
                      "port", &port,
                      NULL);

        error = NULL;
        context = g_initable_new (GUPNP_TYPE_CONTEXT,
                                  NULL,
                                  &error,
                                  "interface", interface,
                                  "port", port,
                                  NULL);
        if (error != NULL) {
                if (!(error->domain == GSSDP_ERROR &&
                      error->code == GSSDP_ERROR_NO_IP_ADDRESS))
                        g_warning
                           ("Failed to create context for interface '%s': %s",
                            interface,
                            error->message);

                g_error_free (error);

                return;
        }

        g_signal_emit_by_name (manager,
                               "context-available",
                               context);

        priv->contexts = g_list_append (priv->contexts,
                                                 context);
}

/*
 * Create a context for all network interfaces that are up.
 */
static gboolean
create_contexts (gpointer data)
{
        GUPnPSimpleContextManager *manager = (GUPnPSimpleContextManager *) data;
        GList *ifaces;
        GUPnPSimpleContextManagerPrivate *priv;

        priv = gupnp_simple_context_manager_get_instance_private (manager);

        priv->idle_context_creation_src = NULL;

        if (priv->contexts != NULL)
               return FALSE;

        ifaces = gupnp_simple_context_manager_get_interfaces (manager);
        while (ifaces) {
                create_and_signal_context ((char *) ifaces->data, manager);
                g_free (ifaces->data);
                ifaces = g_list_delete_link (ifaces, ifaces);
        }

        return FALSE;
}

static void
destroy_contexts (GUPnPSimpleContextManager *manager)
{
        GUPnPSimpleContextManagerPrivate *priv;

        priv = gupnp_simple_context_manager_get_instance_private (manager);
        while (priv->contexts) {
                GUPnPContext *context;

                context = GUPNP_CONTEXT (priv->contexts->data);

                g_signal_emit_by_name (manager,
                                       "context-unavailable",
                                       context);
                g_object_unref (context);

                priv->contexts = g_list_delete_link (priv->contexts,
                                                     priv->contexts);
        }
}

static void
schedule_contexts_creation (GUPnPSimpleContextManager *manager)
{
        GUPnPSimpleContextManagerPrivate *priv;

        priv = gupnp_simple_context_manager_get_instance_private (manager);
        priv->idle_context_creation_src = NULL;

        /* Create contexts in mainloop so that is happens after user has hooked
         * to the "context-available" signal.
         */
        priv->idle_context_creation_src = g_idle_source_new ();
        g_source_attach (priv->idle_context_creation_src,
                         g_main_context_get_thread_default ());
        g_source_set_callback (priv->idle_context_creation_src,
                               create_contexts,
                               manager,
                               NULL);
        g_source_unref (priv->idle_context_creation_src);
}

static void
gupnp_simple_context_manager_init (GUPnPSimpleContextManager *manager)
{
}

static void
gupnp_simple_context_manager_constructed (GObject *object)
{
        GObjectClass *parent_class;
        GUPnPSimpleContextManager *manager;

        manager = GUPNP_SIMPLE_CONTEXT_MANAGER (object);
        schedule_contexts_creation (manager);

        /* Chain-up */
        parent_class = G_OBJECT_CLASS (gupnp_simple_context_manager_parent_class);
        if (parent_class->constructed != NULL)
                parent_class->constructed (object);
}

static void
gupnp_simple_context_manager_dispose (GObject *object)
{
        GUPnPSimpleContextManager *manager;
        GObjectClass *object_class;
        GUPnPSimpleContextManagerPrivate *priv;

        manager = GUPNP_SIMPLE_CONTEXT_MANAGER (object);
        priv = gupnp_simple_context_manager_get_instance_private (manager);

        destroy_contexts (manager);

        if (priv->idle_context_creation_src) {
                g_source_destroy (priv->idle_context_creation_src);
                priv->idle_context_creation_src = NULL;
        }


        /* Call super */
        object_class = G_OBJECT_CLASS (gupnp_simple_context_manager_parent_class);
        object_class->dispose (object);
}

static void
gupnp_simple_context_manager_class_init (GUPnPSimpleContextManagerClass *klass)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (klass);

        object_class->constructed  = gupnp_simple_context_manager_constructed;
        object_class->dispose      = gupnp_simple_context_manager_dispose;
}