HoneyListener.java 1.7 KB

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