diff --git a/parsers/java/src/main/java/com/bitshift/parsing/Parse.java b/parsers/java/src/main/java/com/bitshift/parsing/Parse.java index 302c083..ae54a9e 100644 --- a/parsers/java/src/main/java/com/bitshift/parsing/Parse.java +++ b/parsers/java/src/main/java/com/bitshift/parsing/Parse.java @@ -1,24 +1,12 @@ package com.bitshift.parsing; -import java.io.IOException; - -import java.net.ServerSocket; -import java.net.Socket; - -import com.bitshift.parsing.parsers.JavaParser; +import com.bitshift.parsing.utils.ParseServer; public class Parse { public static void main(String[] args) { - try { - ServerSocket server = new ServerSocket(5033); - - while(true) { - Socket clientSocket = server.accept(); - new Thread(new JavaParser(clientSocket)).start(); - } - } catch (IOException ex) { - } + ParseServer server = new ParseServer(Integer.parseInt(args[0])); + new Thread(server).start(); } } diff --git a/parsers/java/src/main/java/com/bitshift/parsing/utils/ParseServer.java b/parsers/java/src/main/java/com/bitshift/parsing/utils/ParseServer.java new file mode 100644 index 0000000..291be34 --- /dev/null +++ b/parsers/java/src/main/java/com/bitshift/parsing/utils/ParseServer.java @@ -0,0 +1,65 @@ +/* Code for multithreaded server taken from Jakob Jenkov */ +package com.bitshift.parsing.utils; + +import java.net.ServerSocket; +import java.net.Socket; +import java.io.IOException; + +import com.bitshift.parsing.parsers.JavaParser; + +public class ParseServer implements Runnable{ + + protected int serverPort = 8080; + protected ServerSocket serverSocket = null; + protected boolean isStopped = false; + protected Thread runningThread= null; + + public ParseServer(int port){ + this.serverPort = port; + } + + public void run(){ + synchronized(this){ + this.runningThread = Thread.currentThread(); + } + openServerSocket(); + while(! isStopped()){ + Socket clientSocket = null; + try { + clientSocket = this.serverSocket.accept(); + } catch (IOException e) { + if(isStopped()) { + System.out.println("Server Stopped.") ; + return; + } + throw new RuntimeException( + "Error accepting client connection", e); + } + new Thread(new JavaParser(clientSocket)).start(); + } + System.out.println("Server Stopped.") ; + } + + + private synchronized boolean isStopped() { + return this.isStopped; + } + + public synchronized void stop(){ + this.isStopped = true; + try { + this.serverSocket.close(); + } catch (IOException e) { + throw new RuntimeException("Error closing server", e); + } + } + + private void openServerSocket() { + try { + this.serverSocket = new ServerSocket(this.serverPort); + } catch (IOException e) { + throw new RuntimeException("Cannot open port 8080", e); + } + } + +} diff --git a/parsers/java/src/main/java/com/bitshift/parsing/utils/Tuple.java b/parsers/java/src/main/java/com/bitshift/parsing/utils/Tuple.java deleted file mode 100644 index 115a3c6..0000000 --- a/parsers/java/src/main/java/com/bitshift/parsing/utils/Tuple.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.bitshift.parsing.utils; - -import java.util.List; -import java.util.Arrays; - -public class Tuple { - private List _objects; - - public Tuple(T... args) { - _objects = Arrays.asList(args); - } - - public String toString() { - StringBuilder builder = new StringBuilder(); - - for(T o: this._objects) { - builder.append(o + ","); - } - - String s = builder.toString(); - return "(" + s.substring(0, s.length() - 1) + ")"; - } -}