001: /*
002: * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: /*
027: * @(#)PollArrayWrapper.java 1.20 07/05/05
028: */
029:
030: package sun.nio.ch;
031:
032: /**
033: * Manipulates a native array of structs corresponding to (fd, events) pairs.
034: *
035: * typedef struct pollfd {
036: * SOCKET fd; // 4 bytes
037: * short events; // 2 bytes
038: * } pollfd_t;
039: *
040: * @author Konstantin Kladko
041: * @author Mike McCloskey
042: * @version 1.20, 07/05/05
043: */
044:
045: class PollArrayWrapper {
046:
047: private AllocatedNativeObject pollArray; // The fd array
048:
049: long pollArrayAddress; // pollArrayAddress
050:
051: private static final short FD_OFFSET = 0; // fd offset in pollfd
052: private static final short EVENT_OFFSET = 4; // events offset in pollfd
053:
054: static short SIZE_POLLFD = 8; // sizeof pollfd struct
055:
056: // events masks
057: static final short POLLIN = AbstractPollArrayWrapper.POLLIN;
058: static final short POLLOUT = AbstractPollArrayWrapper.POLLOUT;
059: static final short POLLERR = AbstractPollArrayWrapper.POLLERR;
060: static final short POLLHUP = AbstractPollArrayWrapper.POLLHUP;
061: static final short POLLNVAL = AbstractPollArrayWrapper.POLLNVAL;
062: static final short POLLREMOVE = AbstractPollArrayWrapper.POLLREMOVE;
063: static final short POLLCONN = 0x0002;
064:
065: private int size; // Size of the pollArray
066:
067: PollArrayWrapper(int newSize) {
068: int allocationSize = newSize * SIZE_POLLFD;
069: pollArray = new AllocatedNativeObject(allocationSize, true);
070: pollArrayAddress = pollArray.address();
071: this .size = newSize;
072: }
073:
074: // Prepare another pollfd struct for use.
075: void addEntry(int index, SelectionKeyImpl ski) {
076: putDescriptor(index, ski.channel.getFDVal());
077: }
078:
079: // Writes the pollfd entry from the source wrapper at the source index
080: // over the entry in the target wrapper at the target index.
081: void replaceEntry(PollArrayWrapper source, int sindex,
082: PollArrayWrapper target, int tindex) {
083: target.putDescriptor(tindex, source.getDescriptor(sindex));
084: target.putEventOps(tindex, source.getEventOps(sindex));
085: }
086:
087: // Grows the pollfd array to new size
088: void grow(int newSize) {
089: PollArrayWrapper temp = new PollArrayWrapper(newSize);
090: for (int i = 0; i < size; i++)
091: replaceEntry(this , i, temp, i);
092: pollArray.free();
093: pollArray = temp.pollArray;
094: this .size = temp.size;
095: pollArrayAddress = pollArray.address();
096: }
097:
098: void free() {
099: pollArray.free();
100: }
101:
102: // Access methods for fd structures
103: void putDescriptor(int i, int fd) {
104: pollArray.putInt(SIZE_POLLFD * i + FD_OFFSET, fd);
105: }
106:
107: void putEventOps(int i, int event) {
108: pollArray.putShort(SIZE_POLLFD * i + EVENT_OFFSET,
109: (short) event);
110: }
111:
112: int getEventOps(int i) {
113: return pollArray.getShort(SIZE_POLLFD * i + EVENT_OFFSET);
114: }
115:
116: int getDescriptor(int i) {
117: return pollArray.getInt(SIZE_POLLFD * i + FD_OFFSET);
118: }
119:
120: // Adds Windows wakeup socket at a given index.
121: void addWakeupSocket(int fdVal, int index) {
122: putDescriptor(index, fdVal);
123: putEventOps(index, POLLIN);
124: }
125: }
|