/netbeans-proj/src/org/flightgear/fgfsclient/FGFSConnection.java

https://bitbucket.org/muraleen/virtual-star-alliance-autopirep-and-acars-system-flightgear · Java · 330 lines · 94 code · 43 blank · 193 comment · 0 complexity · bd6cf1f8029d437231718c5755041232 MD5 · raw file

  1. // FGFSConnection.java - client library for the FlightGear flight simulator.
  2. // Started June 2002 by David Megginson, david@megginson.com
  3. // This library is in the Public Domain and comes with NO WARRANTY.
  4. package org.flightgear.fgfsclient;
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.net.Socket;
  10. /**
  11. * A connection to a running instance of FlightGear.
  12. *
  13. * <p>This class currently uses the FlightGear telnet interface,
  14. * though it may be modified to use a different TCP/IP interface in
  15. * the future. Client applications can use this library to examine
  16. * and modify internal FlightGear properties.</p>
  17. *
  18. * <p>To start FlightGear with the telnet server activated, use a
  19. * command like this (to listen on port 9000):</p>
  20. *
  21. * <blockquote><pre>
  22. * fgfs --telnet=9000
  23. * </pre></blockquote>
  24. *
  25. * <p>Then create a connection to FlightGear from your Java client
  26. * application:</p>
  27. *
  28. * <blockquote><pre>
  29. * FGFSConnection fgfs = new FGFSConnection("localhost", 9000);
  30. * </pre></blockquote>
  31. *
  32. * <p>Now you can use the connection to get and set FlightGear
  33. * properties:</p>
  34. *
  35. * <blockquote><pre>
  36. * double altitude = fgfs.getDouble("/position/altitude-ft");
  37. * fgfs.setDouble("/orientation/heading", 270.0);
  38. * </pre></blockquote>
  39. *
  40. * <p>All methods that communicate directly with FlightGear are
  41. * synchronized, since they must work over a single telnet
  42. * connection.</p>
  43. *
  44. * String host = jTextField1.getText();
  45. int port = Integer.parseInt(jTextField2.getText());
  46. try {
  47. FGFSConnection fgfs = new FGFSConnection(host, port);
  48. } catch (IOException ex) {
  49. Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex);
  50. }
  51. *
  52. *
  53. *
  54. *
  55. */
  56. public class FGFSConnection
  57. {
  58. ////////////////////////////////////////////////////////////////////
  59. // Constructor.
  60. ////////////////////////////////////////////////////////////////////
  61. /**
  62. * Constructor.
  63. *
  64. * <p>Create a new connection to a running FlightGear program.
  65. * The program must have been started with the --telnet=&lt;port&gt;
  66. * command-line option.</p>
  67. *
  68. * @param host The host name or IP address to connect to.
  69. * @param port The port number where FlightGear is listening.
  70. * @exception IOException If it is not possible to connect to
  71. * a FlightGear process.
  72. */
  73. public FGFSConnection (String host, int port)
  74. throws IOException
  75. {
  76. socket = new Socket(host, port);
  77. in =
  78. new BufferedReader(new InputStreamReader(socket.getInputStream()));
  79. out = new PrintWriter(socket.getOutputStream(), true);
  80. out.println("data\r");
  81. }
  82. ////////////////////////////////////////////////////////////////////
  83. // Primitive getter and setter.
  84. ////////////////////////////////////////////////////////////////////
  85. /**
  86. * Close the connection to FlightGear.
  87. *
  88. * <p>The client application should always invoke this method when
  89. * it has finished with a connection, to allow cleanup.</p>
  90. *
  91. * @exception IOException If there is an error closing the
  92. * connection.
  93. */
  94. public synchronized void close ()
  95. throws IOException
  96. {
  97. out.println("quit\r");
  98. in.close();
  99. out.close();
  100. socket.close();
  101. }
  102. /**
  103. * Get the raw string value for a property.
  104. *
  105. * <p>This is the primitive method for all property lookup;
  106. * everything comes in as a string, and is only later converted by
  107. * methods like {@link #getDouble(String)}. As a result, if you
  108. * need the value as a string anyway, it makes sense to use this
  109. * method directly rather than forcing extra conversions.</p>
  110. *
  111. * @param name The FlightGear property name to look up.
  112. * @return The property value as a string (non-existant properties
  113. * return the empty string).
  114. * @exception IOException If there is an error communicating with
  115. * FlightGear or if the connection is lost.
  116. * @see #getBoolean(String)
  117. * @see #getInt(String)
  118. * @see #getLong(String)
  119. * @see #getFloat(String)
  120. * @see #getDouble(String)
  121. */
  122. public synchronized String get (String name)
  123. throws IOException
  124. {
  125. out.println("get " + name + '\r');
  126. return in.readLine();
  127. }
  128. /**
  129. * Set the raw string value for a property.
  130. *
  131. * <p>This is the primitive method for all property modification;
  132. * everything goes out as a string, after it has been converted by
  133. * methods like {@link #setDouble(String,double)}. As a result, if
  134. * you have the value as a string already, it makes sense to use
  135. * this method directly rather than forcing extra conversions.</p>
  136. *
  137. * @param name The FlightGear property name to modify or create.
  138. * @param value The new value for the property, as a string.
  139. * @exception IOException If there is an error communicating with
  140. * FlightGear or if the connection is lost.
  141. * @see #setBoolean(String,boolean)
  142. * @see #setInt(String,int)
  143. * @see #setLong(String,long)
  144. * @see #setFloat(String,float)
  145. * @see #setDouble(String,double)
  146. */
  147. public synchronized void set (String name, String value)
  148. throws IOException
  149. {
  150. out.println("set " + name + ' ' + value + '\r');
  151. }
  152. ////////////////////////////////////////////////////////////////////
  153. // Derived getters and setters.
  154. ////////////////////////////////////////////////////////////////////
  155. /**
  156. * Get a property value as a boolean.
  157. *
  158. * @param name The property name to look up.
  159. * @return The property value as a boolean.
  160. * @see #get(String)
  161. */
  162. public boolean getBoolean (String name)
  163. throws IOException
  164. {
  165. return get(name).equals("true");
  166. }
  167. /**
  168. * Get a property value as an integer.
  169. *
  170. * @param name The property name to look up.
  171. * @return The property value as an int.
  172. * @see #get(String)
  173. */
  174. public int getInt (String name)
  175. throws IOException
  176. {
  177. return Integer.parseInt(get(name));
  178. }
  179. /**
  180. * Get a property value as a long.
  181. *
  182. * @param name The property name to look up.
  183. * @return The property value as a long.
  184. * @see #get(String)
  185. */
  186. public long getLong (String name)
  187. throws IOException
  188. {
  189. return Long.parseLong(get(name));
  190. }
  191. /**
  192. * Get a property value as a float.
  193. *
  194. * @param name The property name to look up.
  195. * @return The property value as a float.
  196. * @see #get(String)
  197. */
  198. public float getFloat (String name)
  199. throws IOException
  200. {
  201. return Float.parseFloat(get(name));
  202. }
  203. /**
  204. * Get a property value as a double.
  205. *
  206. * @param name The property name to look up.
  207. * @return The property value as a double.
  208. * @see #get(String)
  209. */
  210. public double getDouble (String name)
  211. throws IOException
  212. {
  213. return Double.parseDouble(get(name));
  214. }
  215. /**
  216. * Set a property value from a boolean.
  217. *
  218. * @param name The property name to create or modify.
  219. * @param value The new property value as a boolean.
  220. * @see #set(String,String)
  221. */
  222. public void setBoolean (String name, boolean value)
  223. throws IOException
  224. {
  225. set(name, (value ? "true" : "false"));
  226. }
  227. /**
  228. * Set a property value from an int.
  229. *
  230. * @param name The property name to create or modify.
  231. * @param value The new property value as an int.
  232. * @see #set(String,String)
  233. */
  234. public void setInt (String name, int value)
  235. throws IOException
  236. {
  237. set(name, Integer.toString(value));
  238. }
  239. /**
  240. * Set a property value from a long.
  241. *
  242. * @param name The property name to create or modify.
  243. * @param value The new property value as a long.
  244. * @see #set(String,String)
  245. */
  246. public void setLong (String name, long value)
  247. throws IOException
  248. {
  249. set(name, Long.toString(value));
  250. }
  251. /**
  252. * Set a property value from a float.
  253. *
  254. * @param name The property name to create or modify.
  255. * @param value The new property value as a float.
  256. * @see #set(String,String)
  257. */
  258. public void setFloat (String name, float value)
  259. throws IOException
  260. {
  261. set(name, Float.toString(value));
  262. }
  263. /**
  264. * Set a property value from a double.
  265. *
  266. * @param name The property name to create or modify.
  267. * @param value The new property value as a double.
  268. * @see #set(String,String)
  269. */
  270. public void setDouble (String name, double value)
  271. throws IOException
  272. {
  273. set(name, Double.toString(value));
  274. }
  275. ////////////////////////////////////////////////////////////////////
  276. // Internal state.
  277. ////////////////////////////////////////////////////////////////////
  278. private Socket socket;
  279. private BufferedReader in;
  280. private PrintWriter out;
  281. }
  282. // end of FGFSConnection.java