geronimo-scm mailing list archives

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

  Modified:    modules/network/src/java/org/apache/geronimo/network/protocol
                        SocketProtocol.java
  Log:
  We are always interested in read events.
  
  It seems better to wait for a write ready event before attempting to write to the
  socket channel.
  
  Revision  Changes    Path
  1.9       +35 -31    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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- SocketProtocol.java	24 Apr 2004 17:56:32 -0000	1.8
  +++ SocketProtocol.java	24 Apr 2004 21:34:34 -0000	1.9
  @@ -64,13 +64,14 @@
       ByteBuffer[] sendBuffer;
       ByteBuffer headerBuffer;
       ByteBuffer bodyBuffer;
  -    
  +
       Object serviceReadMutex;
       Object serviceWriteMutex;
  -    
  -    static int nextConnectionId=0;
  +
  +    static int nextConnectionId = 0;
  +
       synchronized static int getNextConnectionId() {
  -    	return nextConnectionId++;    	
  +        return nextConnectionId++;
       }
   
       public Protocol getUpProtocol() {
  @@ -150,18 +151,18 @@
       }
   
       public Protocol cloneProtocol() throws CloneNotSupportedException {
  -    	SocketProtocol p = (SocketProtocol)super.clone();
  -    	p.log = LogFactory.getLog(SocketProtocol.class.getName()+":"+getNextConnectionId());
  +        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);
  -    	serviceReadMutex = new Object();
  -    	serviceWriteMutex = new Object();
  -    	
  +        log = LogFactory.getLog(SocketProtocol.class.getName() + ":" + getNextConnectionId());
  +        sendMutex = new Mutex();
  +        headerBuffer = ByteBuffer.allocate(4);
  +        serviceReadMutex = new Object();
  +        serviceWriteMutex = new Object();
  +
           if (address == null && acceptedSocketChannel == null) throw new IllegalStateException("No
address set");
   
           log.trace("Starting");
  @@ -240,10 +241,9 @@
               sendBuffer[0].putInt(size);
               sendBuffer[0].flip();
   
  -            // This is non blocking call anyways, push out
  -            // the buffer now if we can.
  -            serviceWrite();
  -            
  +            log.trace("OP_READ, OP_WRITE " + selectionKey);
  +            selectorManager.setInterestOps(selectionKey, SelectionKey.OP_READ | SelectionKey.OP_WRITE,
0);
  +
           } catch (InterruptedException e) {
               log.debug("Communications error, closing connection: ", e);
               close();
  @@ -255,16 +255,16 @@
           try {
               if (selection.isReadable()) {
                   synchronized (serviceReadMutex) {
  -                	serviceRead();
  +                    serviceRead();
                   }
               }
               if (selection.isWritable()) {
                   synchronized (serviceWriteMutex) {
  -                	serviceWrite();
  +                    serviceWrite();
                   }
  -            } 
  +            }
           } catch (CancelledKeyException e) {
  -        	log.trace("Key Cancelled:", e);
  +            log.trace("Key Cancelled:", e);
               // who knows, by the time we get here,
               // the key could have been canceled.
           }
  @@ -273,11 +273,11 @@
       private void serviceWrite() {
           log.trace("serviceWrite() triggered.");
           try {
  -        	if( sendBuffer == null ) {
  +            if (sendBuffer == null) {
                   log.trace("Write had allready been serviced.");
                   return;
  -        	}
  -        	
  +            }
  +
               long count = socketChannel.write(sendBuffer);
               log.trace("Wrote " + count);
   
  @@ -285,8 +285,8 @@
                   if (sendBuffer[i].hasRemaining()) {
                       // not all was delivered in this call setup selector
                       // so we setup to finish sending async.
  -                    log.trace("OP_WRITE " + selectionKey);
  -                    selectorManager.setInterestOps(selectionKey, SelectionKey.OP_WRITE,
0);
  +                    log.trace("OP_READ, OP_WRITE " + selectionKey);
  +                    selectorManager.setInterestOps(selectionKey, SelectionKey.OP_READ |
SelectionKey.OP_WRITE, 0);
   
                       return;
                   }
  @@ -297,8 +297,12 @@
   
               log.trace("RELEASING " + sendMutex);
               sendMutex.release();
  -            log.trace("RELEASED " + sendMutex);            
  +            log.trace("RELEASED " + sendMutex);
  +
   
  +            // We are done writing.
  +            log.trace("OP_READ " + selectionKey);
  +            selectorManager.setInterestOps(selectionKey, SelectionKey.OP_READ, 0);
           } catch (IOException e) {
               log.debug("Communications error, closing connection: ", e);
               close();
  @@ -331,7 +335,7 @@
   
                       if (headerBuffer.hasRemaining()) {
                           log.trace("HEADER reamining " + headerBuffer.remaining());
  -                    	break; // not done reading the header.
  +                        break; // not done reading the header.
                       }
   
                       headerBuffer.flip();
  @@ -351,8 +355,8 @@
                       bodyBuffer.clear();
                       bodyBuffer.limit(size);
                   }
  -                
  -                log.trace("BODY... HEADER remaining: " + headerBuffer.remaining()+", "+headerBuffer.hasRemaining());
  +
  +                log.trace("BODY... HEADER remaining: " + headerBuffer.remaining() + ",
" + headerBuffer.hasRemaining());
                   // Are we reading the body??
                   if (bodyBuffer.hasRemaining()) {
                       if (tracing)
  @@ -381,7 +385,7 @@
               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,
  
  
  

Mime
View raw message