All pastes #1569432 Raw Edit

Zeug

public text v1 · immutable
#1569432 ·published 2009-09-17 08:48 UTC
rendered paste body
MainLoop loop;

public class Command
{
    public SourceFunc callback;
}

[DBus (name = "org.example.Test")]
public interface OrgExampleTest
{
    public abstract async bool get_something() throws DBus.Error;
}

public class Mediator : Object
{
    public bool haveBeenHere = false;
    public bool response;

    public async void runIt( SourceFunc cb )
    {
        message( "runIt() enter [cb=%p]", (void*)cb );
        assert( !haveBeenHere );
        haveBeenHere = true;
        var response = yield enqueueCommand();
        message( "runIt() after yield" );
        assert( response == "42" );
        this.response = true;
        message( "runIt() leave [cb=%p]", (void*)cb );
        cb();
    }

    public async string enqueueCommand( )
    {
        message( "enqueueCommand() enter" );
        var cmd = new Command();
        cmd.callback = enqueueCommand.callback;
        Idle.add( cmd.callback );
        yield;
        message( "enqueueCommand() leave" );
        return "42";
    }
}

public class Server : OrgExampleTest, Object
{
    public async bool get_something() throws DBus.Error
    {
        message( "get_something(): enter" );
        var m = new Mediator();
        m.runIt( get_something.callback );
        yield;
        message( "get_something(): mediator completed" );
        assert( m.response );
        message( "get_something(): leave" );
        return m.response;
    }
}

void main()
{
    var server = new Server();
    loop = new MainLoop( null, false );

    var dbusconn = DBus.Bus.get( DBus.BusType.SESSION );
    dynamic DBus.Object dbusobj = dbusconn.get_object( "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus" );

    uint res = dbusobj.request_name( "org.example.test", (uint) 0 );
    assert( res == DBus.RequestNameReply.PRIMARY_OWNER );

    dbusconn.register_object( "/", server );

    loop.run();
}