port.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,800 行 · 第 1/3 页

JAVA
1,800
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.server.port;import com.caucho.config.ConfigException;import com.caucho.config.program.*;import com.caucho.config.types.*;import com.caucho.lifecycle.Lifecycle;import com.caucho.loader.Environment;import com.caucho.loader.EnvironmentBean;import com.caucho.loader.EnvironmentClassLoader;import com.caucho.loader.EnvironmentListener;import com.caucho.management.server.PortMXBean;import com.caucho.management.server.TcpConnectionInfo;import com.caucho.server.cluster.ClusterServer;import com.caucho.server.cluster.Server;import com.caucho.util.*;import com.caucho.vfs.JsseSSLFactory;import com.caucho.vfs.QJniServerSocket;import com.caucho.vfs.QServerSocket;import com.caucho.vfs.QSocket;import com.caucho.vfs.SSLFactory;import com.caucho.webbeans.manager.*;import javax.annotation.PostConstruct;import java.net.ConnectException;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.Socket;import java.net.UnknownHostException;import java.util.logging.Level;import java.util.logging.Logger;import java.util.ArrayList;/** * Represents a protocol connection. */public class Port  implements EnvironmentListener, Runnable{  private static final L10N L = new L10N(Port.class);  private static final Logger log    = Logger.getLogger(Port.class.getName());  private static final int DEFAULT = -0xcafe;  // started at 128, but that seems wasteful since the active threads  // themselves are buffering the free connections  private FreeList<TcpConnection> _freeConn    = new FreeList<TcpConnection>(32);  // The owning server  private ProtocolDispatchServer _server;  // The id  private String _serverId = "";  // The address  private String _address;  // The port  private int _port;  // URL for debugging  private String _url;  // The protocol  private Protocol _protocol;  // The SSL factory, if any  private SSLFactory _sslFactory;    // Secure override for load-balancers/proxies  private boolean _isSecure;  private InetAddress _socketAddress;  private int _acceptThreadMin = DEFAULT;  private int _acceptThreadMax = DEFAULT;  private int _acceptListenBacklog = DEFAULT;  private int _connectionMax = DEFAULT;  private int _keepaliveMax = DEFAULT;    private long _keepaliveTimeMax = DEFAULT;  private long _keepaliveTimeout = DEFAULT;  private long _keepaliveSelectThreadTimeout = DEFAULT;  private int _minSpareConnection = 16;    // default timeout  private long _socketTimeout = DEFAULT;  private long _suspendTimeMax = DEFAULT;  private boolean _tcpNoDelay = true;  // The virtual host name  private String _virtualHost;  private final PortAdmin _admin = new PortAdmin(this);  // the server socket  private QServerSocket _serverSocket;  // the throttle  private Throttle _throttle;  // the selection manager  private AbstractSelectManager _selectManager;  // active list of all connections  private ArrayList<TcpConnection> _activeList    = new ArrayList<TcpConnection>();  // server push (comet) suspend list  private ArrayList<TcpConnection> _suspendList    = new ArrayList<TcpConnection>();  private Alarm _suspendAlarm;  // statistics  private volatile int _threadCount;  private final Object _threadCountLock = new Object();  private volatile int _idleThreadCount;  private volatile int _startThreadCount;  private volatile int _connectionCount;  private volatile long _lifetimeRequestCount;  private volatile long _lifetimeKeepaliveCount;  private volatile long _lifetimeClientDisconnectCount;  private volatile long _lifetimeRequestTime;  private volatile long _lifetimeReadBytes;  private volatile long _lifetimeWriteBytes;  // total keepalive  private volatile int _keepaliveCount;  // thread-based  private volatile int _keepaliveThreadCount;  private final Object _keepaliveCountLock = new Object();  // True if the port has been bound  private volatile boolean _isBind;  private volatile boolean _isPostBind;  // The port lifecycle  private final Lifecycle _lifecycle = new Lifecycle();  public Port()  {  }  public Port(ClusterServer server)  {  }  /**   * Sets the containing server.   */  public void setParent(ProtocolDispatchServer parent)  {    setServer(parent);  }  /**   * Sets the server.   */  public void setServer(ProtocolDispatchServer protocolServer)  {    _server = protocolServer;    if (_protocol != null)      _protocol.setServer(protocolServer);    if (protocolServer instanceof Server) {      Server server = (Server) protocolServer;      if (_acceptThreadMax == DEFAULT)	_acceptThreadMax = server.getAcceptThreadMax();      if (_acceptThreadMin == DEFAULT)	_acceptThreadMin = server.getAcceptThreadMin();      if (_acceptListenBacklog == DEFAULT)	_acceptListenBacklog = server.getAcceptListenBacklog();      if (_connectionMax == DEFAULT)	_connectionMax = server.getConnectionMax();      if (_keepaliveMax == DEFAULT)	_keepaliveMax = server.getKeepaliveMax();      if (_keepaliveTimeMax == DEFAULT)	_keepaliveTimeMax = server.getKeepaliveConnectionTimeMax();      if (_keepaliveTimeout == DEFAULT)	_keepaliveTimeout = server.getKeepaliveTimeout();      if (_keepaliveSelectThreadTimeout == DEFAULT) {	_keepaliveSelectThreadTimeout	  = server.getKeepaliveSelectThreadTimeout();      }      if (_suspendTimeMax == DEFAULT)	_suspendTimeMax = server.getSuspendTimeMax();      if (_socketTimeout == DEFAULT)	_socketTimeout = server.getSocketTimeout();    }  }  /**   * Gets the server.   */  public ProtocolDispatchServer getServer()  {    return _server;  }  /**   * Sets the id.   */  public void setId(String id)  {    _serverId = id;  }  /**   * Sets the server id.   */  public void setServerId(String id)  {    _serverId = id;  }  /**   * Gets the server id.   */  public String getServerId()  {    return _serverId;  }  public void setType(Class cl)  {    setClass(cl);  }  public void setClass(Class cl)  {  }  public PortMXBean getAdmin()  {    return _admin;  }    /**   * Set protocol.   */  public void setProtocol(Protocol protocol)    throws ConfigException  {    /* server/0170    if (_server == null)      throw new IllegalStateException(L.l("Server is not set."));    */    _protocol = protocol;    _protocol.setServer(_server);  }  /**   * Set protocol.   */  public Protocol getProtocol()  {    return _protocol;  }  /**   * Gets the protocol name.   */  public String getProtocolName()  {    if (_protocol != null)      return _protocol.getProtocolName();    else      return null;  }  /**   * Sets the address   */  public void setAddress(String address)    throws UnknownHostException  {    if ("*".equals(address))      address = null;    _address = address;    if (address != null)      _socketAddress = InetAddress.getByName(address);  }  /**   * @deprecated   */  public void setHost(String address)    throws UnknownHostException  {    setAddress(address);  }  /**   * Gets the IP address   */  public String getAddress()  {    return _address;  }  /**   * Sets the port.   */  public void setPort(int port)  {    _port = port;  }  /**   * Gets the port.   */  public int getPort()  {    return _port;  }  /**   * Sets the virtual host for IP-based virtual host.   */  public void setVirtualHost(String host)  {    _virtualHost = host;  }  /**   * Gets the virtual host for IP-based virtual host.   */  public String getVirtualHost()  {    return _virtualHost;  }  /**   * Sets the SSL factory   */  public void setSSL(SSLFactory factory)  {    _sslFactory = factory;  }  /**   * Sets the SSL factory   */  public SSLFactory createOpenssl()    throws ConfigException  {    try {      ClassLoader loader = Thread.currentThread().getContextClassLoader();            Class cl = Class.forName("com.caucho.vfs.OpenSSLFactory", false, loader);      _sslFactory = (SSLFactory) cl.newInstance();      return _sslFactory;    } catch (Throwable e) {      log.log(Level.FINER, e.toString(), e);      throw new ConfigException(L.l("<openssl> requires Resin Professional.  See http://www.caucho.com for more information."));    }  }  /**   * Sets the SSL factory   */  public JsseSSLFactory createJsse()  {    // should probably check that openssl exists    return new JsseSSLFactory();  }  /**   * Sets the SSL factory   */  public void setJsseSsl(JsseSSLFactory factory)  {    _sslFactory = factory;  }  /**   * Gets the SSL factory.   */  public SSLFactory getSSL()  {    return _sslFactory;  }  /**   * Returns true for ssl.   */  public boolean isSSL()  {    return _sslFactory != null;  }  /**   * Sets true for secure   */  public void setSecure(boolean isSecure)  {    _isSecure = isSecure;  }  /**   * Return true for secure   */  public boolean isSecure()  {    return _isSecure || _sslFactory != null;  }  /**   * Sets the server socket.   */  public void setServerSocket(QServerSocket socket)  {    _serverSocket = socket;  }  //  // Configuration/Tuning  //  /**   * Sets the minimum spare listen.   */  public void setAcceptThreadMin(int minSpare)    throws ConfigException  {    if (minSpare < 1)      throw new ConfigException(L.l("accept-thread-min must be at least 1."));    _acceptThreadMin = minSpare;  }  /**   * The minimum spare threads.   */  public int getAcceptThreadMin()  {    return _acceptThreadMin;  }  /**   * Sets the minimum spare listen.   */  public void setAcceptThreadMax(int maxSpare)    throws ConfigException  {    if (maxSpare < 1)      throw new ConfigException(L.l("accept-thread-max must be at least 1."));    _acceptThreadMax = maxSpare;  }  /**   * The maximum spare threads.   */  public int getAcceptThreadMax()  {    return _acceptThreadMax;  }  /**   * Sets the operating system listen backlog   */  public void setAcceptListenBacklog(int listen)    throws ConfigException  {    if (listen < 1)      throw new ConfigException(L.l("accept-listen-backlog must be at least 1."));    _acceptListenBacklog = listen;  }  /**   * The operating system listen backlog   */  public int getAcceptListenBacklog()  {    return _acceptListenBacklog;  }  /**   * Sets the connection max.   */  public void setConnectionMax(int max)  {    _connectionMax = max;  }  /**   * Gets the connection max.   */  public int getConnectionMax()  {    return _connectionMax;  }  /**   * Returns true for ignore-client-disconnect.   */  public boolean isIgnoreClientDisconnect()  {    return _server.isIgnoreClientDisconnect();  }  /**   * Sets the read/write timeout for the accepted sockets.   */  public void setSocketTimeout(Period period)  {    _socketTimeout = period.getPeriod();  }  /**   * Sets the read timeout for the accepted sockets.   *   * @deprecated   */  public void setReadTimeout(Period period)  {    setSocketTimeout(period);  }  /**   * Gets the read timeout for the accepted sockets.   */  public long getSocketTimeout()  {    return _socketTimeout;  }  /**   * Gets the tcp-no-delay property   */  public boolean getTcpNoDelay()  {    return _tcpNoDelay;  }  /**   * Sets the tcp-no-delay property   */  public void setTcpNoDelay(boolean tcpNoDelay)  {    _tcpNoDelay = tcpNoDelay;  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?