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