File: subsystem.vala

package info (click to toggle)
libfsoframework 0.11.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,052 kB
  • sloc: sh: 11,311; ansic: 7,896; makefile: 89
file content (356 lines) | stat: -rw-r--r-- 10,566 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * Copyright (C) 2009-2011 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();
    /**
     * Register an object with the IPC mechanism.
     * If the service name is not claimed yet, attempt to claim it.
     **/
    public abstract void registerObjectForService<T>( string servicename, string objectpath, T obj );
    /**
     * Register an object with the IPC mechanism.
     * If the service name is not claimed yet, attempt to claim it.
     **/
    public abstract void registerObjectForServiceWithPrefix<T>( string servicename, string prefixpath, T obj );
    /**
     * Query registered plugins with a certain path prefix.
     **/
    public abstract Object[] allObjectsWithPrefix( string? prefix = null );
    /**
     * Shutdown the subsystem. This will call shutdown on all plugins.
     **/
    public abstract void shutdown();
    /**
     * Signal sent, when a servicename has been acquired.
     **/
    public signal void serviceNameAcquired( string servicename );
}

/**
 * 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 void registerObjectForService<T>( string servicename, string objectpath, T obj )
    {
        assert_not_reached();
    }

    public virtual void registerObjectForServiceWithPrefix<T>( string servicename, string prefixpath, T obj )
    {
        assert_not_reached();
    }

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

    public virtual Object[] allObjectsWithPrefix( string? prefix = "null" )
    {
        assert_not_reached();
    }

    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 DBusError;
}
*/

/**
 * DBusExportObject
 **/
public class FsoFramework.DBusExportObject
{
    public Object object;
    public int[] refids;
}

/**
 * DBusSubsystem
 */
public class FsoFramework.DBusSubsystem : FsoFramework.AbstractSubsystem
{
    DBusConnection connection;

    Gee.HashMap<string, Gee.ArrayList<uint>> refids;
    Gee.HashMap<string, Object> dbusobjects;
    Gee.HashMap<string, int> counters;
    Gee.HashSet<string> busnames;

    uint watch;

    public DBusSubsystem( string name )
    {
        base( name );
        refids = new Gee.HashMap<string, Gee.ArrayList<uint>>();
        dbusobjects = new Gee.HashMap<string, Object>();
        counters = new Gee.HashMap<string, int>();
        busnames = new Gee.HashSet<string>();
    }

    ~DBusSubsystem()
    {
        /*
        // FIXME: do we need to unregister the objects?
        foreach ( var name in dbusconnections )
        {
            uint res = dbusobj.ReleaseName( name );
        }
        */
    }

    private void ensureConnection()
    {
        if ( connection == null )
        {
            assert( logger.debug( @"Connection not present yet; creating." ) );
            try
            {
                connection = Bus.get_sync( BusType.SYSTEM );
            }
            catch ( Error e )
            {
                logger.critical( @"Could not connect to DBus System bus: $(e.message). dbus-daemon started?" );
                Posix.exit( -1 );
            }
            assert( connection != null );
        }
    }

    public void exportBusnames()
    {
        foreach ( var servicename in busnames )
        {
            // claim bus name
            Bus.own_name_on_connection( connection, servicename, 0,
            ( conn, name ) => {
                assert( logger.debug( @"Successfully claimed $name" ) );
                this.serviceNameAcquired( servicename ); /* SIGNAL */
            }, () => {
                logger.critical( @"Can't claim busname $servicename" );
                Posix.exit( -1 );
            } );
        }
    }

    public override void registerObjectForService<T>( string servicename, string objectpath, T obj )
    {
        ensureConnection();

        var cleanedname = objectpath.replace( "-", "_" ).replace( ":", "_" );
        try
        {
            var refid = connection.register_object<T>( cleanedname, obj );
            Gee.ArrayList<uint>? refidsForObject = refids[servicename];
            if ( refidsForObject == null )
            {
                refidsForObject = new Gee.ArrayList<uint>();
                refids[servicename] = refidsForObject;
            }
            refidsForObject.add( refid );
            dbusobjects[cleanedname] = (Object) obj;
        }
        catch ( Error e )
        {
            logger.error( @"Could not register $(typeof(T).name()) at $objectpath: $(e.message)" );
        }

        assert( logger.debug( @"Registered $(typeof(T).name()) at $objectpath" ) );

        busnames.add( servicename );

        if ( watch == 0 )
        {
            watch = Idle.add( () => {
                exportBusnames();
                return false; // mainloop: don't call again
            } );
        }
    }

    public override void registerObjectForServiceWithPrefix<T>( string servicename, string prefix, T obj )
    {
        var hash = @"$servicename:$prefix";
        int counter = counters[hash];
        registerObjectForService<T>( servicename, @"$prefix/$counter", obj );
        counters[hash] = ++counter;
    }

    public DBusConnection dbusConnection()
    {
        ensureConnection();
        return connection;
    }

    public override Object[] allObjectsWithPrefix( string? prefix = null )
    {
        var result = new Object[] {};
        foreach ( var objectname in dbusobjects.keys )
        {
            if ( prefix == null || objectname.has_prefix( prefix ) )
            {
                result += dbusobjects[objectname];
            }
        }
        return result;
    }
}

// vim:ts=4:sw=4:expandtab