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
|
314
|
8 public class NetworkSimulator {
|
|
9 // we don't use <P> because we need singleton.
|
|
10 public static NetworkSimulator ns = new NetworkSimulator();
|
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
|
314
|
16 public static NetworkSimulator singleton() {
|
|
17 // singleton pattern may used here, but it has a little cost.
|
|
18 return ns;
|
123
|
19 }
|
|
20
|
|
21 int logLevel=5;
|
|
22 /** Listening Servers. */
|
314
|
23 private LinkedList<ServerData> serverList;
|
123
|
24
|
|
25 /** Constructor. */
|
|
26 public NetworkSimulator(){
|
314
|
27 serverList = new LinkedList<ServerData>();
|
177
|
28 logger = REPLogger.singleton();
|
|
29 logger.writeLog("construct Networksimulator", 1);
|
284
|
30 // printAllState();
|
123
|
31 }
|
|
32
|
|
33 /* */
|
314
|
34 synchronized public void listen(InetSocketAddress ip, ServerChannelSimulator<?> scs) {
|
|
35 serverList.add(new ServerData(ip, scs));
|
177
|
36 logger.writeLog("listen", 1);
|
123
|
37 printAllState();
|
|
38 }
|
|
39
|
314
|
40 synchronized public boolean connect(InetSocketAddress ip, ChannelSimulator<?> CHclient) {
|
248
|
41 logger.writeLog("connecting..", 1);
|
314
|
42 for (ServerData sd0: serverList){
|
281
|
43 // ANY address (0.0.0.0/0.0.0.0) should be considered.
|
282
|
44 if (sd0.IP.getAddress().isAnyLocalAddress()) {
|
284
|
45 if (sd0.IP.getPort() != ip.getPort()) continue;
|
|
46 // we have to check, ip is really reachable to sd0 server,
|
|
47 // but this simulator has no notion of host. To distinguish,
|
|
48 // use different port address.
|
282
|
49 } else if (!sd0.IP.equals(ip)) continue;
|
123
|
50
|
314
|
51 //ChannelSimulator<?> CHserver = new ChannelSimulator<?>();
|
|
52 ChannelSimulator<?> CHserver = CHclient.newChannel();
|
|
53 CHserver.setOtherEnd1(CHclient);
|
|
54 CHclient.setOtherEnd1(CHserver);
|
248
|
55
|
|
56 sd0.connectedListS.add(CHserver);
|
|
57 sd0.connectedListC.add(CHclient);
|
|
58 sd0.scs.enQ(CHserver);
|
|
59
|
|
60 logger.writeLog("connected", 1);
|
308
|
61 //printAllState();
|
248
|
62 return true;
|
123
|
63 }
|
|
64 return false;
|
|
65 }
|
|
66
|
|
67 /** for DEBUG methods. */
|
282
|
68 void printAllState(){
|
|
69 String log = "NetworkSimulator State:";
|
314
|
70 for (ServerData sd: serverList){
|
282
|
71 log += "\tSessionManager(ip="+sd.IP.toString()+"): ";
|
|
72 log += channelList(sd.connectedListC);
|
248
|
73 }
|
282
|
74 logger.writeLog(log);
|
123
|
75 }
|
282
|
76
|
314
|
77 private String channelList(LinkedList<ChannelSimulator<?>> list){
|
248
|
78 String tmp = "";
|
314
|
79 for (ChannelSimulator<?> ch: list){
|
248
|
80 tmp += ch.toString()+" ";
|
|
81 }
|
282
|
82 return "\t"+tmp;
|
248
|
83 }
|
156
|
84
|
|
85
|
|
86
|
314
|
87 public synchronized int nslookup(SocketAddress semaIP) {
|
156
|
88 Integer ip;
|
|
89 if ((ip=namedb.get(semaIP))==null) {
|
|
90 namedb.put(semaIP, (ip=ipcount++));
|
|
91 }
|
|
92 return ip;
|
|
93 }
|
123
|
94
|
|
95
|
|
96 }
|
|
97
|
314
|
98 class ServerData {
|
175
|
99 //int virtualIP;
|
282
|
100 InetSocketAddress IP;
|
248
|
101 //SelectorSimulator<P> selector;
|
314
|
102 ServerChannelSimulator<?> scs;
|
|
103 LinkedList<ChannelSimulator<?>> connectedListS;
|
|
104 LinkedList<ChannelSimulator<?>> connectedListC;
|
123
|
105
|
314
|
106 ServerData(InetSocketAddress ip, ServerChannelSimulator<?> _scs){
|
175
|
107 IP = ip;
|
248
|
108 //selector = _selector;
|
|
109 scs = _scs;
|
314
|
110 connectedListS = new LinkedList<ChannelSimulator<?>>();
|
|
111 connectedListC = new LinkedList<ChannelSimulator<?>>();
|
123
|
112 }
|
|
113 } |