File: subsystem.vala

package info (click to toggle)
libfsoframework 0.2.4%2Bgit20100501-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,452 kB
  • ctags: 101
  • sloc: sh: 10,126; makefile: 203
file content (314 lines) | stat: -rw-r--r-- 9,641 bytes parent folder | download
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright (C) 2009-2010 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser 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
 *
 */

using GLib;

/**
 * Subsystem Interface
 *
 * A subsystem hosts a number of plugins that can expose service objects
 * via an IPC mechanism such as DBus.
 */
public interface FsoFramework.Subsystem : Object
{
    /**
     * Register plugins for this subsystem.
     * @return the number of registered plugins.
     **/
    public abstract uint registerPlugins();
    /**
     * Load registered plugins for this subsystem.
     * @return the number of loaded plugins.
     * Plugins are loaded in the order they appear in the register,
     * which in turn is the order they are defined in the configuration
     * file. You can use this to express dependencies.
     **/
    public abstract uint loadPlugins();
    /**
     * @return the name of this subsystem.
     **/
    public abstract string name();
    /**
     * @return plugin information.
     **/
    public abstract List<FsoFramework.PluginInfo?> pluginsInfo();
    /**
     * Claim a service name with the IPC mechanism.
     * @return true, if name could be claimed. false, otherwise.
     **/
    public abstract bool registerServiceName( string servicename );
    /**
     * Export an object via the IPC mechanims.
     * @return true, if object has been exported. false, otherwise.
     **/
    public abstract bool registerServiceObject( string servicename, string objectname, Object obj );
    /**
     * Export an object via the IPC mechanims.
     * @return true, if object has been exported. false, otherwise.
     **/
    public abstract bool registerServiceObjectWithPrefix( string servicename, string prefix, Object obj );
    /**
     * Shutdown the subsystem. This will call shutdown on all plugins.
     **/
    public abstract void shutdown();
}

/**
 * AbstractSubsystem
 */
public abstract class FsoFramework.AbstractSubsystem : FsoFramework.Subsystem, Object
{
    protected FsoFramework.Logger logger;
    string _name;
    List<FsoFramework.Plugin> _plugins;

    public AbstractSubsystem( string name )
    {
        _name = name;
        logger = Logger.createLogger( "libfsoframework", "subsystem" );
    }

    public uint registerPlugins()
    {
        assert ( _plugins == null ); // this method can only be called once
        _plugins = new List<FsoFramework.Plugin>();

        if ( !FsoFramework.SmartKeyFile.defaultKeyFile().hasSection( _name ) )
        {
            logger.warning( @"No section for $_name in configuration file. Not looking for plugins." );
            return 0;
        }
        if ( FsoFramework.SmartKeyFile.defaultKeyFile().boolValue( _name, "disabled", false ) )
        {
            logger.info( @"Subsystem $_name has been disabled in configuration file. Not looking for plugins." );
            return 0;
        }

        var names = FsoFramework.SmartKeyFile.defaultKeyFile().sectionsWithPrefix( _name + "." );
        var defaultpath = GLib.Path.build_filename( Config.PACKAGE_LIBDIR, "modules" );
        //FIXME: document plugin_path setting
        var pluginpath = FsoFramework.SmartKeyFile.defaultKeyFile().stringValue( "cornucopia", "plugin_path", defaultpath );

        assert( logger.debug( @"Pluginpath is $pluginpath" ) );

        foreach ( var name in names )
        {
            var realname = name.replace( _name + ".", "" ); // cut subsystem name and dot
            string filename;
            if ( "./.libs" in pluginpath )
                filename = pluginpath.printf( realname );
            else
                filename = "%s/%s/%s".printf( pluginpath, _name, realname );
            var plugin = new FsoFramework.BasePlugin( filename, this );
            _plugins.append( plugin );
        }

        assert( logger.debug( @"Registered $(_plugins.length()) plugins" ) );
        return _plugins.length();
    }

    public uint loadPlugins()
    {
        uint counter = 0;

        foreach ( var plugin in _plugins )
        {
            try
            {
                plugin.loadAndInit();
                counter++;
            }
            catch ( FsoFramework.PluginError e )
            {
                logger.warning( @"Could not load plugin: $(e.message)" );
                //FIXME: Why do we not remove it here?
            }
        }
        return counter;
    }

    public string name()
    {
        return _name;
    }

    public List<FsoFramework.PluginInfo?> pluginsInfo()
    {
        var list = new List<FsoFramework.PluginInfo?>();
        foreach ( var plugin in _plugins )
        {
            list.append( plugin.info() );
        }
        return list;
    }

    public virtual bool registerServiceName( string servicename )
    {
        return false;
    }

    public virtual bool registerServiceObject( string servicename, string objectname, Object obj )
    {
        return false;
    }

    public virtual bool registerServiceObjectWithPrefix( string servicename, string prefix, Object obj )
    {
        return false;
    }

    public void shutdown()
    {
        foreach ( var plugin in _plugins )
        {
            plugin.shutdown();
        }
    }
}

/**
 * BaseSubsystem
 */
public class FsoFramework.BaseSubsystem : FsoFramework.AbstractSubsystem
{
    public BaseSubsystem( string name )
    {
        base( name );
    }
}

/**
 * Subsystem query interface
 */

/*
[DBus (name = "org.freesmartphone.DBus.Objects")]
public abstract interface DBusObjects
{
    public abstract void getNodes() throws DBus.Error;
}
*/

/**
 * DBusSubsystem
 */
public class FsoFramework.DBusSubsystem : FsoFramework.AbstractSubsystem
{
    DBus.Connection _dbusconn;
    dynamic DBus.Object _dbusobj;

    HashTable<string, DBus.Connection> _dbusconnections;
    HashTable<string, Object> _dbusobjects;

    HashTable<string, int> _counters;

    public DBusSubsystem( string name )
    {
        base( name );
        _dbusconnections = new HashTable<string, DBus.Connection>( str_hash, str_equal );
        _dbusobjects = new HashTable<string, Object>( str_hash, str_equal );
        _counters = new HashTable<string, int>( str_hash, str_equal );
    }

    ~DBusSubsystem()
    {
        // FIXME: do we need to unregister the objects?
        foreach ( var name in _dbusconnections.get_keys() )
        {
            uint res = _dbusobj.release_name( name );
        }
    }

    public override bool registerServiceName( string servicename )
    {
        var connection = _dbusconnections.lookup( servicename );
        if ( connection != null )
        {
            assert( logger.debug( @"Connection for $servicename found; ok." ) );
            return true;
        }

        assert( logger.debug( @"Connection for $servicename not present yet; creating." ) );

        // get bus connection
        if ( _dbusconn == null )
        {
            try
            {
                _dbusconn = DBus.Bus.get( DBus.BusType.SYSTEM );
                _dbusobj = _dbusconn.get_object( DBus.DBUS_SERVICE_DBUS, DBus.DBUS_PATH_DBUS, DBus.DBUS_INTERFACE_DBUS );
            }
            catch ( DBus.Error e )
            {
                logger.critical( @"Could not get handle for DBus service object at system bus: $(e.message)" );
                return false;
            }
        }
        //uint res = _dbusobj.request_name( servicename, (uint) 0 );
        uint res = _dbusobj.RequestName( servicename, (uint) 0 );

        if ( res == DBus.RequestNameReply.PRIMARY_OWNER )
        {
            _dbusconnections.insert( servicename, _dbusconn );
            return true;
        }
        else
        {
            logger.critical( @"Can't acquire service name $servicename; service already running or not allowed in dbus configuration." );
            return false;
        }
    }

    public override bool registerServiceObject( string servicename, string objectname, Object obj )
    {
        var conn = _dbusconnections.lookup( servicename );
        if ( conn == null )
        {
            logger.warning( @"Can't register service object $objectname; service name $servicename could not be acquired." );
            return false;
        }

        // clean objectname
        var cleanedname = objectname.replace( "-", "_" ).replace( ":", "_" );

        conn.register_object( cleanedname, obj );
        _dbusobjects.insert( cleanedname, obj );
        return true;
    }

    public override bool registerServiceObjectWithPrefix( string servicename, string prefix, Object obj )
    {
        var hash = @"$servicename:$prefix";
        int counter = _counters.lookup( hash );
        var ok = registerServiceObject( servicename, @"$prefix/$counter", obj );
        if ( ok )
        {
            _counters.insert( hash, ++counter );
        }
        return ok;
    }


    public DBus.Connection dbusConnection()
    {
        assert( _dbusconn != null );
        return _dbusconn;
    }

}