123
|
1 package rep.channel;
|
|
2
|
156
|
3 import java.net.SocketAddress;
|
|
4 import java.util.HashMap;
|
123
|
5 import java.util.LinkedList;
|
|
6
|
|
7
|
177
|
8 public class NetworkSimulator<P> {
|
123
|
9 public static NetworkSimulator<?> ns;
|
151
|
10
|
177
|
11
|
156
|
12 public HashMap<SocketAddress,Integer>namedb = new HashMap<SocketAddress,Integer>();
|
|
13 public int ipcount = 1;
|
177
|
14 public REPLogger logger;
|
156
|
15
|
175
|
16 @SuppressWarnings("unchecked") // <?> から <T> へのキャストのため.
|
174
|
17 public static <T> NetworkSimulator<T> singleton(){
|
151
|
18 // double check singleton
|
123
|
19 if (ns==null)
|
151
|
20 synchronized (NetworkSimulator.class) {
|
|
21 if (ns==null)
|
|
22 ns = new NetworkSimulator<T>();
|
|
23 }
|
174
|
24 return (NetworkSimulator<T>) ns;
|
123
|
25 }
|
|
26
|
|
27 int logLevel=5;
|
|
28 /** Listening Servers. */
|
|
29 private LinkedList<ServerData<P>> serverList;
|
|
30
|
|
31 /** Constructor. */
|
|
32 public NetworkSimulator(){
|
|
33 serverList = new LinkedList<ServerData<P>>();
|
177
|
34 logger = REPLogger.singleton();
|
|
35 logger.writeLog("construct Networksimulator", 1);
|
123
|
36 printAllState();
|
|
37 }
|
|
38
|
|
39 /* */
|
203
|
40 synchronized public void listen(SocketAddress ip, SelectorSimulator<P> selector) {
|
123
|
41 serverList.add(new ServerData<P>(ip, selector));
|
177
|
42 logger.writeLog("listen", 1);
|
123
|
43 printAllState();
|
|
44 }
|
|
45
|
175
|
46 synchronized public ChannelSimulator<P> accept(SocketAddress ip) {
|
123
|
47 for (ServerData<P> sd: serverList){
|
175
|
48 if (!sd.IP.equals(ip)) continue;
|
177
|
49 logger.writeLog("accepting..", 1);
|
123
|
50
|
|
51 ChannelSimulator<P> serverCH = sd.acceptWaitingList.remove();
|
|
52 sd.establishedList.add(serverCH);
|
|
53
|
177
|
54 logger.writeLog("accepted", 1);
|
123
|
55 printAllState();
|
|
56 return serverCH;
|
|
57 }
|
|
58 return null;
|
|
59 }
|
175
|
60 synchronized public boolean canAccept(SocketAddress ip){
|
123
|
61 for (ServerData<P> sd: serverList){
|
175
|
62 if (!sd.IP.equals(ip)) continue;
|
123
|
63 return !sd.acceptWaitingList.isEmpty();
|
|
64 }
|
|
65 return false;
|
|
66 }
|
|
67
|
175
|
68 public boolean connect(SocketAddress ip, ChannelSimulator<P> clientCH) {
|
123
|
69 ServerData<P> sd = null;
|
177
|
70 logger.writeLog("connecting..", 1);
|
123
|
71 synchronized (this){
|
|
72 for (ServerData<P> sd0: serverList){
|
175
|
73 if (sd0.IP.equals(ip)){
|
|
74 sd=sd0;
|
|
75 break;
|
|
76 }
|
|
77 }
|
123
|
78 if (sd==null) return false;
|
|
79
|
|
80 //ChannelSimulator<P> channel = new ChannelSimulator<P>(sd.selector);
|
|
81 clientCH.createReadQ();
|
|
82 clientCH.createWriteQ();
|
|
83 clientCH.setWriteSelector(sd.selector);
|
|
84
|
|
85 ChannelSimulator<P> serverCH = clientCH.createConjugatedChannel();
|
|
86 sd.acceptWaitingList.add(serverCH);
|
|
87 }
|
|
88
|
|
89 synchronized (sd.selector) {
|
|
90 sd.selector.notifyAll();
|
|
91 }
|
177
|
92 logger.writeLog("connected", 1);
|
123
|
93 printAllState();
|
|
94 return true;
|
|
95 }
|
|
96
|
|
97 /** for DEBUG methods. */
|
|
98 synchronized void printAllState(){
|
177
|
99 logger.writeLog("NetworkSimulator State:");
|
123
|
100 for (ServerData<P> sd: serverList){
|
177
|
101 logger.writeLog("\tSessionManager(ip="+sd.IP.toString()+"): ");
|
|
102 logger.writeLog("\tacceptWaitingList="+sd.acceptWaitingList.size());
|
|
103 logger.writeLog("\testablishedList="+sd.establishedList.size());
|
123
|
104 }
|
|
105 }
|
156
|
106
|
|
107
|
|
108
|
|
109 public int nslookup(SocketAddress semaIP) {
|
|
110 Integer ip;
|
|
111 if ((ip=namedb.get(semaIP))==null) {
|
|
112 namedb.put(semaIP, (ip=ipcount++));
|
|
113 }
|
|
114 return ip;
|
|
115 }
|
123
|
116
|
|
117
|
|
118 }
|
|
119
|
|
120 class ServerData<P> {
|
175
|
121 //int virtualIP;
|
|
122 SocketAddress IP;
|
203
|
123 SelectorSimulator<P> selector;
|
123
|
124 LinkedList<ChannelSimulator<P>> acceptWaitingList;
|
|
125 LinkedList<ChannelSimulator<P>> establishedList;
|
|
126
|
203
|
127 ServerData(SocketAddress ip, SelectorSimulator<P> _selector){
|
175
|
128 IP = ip;
|
123
|
129 selector = _selector;
|
|
130 acceptWaitingList = new LinkedList<ChannelSimulator<P>>();
|
|
131 establishedList = new LinkedList<ChannelSimulator<P>>();
|
|
132 }
|
|
133 } |