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 used to process socket read events.
036: */
037: class ReadEventHandler extends aSocketEventHandler implements
038: EventHandlerIF {
039:
040: private static final boolean DEBUG = false;
041:
042: ReadEventHandler() {
043: }
044:
045: public void init(ConfigDataIF config) {
046: }
047:
048: public void destroy() {
049: }
050:
051: private void processReadRequest(aSocketRequest req)
052: throws IOException {
053:
054: if (req instanceof ATcpStartReadRequest) {
055: ATcpStartReadRequest srreq = (ATcpStartReadRequest) req;
056: SockState ss = srreq.conn.sockState;
057: ss.readInit(selsource, srreq.compQ, srreq.readClogTries);
058:
059: } else if (req instanceof AUdpStartReadRequest) {
060: AUdpStartReadRequest srreq = (AUdpStartReadRequest) req;
061: DatagramSockState ss = srreq.sock.sockState;
062: ss.readInit(selsource, srreq.compQ, srreq.readClogTries);
063:
064: } else {
065: throw new IllegalArgumentException(
066: "Bad request type to enqueueRead");
067: }
068: }
069:
070: public void handleEvent(QueueElementIF qel) {
071: if (DEBUG)
072: System.err.println("ReadEventHandler: Got QEL: " + qel);
073:
074: try {
075: if (qel instanceof SelectQueueElement) {
076: Object attach = ((SelectQueueElement) qel)
077: .getAttachment();
078: if (attach instanceof SockState) {
079: SockState ss = (SockState) attach;
080: if (DEBUG)
081: System.err.println("ReadEventHandler: ss is "
082: + ss);
083: ss.doRead();
084: } else {
085: DatagramSockState ss = (DatagramSockState) attach;
086: if (DEBUG)
087: System.err.println("ReadEventHandler: ss is "
088: + ss);
089: ss.doRead();
090: }
091: if (DEBUG)
092: System.err
093: .println("ReadEventHandler: returned from doRead");
094:
095: } else if (qel instanceof aSocketRequest) {
096: processReadRequest((aSocketRequest) qel);
097:
098: } else {
099: throw new IllegalArgumentException(
100: "ReadEventHandler: Got unknown event type "
101: + qel);
102: }
103:
104: } catch (Exception e) {
105: System.err.println("ReadEventHandler: Got exception: " + e);
106: e.printStackTrace();
107: }
108: }
109:
110: public void handleEvents(QueueElementIF qelarr[]) {
111: for (int i = 0; i < qelarr.length; i++) {
112: handleEvent(qelarr[i]);
113: }
114: }
115:
116: }
|