Advertisement
Chiddix

server v0.2

Aug 19th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. package org.rabrg.net;
  2.  
  3. import java.io.IOException;
  4. import java.net.InetSocketAddress;
  5. import java.nio.channels.SelectionKey;
  6. import java.nio.channels.Selector;
  7. import java.nio.channels.ServerSocketChannel;
  8.  
  9. /**
  10.  * Represents a single non-blocking server.
  11.  * @author Ryan Greene
  12.  *
  13.  */
  14. public final class Server implements Runnable {
  15.  
  16.     /**
  17.      * The inet socket address.
  18.      */
  19.     private final InetSocketAddress inetSocketAddress;
  20.  
  21.     /**
  22.      * The server socket channel.
  23.      */
  24.     private final ServerSocketChannel serverSocketChannel;
  25.  
  26.     /**
  27.      * The selector.
  28.      */
  29.     private final Selector selector;
  30.  
  31.     /**
  32.      * The session.
  33.      */
  34.     private final Session session;
  35.  
  36.     /**
  37.      * The thread the server listens on.
  38.      */
  39.     private final Thread thread;
  40.  
  41.     /**
  42.      * Whether or not the server is running.
  43.      */
  44.     private volatile boolean running;
  45.  
  46.     /**
  47.      * Creates a new server which will be bound to the specified host and port.
  48.      * @param host The host the server is bound to.
  49.      * @param port The port the server is bound to.
  50.      * @param session The attachment of the server's selection keys.
  51.      * @return The created server.
  52.      * @throws IOException If an exception is thrown.
  53.      */
  54.     public static Server createServer(final String host, final int port, final Session session) throws IOException {
  55.         return new Server(new InetSocketAddress(host, port), ServerSocketChannel.open(), Selector.open(), session);
  56.     }
  57.  
  58.     /**
  59.      * Constructs a new server with the specified inet socket address, server socket channel and selector.
  60.      * @param inetSocketAddress The inet socket address.
  61.      * @param serverSocketChannel The server socket channel.
  62.      * @param selector The selector.
  63.      * @param session The attachment of the server's selection keys.
  64.      */
  65.     private Server(final InetSocketAddress inetSocketAddress, final ServerSocketChannel serverSocketChannel, final Selector selector, final Session session) {
  66.         this.inetSocketAddress = inetSocketAddress;
  67.         this.serverSocketChannel = serverSocketChannel;
  68.         this.selector = selector;
  69.         this.thread = new Thread(this);
  70.         this.session = session;
  71.     }
  72.  
  73.     /**
  74.      * Binds the server to the host and port the server was created with.
  75.      * @return The server instance.
  76.      * @throws IOException If an exception is thrown.
  77.      */
  78.     public Server bind() throws IOException {
  79.         serverSocketChannel.configureBlocking(false);
  80.         serverSocketChannel.socket().bind(inetSocketAddress);
  81.         serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT, session);
  82.         return this;
  83.     }
  84.  
  85.     /**
  86.      * Starts the server's listening thread.
  87.      */
  88.     public void start() {
  89.         running = true;
  90.         thread.start();
  91.     }
  92.  
  93.     @Override
  94.     public void run() {
  95.         try {
  96.             while (running) {
  97.                 selector.selectNow();
  98.                 for (SelectionKey selectionKey : selector.selectedKeys()) {
  99.                     if (selectionKey.isAcceptable()) {
  100.                         // register
  101.                     }
  102.                     if (selectionKey.isReadable()) {
  103.                         // read
  104.                     }
  105.                 }
  106.             }
  107.         } catch (IOException e) {
  108.             e.printStackTrace();
  109.         }
  110.     }
  111.  
  112.     /**
  113.      * Stops the server's listening thread.
  114.      */
  115.     public void stop() {
  116.         running = false;
  117.     }
  118.  
  119.     /**
  120.      * Checks whether or not the server is currently running.
  121.      * @return Whether or not the server is currently running.
  122.      */
  123.     public boolean isRunning() {
  124.         return running;
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement