Java 2 SDK for Solaris Developer's Guide

Poller Class Usage

Poller Class

The Poller class demonstration code provides a means of accessing the functionality of the C poll(2) API. It attempts to mirror the C poll(2) API only as much as is possible while allowing for optimal performance. To remove the impact of shuttling large arrays of I/O objects between Java and the kernel, management of the files/sockets to be polled has been moved into the JNI C code (or the /dev/poll device driver itself, if available). The README.txt file included in the Poller class demonstration directory gives more detailed information on using it.

Basics of Poller Class Usage


    Poller Mux = new Poller();

    int serverFd = Mux.add(serverSocket, Poller.POLLIN);    
    int fd1 = Mux.add(socket1, Poller.POLLIN);
    ...
    int fdN = Mux.add(socketN, Poller.POLLIN);
    long timeout = 1000; // one second

    int numEvents = Mux.waitMultiple(100, fds, revents, timeout);

    for (int i = 0; i < numEvents; i++) {
       /*
        * Probably need more sophisticated mapping scheme than this!
        */
        if (fds[i] == serverFd) {
            System.out.println("Got new connection.");
            newSocket = serverSocket.accept();
            newSocketFd = Mux.add(newSocket,Poller.POLLIN);
        } else if (fds[i] == fd1) {
            System.out.println("Got data on socket1");
            socket1.getInputStream().read(byteArray);
            // Do something based upon state of fd1 connection
        }
        ...
    }