HandwritingRecognitionService.java

00001 package edu.stanford.hci.r3.pen.handwriting;
00002 
00003 import java.io.BufferedReader;
00004 import java.io.BufferedWriter;
00005 import java.io.File;
00006 import java.io.IOException;
00007 import java.io.InputStream;
00008 import java.io.InputStreamReader;
00009 import java.io.OutputStreamWriter;
00010 import java.io.PrintWriter;
00011 import java.net.Socket;
00012 import java.net.UnknownHostException;
00013 import java.util.ArrayList;
00014 import java.util.List;
00015 
00016 import edu.stanford.hci.r3.PaperToolkit;
00017 import edu.stanford.hci.r3.config.Constants;
00018 import edu.stanford.hci.r3.util.DebugUtils;
00019 import edu.stanford.hci.r3.util.files.FileUtils;
00020 
00036 public class HandwritingRecognitionService {
00037 
00038         private static final int HWREC_PORT = Constants.Ports.HANDWRITING_RECOGNITION;
00039 
00040         private static final String HWREC_SERVER = "localhost";
00041 
00042         private static HandwritingRecognitionService instance;
00043 
00044         private static final String HW_REC_SERVER_EXE = "HandwritingRecognition.exe";
00045 
00046         private static final String REL_PATH_TO_HWREC_SERVER = "handwritingRec/HWRecServer/bin/Release/";
00047 
00051         public synchronized static HandwritingRecognitionService getInstance() {
00052                 if (instance == null) {
00053                         instance = new HandwritingRecognitionService();
00054                 }
00055                 return instance;
00056         }
00057 
00058         private boolean clientInitialized;
00059 
00060         private BufferedReader clientReader;
00061 
00062         private Socket clientSocket;
00063 
00067         private PrintWriter clientWriter;
00068 
00072         private boolean serverStarted;
00073 
00078         private HandwritingRecognitionService() {
00079                 initializeServer();
00080                 connectToServer();
00081 
00082                 // Exit the Server upon shutdown...
00083                 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
00084                         public void run() {
00085                                 HandwritingRecognitionService.getInstance().exitServer();
00086                         }
00087                 }));
00088         }
00089 
00093         private synchronized void connectToServer() {
00094                 if (clientInitialized) {
00095                         return;
00096                 }
00097 
00098                 while (!serverStarted) {
00099                         try {
00100                                 DebugUtils.println("Waiting for the server to start up...");
00101                                 wait();
00102                         } catch (InterruptedException e) {
00103                                 e.printStackTrace();
00104                         }
00105                 }
00106 
00107                 try {
00108                         clientSocket = new Socket(HWREC_SERVER, HWREC_PORT);
00109                         clientWriter = new PrintWriter(new BufferedWriter(
00110                                         new OutputStreamWriter(clientSocket.getOutputStream())));
00111                         clientReader = new BufferedReader(new InputStreamReader(
00112                                         clientSocket.getInputStream()));
00113                         clientInitialized = true;
00114                 } catch (UnknownHostException e) {
00115                         DebugUtils.println("Unknown Host: " + e.getLocalizedMessage());
00116                         clientInitialized = false;
00117                 } catch (IOException e) {
00118                         DebugUtils.println("IOException: " + e.getLocalizedMessage());
00119                         clientInitialized = false;
00120                 }
00121         }
00122 
00126         private void disconnectClient() {
00127                 try {
00128                         clientSocket.close();
00129                 } catch (IOException e) {
00130                         e.printStackTrace();
00131                 }
00132         }
00133 
00137         public void exitServer() {
00138                 clientWriter.println("[[quitserver]]");
00139                 clientWriter.flush();
00140                 disconnectClient();
00141         }
00142 
00148         public List<String> getAlternatives() {
00149                 clientWriter.println("[[topten]]");
00150                 clientWriter.flush();
00151                 String line = null;
00152                 final List<String> alternatives = new ArrayList<String>();
00153                 try {
00154                         while ((line = clientReader.readLine()) != null) {
00155                                 if (line.equals("[[endofalternatives]]")) {
00156                                         break;
00157                                 }
00158                                 alternatives.add(line);
00159                         }
00160                 } catch (IOException e) {
00161                         e.printStackTrace();
00162                 }
00163                 return alternatives;
00164         }
00165 
00169         private void initializeServer() {
00170 
00171                 final Object mutex = this;
00172 
00173                 // start a thread to start the server
00174                 new Thread(new Runnable() {
00175                         public void run() {
00176                                 // off of the PaperToolkit directory...
00177                                 // handwritingRec\bin\HandwritingRecognition.exe
00178                                 final File hwrecPath = new File(PaperToolkit
00179                                                 .getToolkitRootPath(), REL_PATH_TO_HWREC_SERVER);
00180                                 final File hwrecExe = new File(hwrecPath, HW_REC_SERVER_EXE);
00181                                 final ProcessBuilder builder = new ProcessBuilder(hwrecExe
00182                                                 .getPath());
00183                                 builder.directory(hwrecPath);
00184                                 try {
00185                                         final Process process = builder.start();
00186 
00187                                         DebugUtils.println(builder.directory());
00188 
00189                                         final InputStream inputStream = process.getInputStream();
00190                                         final BufferedReader br = new BufferedReader(
00191                                                         new InputStreamReader(inputStream));
00192                                         String line;
00193                                         // trap the console output
00194                                         while ((line = br.readLine()) != null) {
00195                                                 System.out.println("Handwriting Server: " + line);
00196                                                 synchronized (mutex) {
00197                                                         if (line.contains("[[serverstarted]]")) {
00198                                                                 serverStarted = true;
00199                                                                 mutex.notifyAll();
00200                                                         }
00201                                                 }
00202                                         }
00203                                 } catch (IOException e) {
00204                                         e.printStackTrace();
00205                                 }
00206                         }
00207 
00208                 }).start();
00209         }
00210 
00215         public String recognizeHandwriting(File xmlFile) {
00216                 return recognizeHandwriting(FileUtils.readFileIntoStringBuffer(xmlFile,
00217                                 false).toString());
00218         }
00219 
00227         public String recognizeHandwriting(String xml) {
00228                 clientWriter.println(xml);
00229                 clientWriter.flush();
00230                 try {
00231                         final String returnVal = clientReader.readLine();
00232                         return returnVal;
00233                 } catch (IOException e) {
00234                         e.printStackTrace();
00235                 }
00236                 return "";
00237         }
00238 }

Generated on Sat Apr 14 18:21:35 2007 for R3 Paper Toolkit by  doxygen 1.4.7