Guest User

Untitled

a guest
Jan 22nd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. package org.codeusa.omega.handlers;
  2.  
  3. import java.net.ServerSocket;
  4.  
  5. /**
  6. * Omega
  7. * Boss.java
  8. * @version 1.0.0
  9. * @author SiniSoul (SiniSoul@live.com)
  10. */
  11. public final class BossHandler implements Runnable {
  12.  
  13. /**
  14. * The current thread running this boss handler.
  15. */
  16. private Thread self = null,
  17.  
  18. /**
  19. * The thread that handles incoming connections.
  20. */
  21. acceptor = null;
  22.  
  23. /**
  24. * The socket handler for this handler.
  25. */
  26. private SocketHandler sockethandler = null;
  27.  
  28. /**
  29. * The server socket that this handler services.
  30. */
  31. private ServerSocket socket = null;
  32.  
  33. /**
  34. * If this thread is stopped or not.
  35. */
  36. private boolean running = false;
  37.  
  38. /**
  39. * If this thread is stopped or not.
  40. */
  41. private boolean stopped = false;
  42.  
  43. /**
  44. * Start this handler.
  45. */
  46. public void start(ServerSocket s) {
  47. /* Check to see if it is already stopped */
  48. if(stopped || running)
  49. throw new IllegalStateException("Cannot start this handler - invalid state");
  50. /* Set the socket that this handler services */
  51. socket = s;
  52. /* Set the handler to running */
  53. running = true;
  54. /* Construct the socket handler */
  55. sockethandler = new SocketHandler(this);
  56. /* Construct the acceptor from the socket handler */
  57. acceptor = createThread(sockethandler, 5);
  58. /* Create the self thread */
  59. self = createThread(this, 10);
  60. /* Start the self thread */
  61. self.start();
  62. }
  63.  
  64. /**
  65. * Destroys this handler, can also be considered stopping.
  66. */
  67. public void destroy() {
  68. /* Stop the handler from running another cycle */
  69. running = false;
  70. /* Destroy the self thread */
  71. self = null;
  72. /* Destroy the socket */
  73. socket = null;
  74. }
  75.  
  76. /**
  77. *
  78. */
  79. @Override
  80. public void run() {
  81. try {
  82. while(running) {
  83. /* Construct the socket handler if needed */
  84. if(sockethandler == null)
  85. sockethandler = new SocketHandler(this);
  86. /* Socket handler was unexpectidly stopped */
  87. if(sockethandler.stopped) {
  88. /* Log that the socket handler was unexpectidly stopped */
  89. System.err.println("Socket handler unexpectidly stopped...");
  90. /* Log that we are attempting a restart */
  91. System.err.println("Attempting recovery...");
  92. /* Destroy the socket handler */
  93. sockethandler = null;
  94. /* Construct a new socket handler */
  95. sockethandler = new SocketHandler(this);
  96. /* Destroy the acceptor */
  97. acceptor = null;
  98. /* Consturct the acceptor with the socket handler */
  99. acceptor = createThread(sockethandler, 5);
  100. }
  101. /* Start the socket handler if needed */
  102. if(!sockethandler.running) {
  103. /* Start the socket handler */
  104. sockethandler.start(acceptor);
  105. }
  106. }
  107. } catch(Exception ex) {
  108. /* Log that an error had been caught */
  109. System.err.println("Unexpected fatal error during boss logic : "+ex);
  110. /* Destroy on failure */
  111. destroy();
  112. }
  113. /* Set the thread to stopped */
  114. stopped = true;
  115. }
  116.  
  117. /**
  118. * Gets the socket that this boss services.
  119. * @return The socket that this boss services.
  120. */
  121. public synchronized ServerSocket getSocket() {
  122. return socket;
  123. }
  124.  
  125. /**
  126. * Creates a new thread.
  127. * @param runnable The runnable to use to construct the thread.
  128. * @param priority The thread's priority.
  129. * @return The created thread.
  130. */
  131. public static Thread createThread(Runnable runnable, int priority) {
  132. Thread thread = new Thread(runnable);
  133. thread.setPriority(priority);
  134. return thread;
  135. }
  136. }
Add Comment
Please, Sign In to add comment