Mercurial > hg > RemoteEditor > REPSessionManager
view rep/channel/SelectableChannelSimulator.java @ 144:0bf7f8d0f5f7
*** empty log message ***
author | pin |
---|---|
date | Wed, 27 Aug 2008 22:55:43 +0900 |
parents | 785a3e8ea858 |
children | 31334767e65d |
line wrap: on
line source
package rep.channel; import java.nio.channels.SelectableChannel; import java.nio.channels.SocketChannel; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public abstract class SelectableChannelSimulator<P> extends REPSocketChannel<P>{ protected BlockingQueue<P> qread; protected BlockingQueue<P> qwrite; protected SelectorSimulator writeSelector; protected SelectorSimulator readSelector; public SelectableChannelSimulator(SocketChannel channel) { super(channel); } /* read from Queue. */ public P read(){ try { if(readSelector!=null) synchronized (readSelector){ return qread.take(); } else{ return qread.take(); } } catch (InterruptedException e) { e.printStackTrace(); return null; } } /* write to Queue. */ public boolean write(P p){ try { if (writeSelector!=null) synchronized (writeSelector){ qwrite.put(p); writeSelector.notifyAll(); } else { qwrite.put(p); } return true; } catch (InterruptedException e) { e.printStackTrace(); return false; } } public abstract ChannelSimulator<P> accept(); /* accessor methods. */ public BlockingQueue<P> getReadQ(){ return qread; } public BlockingQueue<P> getWriteQ(){ return qwrite; } public void createReadQ(){ qread = new LinkedBlockingQueue<P>(); } public void createWriteQ(){ qwrite = new LinkedBlockingQueue<P>(); } public void setWriteSelector(SelectorSimulator _selector){ writeSelector = _selector; } /* return state of the Queue */ abstract public boolean isReadable(); abstract public boolean isWritable(); abstract public boolean isAcceptable(); }