annotate src/pathfinder/mergetest/channels/NetworkSimulator.java @ 161:66e9cebce3fa first_channels

move from pathfinder.simulator.channels.*
author kent
date Tue, 26 Aug 2008 19:46:37 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
161
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
1 package pathfinder.mergetest.channels;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
2
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
3 import java.util.LinkedList;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
4
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
5
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
6 public class NetworkSimulator<P> {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
7 public static NetworkSimulator<?> ns;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
8 synchronized public static <T> NetworkSimulator<T> singleton(){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
9 if (ns==null)
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
10 ns = new NetworkSimulator<T>();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
11 return (NetworkSimulator<T>) ns;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
12 // NetworkSimulator<Obj> ns = NetworkSimulator.singleton(new NetworkSimulator<Obj>());
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
13 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
14
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
15 int logLevel=5;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
16 /** Listening Servers. */
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
17 private LinkedList<ServerData<P>> serverList;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
18
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
19 /** Constructor. */
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
20 public NetworkSimulator(){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
21 serverList = new LinkedList<ServerData<P>>();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
22 writeLog("construct Networksimulator", 1);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
23 printAllState();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
24 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
25
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
26
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
27
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
28 /* */
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
29 synchronized public void listen(int ip, SelectorSimulator<P> selector) {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
30 serverList.add(new ServerData<P>(ip, selector));
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
31 writeLog(Thread.currentThread(), "listen", 1);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
32 printAllState();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
33 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
34
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
35 synchronized public ChannelSimulator<P> accept(int ip) {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
36 for (ServerData<P> sd: serverList){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
37 if (sd.virtualIP!=ip) continue;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
38 writeLog(Thread.currentThread(), "accepting..", 1);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
39
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
40 ChannelSimulator<P> serverCH = sd.acceptWaitingList.remove();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
41 sd.establishedList.add(serverCH);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
42
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
43 writeLog(Thread.currentThread(), "accepted", 1);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
44 printAllState();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
45 return serverCH;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
46 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
47 return null;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
48 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
49 synchronized public boolean canAccept(int ip){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
50 for (ServerData<P> sd: serverList){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
51 if (sd.virtualIP!=ip) continue;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
52 return !sd.acceptWaitingList.isEmpty();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
53 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
54 return false;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
55 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
56
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
57 public boolean connect(int ip, ChannelSimulator<P> clientCH) {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
58 ServerData<P> sd = null;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
59 writeLog(Thread.currentThread(), "connecting..", 1);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
60 synchronized (this){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
61 for (ServerData<P> sd0: serverList){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
62 if (sd0.virtualIP!=ip) continue;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
63
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
64 sd = sd0;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
65 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
66 if (sd==null) return false;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
67
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
68 //ChannelSimulator<P> channel = new ChannelSimulator<P>(sd.selector);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
69 clientCH.createReadQ();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
70 clientCH.createWriteQ();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
71 clientCH.setWriteSelector(sd.selector);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
72
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
73 ChannelSimulator<P> serverCH = clientCH.createConjugatedChannel();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
74 sd.acceptWaitingList.add(serverCH);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
75 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
76
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
77 synchronized (sd.selector) {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
78 sd.selector.notifyAll();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
79 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
80 writeLog(Thread.currentThread(), "connected", 1);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
81 printAllState();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
82 return true;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
83 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
84
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
85 /** for DEBUG methods. */
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
86 synchronized void printAllState(){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
87 writeLog("NetworkSimulator State:");
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
88 for (ServerData<P> sd: serverList){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
89 writeLog("\tSessionManager(ip="+sd.virtualIP+"): ");
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
90 writeLog("\tacceptWaitingList="+sd.acceptWaitingList.size());
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
91 writeLog("\testablishedList="+sd.establishedList.size());
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
92 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
93 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
94
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
95 /** simulation log command */
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
96 synchronized public void writeLog(String log, int level){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
97 if ( level<=logLevel )
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
98 System.out.println(log);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
99 System.out.flush();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
100 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
101 public void writeLog(String log){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
102 writeLog(log, 0);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
103 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
104 public void writeLog(Thread thr, String log, int level){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
105 writeLog(thr.getName()+": "+log, level);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
106 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
107 public void setLogLevel(int logLevel) {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
108 this.logLevel = logLevel;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
109 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
110
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
111
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
112 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
113
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
114 class ServerData<P> {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
115 int virtualIP;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
116 SelectorSimulator<P> selector;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
117 LinkedList<ChannelSimulator<P>> acceptWaitingList;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
118 LinkedList<ChannelSimulator<P>> establishedList;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
119
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
120 ServerData(int ip, SelectorSimulator<P> _selector){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
121 virtualIP = ip;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
122 selector = _selector;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
123 acceptWaitingList = new LinkedList<ChannelSimulator<P>>();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
124 establishedList = new LinkedList<ChannelSimulator<P>>();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
125 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
126 }