HoneyListener.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package de.tudarmstadt.informatik.hostage;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.util.ArrayList;
  5. import de.tudarmstadt.informatik.hostage.net.MyServerSocketFactory;
  6. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  7. public class HoneyListener implements Runnable {
  8. private ArrayList<HoneyHandler> handlers = new ArrayList<HoneyHandler>();
  9. public int getHandlerCount() {
  10. return handlers.size();
  11. }
  12. private Protocol protocol;
  13. private ServerSocket server;
  14. private Thread thread;
  15. private HoneyService service;
  16. private boolean running = false;
  17. public boolean isRunning() {
  18. return running;
  19. }
  20. public HoneyListener(HoneyService service, Protocol protocol) {
  21. this.service = service;
  22. this.protocol = protocol;
  23. }
  24. @Override
  25. public void run() {
  26. while (!thread.isInterrupted()) {
  27. addHandler();
  28. }
  29. for (HoneyHandler handler : handlers) {
  30. handler.kill();
  31. }
  32. }
  33. public void start() {
  34. try {
  35. server = new MyServerSocketFactory().createServerSocket(protocol
  36. .getPort());
  37. (this.thread = new Thread(this)).start();
  38. running = true;
  39. service.notifyUI();
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. public void stop() {
  45. try {
  46. thread.interrupt();
  47. server.close();
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. running = false;
  52. service.notifyUI();
  53. }
  54. public String getProtocolName() {
  55. return protocol.toString();
  56. }
  57. public void refreshHandlers() {
  58. for (HoneyHandler handler : handlers) {
  59. if (handler.isTerminated()) {
  60. handlers.remove(handler);
  61. }
  62. }
  63. service.notifyUI();
  64. }
  65. private void addHandler() {
  66. try {
  67. Socket client = server.accept();
  68. handlers.add(new HoneyHandler(service, this, protocol.getClass()
  69. .newInstance(), client));
  70. service.notifyUI();
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. }