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: *
023: * Free SoftwareFoundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.vfs;
031:
032: import com.caucho.log.Log;
033:
034: import java.io.IOException;
035: import java.net.InetAddress;
036: import java.net.ServerSocket;
037: import java.net.Socket;
038: import java.nio.channels.Selector;
039: import java.nio.channels.ServerSocketChannel;
040: import java.nio.channels.spi.SelectorProvider;
041: import java.util.logging.Level;
042: import java.util.logging.Logger;
043:
044: /**
045: * Abstract socket to handle both normal sockets and bin/resin sockets.
046: */
047: public class QServerSocketWrapper extends QServerSocket {
048: private static final Logger log = Log
049: .open(QServerSocketWrapper.class);
050:
051: private ServerSocket _ss;
052: private boolean _tcpNoDelay = true;
053: private int _connectionSocketTimeout = 65000;
054:
055: public QServerSocketWrapper() {
056: }
057:
058: public QServerSocketWrapper(ServerSocket ss) {
059: init(ss);
060: }
061:
062: public void init(ServerSocket ss) {
063: _ss = ss;
064: }
065:
066: public void setTcpNoDelay(boolean delay) {
067: _tcpNoDelay = delay;
068: }
069:
070: public boolean getTcpNoDelay() {
071: return _tcpNoDelay;
072: }
073:
074: public void setConnectionSocketTimeout(int socketTimeout) {
075: _connectionSocketTimeout = socketTimeout;
076: }
077:
078: /**
079: * Accepts a new socket.
080: */
081: public boolean accept(QSocket qSocket) throws IOException {
082: QSocketWrapper s = (QSocketWrapper) qSocket;
083:
084: Socket socket = _ss.accept();
085:
086: if (socket == null)
087: return false;
088:
089: // XXX:
090: if (_tcpNoDelay)
091: socket.setTcpNoDelay(true);
092:
093: if (_connectionSocketTimeout > 0)
094: socket.setSoTimeout(_connectionSocketTimeout);
095:
096: s.init(socket);
097:
098: return true;
099: }
100:
101: /**
102: * Creates a new socket object.
103: */
104: public QSocket createSocket() throws IOException {
105: return new QSocketWrapper();
106: }
107:
108: public InetAddress getLocalAddress() {
109: return _ss.getInetAddress();
110: }
111:
112: public int getLocalPort() {
113: return _ss.getLocalPort();
114: }
115:
116: public Selector getSelector() {
117: try {
118: ServerSocketChannel channel = _ss.getChannel();
119:
120: if (channel == null)
121: return null;
122:
123: SelectorProvider provider = channel.provider();
124:
125: if (provider != null)
126: return provider.openSelector();
127: else
128: return null;
129: } catch (Throwable e) {
130: log.log(Level.WARNING, e.toString(), e);
131: return null;
132: }
133: }
134:
135: /**
136: * Closes the underlying socket.
137: */
138: public void close() throws IOException {
139: ServerSocket ss = _ss;
140: _ss = ss;
141:
142: if (ss != null) {
143: try {
144: ss.close();
145: } catch (Exception e) {
146: }
147: }
148: }
149:
150: public String toString() {
151: return "ServerSocketWrapper[" + getLocalAddress() + ":"
152: + getLocalPort() + "]";
153: }
154: }
|