67
|
1 package pathfinder;
|
|
2
|
|
3 import java.util.ArrayList;
|
|
4 import java.util.LinkedList;
|
68
|
5 import java.util.List;
|
67
|
6 import java.util.Queue;
|
|
7
|
|
8 public class NetworkSimulator<P> {
|
|
9
|
68
|
10 public Queue<ChannelSimulator<P>> acceptList;
|
|
11 public Queue<ChannelSimulator<P>> connectList;
|
67
|
12
|
|
13 public NetworkSimulator(){
|
|
14 acceptList = new LinkedList<ChannelSimulator<P>>();
|
68
|
15 connectList = new LinkedList<ChannelSimulator<P>>();
|
67
|
16 }
|
|
17
|
|
18 public ChannelSimulator<P> accept(){
|
|
19 ChannelSimulator<P> cs = acceptList.poll();
|
68
|
20 connectList.offer(cs);
|
67
|
21
|
|
22 return cs.getServerChannel();
|
|
23 }
|
|
24 public void connect(ChannelSimulator<P> cs){
|
68
|
25 acceptList.offer(cs);
|
67
|
26 }
|
68
|
27
|
67
|
28 public synchronized P read(Queue<P>q) {
|
|
29 return q.poll();
|
|
30 }
|
|
31
|
|
32 public synchronized boolean write(Queue<P>q,P p) {
|
68
|
33 return q.offer(p);
|
67
|
34 }
|
|
35 }
|