comparison rep/simulator/NetworkSimulator.java @ 123:5b1a0574b406 add-simulator

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