Mercurial > hg > RemoteEditor > Eclipse
comparison src/rep/channel/ServerChannelSimulator.java @ 193:3133040ee4f4
(no commit message)
author | one |
---|---|
date | Wed, 31 Dec 2008 15:06:22 +0900 |
parents | |
children | ec69099f8270 |
comparison
equal
deleted
inserted
replaced
192:c921022bf494 | 193:3133040ee4f4 |
---|---|
1 package rep.channel; | |
2 | |
3 import java.io.IOException; | |
4 import java.net.InetSocketAddress; | |
5 import java.net.ServerSocket; | |
6 import java.nio.channels.ClosedChannelException; | |
7 import java.nio.channels.SelectableChannel; | |
8 import java.nio.channels.SelectionKey; | |
9 import java.nio.channels.Selector; | |
10 import java.nio.channels.spi.SelectorProvider; | |
11 import java.util.LinkedList; | |
12 import java.util.Queue; | |
13 | |
14 /* シミュレーションの際にコンストラクトされる REPServerSocketChannel の実装 */ | |
15 public class ServerChannelSimulator<P>extends REPServerSocketChannel<P> { | |
16 protected NetworkSimulator ns; | |
17 //public REPServerSocket<REPSocketChannel<P>> socket; | |
18 protected InetSocketAddress IP; | |
19 protected Queue<ChannelSimulator<P>> acceptQ; | |
20 protected Selector selector; | |
21 protected boolean isBlocking; | |
22 private SelectionKeySimulator<P> key; | |
23 | |
24 /** Constructors. | |
25 * @throws IOException */ | |
26 public ServerChannelSimulator() throws IOException { | |
27 ns = NetworkSimulator.singleton(); | |
28 selector = new NullSelector(); // new Object(); | |
29 acceptQ = new LinkedList<ChannelSimulator<P>>(); | |
30 } | |
31 | |
32 public void bind(InetSocketAddress ip){ | |
33 IP = ip; | |
34 } | |
35 | |
36 public synchronized REPSocketChannel<P> accept1() throws IOException { | |
37 ChannelSimulator<P> channel; | |
38 while ( (channel=acceptQ.poll())==null && isBlocking ) { | |
39 try { | |
40 wait(); | |
41 } catch (InterruptedException e) { | |
42 throw new IOException(); | |
43 } | |
44 } | |
45 return channel; | |
46 } | |
47 | |
48 protected boolean enQ(ChannelSimulator<P> ch){ | |
49 // Don't lock a selector from a locked channel, the selector may | |
50 // use channel.isAble() which locks the channel. | |
51 synchronized(this) { | |
52 acceptQ.offer(ch); | |
53 notify(); | |
54 } | |
55 selector.wakeup(); | |
56 return true; | |
57 } | |
58 | |
59 @SuppressWarnings("unchecked") | |
60 public void enQ(ChannelSimulator<?> hserver) { | |
61 // NetworkSimulator doesn't know P | |
62 ChannelSimulator<P>ch = (ChannelSimulator<P>) hserver; | |
63 enQ(ch); | |
64 } | |
65 | |
66 public ServerSocket socket() { | |
67 try { | |
68 return new REPServerSocket(this); | |
69 } catch (IOException e) { | |
70 e.printStackTrace(); | |
71 return null; | |
72 } | |
73 } | |
74 | |
75 | |
76 | |
77 @SuppressWarnings("unchecked") | |
78 public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException { | |
79 selector = sel; | |
80 REPSelector<P> selector1 = sel; | |
81 ns.listen(IP, this); | |
82 key = (SelectionKeySimulator<P>) selector1.register(this, ops, att); | |
83 return key; | |
84 } | |
85 @SuppressWarnings("unchecked") | |
86 @Override | |
87 public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException { | |
88 selector = sel; | |
89 REPSelector<P> selector1 = (REPSelector<P>)sel; | |
90 ns.listen(IP, this); // bindに移動してもいいよ | |
91 key = (SelectionKeySimulator<P>) selector1.register(this, ops, att); | |
92 return key; | |
93 } | |
94 | |
95 public synchronized boolean isAcceptable() { | |
96 return !acceptQ.isEmpty(); | |
97 } | |
98 | |
99 @Override | |
100 public Object blockingLock() { | |
101 return selector; | |
102 } | |
103 | |
104 public SelectableChannel configureBlocking(boolean block) throws IOException { | |
105 isBlocking = block; | |
106 return this; | |
107 } | |
108 | |
109 @Override | |
110 public boolean isBlocking() { | |
111 return isBlocking; | |
112 } | |
113 | |
114 @Override | |
115 public boolean isRegistered() { | |
116 // TODO Auto-generated method stub | |
117 return false; | |
118 } | |
119 | |
120 @Override | |
121 public SelectionKey keyFor(Selector sel) { | |
122 // TODO Auto-generated method stub | |
123 return null; | |
124 } | |
125 | |
126 @Override | |
127 public SelectorProvider provider() { | |
128 // TODO Auto-generated method stub | |
129 return null; | |
130 } | |
131 | |
132 | |
133 @Override | |
134 public int validOps() { | |
135 // TODO Auto-generated method stub | |
136 return 0; | |
137 } | |
138 | |
139 @Override | |
140 protected void implCloseChannel() throws IOException { | |
141 // TODO Auto-generated method stub | |
142 | |
143 } | |
144 | |
145 | |
146 } |