org.flightgear.fgfsclient
Class FGFSConnection

java.lang.Object
  |
  +--org.flightgear.fgfsclient.FGFSConnection

public class FGFSConnection
extends Object

A connection to a running instance of FlightGear.

This class currently uses the FlightGear telnet interface, though it may be modified to use a different TCP/IP interface in the future. Client applications can use this library to examine and modify internal FlightGear properties.

To start FlightGear with the telnet server activated, use a command like this (to listen on port 9000):

 fgfs --telnet=9000
 

Then create a connection to FlightGear from your Java client application:

 FGFSConnection fgfs = new FGFSConnection("localhost", 9000);
 

Now you can use the connection to get and set FlightGear properties:

 double altitude = fgfs.getDouble("/position/altitude-ft");
 fgfs.setDouble("/orientation/heading", 270.0);
 

All methods that communicate directly with FlightGear are synchronized, since they must work over a single telnet connection.


Constructor Summary
FGFSConnection(String host, int port)
          Constructor.
 
Method Summary
 void close()
          Close the connection to FlightGear.
 String get(String name)
          Get the raw string value for a property.
 boolean getBoolean(String name)
          Get a property value as a boolean.
 double getDouble(String name)
          Get a property value as a double.
 float getFloat(String name)
          Get a property value as a float.
 int getInt(String name)
          Get a property value as an integer.
 long getLong(String name)
          Get a property value as a long.
 void set(String name, String value)
          Set the raw string value for a property.
 void setBoolean(String name, boolean value)
          Set a property value from a boolean.
 void setDouble(String name, double value)
          Set a property value from a double.
 void setFloat(String name, float value)
          Set a property value from a float.
 void setInt(String name, int value)
          Set a property value from an int.
 void setLong(String name, long value)
          Set a property value from a long.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FGFSConnection

public FGFSConnection(String host,
                      int port)
               throws IOException
Constructor.

Create a new connection to a running FlightGear program. The program must have been started with the --telnet=<port> command-line option.

Parameters:
host - The host name or IP address to connect to.
port - The port number where FlightGear is listening.
Throws:
IOException - If it is not possible to connect to a FlightGear process.
Method Detail

close

public void close()
           throws IOException
Close the connection to FlightGear.

The client application should always invoke this method when it has finished with a connection, to allow cleanup.

Throws:
IOException - If there is an error closing the connection.

get

public String get(String name)
           throws IOException
Get the raw string value for a property.

This is the primitive method for all property lookup; everything comes in as a string, and is only later converted by methods like getDouble(String). As a result, if you need the value as a string anyway, it makes sense to use this method directly rather than forcing extra conversions.

Parameters:
name - The FlightGear property name to look up.
Returns:
The property value as a string (non-existant properties return the empty string).
Throws:
IOException - If there is an error communicating with FlightGear or if the connection is lost.
See Also:
getBoolean(String), getInt(String), getLong(String), getFloat(String), getDouble(String)

set

public void set(String name,
                String value)
         throws IOException
Set the raw string value for a property.

This is the primitive method for all property modification; everything goes out as a string, after it has been converted by methods like setDouble(String,double). As a result, if you have the value as a string already, it makes sense to use this method directly rather than forcing extra conversions.

Parameters:
name - The FlightGear property name to modify or create.
value - The new value for the property, as a string.
Throws:
IOException - If there is an error communicating with FlightGear or if the connection is lost.
See Also:
setBoolean(String,boolean), setInt(String,int), setLong(String,long), setFloat(String,float), setDouble(String,double)

getBoolean

public boolean getBoolean(String name)
                   throws IOException
Get a property value as a boolean.
Parameters:
name - The property name to look up.
Returns:
The property value as a boolean.
See Also:
get(String)

getInt

public int getInt(String name)
           throws IOException
Get a property value as an integer.
Parameters:
name - The property name to look up.
Returns:
The property value as an int.
See Also:
get(String)

getLong

public long getLong(String name)
             throws IOException
Get a property value as a long.
Parameters:
name - The property name to look up.
Returns:
The property value as a long.
See Also:
get(String)

getFloat

public float getFloat(String name)
               throws IOException
Get a property value as a float.
Parameters:
name - The property name to look up.
Returns:
The property value as a float.
See Also:
get(String)

getDouble

public double getDouble(String name)
                 throws IOException
Get a property value as a double.
Parameters:
name - The property name to look up.
Returns:
The property value as a double.
See Also:
get(String)

setBoolean

public void setBoolean(String name,
                       boolean value)
                throws IOException
Set a property value from a boolean.
Parameters:
name - The property name to create or modify.
value - The new property value as a boolean.
See Also:
set(String,String)

setInt

public void setInt(String name,
                   int value)
            throws IOException
Set a property value from an int.
Parameters:
name - The property name to create or modify.
value - The new property value as an int.
See Also:
set(String,String)

setLong

public void setLong(String name,
                    long value)
             throws IOException
Set a property value from a long.
Parameters:
name - The property name to create or modify.
value - The new property value as a long.
See Also:
set(String,String)

setFloat

public void setFloat(String name,
                     float value)
              throws IOException
Set a property value from a float.
Parameters:
name - The property name to create or modify.
value - The new property value as a float.
See Also:
set(String,String)

setDouble

public void setDouble(String name,
                      double value)
               throws IOException
Set a property value from a double.
Parameters:
name - The property name to create or modify.
value - The new property value as a double.
See Also:
set(String,String)