enable silent rules, if available
[fso-monitord.git] / src / monitor.vala
blob6849769e25b22152f2b2f993cca754711cb96a69
1 /*
2 * File Name: monitor.vala
3 * Creation Date:
4 * Last Modified:
6 * Authored by Michael 'Mickey' Lauer <mlauer@vanille-media.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 //===========================================================================
25 using GLib;
26 using DBus;
29 namespace FSO
31 public interface IMonitor
33 public abstract void run() throws GLib.Error;
34 public abstract void stop();
36 public class System:GLib.Object, IMonitor
38 protected List<Subsystem?> subsystems = null;
39 protected Logger logger = null;
40 protected DBus.Connection con = null;
41 protected dynamic DBus.Object dbus = null;
42 protected string busname = null;
43 public System( FSO.Logger l, DBus.Connection c)
45 this.con = c;
46 this.logger = l;
48 construct
50 this.subsystems = new List<Subsystem>( );
52 public void name_owner_changed( dynamic DBus.Object obj, string name, string new_owner, string old_owner)
54 if( name == this.busname )
56 stop();
57 Timeout.add_seconds( 60, this.timeout_run );
60 public virtual void run() throws GLib.Error
62 this.dbus = this.con.get_object( "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus" );
63 this.dbus.NameOwnerChanged += this.name_owner_changed;
65 foreach( Subsystem s in this.subsystems )
67 try
69 s.run();
71 catch (GLib.Error e)
73 debug( "Running %s failed", s.get_type().name() );
77 public virtual void stop()
79 this.dbus = null;
80 foreach( Subsystem s in this.subsystems )
82 s.stop();
85 public bool timeout_run()
87 try
89 this.run();
91 catch (GLib.Error e)
93 debug( "failed to run %s: %s", this.get_type().name(), e.message );
94 //call me again
95 return true;
97 return false;
100 public class Subsystem: GLib.Object, IMonitor
102 protected Logger logger = null;
103 protected DBus.Connection con = null;
104 protected dynamic DBus.Object object =null;
105 protected string _IFACE = null;
106 protected string _BUS_NAME = null;
107 protected string _OBJ_PATH = null;
108 protected string name = null;
109 protected uint timer = 0;
110 //currently everything is provided by frameworkd
111 protected string daemon = "frameworkd";
113 private const string PEER_IFACE = "org.freedesktop.DBus.Peer";
114 private dynamic DBus.Object peer;
117 public Subsystem( FSO.Logger l, DBus.Connection c, string name = "" )
119 this.con = c;
120 this.logger = l;
121 this.name = name;
124 public virtual void run() throws GLib.Error
126 debug( "Subsystem.run: %s %s %s", this._BUS_NAME, this._OBJ_PATH, this._IFACE );
127 this.object = this.con.get_object( this._BUS_NAME, this._OBJ_PATH, this._IFACE );
128 this.peer = this.con.get_object( this._BUS_NAME, this._OBJ_PATH, PEER_IFACE );
129 ping();
130 var rand = new Rand();
131 this.timer = Timeout.add_seconds( rand.int_range( 10, FSO.timeout), this.first_ping );
133 public bool first_ping( )
135 ping();
136 this.timer = Timeout.add_seconds( FSO.timeout, this.ping );
138 //Don't call me again
139 return false;
141 public bool ping( )
145 debug( "Pinging %s ", this.peer.get_path( ) );
146 this.peer.Ping();
148 catch( GLib.Error e )
150 debug("Ping failed. BUSNAME: %s OBJPATH: %s IFACE: %s: %s", this._BUS_NAME, this._OBJ_PATH, this._IFACE, e.message );
151 switch( e.code )
153 case DBus.Error.NO_REPLY:
154 case DBus.Error.TIMEOUT:
155 case DBus.Error.TIMED_OUT:
156 debug("Restarting: %s", e.message );
157 FSO.restart( this._BUS_NAME );
158 break;
159 default:
160 critical("Do not restart: %s", e.message);
161 break;
164 //Call me again
165 return true;
167 public virtual void stop()
169 debug( "stopping: %s on %s: %s", this.object.get_path(), this.object.get_bus_name(), this.object.get_interface() );
170 Source.remove( this.timer );
171 this.object= null;
174 public class Monitor: GLib.Object, IMonitor
176 private DBus.Connection conn = null;
177 private Logger logger = null;
178 private List<System> systems = null;
179 public Monitor( Logger l, DBus.Connection c)
181 this.logger = l;
182 this.conn = c;
183 this.logger.logINFO("-------------Monitor restarted------------");
184 this.systems.prepend(new FSO.Device(this.logger, this.conn));
185 this.systems.prepend(new FSO.GSM(this.logger, this.conn));
186 this.systems.prepend(new FSO.Phone(this.logger, this.conn));
187 this.systems.prepend(new FSO.Preferences(this.logger, this.conn));
188 this.systems.prepend(new FSO.Usage(this.logger, this.conn));
189 this.systems.prepend(new FSO.Framework(this.logger, this.conn));
191 construct
193 this.systems = new List<System>();
195 public virtual void run() throws GLib.Error
197 foreach( System s in this.systems )
201 s.run();
203 catch (GLib.Error e)
205 debug( "Starting %s failed: %s", s.get_type().name(), e.message );
209 public virtual void stop()
211 foreach( System s in this.systems )
213 s.stop();
216 public static int main(string[] args)
218 var logger = new FSO.Logger();
219 var loop = new MainLoop(null, false);
222 var con = DBus.Bus.get( DBus.BusType.SYSTEM );
223 var monitor = new FSO.Monitor( logger, con );
224 monitor.run();
225 loop.run();
226 } catch (GLib.Error e) {
227 error ("Oops: %s\n", e.message);
228 return 1;
230 return 0;