Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 22nd, 2012  |  syntax: None  |  size: 4.42 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to let selector that socketchannel key change in java nio
  2. public synchronized int writeData(EventObject source) {      
  3.     int n = 0;
  4.     int count = 0;
  5.  
  6.     SocketChannel socket = (SocketChannel)source.getSource();      
  7.     ByteBuffer buffer = ((WriteEvent)source).getBuffer();  
  8.     try {
  9.         write(socket);
  10.     } catch (IOException e1) {          
  11.         e1.printStackTrace();
  12.     }      
  13.  
  14.     while (buffer.position()>0) {  
  15.         try {          
  16.                 buffer.flip();  
  17.                 n = socket.write(buffer);                                  
  18.                 if(n == 0) {
  19.                         key.interestOps(SelectionKey.OP_WRITE);                         synchronized (this.pendingData) {  
  20.                             List<ByteBuffer> queue = (List<ByteBuffer>) this.pendingData.get(socket);
  21.                             if(queue == null) {
  22.                                 queue = new ArrayList<ByteBuffer>();
  23.                                 this.pendingData.put(socket, queue);
  24.                         }
  25.                         queue.add(buffer);
  26.  
  27.                         logger.logInfo("queue length:" + queue.size());
  28.                     }                                              
  29.                     break;
  30.                 }              
  31.                 count += n;
  32.  
  33.         } catch (IOException e) {              
  34.             e.printStackTrace();
  35.         }   finally {                      
  36.             buffer.compact();              
  37.         }
  38.     }  
  39.  
  40.     if(buffer.position()==0) {                      
  41.         key.interestOps(SelectionKey.OP_READ);                  
  42.     }
  43.             return count;  
  44.  
  45. }
  46.        
  47. public synchronized int write(SocketChannel sc, ByteBuffer wbuf) {        
  48.     int n = 0;
  49.     int count = 0;
  50.  
  51.     SelectionKey key = sc.keyFor(this.dispatcher.getDemultiplexer().getDemux());                
  52.     while (wbuf.position()>0) {    
  53.         try {          
  54.             wbuf.flip();        
  55.  
  56.             n = sc.write(wbuf);            
  57.  
  58.             if(n == 0) {    
  59.                    key.interestOps(SelectionKey.OP_WRITE);                                  
  60.                     synchronized (this.pendingData) {  
  61.                         List<ByteBuffer> queue = (List<ByteBuffer>) this.pendingData.get(sc);
  62.                         if(queue == null) {
  63.                                 queue = new ArrayList<ByteBuffer>();
  64.                                 this.pendingData.put(sc, queue);
  65.                         }
  66.                         queue.add(wbuf);
  67.                     }
  68.  
  69.                     break;
  70.                 }              
  71.                 count += n;
  72.  
  73.         } catch (IOException e) {              
  74.             e.printStackTrace();
  75.         }   finally {              
  76.  
  77.             wbuf.compact();                
  78.         }
  79.     }  
  80.  
  81.     if(wbuf.position()==0) {    
  82.         wbuf.clear();              
  83.         key.interestOps(SelectionKey.OP_READ);          
  84.     }
  85.  
  86. return n;      
  87. }
  88.        
  89. public void write(SocketChannel socketChannel) throws IOException {        
  90.    SelectionKey key = socketChannel.keyFor(this.dispatcher.getDemultiplexer().getDemux());    
  91.     synchronized (this.pendingData) {            
  92.         List<ByteBuffer> queue = (List<ByteBuffer>) this.pendingData.get(socketChannel);              
  93.         if(queue == null || queue.isEmpty()) {                
  94.             // We wrote away all data, so we're no longer interested                
  95.             // in writing on this socket. Switch back to waiting for  data.                
  96.             try {                    
  97.                 if (key!=null)                        
  98.                     key.interestOps(SelectionKey.OP_READ);                
  99.             } catch(Exception ex) {                    
  100.                 if (key!=null)                        
  101.                     key.cancel();                
  102.                 }            
  103.         }          
  104.  
  105.         // Write until there's not more data ...    
  106.         int n = 0;
  107.         while (queue != null && !queue.isEmpty()) {                
  108.             ByteBuffer buf = (ByteBuffer) queue.get(0);  
  109.             // zero length write, break the loop and wait for next writable time
  110.             n = write(socketChannel, buf);
  111.  
  112.             logger.logInfo("queue length:" + queue.size() + " used time: " + (t2-t1) + " ms.");
  113.  
  114.             if(n==0)  {            
  115.                 break;
  116.             }
  117.                       queue.remove(0);
  118.  
  119.         }        
  120.  
  121.  }