# HG changeset patch # User Frank Benoit # Date 1192987361 -7200 # Node ID 7c2c75740370d2674bb363494aea0e1edac2a41c # Parent 427c0332a111f7d8b077076b75d2464d11349e4e code generation for signals diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/README.txt --- a/dbus-d-javatests/README.txt Sun Oct 21 14:49:02 2007 +0200 +++ b/dbus-d-javatests/README.txt Sun Oct 21 19:22:41 2007 +0200 @@ -22,9 +22,17 @@ To make an object accessible, you need to either export it or return it from another another method call. - +4. Create the Java code + Call the CreateInterface tool from dbus-java. + in Eclipse run CreateInterface. Set the working directory to "dbus-d-javatests/jsrc" + or in the run dialog ${workspace_loc:dbus-javatests/jsrc} + give the arguments "--create-files ../dbustest.xml" + the program shall run without any output. After completion the file should be created. + The repitory already contains those files, so no change may be noteable. + +5. Implement the Java code. See dbustest.DBusTester diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/dbustest.xml --- a/dbus-d-javatests/dbustest.xml Sun Oct 21 14:49:02 2007 +0200 +++ b/dbus-d-javatests/dbustest.xml Sun Oct 21 19:22:41 2007 +0200 @@ -47,6 +47,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/dsrc/DBusTesting.d --- a/dbus-d-javatests/dsrc/DBusTesting.d Sun Oct 21 14:49:02 2007 +0200 +++ b/dbus-d-javatests/dsrc/DBusTesting.d Sun Oct 21 19:22:41 2007 +0200 @@ -18,7 +18,10 @@ } } -class Dimpl : DBusInterface.dbustest.DImpl { +//pragma( msg, createDImplementation( "DImplBase", ["DBusInterface.dbustest.DImpl" ] ) ); +mixin( createDImplementation( "DImplBase", ["DBusInterface.dbustest.DImpl" ] )); + +class Dimpl : DImplBase { public void testV_V(){ checkCall( "testV_V" ); } @@ -26,8 +29,52 @@ checkCall( "testI_I" ); return arg1 + 1234; } + public void testParamAllSimpleTyes( + in bool i_bool, + out bool o_bool, + in byte i_byte, + out byte o_byte, + in short i_short, + out short o_short, + in ushort i_ushort, + out ushort o_ushort, + in int i_int, + out int o_int, + in uint i_uint, + out uint o_uint, + in long i_long, + out long o_long, + in ulong i_ulong, + out ulong o_ulong, + in double i_double, + out double o_double, + in char[] i_string, + out char[] o_string ) + { + checkCall( "testParamAllSimpleTyes" ); + o_bool = !i_bool; + o_byte = i_byte + 1; + o_short = i_short + 1; + o_ushort = i_ushort + 1; + o_int = i_int + 1; + o_uint = i_uint + 1; + o_long = i_long + 1; + o_ulong = i_ulong + 1; + o_double = i_double; + o_string = i_string.reverse; + } + + public void triggerSignal( in int count ){ + checkCall( "triggerSignal" ); + for( int i = 0; i < count; i++ ){ + testSigI.opCall( i ); + } + } + } +//mixin( createJavaImplementation( "MyClass", ["DBusInterface.dbustest.DImpl" ] )); + int main(char[][] args) { @@ -38,11 +85,11 @@ return 1; } Stdout.formatln( "D-App started"); - DirectConnection dc = new DirectConnection( args[1] ); + DirectConnection dc = new DirectConnection( address ); Stdout.formatln("D-App connected"); -// auto o = new MyDataFileSystem(); -// o.registerDBusObject( dc.conn ); -// Cout("Export obj : ")( o.getDBusInstanceName() ).newline; + auto o = new Dimpl(); + o.registerDBusObject( dc.conn ); + Stdout.formatln("Export obj : {}", o.getDBusInstanceName() ); dc.mainLoop(); Stdout.formatln("D-App disconnected"); return 0; diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/dsrc/DBusTestingGen.d --- a/dbus-d-javatests/dsrc/DBusTestingGen.d Sun Oct 21 14:49:02 2007 +0200 +++ b/dbus-d-javatests/dsrc/DBusTestingGen.d Sun Oct 21 19:22:41 2007 +0200 @@ -29,16 +29,34 @@ public interface Peer { public void Ping(); public char[] GetMachineId(); + template _StdJavaImpl(){ + } + template _StdDImpl(){ + void _init(){ + } + } } // org.freedesktop.DBus.Introspectable public interface Introspectable { public char[] Introspect(); + template _StdJavaImpl(){ + } + template _StdDImpl(){ + void _init(){ + } + } } // org.freedesktop.DBus.Properties public interface Properties { public DBusVariant Get( in char[] intf_name, in char[] prop_name ); public void Set( in char[] intf_name, in char[] prop_name, in DBusVariant prop ); public DBusVariant[ char[] ] GetAll( in char[] intf_name ); + template _StdJavaImpl(){ + } + template _StdDImpl(){ + void _init(){ + } + } } } } @@ -48,24 +66,62 @@ // dbustest.OtherInterface public interface OtherInterface { public void dummy( in char[] arg1 ); + template _StdJavaImpl(){ + } + template _StdDImpl(){ + void _init(){ + } + } } // dbustest.TestControl public interface TestControl { - public char[] getName(); + public char[][] getCalledTests(); + public void activateExceptions( in bool enable ); + template _StdJavaImpl(){ + } + template _StdDImpl(){ + void _init(){ + } + } } // dbustest.TestListener public interface TestListener { public char[] getName(); + template _StdJavaImpl(){ + } + template _StdDImpl(){ + void _init(){ + } + } } // dbustest.JavaImpl public interface JavaImpl { public void testV_V(); public int testI_I( in int arg1 ); + template _StdJavaImpl(){ + } + template _StdDImpl(){ + void _init(){ + } + } } // dbustest.DImpl public interface DImpl { public void testV_V(); public int testI_I( in int arg1 ); + public void testParamAllSimpleTyes( in bool i_bool, out bool o_bool, in byte i_byte, out byte o_byte, in short i_short, out short o_short, in ushort i_ushort, out ushort o_ushort, in int i_int, out int o_int, in uint i_uint, out uint o_uint, in long i_long, out long o_long, in ulong i_ulong, out ulong o_ulong, in double i_double, out double o_double, in char[] i_string, out char[] o_string ); + public void triggerSignal( in int count ); + public tango.core.Signal.Signal!( int )* testSigI(); + template _StdJavaImpl(){ + } + template _StdDImpl(){ + void _init(){ + } + protected tango.core.Signal.Signal!( int ) _testSigI; + public tango.core.Signal.Signal!( int )* testSigI(){ + return &_testSigI; + } + } } } } @@ -131,9 +187,12 @@ registerIntrospectionData( DBusInterface.dbustest.TestControl.classinfo, "\\n" - " \\n" + " \\n" " \\n" - " \\n" + " \\n" + " \\n" + " \\n" + " \\n" " \\n" "\\n"); registerIntrospectionData( @@ -165,6 +224,31 @@ " \\n" " \\n" " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" + " \\n" "\\n"); } private DBusHandlerResult intfHandler__org_freedesktop_DBus_Peer( DBusConnection* conn, DBusMessage* message, void* user_data ){ @@ -297,11 +381,18 @@ try{ char[METHOD_SIG_MAXLENGTH] buf; switch( methodSignature( message, buf ) ){ - case "getName|": + case "getCalledTests|": { - Struct!( char[] ) po; - po.t[0] = o.getName(); - sendReplyData!( char[] )( conn, message, po ); + Struct!( char[][] ) po; + po.t[0] = o.getCalledTests(); + sendReplyData!( char[][] )( conn, message, po ); + } + break; + case "activateExceptions|b": + { + Struct!( bool ) pi = getCallValues!( bool )( message ); + o.activateExceptions( pi.t[0] ); + sendReply( conn, message ); } break; default: @@ -393,6 +484,27 @@ sendReplyData!( int )( conn, message, po ); } break; + case "testParamAllSimpleTyes|bynqiuxtds": + { + Struct!( bool, byte, short, ushort, int, uint, long, ulong, double, char[] ) pi = getCallValues!( bool, byte, short, ushort, int, uint, long, ulong, double, char[] )( message ); + Struct!( bool, byte, short, ushort, int, uint, long, ulong, double, char[] ) po; + o.testParamAllSimpleTyes( pi.t[0], po.t[0], pi.t[1], po.t[1], pi.t[2], po.t[2], pi.t[3], po.t[3], pi.t[4], po.t[4], pi.t[5], po.t[5], pi.t[6], po.t[6], pi.t[7], po.t[7], pi.t[8], po.t[8], pi.t[9], po.t[9] ); + sendReplyData!( bool, byte, short, ushort, int, uint, long, ulong, double, char[] )( conn, message, po ); + } + break; + case "triggerSignal|i": + { + Struct!( int ) pi = getCallValues!( int )( message ); + o.triggerSignal( pi.t[0] ); + sendReply( conn, message ); + } + break; + case "testSigI>i": + { + Struct!( int ) pi = getCallValues!( int )( message ); + o.testSigI().opCall( pi.t[0] ); + } + break; default: return DBusHandlerResult.DBUS_HANDLER_RESULT_NOT_YET_HANDLED; } diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/jsrc/dbustest/DBusTester.java --- a/dbus-d-javatests/jsrc/dbustest/DBusTester.java Sun Oct 21 14:49:02 2007 +0200 +++ b/dbus-d-javatests/jsrc/dbustest/DBusTester.java Sun Oct 21 19:22:41 2007 +0200 @@ -1,39 +1,109 @@ package dbustest; -import java.io.IOException; +import java.util.Collections; +import java.util.List; +import org.freedesktop.dbus.DBusSigHandler; import org.freedesktop.dbus.DirectConnection; +import org.freedesktop.dbus.UInt16; +import org.freedesktop.dbus.UInt32; +import org.freedesktop.dbus.UInt64; -import dbus_d_javahelper.ConsoleProcess; import dbus_d_javahelper.DBusDLoader; +import dbustest.DImpl.testSigI; public class DBusTester { - static DirectConnection dc; - static ConsoleProcess process; + static int handledCounter; - /** - * @param args - */ + private static void ensure( boolean cond, String msg ){ + if( !cond ){ + throw new RuntimeException( msg ); + } + } + private static void ensureEqualLists( List list1, String[] list2 ){ + Collections.sort(list1); + ensure( list1.size() == list2.length, "List sizes are not equal" ); + for( int i = 0; i < list1.size(); i++ ){ + ensure( list1.get(i).equals(list2[i]), "List content is not equal" ); + } + } + public static void main(String[] args) { - // TODO Auto-generated method stub - final String address = DirectConnection.createDynamicTCPSession(); - DBusDLoader busDLoader = new DBusDLoader( "../dsrc/DBusTesting" ); + // instantiate the DBusDLoader and give the path to the D executable. + // In case you want to run from a jar, the executable can be placed in + // the jar and an optional resource path can be given. + DBusDLoader busDLoader = new DBusDLoader( "dsrc/DBusTesting" ); + try{ + + // Start the D app and use the returned connection System.err.println("Java App: start D"); DirectConnection dc = busDLoader.startup(); - DataFileSystem rem = (DataFileSystem) dc.getRemoteObject( - "/DHelper/MyDataFileSystem/0", DBusTesting.class); + // e.g. get an Object that is exported by the D app. + DImpl rem = (DImpl) dc.getRemoteObject( + "/DBusTesting/Dimpl/0", DImpl.class); + + + // Test calling D + + // 1. void func(void) + rem.testV_V(); + + // 2. int func( int ) + { + int res = rem.testI_I( 4321 ); + System.out.printf( "testI_I( 4321 ) => %d\n", res ); + } + // 3. more simple arguments + { + NTuple10 res = + rem.testParamAllSimpleTyes( + true, + (byte)100, + (short)1000, + new UInt16( 20000 ), + 2000000000, + new UInt32( 4000000000l ), + 9223372036854775806L, + new UInt64( "18446744073709551614" ), + 1.0, + "ABCabc" ); + ensure( res.a == false, "bool" ); + ensure( res.b == 101, "byte" ); + ensure( res.c == 1001, "short" ); + ensure( res.d.equals( new UInt16( 20001 )), "ushort" ); + ensure( res.e == 2000000001, "int" ); + ensure( res.f.equals( new UInt32( 4000000001L )), "uint" ); + ensure( res.g == 9223372036854775807L, "long" ); + ensure( res.h.equals( new UInt64( "18446744073709551615" )), "ulong" ); + ensure( res.i == 1.0, "double" ); + ensure( res.j.equals( "cbaCBA" ), "string" ); + } + + // 4. cause the test object to generate signal + DBusSigHandler handler = new DBusSigHandler(){ + public void handle(testSigI s) { + handledCounter++; + } + }; + dc.addSigHandler( DImpl.testSigI.class, handler ); + + rem.triggerSignal( 10 ); + + Thread.sleep(100); + System.out.printf( "HandledCounter: %d\n", handledCounter ); System.err.println("Java App: startup complete"); + } catch (Exception e) { + e.printStackTrace(); } finally{ busDLoader.disconnect(); } } - } diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/jsrc/dbustest/DImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dbus-d-javatests/jsrc/dbustest/DImpl.java Sun Oct 21 19:22:41 2007 +0200 @@ -0,0 +1,27 @@ +package dbustest; +import org.dsource.dbus.d.Returns; +import org.freedesktop.dbus.DBusInterface; +import org.freedesktop.dbus.DBusSignal; +import org.freedesktop.dbus.UInt16; +import org.freedesktop.dbus.UInt32; +import org.freedesktop.dbus.UInt64; +import org.freedesktop.dbus.exceptions.DBusException; +public interface DImpl extends DBusInterface +{ + public static class testSigI extends DBusSignal + { + public final int arg1; + public testSigI(String path, int arg1) throws DBusException + { + super(path, arg1); + this.arg1 = arg1; + } + } + + public void testV_V(); + @Returns("res") + public int testI_I(int arg1); + public NTuple10 testParamAllSimpleTyes(boolean i_bool, byte i_byte, short i_short, UInt16 i_ushort, int i_int, UInt32 i_uint, long i_long, UInt64 i_ulong, double i_double, String i_string); + public void triggerSignal(int count); + +} diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/jsrc/dbustest/JavaImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dbus-d-javatests/jsrc/dbustest/JavaImpl.java Sun Oct 21 19:22:41 2007 +0200 @@ -0,0 +1,11 @@ +package dbustest; +import org.dsource.dbus.d.Returns; +import org.freedesktop.dbus.DBusInterface; +public interface JavaImpl extends DBusInterface +{ + + public void testV_V(); + @Returns("res") + public int testI_I(int arg1); + +} diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/jsrc/dbustest/NTuple10.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dbus-d-javatests/jsrc/dbustest/NTuple10.java Sun Oct 21 19:22:41 2007 +0200 @@ -0,0 +1,40 @@ +package dbustest; +import org.freedesktop.dbus.Position; +import org.freedesktop.dbus.Tuple; +/** Just a typed container class */ +public final class NTuple10 extends Tuple +{ + @Position(0) + public final A a; + @Position(1) + public final B b; + @Position(2) + public final C c; + @Position(3) + public final D d; + @Position(4) + public final E e; + @Position(5) + public final F f; + @Position(6) + public final G g; + @Position(7) + public final H h; + @Position(8) + public final I i; + @Position(9) + public final J j; + public NTuple10(A a, B b, C c, D d, E e, F f, G g, H h, I i, J j) + { + this.a = a; + this.b = b; + this.c = c; + this.d = d; + this.e = e; + this.f = f; + this.g = g; + this.h = h; + this.i = i; + this.j = j; + } +} diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/jsrc/dbustest/OtherInterface.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dbus-d-javatests/jsrc/dbustest/OtherInterface.java Sun Oct 21 19:22:41 2007 +0200 @@ -0,0 +1,8 @@ +package dbustest; +import org.freedesktop.dbus.DBusInterface; +public interface OtherInterface extends DBusInterface +{ + + public void dummy(String arg1); + +} diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/jsrc/dbustest/TestControl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dbus-d-javatests/jsrc/dbustest/TestControl.java Sun Oct 21 19:22:41 2007 +0200 @@ -0,0 +1,12 @@ +package dbustest; +import java.util.List; +import org.dsource.dbus.d.Returns; +import org.freedesktop.dbus.DBusInterface; +public interface TestControl extends DBusInterface +{ + + @Returns("res") + public List getCalledTests(); + public void activateExceptions(boolean enable); + +} diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/jsrc/dbustest/TestListener.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dbus-d-javatests/jsrc/dbustest/TestListener.java Sun Oct 21 19:22:41 2007 +0200 @@ -0,0 +1,10 @@ +package dbustest; +import org.dsource.dbus.d.Returns; +import org.freedesktop.dbus.DBusInterface; +public interface TestListener extends DBusInterface +{ + + @Returns("res") + public String getName(); + +} diff -r 427c0332a111 -r 7c2c75740370 dbus-d-javatests/jsrc/org/dsource/dbus/d/Returns.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dbus-d-javatests/jsrc/org/dsource/dbus/d/Returns.java Sun Oct 21 19:22:41 2007 +0200 @@ -0,0 +1,8 @@ +package org.dsource.dbus.d; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +@Retention(RetentionPolicy.RUNTIME) +public @interface Returns +{ + String value(); +} diff -r 427c0332a111 -r 7c2c75740370 dbus-d/dsrc/org/freedesktop/dbus/DBus.d --- a/dbus-d/dsrc/org/freedesktop/dbus/DBus.d Sun Oct 21 14:49:02 2007 +0200 +++ b/dbus-d/dsrc/org/freedesktop/dbus/DBus.d Sun Oct 21 19:22:41 2007 +0200 @@ -1105,3 +1105,63 @@ } +/** + * this function shall be used like so: + * --- + * mixin( createJavaImplementation( "MyClass", ["I1", "I2" ] )); + * --- + * This creates a simple standard implementation by generating code in this form: + * --- + * class MyClass : I1, I2 { + * mixin I1.JImpl!(); + * mixin I2.JImpl!(); + * } + * --- + * The given array with interfaces must not be zero length + */ +char[] createJavaImplementation( char[] className, char[][] interfaces ){ + assert( interfaces.length > 0 ); + char[] res = "class "~className~ " "; + bool first = true; + foreach( intf; interfaces ){ + res ~= first ? ": " : ", "; + first = false; + res ~= intf; + } + res ~= " {\n"; + foreach( intf; interfaces ){ + res ~= " mixin "~intf~"._StdJavaImpl!();\n"; + } + res ~= "}\n"; + return res; +} + +char[] ctfeReplace( char[] text, char s, char r ){ + char[] res; + foreach( c; text ){ + res ~= ( c == s ) ? r : c; + } + return res; +} +char[] createDImplementation( char[] className, char[][] interfaces ){ + assert( interfaces.length > 0 ); + char[] res = "abstract class "~className~ " : DBusObjectImpl "; + foreach( intf; interfaces ){ + res ~= ", "; + res ~= intf; + } + res ~= " {\n"; + foreach( intf; interfaces ){ + res ~= " mixin "~intf~"._StdDImpl!() _StdDImpl_"~ctfeReplace( intf, '.', '_' )~";\n"; + } + res ~= " protected this(){\n"; + foreach( intf; interfaces ){ + res ~= " _StdDImpl_"~ ctfeReplace( intf, '.', '_' ) ~"._init();\n"; + } + res ~= " }\n"; + res ~= "}\n"; + return res; +} + + + diff -r 427c0332a111 -r 7c2c75740370 dbus-d/dsrc/org/freedesktop/dbus/tool/CreateInterface.d --- a/dbus-d/dsrc/org/freedesktop/dbus/tool/CreateInterface.d Sun Oct 21 14:49:02 2007 +0200 +++ b/dbus-d/dsrc/org/freedesktop/dbus/tool/CreateInterface.d Sun Oct 21 19:22:41 2007 +0200 @@ -646,6 +646,24 @@ p.formatln( "{}public tango.core.Signal.Signal!({})* {}();", getIndent(indent), argList, sign.mName ); } +void writeInterfaceSignalDImpl( Print!(char) p, DefSignal sign, int indent ){ + char[] argList; + foreach( idx, arg; sign.mArguments ){ + if( idx > 0 ){ + argList ~= ","; + } + argList ~= " "; + argList ~= arg.toDType(); + } + if( argList.length > 0 ){ + argList ~= " "; + } + p.formatln( "{}protected tango.core.Signal.Signal!({}) _{};", getIndent(indent), argList, sign.mName ); + p.formatln( "{}public tango.core.Signal.Signal!({})* {}(){{", getIndent(indent), argList, sign.mName ); + p.formatln( "{}return &_{};", getIndent(indent+1), sign.mName ); + p.formatln( "{}}", getIndent(indent) ); +} + void writeInterface( Print!(char) p, char[] name, int indent ){ p.formatln( "{}// {}", getIndent(indent), name ); char[] nodeName = split( name, "." )[ $-1 ]; @@ -658,6 +676,15 @@ foreach( sign; intf.mSignals ){ writeInterfaceSignal( p, sign, indent+1 ); } + p.formatln( "{}template _StdJavaImpl(){{", getIndent(indent+1) ); + p.formatln( "{}}", getIndent(indent+1) ); + p.formatln( "{}template _StdDImpl(){{", getIndent(indent+1) ); + p.formatln( "{}void _init(){{", getIndent(indent+2) ); + p.formatln( "{}}", getIndent(indent+2) ); + foreach( sign; intf.mSignals ){ + writeInterfaceSignalDImpl( p, sign, indent+2 ); + } + p.formatln( "{}}", getIndent(indent+1) ); } foreach( child; intfTree.getChildNames( name ) ){ writeInterface( p, child, indent +1 );