view dbus-d-javatests/jsrc/dbustest/DBusTester.java @ 5:7c2c75740370

code generation for signals
author Frank Benoit <benoit@tionex.de>
date Sun, 21 Oct 2007 19:22:41 +0200
parents 427c0332a111
children 963d271f7c25
line wrap: on
line source

package dbustest;

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.DBusDLoader;
import dbustest.DImpl.testSigI;

public class DBusTester {

	static int handledCounter;

	private static void ensure( boolean cond, String msg ){
		if( !cond ){
			throw new RuntimeException( msg );
		}
	}
	private static void ensureEqualLists( List<String> 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) {

		// 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();

			// 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<Boolean, Byte, Short, UInt16, Integer, UInt32, Long, UInt64, Double, String> 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<testSigI> handler = new DBusSigHandler<DImpl.testSigI>(){
				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();
		}
	}
}