4 * Copyright (C) 2013 Intel Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU Lesser General Public License,
8 * version 2.1, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 * Regis Merlino <regis.merlino@intel.com>
26 #include "connector-mgr.h"
27 #include "control-point.h"
30 #include "main-loop.h"
32 #include "task-processor.h"
34 typedef struct dleyna_daemon_context_t_ dleyna_daemon_context_t;
35 struct dleyna_daemon_context_t_ {
38 dleyna_connector_id_t connection;
39 dleyna_task_processor_t *processor;
40 dleyna_settings_t *settings;
41 const dleyna_connector_t *connector;
42 const dleyna_control_point_t *control_point;
45 static dleyna_daemon_context_t g_context;
47 static gboolean prv_context_mainloop_quit_cb(gpointer user_data)
49 DLEYNA_LOG_DEBUG("Main loop quit");
51 g_main_loop_quit(g_context.main_loop);
56 static gboolean prv_context_quit_cb(gpointer user_data)
58 DLEYNA_LOG_DEBUG("Quitting");
60 g_context.connector->disconnect();
61 g_context.control_point->stop_service();
63 g_timeout_add_seconds(1, prv_context_mainloop_quit_cb, NULL);
68 static gboolean prv_context_init(const gchar *server,
69 const dleyna_control_point_t *control_point)
71 gboolean retval = TRUE;
72 const gchar *connector;
74 memset(&g_context, 0, sizeof(g_context));
76 g_context.processor = dleyna_task_processor_new(prv_context_quit_cb);
77 g_context.control_point = control_point;
79 dleyna_settings_new(server, &g_context.settings);
81 connector = dleyna_settings_connector_name(g_context.settings);
82 g_context.connector = dleyna_connector_mgr_load(connector);
84 if (!g_context.connector)
90 static void prv_connector_acquired(dleyna_connector_id_t connection)
92 g_context.connection = connection;
94 if (!g_context.control_point->start_service(connection)) {
95 g_context.error = TRUE;
96 g_main_loop_quit(g_context.main_loop);
100 static void prv_name_lost(dleyna_connector_id_t connection)
102 g_context.connection = NULL;
103 dleyna_task_processor_set_quitting(g_context.processor);
106 static void prv_context_free(void)
108 dleyna_task_processor_free(g_context.processor);
110 if (g_context.connector) {
111 g_context.connector->shutdown();
112 dleyna_connector_mgr_release(g_context.connector);
115 if (g_context.main_loop)
116 g_main_loop_unref(g_context.main_loop);
118 if (g_context.settings)
119 dleyna_settings_delete(g_context.settings);
122 int dleyna_main_loop_start(char *server,
123 const dleyna_control_point_t *control_point,
128 #if !GLIB_CHECK_VERSION(2, 35, 0)
132 dleyna_log_init(server);
134 if (!prv_context_init(server, control_point))
137 if (!g_context.connector->initialize(
138 g_context.control_point->server_introspection(),
139 g_context.control_point->root_introspection(),
140 dleyna_error_quark(),
144 g_context.main_loop = g_main_loop_new(NULL, FALSE);
146 g_context.connector->connect(g_context.control_point->server_name(),
147 prv_connector_acquired,
151 g_context.control_point->initialize(g_context.connector,
155 g_main_loop_run(g_context.main_loop);
163 g_context.control_point->free();
169 dleyna_log_finalize();
174 static gboolean prv_set_quitting(gpointer user_data)
176 dleyna_task_processor_set_quitting(g_context.processor);
181 void dleyna_main_loop_quit(void)
183 g_idle_add_full(G_PRIORITY_HIGH_IDLE, prv_set_quitting, NULL, NULL);