/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Accept ( and send ) TCP messages.
*
* @author Costin Manolache
* @author Bill Barker
* jmx:mbean name="jk:service=ChannelNioSocket"
* description="Accept socket connections"
* jmx:notification name="com.sun.grizzly.tcp.INVOKE
* jmx:notification-handler name="org.apache.jk.JK_SEND_PACKET
* jmx:notification-handler name="org.apache.jk.JK_RECEIVE_PACKET
* jmx:notification-handler name="org.apache.jk.JK_FLUSH
*
* Jk can use multiple protocols/transports.
* Various container adapters should load this object ( as a bean ),
* set configurations and use it. Note that the connector will handle
* all incoming protocols - it's not specific to ajp1x. The protocol
* is abstracted by MsgContext/Message/Channel.
*
* A lot of the 'original' behavior is hardcoded - this uses Ajp13 wire protocol,
* TCP, Ajp14 API etc.
* As we add other protocols/transports/APIs this will change, the current goal
* is to get the same level of functionality as in the original jk connector.
*
* XXX Make the 'message type' pluggable
*/
implements NotificationBroadcaster, JkChannel {
private boolean nioIsBroken = false;
/* Turning this to true will reduce the latency with about 20%.
But it requires changes in tomcat to make sure client-requested
flush() is honored ( on my test, I got 367->433 RPS and
52->35ms average time with a simple servlet )
*/
/* ==================== Tcp socket options ==================== */
/**
* jmx:managed-constructor description="default constructor"
*/
public ChannelNioSocket() {
// This should be integrated with the domain setup
}
return tp;
}
public long getRequestCount() {
return requestCount;
}
/** Set the port for the ajp13 channel.
* To support seemless load balancing and jni, we treat this
* as the 'base' port - we'll try up until we find one that is not
* used. We'll also provide the 'difference' to the main coyote
* handler - that will be our 'sessionID' and the position in
* the scoreboard and the suffix for the unix domain socket.
*
* jmx:managed-attribute description="Port to listen" access="READ_WRITE"
*/
}
public int getPort() {
return port;
}
}
bufferSize = bs;
}
}
public int getBufferSize() {
return bufferSize;
}
}
packetSize = ps;
}
public int getPacketSize() {
return packetSize;
}
/**
* jmx:managed-attribute description="Bind on a specified address" access="READ_WRITE"
*/
try {
}
}
return "/0.0.0.0";
}
/**
* Sets the timeout in ms of the server sockets created by this
* server. This method allows the developer to make servers
* more or less responsive to having their server sockets
* shut down.
*
* <p>By default this value is 1000ms.
*/
this.serverTimeout = timeout;
}
public int getServerTimeout() {
return serverTimeout;
}
public void setTcpNoDelay( boolean b ) {
tcpNoDelay=b;
}
public boolean getTcpNoDelay() {
return tcpNoDelay;
}
public void setSoLinger( int i ) {
linger=i;
}
public int getSoLinger() {
return linger;
}
public void setSoTimeout( int i ) {
}
public int getSoTimeout() {
return socketTimeout;
}
public void setMaxPort( int i ) {
maxPort=i;
}
public int getMaxPort() {
return maxPort;
}
/** At startup we'll look for the first free port in the range.
The difference between this port and the beggining of the range
is the 'id'.
This is usefull for lb cases ( less config ).
*/
public int getInstanceId() {
}
/** If set to false, the thread pool will be created in
* non-daemon mode, and will prevent main from exiting
*/
public void setDaemon( boolean b ) {
}
public boolean getDaemon() {
}
public void setMaxThreads( int i ) {
}
tp.setMaxThreads(i);
}
public void setMinSpareThreads( int i ) {
}
tp.setMinSpareThreads(i);
}
public void setMaxSpareThreads( int i ) {
}
tp.setMaxSpareThreads(i);
}
public int getMaxThreads() {
return tp.getMaxThreads();
}
public int getMinSpareThreads() {
return tp.getMinSpareThreads();
}
public int getMaxSpareThreads() {
return tp.getMaxSpareThreads();
}
public void setBacklog(int i) {
}
nioIsBroken = nib;
}
public boolean getNioIsBroken() {
return nioIsBroken;
}
/* ==================== ==================== */
boolean paused = false;
synchronized(this) {
paused = true;
}
}
public void resume() {
synchronized(this) {
paused = false;
notify();
}
}
synchronized(this) {
while(paused) {
try{
wait();
} catch(InterruptedException ie) {
//Ignore, since can't happen
}
}
}
try {
setSocketOptions(s);
} catch(SocketException sex) {
}
requestCount++;
sc.configureBlocking(false);
}
if( socketTimeout > 0 )
s.setSoTimeout( socketTimeout );
if( linger > 0 )
s.setSoLinger( true, linger);
}
public void resetCounters() {
requestCount=0;
}
/** Called after you change some fields at runtime using jmx.
Experimental for now.
*/
destroy();
init();
}
/**
* jmx:managed-operation
*/
// Find a port.
if (startPort == 0) {
port = 0;
running = true;
return;
}
ssc.configureBlocking(false);
try {
iddr = new InetSocketAddress( i);
} else {
}
port=i;
break;
} catch( IOException ex ) {
}
}
return;
}
// If this is not the base port and we are the 'main' channleSocket and
// SHM didn't already set the localId - we'll set the instance id
}
// XXX Reverse it -> this is a notification generator !!
}
running = true;
// Run a thread that will accept connections.
// XXX Try to find a thread first - not sure how...
try {
getChannelName());
rgOName = new ObjectName
} catch (Exception e) {
}
}
}
int JMXRequestNote;
init();
resume();
}
destroy();
}
try {
(getDomain() + ":type=RequestProcessor,worker="+
}
}
}
}
}
s.close();
}
running = false;
try {
/* If we disabled the channel return */
if (port == 0)
return;
}
}
} catch(Exception e) {
e.toString());
}
}
throws IOException {
return len;
}
throws IOException {
return 0;
}
throws IOException {
}
// XXX If the length in the packet header doesn't agree with the
// actual number of bytes read, it should probably return an error
// value. Also, callers of this method never use the length
if(rd < 0) {
// Most likely normal apache restart.
// log.warn("Wrong message " + rd );
return rd;
}
msg.processHeader();
/* After processing the header we know the body
length
*/
// XXX check if enough space - it's assert()-ed !!!
int total_read = 0;
return -1;
}
if (total_read != blen) {
" got only " + total_read);
return -2;
}
return total_read;
}
/**
* Read N bytes from the InputStream, and ensure we got them all
* Under heavy load we could experience many fragmented packets
* just read Unix Network Programming to recall that a call to
* read didn't ensure you got all the data you want
*
* from read() Linux manual
*
* On success, the number of bytes read is returned (zero indicates end
* of file),and the file position is advanced by this number.
* It is not an error if this number is smaller than the number of bytes
* requested; this may happen for example because fewer bytes
* are actually available right now (maybe because we were close to
* end-of-file, or because we are reading from a pipe, or from a
* terminal), or because read() was interrupted by a signal.
* On error, -1 is returned, and errno is set appropriately. In this
* case it is left unspecified whether the file position (if any) changes.
*
**/
throws IOException
{
int pos = 0;
int got;
try {
} catch(ClosedChannelException sex) {
if(pos > 0) {
} else {
}
got = -1;
}
}
// connection just closed by remote.
if (got <= 0) {
// This happens periodically, as apache restarts
// periodically.
// It should be more gracefull ! - another feature for Ajp14
// log.warn( "server has closed the current connection (-1)" );
return -3;
}
}
return pos;
}
protected boolean running=true;
/** Accept incoming connections, dispatch to the thread pool
*/
void acceptConnections() {
if( running ) {
try{
if( !running ) return;
// Since this is a long-running connection, we don't care
// about the small GC
new SocketConnection( ep);
if (running)
}
}
}
// XXX This should become handleNotification
switch( type ) {
case JkHandler.HANDLE_SEND_PACKET:
case JkHandler.HANDLE_FLUSH:
}
// Send notification
}
}
} else {
}
return OK;
}
}
encodedAddr = getAddress();
}
}
/**
* Return <code>true</code> if the specified client and server addresses
* are the same. This method works around a bug in the IBM 1.1.8 JVM on
* Linux, where the address bytes are returned reversed in some
* circumstances.
*
* @param server The server's InetAddress
* @param client The client's InetAddress
*/
{
// Compare the byte array versions of the two addresses
return (false);
boolean match = true;
if (serverAddr[i] != clientAddr[i]) {
match = false;
break;
}
}
if (match)
return (true);
// Compare the reversed form of the two addresses
return (false);
}
return (true);
}
}
throws IllegalArgumentException
{
}
throws ListenerNotFoundException
{
}
}
return notifInfo;
}
boolean inProgress = false;
}
return null;
}
if(!processConnection(ep)) {
unregister(ep);
}
}
public boolean isRunning() {
return inProgress;
}
public void setFinished() {
inProgress = false;
}
/** Process a single ajp connection.
*/
try {
boolean haveInput = true;
while(haveInput) {
return false;
}
if( status <= 0 ) {
if( status==-3)
else
return false;
}
// Will call next
return false;
}
synchronized(this) {
synchronized(sis) {
}
if(!haveInput) {
setFinished();
} else {
}
}
}
else
return false;
}
return true;
}
return;
}
if(sk.isReadable()) {
if(!inProgress) {
if(isok) {
inProgress = true;
}
} else {
unregister(ep);
return;
}
}
}
if(sk.isWritable()) {
synchronized(os) {
}
}
}
try{
} catch(Exception e) {
}
try{
}
}
}
}
try {
} catch(IOException iex) {
unregister(ep);
}
}
}
Poller() {
}
return null;
}
while(running) {
try {
if(ns > 0) {
if(sk.isAcceptable()) {
} else {
}
}
}
} catch(ClosedSelectorException cse) {
return;
} catch(CancelledKeyException cke) {
} catch(IOException iex) {
}
}
}
}
private boolean blocking = false;
private boolean isClosed = false;
private volatile boolean dataAvailable = false;
}
public int available() {
}
}
public boolean markSupported() {
return true;
}
public void reset() {
}
if(!checkAvailable(1)) {
block(1);
}
}
if(isClosed) {
throw new ClosedChannelException();
}
}
int read = 0;
boolean eof = false;
// should rarely happen, so short-lived GC shouldn't hurt
// as much as allocating a long-lived buffer for this
}
}
while(rem > 0) {
if(count < 0) {
eof = true;
break;
} else if(count == 0) {
break;
}
}
}
synchronized boolean readAvailable() {
if(blocking) {
dataAvailable = true;
notify();
} else if(dataAvailable) {
} else {
int nr=0;
try {
} catch(ClosedChannelException cce) {
nr = -1;
} catch(IOException iex) {
}
if(nr < 0) {
closeIt();
return false;
} else if(nr == 0) {
if(!nioIsBroken) {
}
}
}
return true;
}
synchronized void closeIt() {
isClosed = true;
if(blocking)
notify();
}
}
while(!checkAvailable(len)) {
if(avail > 0) {
}
}
return olen;
}
if(len <= 0) {
return;
}
if(!dataAvailable) {
blocking = true;
try{
}catch(InterruptedException iex) {
}
blocking = false;
}
if(dataAvailable) {
dataAvailable = false;
isClosed = true;
}
} else if(!isClosed) {
throw new SocketTimeoutException("Read request timed out");
}
}
}
}
if(!checkAvailable(1)) {
flush();
}
}
}
if(!checkAvailable(len)) {
flush();
}
}
while(buffer.hasRemaining()) {
if(count == 0) {
synchronized(this) {
try {
wait();
} catch(InterruptedException iex) {
// ignore, since can't happen
}
}
}
}
}
}
}
}