001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.connection;
030:
031: import com.caucho.util.L10N;
032: import com.caucho.vfs.ReadStream;
033: import com.caucho.vfs.WriteStream;
034:
035: import java.net.InetAddress;
036:
037: /**
038: * Represents a protocol-independent connection. Prococol servers and
039: * their associated Requests use Connection to retrieve the read and
040: * write streams and to get information about the connection.
041: *
042: * <p>TcpConnection is the most common implementation. The test harness
043: * provides a string based Connection.
044: */
045: public abstract class Connection {
046: private static final L10N L = new L10N(Connection.class);
047:
048: private final ReadStream _readStream;
049: private final WriteStream _writeStream;
050:
051: protected ConnectionController _controller;
052:
053: public Connection() {
054: _readStream = new ReadStream();
055: _readStream.setReuseBuffer(true);
056: _writeStream = new WriteStream();
057: _writeStream.setReuseBuffer(true);
058: }
059:
060: /**
061: * Returns the connection id. Primarily for debugging.
062: */
063: abstract public int getId();
064:
065: /**
066: * Returns the connection's buffered read stream. If the ReadStream
067: * needs to block, it will automatically flush the corresponding
068: * WriteStream.
069: */
070: public final ReadStream getReadStream() {
071: return _readStream;
072: }
073:
074: /**
075: * Returns the connection's buffered write stream. If the ReadStream
076: * needs to block, it will automatically flush the corresponding
077: * WriteStream.
078: */
079: public final WriteStream getWriteStream() {
080: return _writeStream;
081: }
082:
083: /**
084: * Returns true if secure (ssl)
085: */
086: public boolean isSecure() {
087: return false;
088: }
089:
090: /**
091: * Returns the static virtual host
092: */
093: public String getVirtualHost() {
094: return null;
095: }
096:
097: /**
098: * Returns the local address of the connection
099: */
100: public abstract InetAddress getLocalAddress();
101:
102: /**
103: * Returns the local port of the connection
104: */
105: public abstract int getLocalPort();
106:
107: /**
108: * Returns the remote address of the connection
109: */
110: public abstract InetAddress getRemoteAddress();
111:
112: /**
113: * Returns the remote client's inet address.
114: */
115: public String getRemoteHost() {
116: return getRemoteAddress().getHostAddress();
117: }
118:
119: /**
120: * Returns the remote address of the connection
121: */
122: public int getRemoteAddress(byte[] buffer, int offset, int length) {
123: InetAddress remote = getRemoteAddress();
124: String name = remote.getHostAddress();
125: int len = name.length();
126:
127: for (int i = 0; i < len; i++)
128: buffer[offset + i] = (byte) name.charAt(i);
129:
130: return len;
131: }
132:
133: /**
134: * Returns the remove port of the connection
135: */
136: public abstract int getRemotePort();
137:
138: /**
139: * Sends a broadcast request.
140: */
141: public void sendBroadcast(BroadcastTask task) {
142: }
143:
144: /**
145: * Connection controller.
146: */
147: void setController(ConnectionController controller) {
148: synchronized (this ) {
149: if (_controller != null)
150: throw new IllegalStateException(L
151: .l("ConnectionController is already set."));
152:
153: _controller = controller;
154: }
155: }
156:
157: /**
158: * Connection controller.
159: */
160: public ConnectionController getController() {
161: return _controller;
162: }
163:
164: /**
165: * Wake the controller.
166: */
167: protected boolean wake() {
168: return false;
169: }
170:
171: /**
172: * Connection controller.
173: */
174: void closeController(ConnectionController controller) {
175: synchronized (this ) {
176: if (_controller == controller)
177: _controller = null;
178: else
179: controller = null;
180: }
181:
182: if (controller != null)
183: closeControllerImpl();
184: }
185:
186: protected void closeControllerImpl() {
187: }
188: }
|