-
Notifications
You must be signed in to change notification settings - Fork 1
/
Simulator.java
executable file
·214 lines (166 loc) · 5.79 KB
/
Simulator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import java.io.*;
import java.util.*;
public class Simulator {
static String runName = " population based load";
// sizes
static int maxPath = 9;
static int maxLevel = 3;
static int maxOrder = 9;
static int maxDelay = 255;
static double maxClock = 300.0;
// xmit scheduling parameters
static double waitTime = 0.50;
static double xmitTime = 0.13;
// node buffer limit
static int queueLimit = 95;
// load specifications
static double lastArrival = 240.0;
static double interArrival = 20.0; // typ 30.0
static double arrivalTime = 2.0;
static boolean dynamic = true; // load dependent routing
static boolean linked = true; // trans-con link
static boolean popBased = true; // population based routing
// major state
static double clock = 0.0; // simulated time
static int meanPop = 0; // mean population
static int totalPop = 0; // total population
static int eventCount = 0; // sample statistics
static int msgCount = 0;
static int updateCount = 0;
// tracing
static class Trace {
double time;
String label;
Object a, b;
public String toString() {
return
td(w(time, 8)) +
td(label) +
td(a.toString()) +
td(b.toString());
}
String td (String d) {
return "<td valign=top>" + d + "</td>";
}
}
static Trace traces[] = new Trace [200];
static int nextTrace = 0;
static void halt (String err) {
trace("Halt", err);
printTrace();
System.err.println(err + " (see trace)");
System.exit(-1);
}
static void printTrace() {
PrintStream out;
try {out = new PrintStream(new FileOutputStream("output/trace.html"));}
catch (FileNotFoundException e) {out=System.out;}
printTrace(new PrintWriter(out));
}
static void printTrace(PrintWriter out) {
out.println("<table CELLSPACING=0 CELLPADDING=2>");
double clock=0;
int tick=0;
for (int i=nextTrace+1; (i%traces.length)!=nextTrace; i=(i+1)%traces.length) {
Trace t = traces[i];
if (t==null) continue;
if (t.time != clock) {
clock = t.time;
tick++;
}
String color = tick%2==0 ? "#cfffcf" : "#afffaf";
out.println("<tr bgcolor=" + color + ">" + t + "</tr>");
}
out.println("</table>");
out.flush();
}
static void trace (String label, Object a, Object b) {
Trace newTrace = traces[nextTrace];
if (newTrace == null) {
newTrace = traces[nextTrace] = new Trace();
}
nextTrace=(nextTrace+1)%traces.length;
newTrace.time = clock;
newTrace.label = label;
newTrace.a = a;
newTrace.b = b;
}
static void trace (String label, Object a) {
trace(label, a, "");
}
static void trace (String label) {
trace(label, "", "");
}
// event queue
static EventBlock thisEvent; // event queue (holds blocks)
static SortedMap eventQueue = new TreeMap();
static void queue (EventBlock entry) {
trace("queue", entry);
double fuzz = rand.nextDouble()/1000.0; // avoid collisions
eventQueue.put(new Double(entry.time + fuzz), entry);
}
static EventBlock dequeue() {
Double key = (Double)(eventQueue.firstKey());
return (EventBlock)(eventQueue.remove(key));
}
static double queueTime() {
Double key = (Double)(eventQueue.firstKey());
EventBlock event = (EventBlock)(eventQueue.get(key));
return event.time;
}
// reports
static List reports = new LinkedList();
void report() {
for (Iterator iterator = reports.iterator(); iterator.hasNext();) {
Simulator s = (Simulator) iterator.next();
s.report();
}
}
static InputStream in;
static PrintStream out;
static PrintStream plotfil; // trace output for plot
static Station station[]; // all stations
static Graph retrys = new Graph("retransmissions", 2.0);
static Graph queueTime = new Graph("queueing delay", 5.0);
static Graph transitTime = new Graph("transit time", 50.0);
static Graph hops = new Graph("hop count", 50);
static Scatter queuing = new Scatter("elapsed time", maxClock, "message queue length", 100.0);
static Scatter backlog = new Scatter("elapsed time", maxClock, "messages in transit", 300.0);
static Scatter routing = new Scatter("sample time", maxClock, "routing broadcasts", 500.0);
static Scatter delivery = new Scatter("start time", maxClock, "transit time", 50.0);
static PrintStream output(String name) {
String path = "output/" + name + ".txt";
PrintStream out;
try {out = new PrintStream(new FileOutputStream(path));}
catch (FileNotFoundException e) {throw new Error("can't open " + path);}
return out;
}
// utilities
static Random rand = new Random();
static double uniform () {
return rand.nextDouble();
}
static double exponential () {
return - Math.log(uniform());
}
static double normal () {
double sum=0;
for (int i = 0; i < 12; i++) {
sum += uniform();
}
return sum - 6.0;
}
static int limit (double r, int max) {
int i = (int)Math.round(r);
return i<0 ? 0 : i>=max ? max-1 : i;
}
static String w(String s, int w) {
return (s+" ").substring(0, w-1)+" ";
}
static String w(double d, int w) {
return w(Double.toString(d), w);
}
static String w(int i, int w) {
return w(Integer.toString(i), w);
}
}