001: /*
002: * Copyright (c) 2000 by Matt Welsh and The Regents of the University of
003: * California. All rights reserved.
004: *
005: * Permission to use, copy, modify, and distribute this software and its
006: * documentation for any purpose, without fee, and without written agreement is
007: * hereby granted, provided that the above copyright notice and the following
008: * two paragraphs appear in all copies of this software.
009: *
010: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
011: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
012: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
013: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014: *
015: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
016: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
017: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
018: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
019: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
020: *
021: * Author: Matt Welsh <mdw@cs.berkeley.edu>
022: *
023: */
024:
025: package seda.nbio;
026:
027: /**
028: * A SelectItem represents a single socket/file descriptor/etc. which
029: * can be handled by a SelectSet. Each SelectItem has an associated
030: * Selectable as well as two event masks: 'events' and 'revents'.
031: * Setting 'events' allows you to specify which events you are
032: * interested in receiving notification on for this Selectable.
033: * After calling SelectSet.select(), 'revents' will be set to the
034: * set of events that occurred.
035: */
036: public class SelectItem {
037: private Selectable sel; // Do not let user change this without updating fd
038:
039: /**
040: * The set of events that you are interested in receiving notification on.
041: * The event types are specified by the constants in Selectable.
042: *
043: * <p><b>Important:</b> If you change the events field of a SelectItem
044: * after registering it with a SelectSet (using SelectSet.add()),
045: * you must invoke SelectSet.update() to push the new event mask to the
046: * SelectSet.
047: *
048: * @see Selectable
049: * @see SelectSet
050: */
051: public short events;
052:
053: /**
054: * The set of events that occurred.
055: * The event types are specified by the constants in Selectable.
056: * @see Selectable
057: */
058: public short revents;
059:
060: /**
061: * A state object associated with this SelectItem. You can use this
062: * for any purpose you like.
063: */
064: public Object obj;
065:
066: private NBIOFileDescriptor fd;
067:
068: private void updatefd() {
069: // We want to keep a clone of the fd here, since if the socket
070: // closes, we want to remember the fd for deregistering with SelectSet.
071: if (sel instanceof NonblockingSocket) {
072: fd = ((NonblockingSocket) sel).impl.getFileDescriptor()
073: .getClone();
074: } else if (sel instanceof NonblockingServerSocket) {
075: fd = ((NonblockingServerSocket) sel).impl
076: .getFileDescriptor().getClone();
077: } else if (sel instanceof NonblockingDatagramSocket) {
078: fd = ((NonblockingDatagramSocket) sel).impl
079: .getFileDescriptor().getClone();
080: } else {
081: throw new IllegalArgumentException(
082: "Selectable must support internal NBIOFileDescriptor");
083: }
084: }
085:
086: // So SelectSet can get fd
087: NBIOFileDescriptor getFD() {
088: return fd;
089: }
090:
091: /**
092: * Create a SelectItem with the given Selectable, given state pointer,
093: * and the given event mask.
094: */
095: public SelectItem(Selectable sel, Object obj, short events) {
096: this .sel = sel;
097: this .obj = obj;
098: this .events = events;
099: this .revents = (short) 0;
100: updatefd();
101: }
102:
103: /**
104: * Create a SelectItem with the given Selectable and the given event
105: * mask.
106: */
107: public SelectItem(Selectable sel, short events) {
108: this (sel, null, events);
109: }
110:
111: /**
112: * Return the Selectable associated with this SelectItem.
113: */
114: public Selectable getSelectable() {
115: return sel;
116: }
117:
118: /**
119: * Return the state pointer associated with this SelectItem.
120: */
121: public Object getObj() {
122: return obj;
123: }
124:
125: /**
126: * Return the requested events mask.
127: */
128: public short getEvents() {
129: return events;
130: }
131:
132: /**
133: * Return the returned events mask.
134: */
135: public short returnedEvents() {
136: return revents;
137: }
138:
139: public String toString() {
140: return "SelectItem [sel=" + sel + "] events=0x"
141: + Integer.toHexString(events) + " revents=0x"
142: + Integer.toHexString(revents);
143: }
144:
145: }
|