Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.rabrg.net;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.nio.channels.SelectionKey;
- import java.nio.channels.Selector;
- import java.nio.channels.ServerSocketChannel;
- /**
- * Represents a single non-blocking server.
- * @author Ryan Greene
- *
- */
- public final class Server implements Runnable {
- /**
- * The inet socket address.
- */
- private final InetSocketAddress inetSocketAddress;
- /**
- * The server socket channel.
- */
- private final ServerSocketChannel serverSocketChannel;
- /**
- * The selector.
- */
- private final Selector selector;
- /**
- * The session.
- */
- private final Session session;
- /**
- * The thread the server listens on.
- */
- private final Thread thread;
- /**
- * Whether or not the server is running.
- */
- private volatile boolean running;
- /**
- * Creates a new server which will be bound to the specified host and port.
- * @param host The host the server is bound to.
- * @param port The port the server is bound to.
- * @param session The attachment of the server's selection keys.
- * @return The created server.
- * @throws IOException If an exception is thrown.
- */
- public static Server createServer(final String host, final int port, final Session session) throws IOException {
- return new Server(new InetSocketAddress(host, port), ServerSocketChannel.open(), Selector.open(), session);
- }
- /**
- * Constructs a new server with the specified inet socket address, server socket channel and selector.
- * @param inetSocketAddress The inet socket address.
- * @param serverSocketChannel The server socket channel.
- * @param selector The selector.
- * @param session The attachment of the server's selection keys.
- */
- private Server(final InetSocketAddress inetSocketAddress, final ServerSocketChannel serverSocketChannel, final Selector selector, final Session session) {
- this.inetSocketAddress = inetSocketAddress;
- this.serverSocketChannel = serverSocketChannel;
- this.selector = selector;
- this.thread = new Thread(this);
- this.session = session;
- }
- /**
- * Binds the server to the host and port the server was created with.
- * @return The server instance.
- * @throws IOException If an exception is thrown.
- */
- public Server bind() throws IOException {
- serverSocketChannel.configureBlocking(false);
- serverSocketChannel.socket().bind(inetSocketAddress);
- serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT, session);
- return this;
- }
- /**
- * Starts the server's listening thread.
- */
- public void start() {
- running = true;
- thread.start();
- }
- @Override
- public void run() {
- try {
- while (running) {
- selector.selectNow();
- for (SelectionKey selectionKey : selector.selectedKeys()) {
- if (selectionKey.isAcceptable()) {
- // register
- }
- if (selectionKey.isReadable()) {
- // read
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * Stops the server's listening thread.
- */
- public void stop() {
- running = false;
- }
- /**
- * Checks whether or not the server is currently running.
- * @return Whether or not the server is currently running.
- */
- public boolean isRunning() {
- return running;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement