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 java.io.*;
029: import java.net.*;
030:
031: /**
032: * An ATcpClientSocket implements an asynchronous outgoing socket connection.
033: * Applications create an ATcpClientSocket and associate a SinkIF with it.
034: * When the connection is established an ATcpConnection object will be pushed
035: * to the given SinkIF. The ATcpConnection is then used for actual
036: * communication.
037: *
038: * <p> A Sandstorm stage would use this as follows:
039: * <pre>
040: * SinkIF mySink = config.getSink(); // 'config' passed to stage init()
041: * ATcpClientSocket sock = new ATcpClientSocket(addr, port, mySink);
042: * </pre>
043: *
044: * @author Matt Welsh
045: * @see ATcpConnection
046: */
047: public class ATcpClientSocket {
048:
049: private InetAddress address;
050: private int port;
051:
052: /**
053: * Create a socket connecting to the given address and port.
054: * An ATcpConnection will be posted to the given SinkIF when the
055: * connection is established. If an error occurs, an
056: * ATcpConnectFailedEvent will be posted instead.
057: */
058: public ATcpClientSocket(InetAddress addr, int port, SinkIF compQ) {
059: this (addr, port, compQ, -1, -1);
060: }
061:
062: /**
063: * Create a socket connecting to the given host and port.
064: * An ATcpConnection will be posted to the given compQ when the
065: * connection is established. If an error occurs, an
066: * ATcpConnectFailedEvent will be posted instead.
067: */
068: public ATcpClientSocket(String host, int port, SinkIF compQ)
069: throws UnknownHostException {
070: this (InetAddress.getByName(host), port, compQ, -1, -1);
071: }
072:
073: /**
074: * Create a socket connecting to the given address and port with the
075: * given writeClogThreshold value.
076: * An ATcpConnection will be posted to the given compQ when the
077: * connection is established. If an error occurs, an
078: * ATcpConnectFailedEvent will be posted instead.
079: *
080: * @param writeClogThreshold The maximum number of outstanding writes
081: * on this socket before a SinkCloggedEvent is pushed to the
082: * connection's completion queue. This is effectively the maximum depth
083: * threshold for this connection's SinkIF. The default value is -1, which
084: * indicates that no SinkCloggedEvents will be generated.
085: *
086: * @param connectClogTries The number of times the aSocket layer will
087: * attempt to push a new entry onto the given SinkIF while the
088: * SinkIF is full. The queue entry will be dropped after this many
089: * tries. The default value is -1, which indicates that the aSocket
090: * layer will attempt to push the queue entry indefinitely.
091: *
092: */
093: public ATcpClientSocket(InetAddress addr, int port, SinkIF compQ,
094: int writeClogThreshold, int connectClogTries) {
095: this .address = addr;
096: this .port = port;
097: aSocketMgr.enqueueRequest(new ATcpConnectRequest(this , addr,
098: port, compQ, writeClogThreshold, connectClogTries));
099: }
100:
101: /**
102: * Create a socket connecting to the given host and port with the
103: * given writeClogThreshold value.
104: * An ATcpConnection will be posted to the given compQ when the
105: * connection is established. If an error occurs, an
106: * ATcpConnectFailedEvent will be posted instead.
107: *
108: * @param writeClogThreshold The maximum number of outstanding writes
109: * on this socket before a SinkCloggedEvent is pushed to the
110: * connection's completion queue. This is effectively the maximum depth
111: * threshold for this connection's SinkIF. The default value is -1, which
112: * indicates that no SinkCloggedEvents will be generated.
113: *
114: * @param connectClogTries The number of times the aSocket layer will
115: * attempt to push a new entry onto the given SinkIF while the
116: * SinkIF is full. The queue entry will be dropped after this many
117: * tries. The default value is -1, which indicates that the aSocket
118: * layer will attempt to push the queue entry indefinitely.
119: *
120: */
121: public ATcpClientSocket(String host, int port, SinkIF compQ,
122: int writeClogThreshold, int connectClogTries)
123: throws UnknownHostException {
124: this (InetAddress.getByName(host), port, compQ,
125: writeClogThreshold, connectClogTries);
126: }
127:
128: protected ATcpClientSocket() {
129: }
130:
131: /**
132: * Return the InetAddress which this socket is connected to.
133: */
134: public InetAddress getAddress() {
135: return address;
136: }
137:
138: /**
139: * Return the port which this socket is connected to.
140: */
141: public int getPort() {
142: return port;
143: }
144:
145: // public String toString() {
146: // return "ATcpClientSocket ["+address+":"+port+"]";
147: // }
148: }
|