1
|
1 package suikwasha.distributedalgorithm.framework;
|
|
2
|
|
3 import java.util.LinkedList;
|
|
4 import java.util.concurrent.SynchronousQueue;
|
|
5
|
|
6 public class Selector
|
|
7 {
|
|
8 private final SynchronousQueue<Port> queue;
|
|
9 private final LinkedList<Port> watchList;
|
|
10
|
|
11 public Selector()
|
|
12 {
|
|
13 queue = new SynchronousQueue<Port>();
|
|
14 watchList = new LinkedList<Port>();
|
|
15 }
|
|
16
|
|
17 public void signal(Port _p)
|
|
18 {
|
|
19 queue.offer(_p);
|
|
20 }
|
|
21
|
|
22 public void register(Port _p)
|
|
23 {
|
|
24 watchList.add(_p);
|
|
25 }
|
|
26
|
|
27 public Port select() throws InterruptedException
|
|
28 {
|
|
29 Port availablePort;
|
|
30 int length = watchList.size();
|
|
31 for(int i = 0;i < length;i ++){
|
|
32 availablePort = watchList.poll();
|
|
33 if(availablePort.isReady()){
|
|
34 watchList.addLast(availablePort);
|
|
35 return availablePort;
|
|
36 }
|
|
37 watchList.addLast(availablePort);
|
|
38 }
|
|
39
|
|
40 return queue.take();
|
|
41 }
|
|
42 }
|