pier 00/11/30 08:34:31
Added: catalina/src/share/org/apache/catalina/connector/warp
WarpConnection.java WarpConnectionHandler.java
WarpConstants.java WarpDebug.java WarpHandler.java
WarpHandlerTable.java WarpPacket.java
WarpReader.java
Log:
Warp connector primary checkin (more to come).
Revision Changes Path
1.1
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/warp/WarpConnection.java
Index: WarpConnection.java
===================================================================
/* ========================================================================= *
* *
* The Apache Software License, Version 1.1 *
* *
* Copyright (c) 1999, 2000 The Apache Software Foundation. *
* All rights reserved. *
* *
* ========================================================================= *
* *
* Redistribution and use in source and binary forms, with or without modi- *
* fication, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice *
* notice, this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. The end-user documentation included with the redistribution, if any, *
* must include the following acknowlegement: *
* *
* "This product includes software developed by the Apache Software *
* Foundation <http://www.apache.org/>." *
* *
* Alternately, this acknowlegement may appear in the software itself, if *
* and wherever such third-party acknowlegements normally appear. *
* *
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software *
* Foundation" must not be used to endorse or promote products derived *
* from this software without prior written permission. For written *
* permission, please contact <[EMAIL PROTECTED]>. *
* *
* 5. Products derived from this software may not be called "Apache" nor may *
* "Apache" appear in their names without prior written permission of the *
* Apache Software Foundation. *
* *
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY *
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, *
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
* ========================================================================= *
* *
* This software consists of voluntary contributions made by many indivi- *
* duals on behalf of the Apache Software Foundation. For more information *
* on the Apache Software Foundation, please see <http://www.apache.org/>. *
* *
* ========================================================================= */
package org.apache.catalina.connector.warp;
import java.io.*;
import java.net.*;
/**
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @author Copyright © 1999, 2000 <a href="http://www.apache.org">The
* Apache Software Foundation.
* @version CVS $Id: WarpConnection.java,v 1.1 2000/11/30 16:34:30 pier Exp $
*/
public class WarpConnection implements Runnable {
/** The DEBUG flag, to compile out debugging informations. */
private static final boolean DEBUG = WarpConstants.DEBUG;
private WarpHandlerTable table;
private Socket socket;
private String name;
public WarpConnection() {
super();
this.table=new WarpHandlerTable();
this.socket=null;
this.name=null;
}
/**
*
*/
public void run() {
WarpHandler han=null;
InputStream in=null;
int rid=0;
int typ=0;
int len=0;
int ret=0;
int b1=0;
int b2=0;
byte buf[]=null;
// Log the connection opening
this.log("Connection opened");
try {
// Open the socket InputStream
in=this.socket.getInputStream();
// Read packets
while(true) {
// RID number
b1=in.read();
b2=in.read();
if ((b1 | b2)==-1) {
this.log("Premature RID end");
break;
}
rid=(((b1 & 0x0ff) << 8) | (b2 & 0x0ff));
// Packet type
b1=in.read();
b2=in.read();
if ((b1 | b2)==-1) {
this.log("Premature TYPE end");
break;
}
typ=(((b1 & 0x0ff) << 8) | (b2 & 0x0ff));
// Packet payload length
b1=in.read();
b2=in.read();
if ((b1 | b2)==-1) {
this.log("Premature LENGTH end");
break;
}
len=(((b1 & 0x0ff) << 8) | (b2 & 0x0ff));
// Packet payload
buf=new byte[len];
if ((ret=in.read(buf,0,len))!=len) {
this.log("Premature PAYLOAD end ("+ret+" of "+len+")");
break;
}
// Check if we have the special RID 0x0ffff (disconnect)
if (rid==0x0ffff) {
this.log("Connection closing ("+new String(buf)+")");
break;
}
// Dispatch packet
synchronized (this) { han=this.table.get(rid); }
if (han==null) {
this.log("Handler for RID "+rid+" not found");
break;
}
han.processData(typ,buf);
}
} catch (IOException e) {
this.log(e);
}
// Close this connection before terminating the thread
this.close();
if (DEBUG) this.debug("Thread exited");
}
/**
* Initialize this connection.
*
* @param sock The socket used by this connection to transfer data.
*/
public void init(Socket sock) {
// Paranoia checks.
if (sock==null) throw new NullPointerException("Null Socket");
this.socket=sock;
// Register the WarpConnectionHandler for RID=0 (connection)
WarpHandler h=new WarpConnectionHandler();
h.init(this,0);
// Paranoia check
if(this.registerHandler(h,0)!=true) {
System.err.println("Something happened creating the connection");
this.close();
return;
}
// Set the thread and connection name and start the thread
this.name=sock.getInetAddress().getHostAddress()+":"+sock.getPort();
new Thread(this,name).start();
}
/**
* Send a WARP packet.
*/
public void send(int rid, int type, byte buffer[], int offset, int len)
throws IOException {
if (this.socket==null) throw new IOException("Connection closed "+type);
OutputStream out=this.socket.getOutputStream();
byte hdr[]=new byte[6];
// Set the RID number
hdr[0]=(byte)((rid>>8)&0x0ff);
hdr[1]=(byte)(rid&0x0ff);
// Set the TYPE
hdr[2]=(byte)((type>>8)&0x0ff);
hdr[3]=(byte)(type&0x0ff);
// Set the payload length
hdr[4]=(byte)((len>>8)&0x0ff);
hdr[5]=(byte)(len&0x0ff);
// Send the header and payload
synchronized(this) {
out.write(hdr,0,6);
out.write(buffer,offset,len);
out.flush();
}
}
/**
* Close this connection.
* <br>
* The socket associated with this connection is closed, all handlers are
* stopped and the thread reading from the socket is interrupted.
*/
public void close() {
// Stop all handlers
WarpHandler handlers[]=this.table.handlers();
for (int x=0; x<handlers.length; x++) handlers[x].stop();
// Close the socket (this will make the thread exit)
if (this.socket!=null) try {
this.socket.close();
} catch (IOException e) {
this.log(e);
}
this.socket=null;
// Log this step
this.log("Connection closed");
}
/**
* Add a WarpHandler to this connection.
*
* @param han The WarpHandler add to this connection.
* @param rid The RID number associated with the WarpHandler.
* @return If another WarpHandler is associated with this RID return
* false, otherwise return true.
*/
protected synchronized boolean registerHandler(WarpHandler han, int rid) {
return(this.table.add(han, rid));
}
/**
* Remove a WarpHandler from this connection.
*
* @param rid The RID number associated with the WarpHandler to remove.
* @return The old WarpHandler associated with the specified RID or null.
*/
protected synchronized WarpHandler removeHandler(int rid) {
return(this.table.remove(rid));
}
/**
* Log a message.
*
* @param msg The error message to log.
*/
public void log(String msg) {
System.out.println("[WarpConnection("+this.name+")] "+msg);
System.out.flush();
}
/**
* Log an exception.
*
* @param e The exception to log.
*/
public void log(Exception e) {
System.out.print("[WarpConnection("+this.name+")] ");
e.printStackTrace(System.out);
System.out.flush();
}
/**
* Dump some debugging information.
*
* @param msg The error message to log.
*/
public void debug(String msg) {
if(DEBUG) this.log(msg);
}
}
1.1
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/warp/WarpConnectionHandler.java
Index: WarpConnectionHandler.java
===================================================================
/* ========================================================================= *
* *
* The Apache Software License, Version 1.1 *
* *
* Copyright (c) 1999, 2000 The Apache Software Foundation. *
* All rights reserved. *
* *
* ========================================================================= *
* *
* Redistribution and use in source and binary forms, with or without modi- *
* fication, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice *
* notice, this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. The end-user documentation included with the redistribution, if any, *
* must include the following acknowlegement: *
* *
* "This product includes software developed by the Apache Software *
* Foundation <http://www.apache.org/>." *
* *
* Alternately, this acknowlegement may appear in the software itself, if *
* and wherever such third-party acknowlegements normally appear. *
* *
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software *
* Foundation" must not be used to endorse or promote products derived *
* from this software without prior written permission. For written *
* permission, please contact <[EMAIL PROTECTED]>. *
* *
* 5. Products derived from this software may not be called "Apache" nor may *
* "Apache" appear in their names without prior written permission of the *
* Apache Software Foundation. *
* *
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY *
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, *
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
* ========================================================================= *
* *
* This software consists of voluntary contributions made by many indivi- *
* duals on behalf of the Apache Software Foundation. For more information *
* on the Apache Software Foundation, please see <http://www.apache.org/>. *
* *
* ========================================================================= */
package org.apache.catalina.connector.warp;
import java.io.IOException;
/**
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @author Copyright © 1999, 2000 <a href="http://www.apache.org">The
* Apache Software Foundation.
* @version CVS $Id: WarpConnectionHandler.java,v 1.1 2000/11/30 16:34:30 pier Exp $
*/
public class WarpConnectionHandler extends WarpHandler {
/** The WarpReader associated with this WarpConnectionHandler. */
private WarpReader reader=new WarpReader();
/** The WarpPacket used to write data. */
private WarpPacket packet=new WarpPacket();
/**
* Process a WARP packet.
* <br>
* This method is the one which will actually perform the operation of
* analyzing the packet and doing whatever needs to be done.
* <br>
* This method will return true if another packet is expected for this
* RID, or it will return false if this was the last packet for this RID.
* When we return false this handler is unregistered, and the Thread
* started in the init() method is terminated.
*
* @param type The WARP packet type.
* @param buffer The WARP packet payload.
* @return If more packets are expected for this RID, true is returned,
* false if this was the last packet.
*/
public boolean process(int type, byte buffer[]) {
this.reader.reset(buffer);
this.packet.reset();
try {
switch (type) {
case WarpConstants.TYP_HOST:
if (DEBUG) this.debug("HOST "+reader.readString()+":"+
reader.readShort()+"="+123);
this.packet.writeShort(123);
this.send(WarpConstants.TYP_HOST_ID,this.packet);
break;
case WarpConstants.TYP_APPLICATION:
if (DEBUG)
this.debug("APPL
"+reader.readString()+":"+reader.readString()+"="+321);
this.packet.reset();
this.packet.writeShort(321);
this.send(WarpConstants.TYP_APPLICATION_ID,this.packet);
break;
default:
if (DEBUG)
this.debug("[Type="+type+"] ["+new String(buffer)+"]");
break;
}
return(true);
} catch (IOException e) {
this.log(e);
return(false);
}
}
}
1.1
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/warp/WarpConstants.java
Index: WarpConstants.java
===================================================================
/* ========================================================================= *
* *
* The Apache Software License, Version 1.1 *
* *
* Copyright (c) 1999, 2000 The Apache Software Foundation. *
* All rights reserved. *
* *
* ========================================================================= *
* *
* Redistribution and use in source and binary forms, with or without modi- *
* fication, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice *
* notice, this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. The end-user documentation included with the redistribution, if any, *
* must include the following acknowlegement: *
* *
* "This product includes software developed by the Apache Software *
* Foundation <http://www.apache.org/>." *
* *
* Alternately, this acknowlegement may appear in the software itself, if *
* and wherever such third-party acknowlegements normally appear. *
* *
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software *
* Foundation" must not be used to endorse or promote products derived *
* from this software without prior written permission. For written *
* permission, please contact <[EMAIL PROTECTED]>. *
* *
* 5. Products derived from this software may not be called "Apache" nor may *
* "Apache" appear in their names without prior written permission of the *
* Apache Software Foundation. *
* *
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY *
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, *
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
* ========================================================================= *
* *
* This software consists of voluntary contributions made by many indivi- *
* duals on behalf of the Apache Software Foundation. For more information *
* on the Apache Software Foundation, please see <http://www.apache.org/>. *
* *
* ========================================================================= */
package org.apache.catalina.connector.warp;
/**
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @author Copyright © 1999, 2000 <a href="http://www.apache.org">The
* Apache Software Foundation.
* @version CVS $Id: WarpConstants.java,v 1.1 2000/11/30 16:34:30 pier Exp $
*/
public class WarpConstants {
/* The DEBUG flag. (If false debug information will not be compiled in). */
public static final boolean DEBUG = true;
/* The RID associated with the connection controller handler (0x00000). */
public static final int RID_CONNECTION = 0x00000;
/* The RID indicating that the connection must be closed (0x0ffff). */
public static final int RID_DISCONNECT = 0x0ffff;
/* The RID minimum value (0x00001). */
public static final int RID_MIN = 0x00001;
/* The RID maximum value (0x0fffe). */
public static final int RID_MAX = 0x0fffe;
public static final int TYP_HOST = 0x00000;
public static final int TYP_HOST_ID = 0x00001;
public static final int TYP_APPLICATION = 0x00002;
public static final int TYP_APPLICATION_ID = 0x00003;
public static final int TYP_REQUEST = 0x00004;
public static final int TYP_REQUEST_ID = 0x00005;
}
1.1
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/warp/WarpDebug.java
Index: WarpDebug.java
===================================================================
/* ========================================================================= *
* *
* The Apache Software License, Version 1.1 *
* *
* Copyright (c) 1999, 2000 The Apache Software Foundation. *
* All rights reserved. *
* *
* ========================================================================= *
* *
* Redistribution and use in source and binary forms, with or without modi- *
* fication, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice *
* notice, this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. The end-user documentation included with the redistribution, if any, *
* must include the following acknowlegement: *
* *
* "This product includes software developed by the Apache Software *
* Foundation <http://www.apache.org/>." *
* *
* Alternately, this acknowlegement may appear in the software itself, if *
* and wherever such third-party acknowlegements normally appear. *
* *
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software *
* Foundation" must not be used to endorse or promote products derived *
* from this software without prior written permission. For written *
* permission, please contact <[EMAIL PROTECTED]>. *
* *
* 5. Products derived from this software may not be called "Apache" nor may *
* "Apache" appear in their names without prior written permission of the *
* Apache Software Foundation. *
* *
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY *
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, *
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
* ========================================================================= *
* *
* This software consists of voluntary contributions made by many indivi- *
* duals on behalf of the Apache Software Foundation. For more information *
* on the Apache Software Foundation, please see <http://www.apache.org/>. *
* *
* ========================================================================= */
package org.apache.catalina.connector.warp;
/**
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @author Copyright © 1999, 2000 <a href="http://www.apache.org">The
* Apache Software Foundation.
* @version CVS $Id: WarpDebug.java,v 1.1 2000/11/30 16:34:30 pier Exp $
*/
import java.io.*;
import java.net.*;
public class WarpDebug {
public static void main(String argv[]) {
try {
ServerSocket k=new ServerSocket(8008);
while (true) {
Socket s=k.accept();
new WarpConnection().init(s);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Terminated");
System.exit(0);
}
}
1.1
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/warp/WarpHandler.java
Index: WarpHandler.java
===================================================================
/* ========================================================================= *
* *
* The Apache Software License, Version 1.1 *
* *
* Copyright (c) 1999, 2000 The Apache Software Foundation. *
* All rights reserved. *
* *
* ========================================================================= *
* *
* Redistribution and use in source and binary forms, with or without modi- *
* fication, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice *
* notice, this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. The end-user documentation included with the redistribution, if any, *
* must include the following acknowlegement: *
* *
* "This product includes software developed by the Apache Software *
* Foundation <http://www.apache.org/>." *
* *
* Alternately, this acknowlegement may appear in the software itself, if *
* and wherever such third-party acknowlegements normally appear. *
* *
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software *
* Foundation" must not be used to endorse or promote products derived *
* from this software without prior written permission. For written *
* permission, please contact <[EMAIL PROTECTED]>. *
* *
* 5. Products derived from this software may not be called "Apache" nor may *
* "Apache" appear in their names without prior written permission of the *
* Apache Software Foundation. *
* *
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY *
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, *
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
* ========================================================================= *
* *
* This software consists of voluntary contributions made by many indivi- *
* duals on behalf of the Apache Software Foundation. For more information *
* on the Apache Software Foundation, please see <http://www.apache.org/>. *
* *
* ========================================================================= */
package org.apache.catalina.connector.warp;
import java.io.IOException;
/**
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @author Copyright © 1999, 2000 <a href="http://www.apache.org">The
* Apache Software Foundation.
* @version CVS $Id: WarpHandler.java,v 1.1 2000/11/30 16:34:31 pier Exp $
*/
public abstract class WarpHandler implements Runnable {
/**
* The DEBUG flag, used to compile out debugging informations.
* <br>
* Sub classes of WarpHandler can write statements like
* "if (DEBUG) this.debug(...);" to compile in or out all
* debugging information calls.
*/
protected static final boolean DEBUG = WarpConstants.DEBUG;
/** The WarpConnection under which this handler is registered. */
private WarpConnection connection;
/** The WARP RID associated with this WarpHandler. */
private int rid;
/** The WARP packet type. */
private int type;
/** The WARP packet payload. */
private byte buffer[];
/** Wether type and buffer have already been processed by notifyData(). */
private boolean processed;
/** Wether the stop() method was called. */
private boolean stopped;
/** The name of this handler. */
private String name;
/**
* Construct a new instance of a WarpHandler.
*/
public WarpHandler() {
super();
}
/**
* Initialize this handler instance.
* <br>
* This method will initialize this handler and starts the despooling
* thread that will handle the data to the processData() method.
* <br>
* NOTE: To receive data notification this WarpConnection must be
* registered in the WarpConnection. Only un-registration (when the thread
* exits) is performed.
*
* @param connection The WarpConnection associated with this handler.
* @param rid The WARP request ID associated with this handler.
*/
protected final void init(WarpConnection connection, int rid) {
this.connection=connection;
this.rid=rid;
this.processed=true;
this.stopped=false;
this.name=this.getClass().getName()+"[RID="+rid+"]";
new Thread(this,this.name).start();
}
/**
* Process WARP packets.
* <br>
* This method will wait for data to be notified by a WarpConnection with
* the processData() method, and will handle it to the abstract process()
* method.
* <br>
* When process() returns false (meaning that there is no more
* data to process) this method will unregister this handler from the
* WarpConnection, and the thread created in init() will terminate.
*/
public final synchronized void run() {
while(true) {
try {
// Wait for this thread to be notified by processData().
while (this.processed==true) {
// Check if we were notified by stop()
this.wait();
if (this.stopped) {
this.processed=true;
this.notifyAll();
this.connection.removeHandler(this.rid);
if (DEBUG) this.debug("Thread exited");
}
}
} catch (InterruptedException e) {
// Uh-oh.. Something bad happened. Drop this handler.
this.connection.removeHandler(this.rid);
return;
}
// If processed is set to true, it means that somehow we were
// not notified by the processData() method. So we just ignore
// and go back to sleep.
if (this.processed) continue;
// The processed flag is set to false, so we need to handle the
// type and buffer to the process() method. If this method
// returns true, we have some more packets to process before
// terminating this method.
if (process(this.type, this.buffer)) {
// The process method returned true, meaning that we still have
// some other packets to wait for this WARP RID. Set the
// processed flag to true and notify the thread that might be
// waiting in the processData() method, then get back to sleep.
this.processed=true;
this.notifyAll();
continue;
} else {
// The process() method returned false, meaning that our work
// is done. Let's get out.
break;
}
}
// We got out of the loop (the process() method returned false). Let's
// notify all other threads (just in case after a packet is processed
// another one for the same handler has been received), unregister
// this handler and terminate.
this.processed=true;
this.notifyAll();
this.connection.removeHandler(this.rid);
if (DEBUG) this.debug("Thread exited");
}
/**
* Stop this handler.
* <br>
* The thread associated with this handler is stopped and this instance is
* unregistered from the WarpConnection.
*/
protected final synchronized void stop() {
// Set the stopped flag and notify all threads
if (DEBUG) this.debug("Stopping");
this.stopped=true;
this.notifyAll();
if (DEBUG) this.debug("Stopped");
}
/**
* Receive notification of data from a WarpConnection.
*
* @param type The WARP packet type.
* @param buffer The WARP packet payload.
*/
protected final synchronized void processData(int type, byte buffer[]) {
// Check if the run() method already handled the previous packet to
// the process() method. If not so, wait until that happens.
while(this.processed==false) try {
this.wait();
} catch (InterruptedException e) {
return;
}
// We were notified and the processed flag is set to true, meanging
// that the previous packet was successfully despooled by the run()
// method, so we can safely set the type and buffer in our local
// variables.
this.type=type;
this.buffer=buffer;
// Set the processed flag to false, and notify the thread waiting in
// the run() method that another packet is ready to process.
this.processed=false;
this.notifyAll();
}
/**
* Send a WARP packet.
* <br>
* This method will send a WARP packet back to the client, invoking
* the send() method in the WarpConnection associated with this
* WarpHandler with the appropriate WARP RID.
*
* @param type The WARP packet type.
* @param buffer The buffer containing the data to send.
* @param off The offset in the buffer of the data to send.
* @param len The number of bytes to send.
* @exception IOException If something happened sending the data.
*/
public final void send(int type, byte buffer[], int offset, int len)
throws IOException {
this.connection.send(this.rid, type, buffer, offset, len);
}
/**
* Send a WARP packet.
* <br>
* This method will send a WARP packet back to the client, invoking
* the send() method in the WarpConnection associated with this
* WarpHandler with the appropriate WARP RID.
*
* @param type The WARP packet type.
* @param packet The packet to write.
* @exception IOException If something happened sending the data.
*/
public final void send(int type, WarpPacket packet)
throws IOException {
this.send(type,packet.getBuffer(),0,packet.getLength());
}
/**
* Send a WARP packet.
* <br>
* This method is equivalent to send(type, buffer, 0, buffer.length).
*
* @param type The WARP packet type.
* @param buffer The buffer containing the data to send.
* @exception IOException If something happened sending the data.
*/
public final void send(int type, byte buffer[])
throws IOException {
this.send(type, buffer, 0, buffer.length);
}
/**
* Process a WARP packet.
* <br>
* This method is the one which will actually perform the operation of
* analyzing the packet and doing whatever needs to be done.
* <br>
* This method will return true if another packet is expected for this
* RID, or it will return false if this was the last packet for this RID.
* When we return false this handler is unregistered, and the Thread
* started in the init() method is terminated.
*
* @param type The WARP packet type.
* @param buffer The WARP packet payload.
* @return If more packets are expected for this RID, true is returned,
* false if this was the last packet.
*/
public abstract boolean process(int type, byte buffer[]);
/**
* Return the WarpConnection associated with this handler.
*
* @return The WarpConnection associated with this handler or null if the
* init() method was never called.
*/
protected WarpConnection getConnection() {
return(this.connection);
}
/**
* Log a message.
*
* @param msg The error message to log.
*/
public void log(String msg) {
this.getConnection().log("{"+this.name+"} "+msg);
}
/**
* Log an exception.
*
* @param e The exception to log.
*/
public void log(Exception e) {
this.log("Caught exception");
this.getConnection().log(e);
}
/**
* Dump some debugging information.
*
* @param msg The error message to log.
*/
public void debug(String msg) {
this.getConnection().debug("{"+this.name+"} "+msg);
}
}
1.1
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/warp/WarpHandlerTable.java
Index: WarpHandlerTable.java
===================================================================
/* ========================================================================= *
* *
* The Apache Software License, Version 1.1 *
* *
* Copyright (c) 1999, 2000 The Apache Software Foundation. *
* All rights reserved. *
* *
* ========================================================================= *
* *
* Redistribution and use in source and binary forms, with or without modi- *
* fication, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice *
* notice, this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. The end-user documentation included with the redistribution, if any, *
* must include the following acknowlegement: *
* *
* "This product includes software developed by the Apache Software *
* Foundation <http://www.apache.org/>." *
* *
* Alternately, this acknowlegement may appear in the software itself, if *
* and wherever such third-party acknowlegements normally appear. *
* *
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software *
* Foundation" must not be used to endorse or promote products derived *
* from this software without prior written permission. For written *
* permission, please contact <[EMAIL PROTECTED]>. *
* *
* 5. Products derived from this software may not be called "Apache" nor may *
* "Apache" appear in their names without prior written permission of the *
* Apache Software Foundation. *
* *
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY *
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, *
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
* ========================================================================= *
* *
* This software consists of voluntary contributions made by many indivi- *
* duals on behalf of the Apache Software Foundation. For more information *
* on the Apache Software Foundation, please see <http://www.apache.org/>. *
* *
* ========================================================================= */
package org.apache.catalina.connector.warp;
/**
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @author Copyright © 1999, 2000 <a href="http://www.apache.org">The
* Apache Software Foundation.
* @version CVS $Id: WarpHandlerTable.java,v 1.1 2000/11/30 16:34:31 pier Exp $
*/
public class WarpHandlerTable {
/** The default size of our tables. */
private static int defaultsize=32;
/** The current size of our arrays. */
private int size;
/** The number of elements present in our arrays. */
private int num;
/** The array of handlers. */
private WarpHandler handlers[];
/** The array of rids. */
private int rids[];
/**
* Construct a new WarpHandlerTable instance with the default size.
*/
public WarpHandlerTable() {
this(defaultsize);
}
/**
* Construct a new WarpHandlerTable instance with a specified size.
*
* @param size The initial size of the table.
*/
public WarpHandlerTable(int size) {
super();
// Paranoid, it's pointless to build a smaller than minimum size.
if (size<defaultsize) size=defaultsize;
// Set the current and default sizes (in Hashtable terms the load
// factor is always 1.0)
this.defaultsize=size;
this.size=size;
// Set the initial values.
this.num=0;
this.rids=new int[size];
this.handlers=new WarpHandler[size];
}
/**
* Get the WarpHandler associated with a specific RID.
*
* @param rid The RID number.
* @return The WarpHandler or null if the RID was not associated with the
* specified RID.
*/
public WarpHandler get(int rid) {
// Iterate thru the array of rids
for (int x=0; x<this.num; x++) {
// We got our rid, return the handler at the same position
if (this.rids[x]==rid) return(this.handlers[x]);
}
// Not found.
return(null);
}
/**
* Associate a WarpHandler with a specified RID.
*
* @param handler The WarpHandler to put in the table.
* @param rid The RID number associated with the WarpHandler.
* @return If another WarpHandler is associated with this RID return
* false, otherwise return true.
*/
public boolean add(WarpHandler handler, int rid)
throws NullPointerException {
// Check if we were given a valid handler
if (handler==null) throw new NullPointerException("Null Handler");
// Check if another handler was registered for the specified rid.
if (this.get(rid)!=null) return(false);
// Check if we reached the capacity limit
if(this.size==this.num) {
// Allocate some space for the new arrays
int newsize=this.size+defaultsize;
WarpHandler newhandlers[]=new WarpHandler[newsize];
int newrids[]=new int[newsize];
// Copy the original arrays into the new arrays
System.arraycopy(this.handlers,0,newhandlers,0,this.num);
System.arraycopy(this.rids,0,newrids,0,this.num);
// Update our variables
this.size=newsize;
this.handlers=newhandlers;
this.rids=newrids;
}
// Add the handler and its rid to the arrays
this.handlers[this.num]=handler;
this.rids[this.num]=rid;
this.num++;
// Whohoo!
return(true);
}
/**
* Remove the WarpHandler associated with a specified RID.
*
* @param rid The RID number of the WarpHandler to remove.
* @return The old WarpHandler associated with the specified RID or null.
*/
public WarpHandler remove(int rid) {
// Iterate thru the array of rids
for (int x=0; x<this.num; x++) {
// We got our rid, and we need to get "rid" of its handler :)
if (this.rids[x]==rid) {
WarpHandler oldhandler=this.handlers[x];
// Decrease the number of handlers stored in this table
this.num--;
// Move the last record in our arrays in the current position
// (dirty way to compact a table)
this.handlers[x]=this.handlers[this.num];
this.rids[x]=this.rids[this.num];
// Now we want to see if we need to shrink our arrays (we don't
// want them to grow indefinitely in case of peak data storage)
// We check the number of available positions against the value
// of defaultsize*2, so that our free positions are always
// between 0 and defaultsize*2 (this will reduce the number of
// System.arraycopy() calls. Even after the shrinking is done
// we still have defaultsize positions available.
if ((this.size-this.num)>(this.defaultsize<<1)) {
// Allocate some space for the new arrays
int newsize=this.size-defaultsize;
WarpHandler newhandlers[]=new WarpHandler[newsize];
int newrids[]=new int[newsize];
// Copy the original arrays into the new arrays
System.arraycopy(this.handlers,0,newhandlers,0,this.num);
System.arraycopy(this.rids,0,newrids,0,this.num);
// Update our variables
this.size=newsize;
this.handlers=newhandlers;
this.rids=newrids;
}
// The handler and its rid were removed, and if necessary the
// arrays were shrunk. We just need to return the old handler.
return(oldhandler);
}
}
// Not found.
return(null);
}
/**
* Return the array of WarpHandler objects present in this table.
*
* @return An array (maybe empty) of WarpHandler objects.
*/
public WarpHandler[] handlers() {
WarpHandler buff[]=new WarpHandler[this.num];
if (this.num>0) System.arraycopy(this.handlers,0,buff,0,this.num);
return(buff);
}
}
1.1
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/warp/WarpPacket.java
Index: WarpPacket.java
===================================================================
/* ========================================================================= *
* *
* The Apache Software License, Version 1.1 *
* *
* Copyright (c) 1999, 2000 The Apache Software Foundation. *
* All rights reserved. *
* *
* ========================================================================= *
* *
* Redistribution and use in source and binary forms, with or without modi- *
* fication, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice *
* notice, this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. The end-user documentation included with the redistribution, if any, *
* must include the following acknowlegement: *
* *
* "This product includes software developed by the Apache Software *
* Foundation <http://www.apache.org/>." *
* *
* Alternately, this acknowlegement may appear in the software itself, if *
* and wherever such third-party acknowlegements normally appear. *
* *
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software *
* Foundation" must not be used to endorse or promote products derived *
* from this software without prior written permission. For written *
* permission, please contact <[EMAIL PROTECTED]>. *
* *
* 5. Products derived from this software may not be called "Apache" nor may *
* "Apache" appear in their names without prior written permission of the *
* Apache Software Foundation. *
* *
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY *
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, *
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
* ========================================================================= *
* *
* This software consists of voluntary contributions made by many indivi- *
* duals on behalf of the Apache Software Foundation. For more information *
* on the Apache Software Foundation, please see <http://www.apache.org/>. *
* *
* ========================================================================= */
package org.apache.catalina.connector.warp;
/**
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @author Copyright © 1999, 2000 <a href="http://www.apache.org">The
* Apache Software Foundation.
* @version CVS $Id: WarpPacket.java,v 1.1 2000/11/30 16:34:31 pier Exp $
*/
public class WarpPacket {
/** The default size. */
private static final int defaultsize=4096;
/** The current buffer. */
private byte buf[];
/** The current position in the buffer. */
private int pos;
public WarpPacket() {
super();
this.buf=new byte[defaultsize];
this.reset();
}
public void reset() {
this.pos=0;
if (this.buf.length>(defaultsize<<1)) this.buf=new byte[defaultsize];
}
public void writeShort(int k) {
// Check if we have room
if ((this.pos+2)<this.buf.length) {
byte newbuf[]=new byte[this.buf.length+defaultsize];
System.arraycopy(this.buf,0,newbuf,0,this.pos);
this.buf=newbuf;
}
// Store the number
this.buf[this.pos++]=(byte)((k>>8)&0x0ff);
this.buf[this.pos++]=(byte)(k&0x0ff);
}
public void writeString(String s) {
// Retrieve the string bytes
byte k[]=s.getBytes();
// Write the string length
this.writeShort(k.length);
// Check if we have room
if ((this.pos+k.length)<this.buf.length) {
byte newbuf[]=new byte[this.buf.length+k.length+defaultsize];
System.arraycopy(this.buf,0,newbuf,0,this.pos);
this.buf=newbuf;
}
// Store the string
System.arraycopy(k,0,this.buf,this.pos,k.length);
this.pos+=k.length;
}
public byte[] getBuffer() {
return(this.buf);
}
public int getLength() {
return(this.pos);
}
}
1.1
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/warp/WarpReader.java
Index: WarpReader.java
===================================================================
/* ========================================================================= *
* *
* The Apache Software License, Version 1.1 *
* *
* Copyright (c) 1999, 2000 The Apache Software Foundation. *
* All rights reserved. *
* *
* ========================================================================= *
* *
* Redistribution and use in source and binary forms, with or without modi- *
* fication, are permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice *
* notice, this list of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in the *
* documentation and/or other materials provided with the distribution. *
* *
* 3. The end-user documentation included with the redistribution, if any, *
* must include the following acknowlegement: *
* *
* "This product includes software developed by the Apache Software *
* Foundation <http://www.apache.org/>." *
* *
* Alternately, this acknowlegement may appear in the software itself, if *
* and wherever such third-party acknowlegements normally appear. *
* *
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software *
* Foundation" must not be used to endorse or promote products derived *
* from this software without prior written permission. For written *
* permission, please contact <[EMAIL PROTECTED]>. *
* *
* 5. Products derived from this software may not be called "Apache" nor may *
* "Apache" appear in their names without prior written permission of the *
* Apache Software Foundation. *
* *
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES *
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY *
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY *
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, *
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
* ========================================================================= *
* *
* This software consists of voluntary contributions made by many indivi- *
* duals on behalf of the Apache Software Foundation. For more information *
* on the Apache Software Foundation, please see <http://www.apache.org/>. *
* *
* ========================================================================= */
package org.apache.catalina.connector.warp;
/**
*
*
* @author <a href="mailto:[EMAIL PROTECTED]">Pier Fumagalli</a>
* @author Copyright © 1999, 2000 <a href="http://www.apache.org">The
* Apache Software Foundation.
* @version CVS $Id: WarpReader.java,v 1.1 2000/11/30 16:34:31 pier Exp $
*/
public class WarpReader {
/** The current buffer. */
private byte buf[];
/** The current position in the buffer. */
private int pos;
public WarpReader() {
this(null);
}
public WarpReader(byte buf[]) {
super();
this.reset(buf);
}
public void reset(byte buf[]) {
this.buf=buf;
this.pos=0;
}
public int readShort() {
// Check if we have a buffer
if (this.buf==null) throw new NullPointerException("No buffer");
// Check if we have room for a short in the buffer
if ((this.pos+2)>this.buf.length)
throw new IllegalStateException("Not enough data ("+this.pos+"/"+
this.buf.length+")");
// Read the short and return it
int x=(((int)this.buf[this.pos++])&0x0ff);
x=((x<<8)|(((int)this.buf[this.pos++])&0x0ff));
return(x);
}
public String readString() {
// Check if we have a buffer
if (this.buf==null) throw new NullPointerException("No buffer");
// Attempt to read the string length
int len=this.readShort();
// Check if we have enough space in the buffer for the string
if ((this.pos+len)>buf.length)
throw new IllegalStateException("Not enough data ("+this.pos+"+"+
len+"="+this.buf.length+")");
// Create the string, update the position and return.
String s=new String(this.buf,this.pos,len);
this.pos+=len;
return(s);
}
}