Difference between revisions of "D-Bus examples"

From Apertium
Jump to navigation Jump to search
(→‎Java: Pruned the Java section to a snippet.)
Line 28: Line 28:
==Java==
==Java==


{{Java_D-Bus_snippet}}
''Read the guide to [[Installing_the_Java_D-Bus_bindings|installing the Java D-Bus bindings]] if you don't have the library installed already.''


We have a guide to [[Installing the Java D-Bus bindings|installing the Java D-Bus bindings]] and a guide to [[Compiling a Java D-Bus program|compiling a small Java application which uses an Apertium D-Bus service]].
Since both D-Bus and Java use statically typed systems, Java needs to have the D-Bus interface descriptions of the objects it will use available at compile time. The 'CreateInterface' program in the Java D-Bus distribution creates Java interfaces by introspecting D-Bus objects.

We will create a Java interface corresponding to the info.apertium.Info interface and we will store it under the file org/apertium/Info.java. To do this, execute:
<pre>CreateInterface org.apertium.info / > org/apertium/Info.java</pre>
(and remember to use <code>dbus-launch --exit-with-session</code> if this does not work).

The contents out the newly created file should look something like:
<pre>
/* File: org/apertium/Info.java */
package org.apertium;
import java.util.List;
import org.freedesktop.dbus.DBusInterface;
public interface Info extends DBusInterface
{

public String directory();
public List<List<String>> get_pipeline(String mode);
public List<String> get_filters(String _type);
public List<String> modes();
public String mode_directory();

}
</pre>

The next step is to create our minimal Java program. Create a file called <code>TestDBus</code> with the following contents:
<pre>
import org.freedesktop.DBus;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;

import org.apertium.Info; // The interface we use to access org.apertium.info/

class TestDBus {

public static void main(String[] args) throws org.freedesktop.dbus.exceptions.DBusException {
DBusConnection bus = null;
Info info = null;

bus = DBusConnection.getConnection(DBusConnection.SESSION);
info = bus.getRemoteObject("org.apertium.info", "/", Info.class);

for (String s : info.modes()) {
System.out.println(s);
}

bus.disconnect();
}
}
</pre>

To compile this program, we need to include the current path on the class path as well as the jar files needed by the Java D-Bus binding. To compile, execute (you should modify the directories to suit your installation):
<pre>
javac -cp .:/usr/local/share/java/dbus.jar:/usr/local/share/java/unix.jar:\
/usr/local/share/java/debug-disable.jar TestDBus.java
</pre>

To run the program, you need to specify the class paths specified above, as well as the JNI code (which installs to <code>/usr/local/lib/jni</code> by default (if you are using TCP instead of UNIX domain sockets for transport, then this won't be needed). You can run the program using:
<pre>
java -cp .:/usr/local/share/java/dbus.jar:\
/usr/local/share/java/unix.jar:/usr/local/share/java/debug-disable.jar\
-Djava.library.path=/usr/local/lib/jni TestDBus
</pre>
(and if it does not work, prefix the command with <code>dbus-launch --exit-with-session</code>).

Of course, if you are writing an application, you would hide all of these flags in a wrapper script.


[[Category:Development]]
[[Category:Development]]

Revision as of 13:00, 19 December 2007

Here are some code snippets for various programming languages (complete with completely unnecessary comments) showing how you can interface with Apertium by means of D-Bus. You'll need to install the D-Bus service for Apertium.

Python

#!/usr/bin/python
# coding=utf-8
# -*- encoding: utf-8 -*-

import dbus, sys, codecs;

sys.stdin = codecs.getwriter('utf-8')(sys.stdin);
sys.stdout = codecs.getwriter('utf-8')(sys.stdout);

mode_name = 'en-af'; # The name of the installed mode
dbus_mode_name = '/' + '_'.join(pair_name.split('-')); # Some mandatory mangling

bus = dbus.SessionBus(); # Create a new session bus

translator = dbus.Interface(bus.get_object('org.apertium.mode', dbus_mode_name), 'org.apertium.Mode'); # Get the translator

input = sys.stdin.read(); # Read the data

print translator.translate({}, input); # Print the translation

Java

import org.freedesktop.DBus;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;

import org.apertium.Info; // The interface we use to access org.apertium.info/

class TestDBus {

        public static void main(String[] args) throws org.freedesktop.dbus.exceptions.DBusException {
                DBusConnection bus = null;
                Info info = null;

                bus = DBusConnection.getConnection(DBusConnection.SESSION);
                info = bus.getRemoteObject("org.apertium.info", "/", Info.class);

                for (String s : info.modes()) {
                        System.out.println(s);
                }

                bus.disconnect();
        }
}

We have a guide to installing the Java D-Bus bindings and a guide to compiling a small Java application which uses an Apertium D-Bus service.