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.sandStorm.lib.aSocket;
026:
027: import seda.sandStorm.api.*;
028: import seda.sandStorm.core.*;
029:
030: import java.net.*;
031: import java.io.*;
032: import java.util.*;
033:
034: /**
035: * Internal event handler for socket listen events.
036: */
037: class ListenEventHandler extends aSocketEventHandler implements
038: EventHandlerIF {
039:
040: private static final boolean DEBUG = false;
041:
042: ListenEventHandler() {
043: }
044:
045: public void init(ConfigDataIF config) {
046: }
047:
048: public void destroy() {
049: }
050:
051: private void processAccept(ListenSockState lss) throws IOException {
052: if (DEBUG)
053: System.err
054: .println("ListenEventHandler: processAccept called");
055:
056: Socket sock;
057:
058: int numAccepted = 0;
059:
060: // Try to do as many accepts as we can in one go
061: while (numAccepted++ < aSocketConst.MAX_ACCEPTS_AT_ONCE) {
062: // XXX: must check this.
063: sock = lss.accept();
064:
065: if (sock == null)
066: break;
067:
068: if (DEBUG)
069: System.err.println("ListenThread: did accept on "
070: + sock.getInetAddress().getHostAddress() + ":"
071: + sock.getPort());
072: ATcpConnection conn = new ATcpConnection(lss.servsock, sock
073: .getInetAddress(), sock.getPort());
074: if (DEBUG)
075: System.err.println("ListenThread: Created new conn "
076: + conn);
077: SockState ss = aSocketMgr.getFactory().newSockState(conn,
078: sock, lss.writeClogThreshold);
079: if (DEBUG)
080: System.err
081: .println("ListenThread: Created new sockstate "
082: + ss);
083: conn.sockState = ss;
084: if (DEBUG)
085: System.err
086: .println("ListenThread: Calling lss complete");
087: lss.complete(conn);
088: }
089:
090: if (DEBUG)
091: System.err
092: .println("ListenEventHandler: processAccept finished");
093: }
094:
095: private void processListenRequest(aSocketRequest req)
096: throws IOException {
097:
098: if (req instanceof ATcpListenRequest) {
099: // This registers itself
100: ListenSockState lss;
101: lss = aSocketMgr.getFactory().newListenSockState(
102: (ATcpListenRequest) req, selsource);
103:
104: } else if (req instanceof ATcpSuspendAcceptRequest) {
105: ATcpSuspendAcceptRequest susreq = (ATcpSuspendAcceptRequest) req;
106:
107: ListenSockState lss = susreq.servsock.lss;
108: if (lss == null) {
109: throw new Error(
110: "ListenEventHandler: Got ATcpSuspendAcceptRequest for server socket "
111: + susreq.servsock + " with null lss!");
112: }
113: lss.suspend();
114:
115: } else if (req instanceof ATcpResumeAcceptRequest) {
116: ATcpResumeAcceptRequest resreq = (ATcpResumeAcceptRequest) req;
117:
118: ListenSockState lss = resreq.servsock.lss;
119: if (lss == null) {
120: throw new Error(
121: "ListenEventHandler: Got ATcpResumeAcceptRequest for server socket "
122: + resreq.servsock + " with null lss!");
123: }
124: lss.resume();
125:
126: } else if (req instanceof ATcpCloseServerRequest) {
127: ATcpCloseServerRequest creq = (ATcpCloseServerRequest) req;
128:
129: ListenSockState lss = creq.servsock.lss;
130: // OK for lss to be null if closed down already
131: if (lss != null)
132: lss.close();
133:
134: } else {
135: throw new IllegalArgumentException(
136: "Bad request type to enqueueListen");
137: }
138: }
139:
140: public void handleEvent(QueueElementIF qel) {
141: if (DEBUG)
142: System.err.println("ListenEventHandler: Got QEL: " + qel);
143:
144: try {
145:
146: if (qel instanceof SelectQueueElement) {
147: ListenSockState lss;
148: lss = (ListenSockState) ((SelectQueueElement) qel)
149: .getAttachment();
150: ((SelectQueueElement) qel).clearEvents();
151:
152: processAccept(lss);
153:
154: } else if (qel instanceof aSocketRequest) {
155: processListenRequest((aSocketRequest) qel);
156:
157: } else {
158: throw new IllegalArgumentException(
159: "ReadEventHandler: Got unknown event type "
160: + qel);
161: }
162:
163: } catch (Exception e) {
164: System.err.println("ListenEventHandler: Got exception: "
165: + e);
166: e.printStackTrace();
167: }
168: }
169:
170: public void handleEvents(QueueElementIF qelarr[]) {
171: for (int i = 0; i < qelarr.length; i++) {
172: handleEvent(qelarr[i]);
173: }
174: }
175:
176: }
|