/*
Copyright (c) 2013, Colorado State University
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright holder or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
*/

package galileo.net;

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * Handles message routing on a {@link java.nio.channels.ServerSocketChannel}.
 * This class is useful for components that must accept incoming requests from
 * clients.
 *
 * @author malensek
 */
public class ServerMessageRouter extends MessageRouter {

    private int port;
    private ServerSocketChannel serverChannel;

    public ServerMessageRouter(int port) {
        this.port = port;
    }

    public ServerMessageRouter(int port,
            int readBufferSize, int maxWriteQueueSize) {
        super(readBufferSize, maxWriteQueueSize);
        this.port = port;
    }

    private synchronized void initializeSelector()
    throws IOException {
        this.selector = Selector.open();
    }

    /**
     * Initializes the server socket channel for incoming client connections and
     * begins listening for messages.
     */
    @Deprecated
    public void listen()
    throws IOException {
        initializeSelector();
        serverChannel = ServerSocketChannel.open();
        serverChannel.configureBlocking(false);
        serverChannel.socket().bind(new InetSocketAddress(this.port));
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);
        this.online = true;

        Thread selectorThread = new Thread(this);
        selectorThread.start();
    }

    /**
     * Initializes the server socket channel for incoming client connections and
     * begins listening for messages.
     */
    public void listen(int port)
    throws IOException {
        //TODO: should be able to listen on multiple ports with this method
    }

    /**
     * Closes the server socket channel and stops processing incoming
     * messages.
     */
    public void shutdown() throws IOException {
        serverChannel.close();
        this.online = false;
        selector.wakeup();
    }
}

/*
Copyright (c) 2013, Colorado State University
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright holder or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
*/

package galileo.net;

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;

/**
 * Handles message routing on a {@link java.nio.channels.ServerSocketChannel}.
 * This class is useful for components that must accept incoming requests from
 * clients.
 *
 * @author malensek
 */
public class ServerMessageRouter extends MessageRouter {

    private int port;
    private ServerSocketChannel serverChannel;

    public ServerMessageRouter(int port) {
        this.port = port;
    }

    public ServerMessageRouter(int port,
            int readBufferSize, int maxWriteQueueSize) {
        super(readBufferSize, maxWriteQueueSize);
        this.port = port;
    }

    private synchronized void initializeSelector()
    throws IOException {
        if (this.selector == null) {
            this.selector = Selector.open();
        }
    }

    /**
     * Initializes the server socket channel for incoming client connections and
     * begins listening for messages.
     */
    @Deprecated
    public void listen()
    throws IOException {
        initializeSelector();
        serverChannel = ServerSocketChannel.open();
        serverChannel.configureBlocking(false);
        serverChannel.socket().bind(new InetSocketAddress(this.port));
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);
        this.online = true;

        Thread selectorThread = new Thread(this);
        selectorThread.start();
    }

    /**
     * Initializes the server socket channel for incoming client connections and
     * begins listening for messages.
     */
    public void listen(int port)
    throws IOException {
        //TODO: should be able to listen on multiple ports with this method
    }

    /**
     * Closes the server socket channel and stops processing incoming
     * messages.
     */
    public void shutdown() throws IOException {
        serverChannel.close();
        this.online = false;
        selector.wakeup();
    }
}

Check for null selector before creating