package com.hantino.adictionaryclient.server;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.hantino.adictionaryclient.client.DictionaryService;
import com.hantino.adictionaryclient.client.HostPort;

@SuppressWarnings("serial")
public class DictionaryServiceImpl extends RemoteServiceServlet implements DictionaryService {
	
	private static Socket socket;
	private static PrintWriter out;
	private static BufferedReader in;
	
	public ArrayList<String> getDictionaries(HostPort hostport) {
		// TODO Auto-generated method stub
		openCommand(hostport.getHost(), hostport.getPort());
		String fromServer;
		String fromUser = "SHOW DB";
		ArrayList<String> dicts = new ArrayList<String>();
		try {
			out.println(fromUser);
			while ((fromServer = in.readLine()) != null) {
				// check to see if it is a status code
				boolean isResponseCode = (fromServer.trim().length() > 2 
						&& Character.isDigit(fromServer.trim().charAt(0))
						&& Character.isDigit(fromServer.trim().charAt(1))
						&& Character.isDigit(fromServer.trim().charAt(2)));
		
				if (!isResponseCode) {
					//System.out.println(fromServer);
					dicts.add(fromServer);
				}
				else if (fromServer.trim().contains("250 ok")) {
					break;	
				}
			}
		} catch (IOException e) {
			System.out.println("");
			closeCommand();
		}
		closeCommand();
		return dicts;
	}
	
	// Command to open connection to server
		static void openCommand(String host, String port) {

			String hostName = host;
			int portNumber = Integer.parseInt(port);


			try {			
				socket = new Socket(hostName, portNumber);
				out = new PrintWriter(socket.getOutputStream(), true);
				in = new BufferedReader(new 
						InputStreamReader(socket.getInputStream()));

				// default dictionary
				
				//System.out.println("Connected");
			} catch(IOException e) {
				System.out.println("920 Control connection to " + 
						hostName +" on port "+ 
						portNumber +" failed to open.");
			}
		}
		
		// Command to close connection 
		public static void closeCommand() {
			try {
				out.close();
				in.close();
				socket.close();
			} catch (IOException e) {
				System.out.println("");
			}
		}
	
}

@@ -1342,16 +1342,20 @@
 on e) %7B%0A
+    
 %09%09%09Syste
@@ -1348,35 +1348,35 @@
 %7B%0A    %09%09%09System.
-out
+err
 .println(%22%22);%0A%09%09
@@ -1373,35 +1373,174 @@
 ln(%22
-%22);%0A%09%09%09closeCommand();%0A%09%09%7D%0A
+Caught IOException: %22 + e.getMessage());%0A%09%09%7D catch (NullPointerException e) %7B%0A%09%09%09System.err.println(%22Caught NullPointerException: %22 + e.getMessage());%0A%09%09%7D finally %7B%0A%09
 %09%09cl
@@ -1553,16 +1553,20 @@
 mand();%0A
+%09%09%7D%0A
 %09%09return

Remove duplicate code