File: StartServer.java

package info (click to toggle)
libglazedlists-java 1.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,024 kB
  • ctags: 4,252
  • sloc: java: 22,561; xml: 818; sh: 51; makefile: 5
file content (62 lines) | stat: -rw-r--r-- 2,254 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* Glazed Lists                                                 (c) 2003-2006 */
/* http://publicobject.com/glazedlists/                      publicobject.com,*/
/*                                                     O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.impl.ctp;

// NIO is used for CTP
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.util.logging.Logger;

/**
 * A task that starts the CTP Connection manager.
 *
 * @author <a href="mailto:jesse@swank.ca">Jesse Wilson</a>
 */
class StartServer implements Runnable {
    
    /** logging */
    private static Logger logger = Logger.getLogger(StartServer.class.toString());

    /** the I/O event queue daemon */
    private CTPConnectionManager connectionManager = null;
    
    /** port to listen for incoming connections */
    private int listenPort = -1;
    
    /**
     * Create a new CTPStartUp that starts a server using the specified
     * NIODaemon.
     */
    public StartServer(CTPConnectionManager connectionManager, int listenPort) {
        this.connectionManager = connectionManager;
        this.listenPort = listenPort;
    }
    
    /**
     * Runs the specified task.
     */
    public void run() {
        try {
            // open a channel and bind
            ServerSocketChannel serverChannel = ServerSocketChannel.open();
            ServerSocket serverSocket = serverChannel.socket();
            serverSocket.setReuseAddress(false); // fix for Apple JVM bug 3922515
            InetSocketAddress listenAddress = new InetSocketAddress(listenPort);
            serverSocket.bind(listenAddress);
            
            // prepare for non-blocking, selectable IO
            serverChannel.configureBlocking(false);
            serverChannel.register(connectionManager.getNIODaemon().getSelector(), SelectionKey.OP_ACCEPT);
            connectionManager.getNIODaemon().setServer(connectionManager);
    
            // bind success
            logger.info("Connection Manager ready, listening on " + listenAddress);
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
    }
}