// Path: core/src/main/java/com/threerings/nexus/util/Log.java
// public static Logger log = new JavaLogger("nexus");


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import static com.threerings.nexus.util.Log.log;
//
// Nexus JVMServer - server-side support for Nexus java.nio-based services
// http://github.com/threerings/nexus/blob/master/LICENSE

package com.threerings.nexus.server;





/**
 * Handles listening for Nexus connections and reading and writing over the sockets. Interacts with
 * a {@link SessionManager} to source and sink messages.
 */
public class JVMConnectionManager
{
    /**
     * Creates a connection manager that will listen for connections on the specified host/port
     * combination and establish sessions with the supplied session manager.
     */
    public JVMConnectionManager (SessionManager smgr) throws IOException {
        _smgr = smgr;
        _selector = Selector.open();
    }

    /**
     * Binds a listening socket on the specified host and port.
     * @param bindHost the address on which to listen, or null to listen on 0.0.0.0.
     * @param bindPort the port on which to listen.
     * @throws IOException if a failure occurs binding the socket.
     */
    public void listen (String bindHost, int bindPort) throws IOException {
        final ServerSocketChannel ssocket = ServerSocketChannel.open();
        ssocket.configureBlocking(false);
        InetSocketAddress addr = Strings.isNullOrEmpty(bindHost) ?
            new InetSocketAddress(bindPort) : new InetSocketAddress(bindHost, bindPort);
        ssocket.socket().bind(addr);

        SelectionKey key = ssocket.register(_selector, SelectionKey.OP_ACCEPT);
        key.attach(new IOHandler() {
            public void handleIO () {
                handleAccept(ssocket);
            }
        });
        _ssocks.add(ssocket);

// Path: core/src/main/java/com/threerings/nexus/util/Log.java
// public static Logger log = new JavaLogger("nexus");


// Path: jvm-server/src/main/java/com/threerings/nexus/server/JVMConnectionManager.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import static com.threerings.nexus.util.Log.log;

//
// Nexus JVMServer - server-side support for Nexus java.nio-based services
// http://github.com/threerings/nexus/blob/master/LICENSE

package com.threerings.nexus.server;





/**
 * Handles listening for Nexus connections and reading and writing over the sockets. Interacts with
 * a {@link SessionManager} to source and sink messages.
 */
public class JVMConnectionManager
{
    /**
     * Creates a connection manager that will listen for connections on the specified host/port
     * combination and establish sessions with the supplied session manager.
     */
    public JVMConnectionManager (SessionManager smgr) throws IOException {
        _smgr = smgr;
        _selector = Selector.open();
    }

    /**
     * Binds a listening socket on the specified host and port.
     * @param bindHost the address on which to listen, or null to listen on 0.0.0.0.
     * @param bindPort the port on which to listen.
     * @throws IOException if a failure occurs binding the socket.
     */
    public void listen (String bindHost, int bindPort) throws IOException {
        final ServerSocketChannel ssocket = ServerSocketChannel.open();
        ssocket.configureBlocking(false);
        InetSocketAddress addr = Strings.isNullOrEmpty(bindHost) ?
            new InetSocketAddress(bindPort) : new InetSocketAddress(bindHost, bindPort);
        ssocket.socket().bind(addr);

        SelectionKey key = ssocket.register(_selector, SelectionKey.OP_ACCEPT);
        key.attach(new IOHandler() {
            public void handleIO () {
                handleAccept(ssocket);
            }
        });
        _ssocks.add(ssocket);

        log.info("Server listening on " + addr);