123
|
1 package rep.channel;
|
|
2
|
282
|
3 import java.net.InetSocketAddress;
|
156
|
4 import java.net.SocketAddress;
|
|
5 import java.util.HashMap;
|
123
|
6 import java.util.LinkedList;
|
|
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);
|
284
|
36 // printAllState();
|
123
|
37 }
|
|
38
|
|
39 /* */
|
282
|
40 synchronized public void listen(InetSocketAddress ip, ServerChannelSimulator<P> scs) {
|
248
|
41 serverList.add(new ServerData<P>(ip, scs));
|
177
|
42 logger.writeLog("listen", 1);
|
123
|
43 printAllState();
|
|
44 }
|
|
45
|
284
|
46 synchronized public boolean connect(InetSocketAddress ip, ChannelSimulator<P> CHclient) {
|
248
|
47 logger.writeLog("connecting..", 1);
|
|
48 for (ServerData<P> sd0: serverList){
|
281
|
49 // ANY address (0.0.0.0/0.0.0.0) should be considered.
|
282
|
50 if (sd0.IP.getAddress().isAnyLocalAddress()) {
|
284
|
51 if (sd0.IP.getPort() != ip.getPort()) continue;
|
|
52 // we have to check, ip is really reachable to sd0 server,
|
|
53 // but this simulator has no notion of host. To distinguish,
|
|
54 // use different port address.
|
282
|
55 } else if (!sd0.IP.equals(ip)) continue;
|
123
|
56
|
248
|
57 ChannelSimulator<P> CHserver = new ChannelSimulator<P>();
|
|
58 CHserver.setOtherEnd(CHclient);
|
|
59 CHclient.setOtherEnd(CHserver);
|
|
60
|
|
61 sd0.connectedListS.add(CHserver);
|
|
62 sd0.connectedListC.add(CHclient);
|
|
63 sd0.scs.enQ(CHserver);
|
|
64
|
|
65 logger.writeLog("connected", 1);
|
308
|
66 //printAllState();
|
248
|
67 return true;
|
123
|
68 }
|
|
69 return false;
|
|
70 }
|
|
71
|
|
72 /** for DEBUG methods. */
|
282
|
73 void printAllState(){
|
|
74 String log = "NetworkSimulator State:";
|
123
|
75 for (ServerData<P> sd: serverList){
|
282
|
76 log += "\tSessionManager(ip="+sd.IP.toString()+"): ";
|
|
77 log += channelList(sd.connectedListC);
|
248
|
78 }
|
282
|
79 logger.writeLog(log);
|
123
|
80 }
|
282
|
81
|
|
82 String channelList(LinkedList<ChannelSimulator<P>> list){
|
248
|
83 String tmp = "";
|
|
84 for (ChannelSimulator<P> ch: list){
|
|
85 tmp += ch.toString()+" ";
|
|
86 }
|
282
|
87 return "\t"+tmp;
|
248
|
88 }
|
156
|
89
|
|
90
|
|
91
|
|
92 public int nslookup(SocketAddress semaIP) {
|
|
93 Integer ip;
|
|
94 if ((ip=namedb.get(semaIP))==null) {
|
|
95 namedb.put(semaIP, (ip=ipcount++));
|
|
96 }
|
|
97 return ip;
|
|
98 }
|
123
|
99
|
|
100
|
|
101 }
|
|
102
|
|
103 class ServerData<P> {
|
175
|
104 //int virtualIP;
|
282
|
105 InetSocketAddress IP;
|
248
|
106 //SelectorSimulator<P> selector;
|
|
107 ServerChannelSimulator<P> scs;
|
|
108 LinkedList<ChannelSimulator<P>> connectedListS;
|
|
109 LinkedList<ChannelSimulator<P>> connectedListC;
|
123
|
110
|
282
|
111 ServerData(InetSocketAddress ip, ServerChannelSimulator<P> _scs){
|
175
|
112 IP = ip;
|
248
|
113 //selector = _selector;
|
|
114 scs = _scs;
|
|
115 connectedListS = new LinkedList<ChannelSimulator<P>>();
|
|
116 connectedListC = new LinkedList<ChannelSimulator<P>>();
|
123
|
117 }
|
|
118 } |