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 class used to represent state of an active socket connection.
036: */
037: public abstract class SockState implements aSocketConst {
038:
039: private static final boolean DEBUG = false;
040:
041: protected Socket nbsock;
042: protected ATcpConnection conn;
043: protected SinkIF readCompQ;
044: protected QueueElementIF clogged_qel;
045: protected int clogged_numtries;
046: protected int readClogTries, writeClogThreshold;
047: protected byte readBuf[];
048: protected boolean closed = false;
049: protected long seqNum = 1;
050:
051: protected int outstanding_writes, numEmptyWrites;
052: protected ssLinkedList writeReqList;
053: protected ATcpWriteRequest cur_write_req;
054: protected int cur_offset, cur_length_target;
055: protected byte writeBuf[];
056: protected ATcpInPacket pkt;
057:
058: protected static int numActiveWriteSockets = 0;
059:
060: // This is synchronized with close()
061: protected abstract void readInit(SelectSourceIF read_selsource,
062: SinkIF compQ, int readClogTries);
063:
064: protected abstract void doRead();
065:
066: // XXX This is synchronized with close() to avoid a race with close()
067: // removing the writeReqList while this method is being called.
068: // Probably a better way to do this...
069: protected abstract boolean addWriteRequest(aSocketRequest req,
070: SelectSourceIF write_selsource);
071:
072: protected abstract void initWrite(ATcpWriteRequest req);
073:
074: protected abstract boolean tryWrite() throws SinkClosedException;
075:
076: void writeReset() {
077: this .cur_write_req = null;
078: this .outstanding_writes--;
079: }
080:
081: protected abstract void writeMaskEnable();
082:
083: protected abstract void writeMaskDisable();
084:
085: static int numActiveWriters() {
086: return numActiveWriteSockets;
087: }
088:
089: boolean isClosed() {
090: return closed;
091: }
092:
093: // XXX This is synchronized to avoid close() interfering with
094: // addWriteRequest
095: protected abstract void close(SinkIF closeEventQueue);
096:
097: public String toString() {
098: return "SockState [" + nbsock + "]";
099: }
100:
101: }
|