-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSMA_Simulation.cs
402 lines (347 loc) · 14 KB
/
CSMA_Simulation.cs
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using System;
using System.Collections.Generic;
using System.IO;
namespace CSMA_Simulation
{
public enum NodeState { Generate, Sense, Transmit, Backoff }
public enum MediumState { Idle, Busy }
public class CommNode
{
public static Random rand = new Random(); // For random number generation.
public CommNode()
{
_state = NodeState.Generate;
_generate_timer = _PickGentime();
_transmit_timer = 0L;
_backoff_timer = 0L;
_curr_packet_delay = 0L;
if (CommManager.Instance.scheme == 2) _cw = 2;
else _cw = CommManager.Instance.CW;
_timeout_added = false;
}
private NodeState _state;
private long _generate_timer;
private long _transmit_timer;
private long _backoff_timer;
private long _curr_packet_delay;
private long _cw;
private bool _timeout_added; // Only for CSMA w/o CD
private long _PickGentime()
{
double ln_x = Math.Log(rand.NextDouble());
long gentime;
try
{
gentime = Convert.ToInt64(-10000.0 * ln_x);
}
catch (OverflowException _)
{
gentime = Int64.MaxValue;
}
if (gentime == 0L) gentime = 1L;
return gentime;
}
public void ProcessNode()
{
CommManager manager = CommManager.Instance;
//------------------------------- State Transition --------------------------------------
switch (_state)
{
case NodeState.Generate:
if (_generate_timer == 0L) // 패킷 생성 후 전송 시도
{
_state = NodeState.Sense;
_curr_packet_delay = 0L; // 패킷 전송에 걸린 시간 측정을 시작한다
}
break;
case NodeState.Transmit:
if(manager.scheme == 0 && manager.collision_trigger == true) // CSMA
{
if(_timeout_added == false)
{
_transmit_timer += (_cw / 16L) * 50L; // (timeout timer) = (CW / 16) * (slot time)
_timeout_added = true;
}
else if(_transmit_timer == 0L) // ACK를 기다리다 timeout된 상황
{
_state = NodeState.Backoff;
_backoff_timer = (rand.Next(1, Convert.ToInt32(_cw) + 1) * 50);
manager.n_collided_request++;
manager.next_collision_trigger = false;
manager.medium_next_state = MediumState.Idle;
_timeout_added = false;
}
}
else if(manager.collision_trigger == true) // CSMA/CD
{
_transmit_timer = 0L;
_state = NodeState.Backoff;
_backoff_timer = (rand.Next(1, Convert.ToInt32(_cw) + 1) * 50);
if (manager.scheme == 2 && _cw < 1024L) // CSMA/CD with binary exponential backoff
_cw = 2L * _cw;
manager.n_collided_request++;
manager.next_collision_trigger = false;
manager.medium_next_state = MediumState.Idle;
}
else if (_transmit_timer == 0L) // 성공적인 전송
{
_state = NodeState.Generate;
_generate_timer = _PickGentime();
manager.sum_packet_delay += _curr_packet_delay;
manager.n_successful_packet++;
if (manager.scheme == 2)
_cw = 2L;
_curr_packet_delay = 0L;
manager.medium_next_state = MediumState.Idle;
}
break;
case NodeState.Backoff:
if (_backoff_timer == 0L) // Backoff 끝 -> 재전송 시도
{
_state = NodeState.Sense;
}
break;
}
if(_state == NodeState.Sense && manager.medium_state == MediumState.Idle) // 매체 Idle이라 전송 진행
{
_state = NodeState.Transmit;
_transmit_timer = 800L;
manager.n_request++;
if (manager.next_collision_trigger == false && manager.medium_next_state == MediumState.Busy)
manager.next_collision_trigger = true;
manager.medium_next_state = MediumState.Busy;
}
//------------------------------- Timer Processing --------------------------------------
switch (_state)
{
case NodeState.Generate:
_generate_timer--;
break;
case NodeState.Sense:
_curr_packet_delay++;
break;
case NodeState.Transmit:
_transmit_timer--;
_curr_packet_delay++;
break;
case NodeState.Backoff:
_backoff_timer--;
_curr_packet_delay++;
break;
}
}
}
public class CommManager
{
private CommManager()
{
medium_state = MediumState.Idle;
_node_list = new List<CommNode>();
}
private static CommManager _instance;
public static CommManager Instance
{
get
{
if(_instance == null)
{
_instance = new CommManager();
}
return _instance;
}
}
// Needed fields
private List<CommNode> _node_list;
public StreamWriter output_file = null;
public MediumState medium_state;
public MediumState medium_next_state;
public bool collision_trigger;
public bool next_collision_trigger;
// parameters
public int scheme;
public long CW;
public long node_num;
public long process_time;
// simulation result variables
public long n_successful_packet;
public long sum_packet_delay;
public long n_request;
public long n_collided_request;
public long elapsed_time;
// mesurment properties
public double Throughput
{
get
{
double elapsed_time_sec = elapsed_time / 1000000.0;
return n_successful_packet / elapsed_time_sec;
}
}
public double MeanPacketDelay
{
get
{
return sum_packet_delay / Convert.ToDouble(n_successful_packet);
}
}
public double CollisionProb
{
get
{
return n_collided_request / Convert.ToDouble(n_request);
}
}
public void PrintCurrentState(StreamWriter file=null)
{
if (file == null) // for debugging, pretty-print to console.
{
Console.WriteLine(" Result at {0} ms:", elapsed_time / 1000.0);
Console.WriteLine(" Throughput: {0,10} packet/sec", Throughput);
Console.WriteLine(" Mean Packet Delay: {0,10} us", MeanPacketDelay);
Console.WriteLine(" Collision Probablity: {0,10} %", CollisionProb * 100.0);
Console.WriteLine("|--------------------------------------------------------------------------|");
}
else
{
file.WriteLine("{0} {1} {2}", Throughput, MeanPacketDelay, CollisionProb);
}
}
public void ResetManager()
{
n_successful_packet = 0L;
n_request = 0L;
n_collided_request = 0L;
sum_packet_delay = 0L;
process_time = 0L;
elapsed_time = 0L;
medium_state = MediumState.Idle;
medium_next_state = MediumState.Idle;
collision_trigger = false;
next_collision_trigger = false;
_node_list.Clear();
}
public void ChangeScheme(int new_scheme)
{
scheme = new_scheme;
}
public void ChangeParameters(long new_cw, long new_node_num)
{
CW = new_cw;
node_num = new_node_num;
}
public void Simulate(long period_sec)
{
ResetManager();
process_time = period_sec * 1000000L;
for(long i = 0L; i < node_num; i++)
{
_node_list.Add(new CommNode());
}
for(long t = 0L; t < process_time; t++)
{
medium_next_state = medium_state;
next_collision_trigger = collision_trigger;
// 노드별 시간 진행 및 스테이트 조절
foreach(CommNode node in _node_list)
{
node.ProcessNode();
}
// 매체 상태 조절
medium_state = medium_next_state;
collision_trigger = next_collision_trigger;
elapsed_time++;
// 상태 출력
/*
if (elapsed_time % 100000L == 0)
PrintCurrentState(output_file);
*/
}
PrintCurrentState(output_file);
}
}
public class CSMA_Simulation
{
static void Main(string[] args)
{
CommManager m = CommManager.Instance;
m.output_file = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "simulation_output.txt");
int attempt = 10;
long sec = 100L;
if (m.output_file == null)
{
Console.WriteLine("File Stream opening Failed!");
return;
}
Console.WriteLine("Simulation Start...");
m.ChangeScheme(0);
m.output_file.WriteLine("///////////////////// Scheme: CSMA /////////////////////////////");
Console.WriteLine("///////////////////// Scheme: CSMA /////////////////////////////");
for (long CW = 32L; CW < 129L; CW = CW * 2L)
{
m.ChangeParameters(CW, 15L);
m.output_file.WriteLine("----- CW: {0}, N: {1} -----", CW, 15L);
Console.WriteLine("----- CW: {0}, N: {1} -----", CW, 15L);
for (int i = 0; i < attempt; i++)
{
m.Simulate(sec);
Console.WriteLine("{0}-th attempt done.", i + 1);
}
}
for (long N = 5L; N < 30L; N += 5L)
{
if (N == 15L) continue;
m.ChangeParameters(64L, N);
m.output_file.WriteLine("----- CW: {0}, N: {1} -----", 64L, N);
Console.WriteLine("----- CW: {0}, N: {1} -----", 64L, N);
for (int i = 0; i < attempt; i++)
{
m.Simulate(sec);
Console.WriteLine("{0}-th attempt done.", i + 1);
}
}
m.ChangeScheme(1);
m.output_file.WriteLine("///////////////////// Scheme: CSMA/CD //////////////////////////");
Console.WriteLine("///////////////////// Scheme: CSMA/CD //////////////////////////");
for (long CW = 32L; CW < 129L; CW = CW * 2L)
{
m.ChangeParameters(CW, 15L);
m.output_file.WriteLine("----- CW: {0}, N: {1} -----", CW, 15L);
Console.WriteLine("----- CW: {0}, N: {1} -----", CW, 15L);
for (int i = 0; i < attempt; i++)
{
m.Simulate(sec);
Console.WriteLine("{0}-th attempt done.", i + 1);
}
}
for (long N = 5L; N < 30L; N += 5L)
{
if (N == 15L) continue;
m.ChangeParameters(64L, N);
m.output_file.WriteLine("----- CW: {0}, N: {1} -----", 64L, N);
Console.WriteLine("----- CW: {0}, N: {1} -----", 64L, N);
for (int i = 0; i < attempt; i++)
{
m.Simulate(sec);
Console.WriteLine("{0}-th attempt done.", i + 1);
}
}
m.ChangeScheme(2);
m.output_file.WriteLine("///////////////////// Scheme: CSMA/CD binary backoff ///////////");
Console.WriteLine("///////////////////// Scheme: CSMA/CD binary backoff ///////////");
for (long N = 20L; N < 30L; N += 5L)
{
m.ChangeParameters(64L, N);
m.output_file.WriteLine("--------- N: {0} ---------", N);
Console.WriteLine("--------- N: {0} ---------", N);
for (int i = 0; i < attempt; i++)
{
m.Simulate(sec);
Console.WriteLine("{0}-th attempt done.", i + 1);
}
}
Console.WriteLine("\tDone.");
m.output_file.WriteLine("\tDone.");
m.output_file.Close();
}
}
}