File: SelectAndHandle.java

package info (click to toggle)
libglazedlists-java 1.9.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,012 kB
  • sloc: java: 22,561; xml: 940; makefile: 5
file content (91 lines) | stat: -rw-r--r-- 2,950 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* Glazed Lists                                                 (c) 2003-2006 */
/* http://publicobject.com/glazedlists/                      publicobject.com,*/
/*                                                     O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.impl.nio;

// NIO is used for CTP
import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The SelectAndHandle selects ready keys and handles them.
 */
class SelectAndHandle implements Runnable {
     
    /** logging */
    private static Logger logger = Logger.getLogger(SelectAndHandle.class.toString());

    /** the I/O event queue daemon */
    private NIODaemon nioDaemon = null;
    
    /**
     * Create a new SelectorHandler for the specified NIO Daemon.
     */
    public SelectAndHandle(NIODaemon nioDaemon) {
        this.nioDaemon = nioDaemon;
    }
    
    /**
     * Select and handle.
     */
    public void run() {
        select();
        handle();
    }
    
    /**
     * Selects keys which are ready to be processed.
     */
    void select() {
        // This may block for a long time. Upon returning, the
        // selected set contains keys of the ready channels
        try {
            nioDaemon.getSelector().select();
        } catch(IOException e) {
            logger.log(Level.WARNING, e.getMessage(), e);
        }
    }
    
    /**
     * Handles all keys which are ready to be processed.
     */
    void handle() {
        // Iterate over the selected keys
        for(Iterator i = nioDaemon.getSelector().selectedKeys().iterator(); i.hasNext(); ) {
            SelectionKey key = (SelectionKey)i.next();
            i.remove();
            
            // Is a new connection coming in?
            if(key.isValid() && key.isAcceptable()) {
                nioDaemon.getServer().handleAccept(key, nioDaemon.getSelector());
            }
            
            // an outgoing connection has been established
            if(key.isValid() && key.isConnectable()) {
                NIOAttachment attachment = (NIOAttachment)key.attachment();
                attachment.handleConnect();
            }

            // incoming data can be read
            if(key.isValid() && key.isReadable()) {
                NIOAttachment attachment = (NIOAttachment)key.attachment();
                attachment.handleRead();
            }
            
            // outgoing data can be written
            if(key.isValid() && key.isWritable()) {
                NIOAttachment attachment = (NIOAttachment)key.attachment();
                attachment.handleWrite();
            }

            // clean up broken connections
            if(!key.isValid()) {
                NIOAttachment attachment = (NIOAttachment)key.attachment();
                attachment.close(new IOException("Connection closed"));
            }
        }
    }
}