Kaynağa Gözat

Can specify port number to run java server on.

tags/v1.0^2
Benjamin Attal 10 yıl önce
ebeveyn
işleme
6edb142b4a
3 değiştirilmiş dosya ile 68 ekleme ve 38 silme
  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 Dosyayı Görüntüle

@@ -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 Dosyayı Görüntüle

@@ -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 Dosyayı Görüntüle

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

Yükleniyor…
İptal
Kaydet