Sfoglia il codice sorgente

Can specify port number to run java server on.

tags/v1.0^2
Benjamin Attal 10 anni fa
parent
commit
6edb142b4a
3 ha cambiato i file con 68 aggiunte e 38 eliminazioni
  1. +3
    -15
      parsers/java/src/main/java/com/bitshift/parsing/Parse.java
  2. +65
    -0
      parsers/java/src/main/java/com/bitshift/parsing/utils/ParseServer.java
  3. +0
    -23
      parsers/java/src/main/java/com/bitshift/parsing/utils/Tuple.java

+ 3
- 15
parsers/java/src/main/java/com/bitshift/parsing/Parse.java Vedi File

@@ -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();
}

}

+ 65
- 0
parsers/java/src/main/java/com/bitshift/parsing/utils/ParseServer.java Vedi File

@@ -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);
}
}

}

+ 0
- 23
parsers/java/src/main/java/com/bitshift/parsing/utils/Tuple.java Vedi File

@@ -1,23 +0,0 @@
package com.bitshift.parsing.utils;

import java.util.List;
import java.util.Arrays;

public class Tuple<T> {
private List<T> _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) + ")";
}
}

Caricamento…
Annulla
Salva