2 * Copyright (C) 2008 Nokia Corporation.
3 * Copyright (C) 2008 Zeeshan Ali (Khattak) <zeeshanak@gnome.org>.
5 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
7 * This file is part of Rygel.
9 * Rygel is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Rygel is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 public class Rygel.Main : Object {
29 private PluginLoader plugin_loader;
30 private ContextManager context_manager;
31 private ArrayList <RootDeviceFactory> factories;
32 private ArrayList <RootDevice> root_devices;
34 private Configuration config;
36 private MainLoop main_loop;
38 private int exit_code;
40 public Main () throws GLib.Error {
41 Environment.set_application_name (_(BuildConfig.PACKAGE_NAME));
43 this.config = MetaConfig.get_default ();
44 this.plugin_loader = new PluginLoader ();
45 this.root_devices = new ArrayList <RootDevice> ();
46 this.factories = new ArrayList <RootDeviceFactory> ();
47 this.context_manager = this.create_context_manager ();
48 this.main_loop = new GLib.MainLoop (null, false);
52 this.plugin_loader.plugin_available += this.on_plugin_loaded;
54 Utils.on_application_exit (this.application_exit_cb);
58 this.plugin_loader.load_plugins ();
60 this.main_loop.run ();
62 return this.exit_code;
65 public void exit (int exit_code) {
66 this.exit_code = exit_code;
67 this.main_loop.quit ();
70 private void application_exit_cb () {
74 private void on_plugin_loaded (PluginLoader plugin_loader,
76 foreach (var factory in this.factories) {
77 this.create_device (plugin, factory);
81 private ContextManager create_context_manager () {
85 port = this.config.get_port ();
86 } catch (GLib.Error err) {}
88 var manager = new ContextManager (null, port);
90 manager.context_available.connect (this.on_context_available);
91 manager.context_unavailable.connect (this.on_context_unavailable);
96 private void on_context_available (GUPnP.ContextManager manager,
97 GUPnP.Context context) {
98 string host_ip = null;
100 debug ("new network context %s (%s) available.",
105 host_ip = this.config.get_host_ip ();
106 } catch (GLib.Error err) {}
108 if (host_ip == null || host_ip == context.host_ip) {
109 var factory = new RootDeviceFactory (context);
111 foreach (var plugin in this.plugin_loader.list_plugins ()) {
112 this.create_device (plugin, factory);
115 this.factories.add (factory);
117 debug ("Ignoring network context %s (%s).",
123 private void on_context_unavailable (GUPnP.ContextManager manager,
124 GUPnP.Context context) {
125 debug ("Network context %s (%s) now unavailable.",
129 var factory_list = new ArrayList <RootDeviceFactory> ();
130 foreach (var factory in this.factories) {
131 if (context == factory.context) {
132 factory_list.add (factory);
136 foreach (var factory in factory_list) {
137 this.factories.remove (factory);
140 var device_list = new ArrayList <RootDevice> ();
141 foreach (var device in this.root_devices) {
142 if (context == device.context) {
143 device_list.add (device);
147 foreach (var device in device_list) {
148 device.available = false;
149 this.root_devices.remove (device);
153 private void create_device (Plugin plugin,
154 RootDeviceFactory factory) {
156 var device = factory.create (plugin);
158 device.available = plugin.available;
160 this.root_devices.add (device);
162 plugin.notify["available"] += this.on_plugin_notify;
163 } catch (GLib.Error error) {
164 warning ("Failed to create RootDevice for %s. Reason: %s\n",
170 private void on_plugin_notify (Plugin plugin,
172 foreach (var device in this.root_devices) {
173 if (device.resource_factory == plugin) {
174 device.available = plugin.available;
179 public static int main (string[] args) {
184 // Parse commandline options
185 CmdlineConfig.parse_args (ref args);
187 // initialize gstreamer
188 var dummy_args = new string[0];
189 Gst.init (ref dummy_args);
192 service = new DBusService (main);
193 } catch (CmdlineConfigError.VERSION_ONLY err) {
195 } catch (GLib.Error err) {
196 error ("%s", err.message);
201 int exit_code = main.run ();