HoneyListener.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package de.tudarmstadt.informatik.hostage;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import android.content.Context;
  7. import android.content.SharedPreferences;
  8. import android.content.SharedPreferences.Editor;
  9. import android.widget.Toast;
  10. import de.tudarmstadt.informatik.hostage.handler.AbstractHandler;
  11. import de.tudarmstadt.informatik.hostage.handler.ByteArrayHandlerImpl;
  12. import de.tudarmstadt.informatik.hostage.handler.StringHandlerImpl;
  13. import de.tudarmstadt.informatik.hostage.net.MyServerSocketFactory;
  14. import de.tudarmstadt.informatik.hostage.protocol.Protocol;
  15. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  16. import de.tudarmstadt.informatik.hostage.wrapper.ByteArray;
  17. public class HoneyListener implements Runnable {
  18. private ArrayList<AbstractHandler> handlers = new ArrayList<AbstractHandler>();
  19. public int getHandlerCount() {
  20. return handlers.size();
  21. }
  22. private Protocol protocol;
  23. private ServerSocket server;
  24. private Thread thread;
  25. private HoneyService service;
  26. // Shared Preferences
  27. private SharedPreferences pref;
  28. // Editor for Shared preferences
  29. private Editor editor;
  30. private boolean running = false;
  31. public boolean isRunning() {
  32. return running;
  33. }
  34. public HoneyListener(HoneyService service, Protocol protocol) {
  35. this.service = service;
  36. this.protocol = protocol;
  37. pref = service.getApplicationContext().getSharedPreferences(MainActivity.PREF_NAME, Context.MODE_PRIVATE);
  38. editor = pref.edit();
  39. }
  40. @Override
  41. public void run() {
  42. while (!thread.isInterrupted()) {
  43. addHandler();
  44. }
  45. for (AbstractHandler handler : handlers) {
  46. handler.kill();
  47. }
  48. }
  49. public void start() {
  50. try {
  51. server = new MyServerSocketFactory().createServerSocket(protocol
  52. .getPort());
  53. (this.thread = new Thread(this)).start();
  54. running = true;
  55. editor.putBoolean(protocol + MainActivity.LISTENER, true);
  56. editor.commit();
  57. service.notifyUI(MainActivity.LISTENER);
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. }
  62. public void stop() {
  63. try {
  64. thread.interrupt();
  65. server.close();
  66. running = false;
  67. editor.putBoolean(protocol + MainActivity.LISTENER, false);
  68. editor.commit();
  69. service.notifyUI(MainActivity.LISTENER);
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. public String getProtocolName() {
  75. return protocol.toString();
  76. }
  77. public void refreshHandlers() {
  78. for (Iterator<AbstractHandler> iterator = handlers.iterator(); iterator.hasNext(); ) {
  79. AbstractHandler handler = iterator.next();
  80. if (handler.isTerminated()) {
  81. iterator.remove();
  82. }
  83. }
  84. }
  85. private void addHandler() {
  86. try {
  87. Socket client = server.accept();
  88. handlers.add(newInstance(service, this, protocol.getClass()
  89. .newInstance(), client));
  90. int handlerCount = pref.getInt(protocol + MainActivity.HANDLER_COUNT, 0);
  91. editor.putInt(protocol + MainActivity.HANDLER_COUNT, handlerCount + 1);
  92. editor.commit();
  93. service.notifyUI(MainActivity.HANDLER_COUNT);
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. private AbstractHandler newInstance(HoneyService service,
  99. HoneyListener listener, Protocol protocol, Socket client) {
  100. if (protocol.getType().equals(String.class)) {
  101. return new StringHandlerImpl(service, listener, protocol, client);
  102. } else if (protocol.getType().equals(ByteArray.class)) {
  103. return new ByteArrayHandlerImpl(service, listener, protocol, client);
  104. } else {
  105. return null;
  106. }
  107. }
  108. }