A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

66 lines
1.8 KiB

  1. /* Code for multithreaded server taken from Jakob Jenkov */
  2. package com.bitshift.parsing.utils;
  3. import java.net.ServerSocket;
  4. import java.net.Socket;
  5. import java.io.IOException;
  6. import com.bitshift.parsing.parsers.JavaParser;
  7. public class ParseServer implements Runnable{
  8. protected int serverPort = 8080;
  9. protected ServerSocket serverSocket = null;
  10. protected boolean isStopped = false;
  11. protected Thread runningThread= null;
  12. public ParseServer(int port){
  13. this.serverPort = port;
  14. }
  15. public void run(){
  16. synchronized(this){
  17. this.runningThread = Thread.currentThread();
  18. }
  19. openServerSocket();
  20. while(! isStopped()){
  21. Socket clientSocket = null;
  22. try {
  23. clientSocket = this.serverSocket.accept();
  24. } catch (IOException e) {
  25. if(isStopped()) {
  26. System.out.println("Server Stopped.") ;
  27. return;
  28. }
  29. throw new RuntimeException(
  30. "Error accepting client connection", e);
  31. }
  32. new Thread(new JavaParser(clientSocket)).start();
  33. }
  34. System.out.println("Server Stopped.") ;
  35. }
  36. private synchronized boolean isStopped() {
  37. return this.isStopped;
  38. }
  39. public synchronized void stop(){
  40. this.isStopped = true;
  41. try {
  42. this.serverSocket.close();
  43. } catch (IOException e) {
  44. throw new RuntimeException("Error closing server", e);
  45. }
  46. }
  47. private void openServerSocket() {
  48. try {
  49. this.serverSocket = new ServerSocket(this.serverPort);
  50. } catch (IOException e) {
  51. throw new RuntimeException("Cannot open port 8080", e);
  52. }
  53. }
  54. }