2 * File Name: monitor.vala
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 //===========================================================================
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
)
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
)
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
)
73 debug( "Running %s failed", s
.get_type().name() );
77 public virtual void stop()
80 foreach( Subsystem s
in this
.subsystems
)
85 public bool timeout_run()
93 debug( "failed to run %s: %s", this
.get_type().name(), e
.message
);
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
= "" )
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
);
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( )
136 this
.timer
= Timeout
.add_seconds( FSO
.timeout
, this
.ping
);
138 //Don't call me again
145 debug( "Pinging %s ", this
.peer
.get_path( ) );
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
);
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
);
160 critical("Do not restart: %s", e
.message
);
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
);
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
)
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
));
193 this
.systems
= new List
<System
>();
195 public virtual void run() throws GLib
.Error
197 foreach( System s
in this
.systems
)
205 debug( "Starting %s failed: %s", s
.get_type().name(), e
.message
);
209 public virtual void stop()
211 foreach( System s
in this
.systems
)
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
);
226 } catch (GLib
.Error e
) {
227 error ("Oops: %s\n", e
.message
);