Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.codeusa.omega.handlers;
- import java.net.ServerSocket;
- /**
- * Omega
- * Boss.java
- * @version 1.0.0
- * @author SiniSoul (SiniSoul@live.com)
- */
- public final class BossHandler implements Runnable {
- /**
- * The current thread running this boss handler.
- */
- private Thread self = null,
- /**
- * The thread that handles incoming connections.
- */
- acceptor = null;
- /**
- * The socket handler for this handler.
- */
- private SocketHandler sockethandler = null;
- /**
- * The server socket that this handler services.
- */
- private ServerSocket socket = null;
- /**
- * If this thread is stopped or not.
- */
- private boolean running = false;
- /**
- * If this thread is stopped or not.
- */
- private boolean stopped = false;
- /**
- * Start this handler.
- */
- public void start(ServerSocket s) {
- /* Check to see if it is already stopped */
- if(stopped || running)
- throw new IllegalStateException("Cannot start this handler - invalid state");
- /* Set the socket that this handler services */
- socket = s;
- /* Set the handler to running */
- running = true;
- /* Construct the socket handler */
- sockethandler = new SocketHandler(this);
- /* Construct the acceptor from the socket handler */
- acceptor = createThread(sockethandler, 5);
- /* Create the self thread */
- self = createThread(this, 10);
- /* Start the self thread */
- self.start();
- }
- /**
- * Destroys this handler, can also be considered stopping.
- */
- public void destroy() {
- /* Stop the handler from running another cycle */
- running = false;
- /* Destroy the self thread */
- self = null;
- /* Destroy the socket */
- socket = null;
- }
- /**
- *
- */
- @Override
- public void run() {
- try {
- while(running) {
- /* Construct the socket handler if needed */
- if(sockethandler == null)
- sockethandler = new SocketHandler(this);
- /* Socket handler was unexpectidly stopped */
- if(sockethandler.stopped) {
- /* Log that the socket handler was unexpectidly stopped */
- System.err.println("Socket handler unexpectidly stopped...");
- /* Log that we are attempting a restart */
- System.err.println("Attempting recovery...");
- /* Destroy the socket handler */
- sockethandler = null;
- /* Construct a new socket handler */
- sockethandler = new SocketHandler(this);
- /* Destroy the acceptor */
- acceptor = null;
- /* Consturct the acceptor with the socket handler */
- acceptor = createThread(sockethandler, 5);
- }
- /* Start the socket handler if needed */
- if(!sockethandler.running) {
- /* Start the socket handler */
- sockethandler.start(acceptor);
- }
- }
- } catch(Exception ex) {
- /* Log that an error had been caught */
- System.err.println("Unexpected fatal error during boss logic : "+ex);
- /* Destroy on failure */
- destroy();
- }
- /* Set the thread to stopped */
- stopped = true;
- }
- /**
- * Gets the socket that this boss services.
- * @return The socket that this boss services.
- */
- public synchronized ServerSocket getSocket() {
- return socket;
- }
- /**
- * Creates a new thread.
- * @param runnable The runnable to use to construct the thread.
- * @param priority The thread's priority.
- * @return The created thread.
- */
- public static Thread createThread(Runnable runnable, int priority) {
- Thread thread = new Thread(runnable);
- thread.setPriority(priority);
- return thread;
- }
- }
Add Comment
Please, Sign In to add comment