Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
142 lines (118 loc) · 4.97 KB
/
Copy pathmain.cpp
File metadata and controls
142 lines (118 loc) · 4.97 KB
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
//
// main.cpp
// Duffy
//
// Created by Luis Sanabria Russo on 8/24/12.
// Copyright (c) 2012 Luis Sanabria Russo. All rights reserved.
//
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
//#include "defs/constants.h"
#define MIN(a,b) (a < b) ? (a) : (b)
#define MAX_SLOTS 100000
#define MAX_STA 32
#define CWmin 16
#define MAX_STA_BACKOFF_STAGE 5
#define MAX_STICKYNESS 2
using namespace std;
int main(int argc, const char * argv[])
{
int number_sta; //number of stations
int sticky; //stickness as Ken Duffy's proposal
int PRINT;
if(argc >=2) number_sta = atoi(argv[1]);
if(argc >=3) sticky = atoi(argv[2]);
if(argc >=4) PRINT = atoi(argv[3]);
srand(getpid());
//number_sta = 5;
//sticky = 1;
if(PRINT == 1)cout << "Number of stations: " << number_sta << endl;
//--------------------------------------------------//
long int sta_backoff_counter[number_sta];
long int slot_tx[MAX_SLOTS];
int sta_collisions[number_sta]; //collisions per station
int sta_tx[number_sta]; //transmitted packets per station
int sta_stickyness[number_sta]; //it is 1 if the station is allowed to repeat its backoff timer, 0 otherwise
int sta_backoff_stage[number_sta]; //when increases, decreases the chances of collision
//Filling the arrays of the size of number of stations
for(int sta = 0; sta <= number_sta; sta++){
sta_backoff_counter[sta] = 0;
sta_collisions[sta] = 0;
sta_tx[sta] = 0;
sta_stickyness[sta] = 0;
sta_backoff_stage[sta] = 0;
}
//Transmissions
for(int sta = 0; sta <= number_sta; sta++){
sta_backoff_counter[sta] = rand()%CWmin;
if(PRINT == 1)cout << "Station: " << sta << " setted backoff timer to: " << sta_backoff_counter[sta] << endl;
}
for(long int slots = 0; slots <= MAX_SLOTS; slots++){
slot_tx[slots] = 0;
//Checking if there are transmissions in this slot
//Transmitting if the medium is free
for(int sta = 0; sta <= number_sta; sta++){
if(sta_backoff_counter[sta] == 0){
slot_tx[slots]++;
if(PRINT == 1)cout << "**Station: " << sta << " sent a packet at slot: " << slots << endl;
}
}
switch (slot_tx[slots]) {
case 0: //free slot
break;
case 1: //there has been 1 successful transmission
for(int sta = 0; sta <= number_sta; sta++){
if(sta_backoff_counter[sta] == 0) {
sta_tx[sta]++;
if(sticky == 1){ //determines if the station should pick a deterministic backoff timer
sta_backoff_counter[sta] = (CWmin/2) << sta_backoff_stage[sta];
if(sta_stickyness[sta] < MAX_STICKYNESS){
sta_stickyness[sta] += 1; //gains the ability to pick the same backoff on the next slot
}
else{
sta_stickyness[sta] = MAX_STICKYNESS;
}
}
else{
sta_backoff_counter[sta] = rand()%(CWmin << sta_backoff_stage[sta]);
}
}
}
break;
default: //collisions
if(PRINT == 1)cout << "##There are: " << slot_tx[slots] << " packets colliding at slot: " << slots << endl;
for(int sta = 0; sta <= number_sta; sta++){
if(sta_backoff_counter[sta] == 0){
sta_collisions[sta]++;
if(sta_stickyness[sta] > 0){
sta_backoff_counter[sta] = (CWmin/2) << sta_backoff_stage[sta];
sta_stickyness[sta] -= 1; //losses one opportunity to chose a deterministic backoff in the next round if it collides
}
else{
sta_backoff_stage[sta] = MIN(sta_backoff_stage[sta] + 1, MAX_STA_BACKOFF_STAGE);
sta_backoff_counter[sta] = rand()%(CWmin << sta_backoff_stage[sta]);
}
}
}
break;
}
//Decrememting the backoff counters
for(int sta = 0; sta <= number_sta ; sta++){
if(sta_backoff_counter[sta] == 0) continue;
sta_backoff_counter[sta]--;
if(PRINT == 1)cout << "Station: " << sta << " new backoff timer is: " << sta_backoff_counter[sta] << endl;
}
}
//Gathering statistics
int tx_packets = 0;
int overallCollisions = 0;
for(int sta = 0; sta <= number_sta; sta++){
tx_packets += sta_tx[sta];
overallCollisions += sta_collisions[sta];
}
cout << "The overall number of packets sent are: " << tx_packets << endl;
cout << "The overall collisions are: " << overallCollisions << endl;
return 0;
}