Mercurial > hg > RemoteEditor > REPSessionManager
comparison rep/channel/SelectableChannelSimulator.java @ 123:5b1a0574b406 add-simulator
*** empty log message ***
author | pin |
---|---|
date | Wed, 27 Aug 2008 17:21:25 +0900 |
parents | |
children | 785a3e8ea858 |
comparison
equal
deleted
inserted
replaced
122:790c8dd42a7b | 123:5b1a0574b406 |
---|---|
1 package rep.channel; | |
2 | |
3 import java.nio.channels.SelectableChannel; | |
4 import java.nio.channels.SocketChannel; | |
5 import java.util.concurrent.BlockingQueue; | |
6 import java.util.concurrent.LinkedBlockingQueue; | |
7 | |
8 | |
9 | |
10 public abstract class SelectableChannelSimulator<P> extends REPSocketChannel<P>{ | |
11 | |
12 protected BlockingQueue<P> qread; | |
13 protected BlockingQueue<P> qwrite; | |
14 protected SelectorSimulator<P> writeSelector; | |
15 protected SelectorSimulator<P> readSelector; | |
16 | |
17 public SelectableChannelSimulator(SocketChannel channel) { | |
18 super(channel); | |
19 } | |
20 | |
21 /* read from Queue. */ | |
22 public P read(){ | |
23 try { | |
24 if(readSelector!=null) | |
25 synchronized (readSelector){ | |
26 return qread.take(); | |
27 } | |
28 else{ | |
29 return qread.take(); | |
30 } | |
31 } catch (InterruptedException e) { | |
32 e.printStackTrace(); | |
33 return null; | |
34 } | |
35 } | |
36 /* write to Queue. */ | |
37 public boolean write(P p){ | |
38 try { | |
39 if (writeSelector!=null) | |
40 synchronized (writeSelector){ | |
41 qwrite.put(p); | |
42 writeSelector.notifyAll(); | |
43 } | |
44 else { | |
45 qwrite.put(p); | |
46 } | |
47 return true; | |
48 } catch (InterruptedException e) { | |
49 e.printStackTrace(); | |
50 return false; | |
51 } | |
52 } | |
53 public abstract ChannelSimulator<P> accept(); | |
54 | |
55 /* accessor methods. */ | |
56 public BlockingQueue<P> getReadQ(){ | |
57 return qread; | |
58 } | |
59 public BlockingQueue<P> getWriteQ(){ | |
60 return qwrite; | |
61 } | |
62 public void createReadQ(){ | |
63 qread = new LinkedBlockingQueue<P>(); | |
64 } | |
65 public void createWriteQ(){ | |
66 qwrite = new LinkedBlockingQueue<P>(); | |
67 } | |
68 public void setWriteSelector(SelectorSimulator<P> _selector){ | |
69 writeSelector = _selector; | |
70 } | |
71 | |
72 | |
73 /* return state of the Queue */ | |
74 abstract public boolean isReadable(); | |
75 abstract public boolean isWritable(); | |
76 abstract public boolean isAcceptable(); | |
77 | |
78 } |