/**
 * Copyright (c) 2010 David Dearing
 */

package com.dpdearing.sandbox.gpsemulator.server;

import java.io.IOException;

import com.dpdearing.sandbox.gpsemulator.common.LocationService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.mud.telnet.TelnetWrapper;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class LocationServiceImpl extends RemoteServiceServlet implements
      LocationService {
   /**
    * The telnet wrapper.
    */
   private TelnetWrapper _telnet;

   /**
    * Constructor.
    */
   public LocationServiceImpl() {
      _telnet = new TelnetWrapper();
   }
   
   /**
    * {@inheritDoc}
    */
   public void setPort(final int port) throws IOException {
      // disconnect from any previous connection
      _telnet.disconnect();
      // reconnect to the new port
      _telnet.connect("localhost", port);
   }
   
   /**
    * {@inheritDoc}
    */
   public void setLocation(final double latitude, final double longitude)
         throws IOException {
      // telnet the geo fix location: longitude first!
      _telnet.send("geo fix " + longitude + " " + latitude);
   }
}

@@ -19,16 +19,19 @@
 (c) 2010
+-17
  David D
@@ -94,24 +94,46 @@
 .server;%0D%0A%0D%0A
+import java.io.File;%0D%0A
 import java.
@@ -148,16 +148,110 @@
 eption;%0D
+%0Aimport java.util.Scanner;%0D%0Aimport java.util.logging.Level;%0D%0Aimport java.util.logging.Logger;%0D
 %0A%0D%0Aimpor
@@ -579,23 +579,16 @@
 plements
-%0D%0A     
  Locatio
@@ -597,16 +597,122 @@
 ervice %7B
+%0D%0A%0D%0A   private static Logger logger =%0D%0A           Logger.getLogger(LocationServiceImpl.class.getName());%0D%0A
 %0D%0A   /**
@@ -747,24 +747,30 @@
 %0A    */%0D%0A   
+final 
 private Teln
@@ -809,101 +809,891 @@
   * 
-Constructor.%0D%0A    */%0D%0A   public LocationServiceImpl() %7B%0D%0A      _telnet = new TelnetWrapper();
+The emulator authentication token%0D%0A    */%0D%0A   private String _emulator_console_auth_token = null;%0D%0A%0D%0A   /**%0D%0A    * Constructor.%0D%0A    */%0D%0A   public LocationServiceImpl() %7B%0D%0A      _telnet = new TelnetWrapper();%0D%0A%0D%0A      //TODO does this work on all platforms?%0D%0A      final String home = System.getProperty(%22user.home%22);%0D%0A      final File authTokenFile = new File(home, %22.emulator_console_auth_token%22);%0D%0A      try %7B%0D%0A         // read the entire contents of the%0D%0A         _emulator_console_auth_token = new Scanner(authTokenFile).useDelimiter(%22%5C%5CZ%22).next();%0D%0A         logger.info(%22Loaded emulator_console_auth_token %22 + _emulator_console_auth_token);%0D%0A      %7D catch (IOException ioe) %7B%0D%0A         logger.log(Level.SEVERE,%0D%0A                 %22Failed to read the emulator_console_auth_token from %22 +%0D%0A                         authTokenFile.getAbsolutePath(),%0D%0A                 ioe);%0D%0A      %7D
 %0D%0A  
@@ -1887,18 +1887,16 @@
      // 
-re
 connect 
@@ -1906,16 +1906,89 @@
 the 
-new
+specified port%0D%0A      logger.info(%22Connecting to emulator at localhost:%22 +
  port
+);
 %0D%0A  
@@ -2024,24 +2024,353 @@
 t%22, port);%0D%0A
+      if (_emulator_console_auth_token == null) %7B%0D%0A         logger.info(%22No emulator_console_auth_token; skipping authentication%22);%0D%0A      %7D else %7B%0D%0A         logger.info(%22Authenticating with emulator_console_auth_token: %22 + _emulator_console_auth_token);%0D%0A         _telnet.send(%22auth %22 + _emulator_console_auth_token);%0D%0A      %7D%0D%0A
    %7D%0D%0A   %0D%0A 

Fix for issue #15 - use the emulator authentication token after first connecting via telnet