geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject cvs commit: incubator-geronimo/modules/network/src/java/org/apache/geronimo/network/protocol SocketProtocol.java
Date Sat, 24 Apr 2004 04:07:14 GMT
chirino     2004/04/23 21:07:13

  Modified:    modules/network/src/java/org/apache/geronimo/network/protocol
                        SocketProtocol.java
  Log:
  The clone method that was being used resulted in many key data structures being shared accross
multiple SocketProtocol objects.  One of the most important being the headerBuffer.  These
data structures are now created at setup() and therefore clone does not copy them.
  
  Revision  Changes    Path
  1.6       +46 -18    incubator-geronimo/modules/network/src/java/org/apache/geronimo/network/protocol/SocketProtocol.java
  
  Index: SocketProtocol.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/network/src/java/org/apache/geronimo/network/protocol/SocketProtocol.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SocketProtocol.java	19 Apr 2004 16:30:33 -0000	1.5
  +++ SocketProtocol.java	24 Apr 2004 04:07:13 -0000	1.6
  @@ -41,8 +41,7 @@
    */
   public class SocketProtocol implements AcceptableProtocol, SelectionEventListner {
   
  -    final static private Log log = LogFactory.getLog(SocketProtocol.class);
  -
  +    private Log log = LogFactory.getLog(SocketProtocol.class);
       private Protocol up;
   
       private SocketChannel acceptedSocketChannel;
  @@ -51,7 +50,7 @@
       private SocketAddress socketInterface;
   
       private long timeout;
  -    private Mutex sendMutex = new Mutex();
  +    private Mutex sendMutex;
       private SelectorManager selectorManager;
       private SelectionKey selectionKey;
   
  @@ -62,6 +61,14 @@
       private static final int STOPPED = 1;
       private int state = STOPPED;
   
  +    ByteBuffer[] sendBuffer;
  +    ByteBuffer headerBuffer;
  +    ByteBuffer bodyBuffer;
  +    
  +    static int nextConnectionId=0;
  +    synchronized static int getNextConnectionId() {
  +    	return nextConnectionId++;    	
  +    }
   
       public Protocol getUpProtocol() {
           return up;
  @@ -140,10 +147,16 @@
       }
   
       public Protocol cloneProtocol() throws CloneNotSupportedException {
  -        return (Protocol) super.clone();
  +    	SocketProtocol p = (SocketProtocol)super.clone();
  +    	p.log = LogFactory.getLog(SocketProtocol.class.getName()+":"+getNextConnectionId());
  +        return p;
       }
   
       public void setup() throws ProtocolException {
  +    	log = LogFactory.getLog(SocketProtocol.class.getName()+":"+getNextConnectionId());
  +    	sendMutex = new Mutex();
  +    	headerBuffer = ByteBuffer.allocate(4);
  +    	
           if (address == null && acceptedSocketChannel == null) throw new IllegalStateException("No
address set");
   
           log.trace("Starting");
  @@ -245,12 +258,10 @@
           }
       }
   
  -    ByteBuffer[] sendBuffer;
  -    ByteBuffer headerBuffer = ByteBuffer.allocate(4);
  -    ByteBuffer bodyBuffer = null;
  -
  -    private void serviceWrite() {
  +    synchronized private void serviceWrite() {
  +        log.trace("serviceWrite() triggered.");
           try {
  +
               long count = socketChannel.write(sendBuffer);
               log.trace("Wrote " + count);
   
  @@ -278,19 +289,20 @@
           } catch (IOException e) {
               log.debug("Communications error, closing connection: ", e);
               close();
  +        } finally {
  +            log.trace("serviceWrite() done.");
           }
       }
   
  -    public void serviceRead() {
  +    synchronized public void serviceRead() {
           boolean tracing = log.isTraceEnabled();
  -
  -        if (tracing) log.trace("ReadDataAction triggered.");
  -
  +        if (tracing) log.trace("serviceRead() triggered.");
           lastUsed = System.currentTimeMillis();
  -
           try {
               while (true) {
   
  +                log.trace("HEADER reamining " + headerBuffer.remaining());
  +
                   // Are we reading the header??
                   if (headerBuffer.hasRemaining()) {
                       if (tracing)
  @@ -304,7 +316,10 @@
                           return;
                       }
   
  -                    if (headerBuffer.hasRemaining()) break; // not done reading the header.
  +                    if (headerBuffer.hasRemaining()) {
  +                        log.trace("HEADER reamining " + headerBuffer.remaining());
  +                    	break; // not done reading the header.
  +                    }
   
                       headerBuffer.flip();
   
  @@ -323,6 +338,8 @@
                       bodyBuffer.clear();
                       bodyBuffer.limit(size);
                   }
  +                
  +                log.trace("BODY... HEADER remaining: " + headerBuffer.remaining()+", "+headerBuffer.hasRemaining());
                   // Are we reading the body??
                   if (bodyBuffer.hasRemaining()) {
                       if (tracing)
  @@ -342,16 +359,22 @@
   
                       // release old buffer
                       bodyBuffer = null;
  +                    headerBuffer.clear();
   
                       up.sendUp(packet);
   
  -                    headerBuffer.clear();
                   }
               }
               log.trace("OP_READ " + selectionKey);
               selectorManager.setInterestOps(selectionKey, SelectionKey.OP_READ, 0);
               if (tracing) log.trace("No more data available to be read.");
  +            
  +        } catch (CancelledKeyException e) {
  +            log.trace("Key Cancelled: ", e);
  +            // who knows, by the time we get here,
  +            // the key could have been canceled.
           } catch (ClosedChannelException e) {
  +            log.trace("Channel Closed: ", e);
               // who knows, by the time we get here,
               // the channel could have been closed.
           } catch (IOException e) {
  @@ -360,6 +383,11 @@
           } catch (ProtocolException e) {
               log.debug("Communications error, closing connection: ", e);
               close();
  +        } catch (Throwable e) {
  +            log.debug("Unhandled error, closing connection: ", e);
  +            close();
  +        } finally {
  +            if (tracing) log.trace("serviceRead() done.");
           }
       }
   
  @@ -382,4 +410,4 @@
       public void accept(SocketChannel socketChannel) {
           this.acceptedSocketChannel = socketChannel;
       }
  -}
  +}
  \ No newline at end of file
  
  
  

Mime
View raw message