390 lines
20 KiB
C++
390 lines
20 KiB
C++
|
#include <iostream>
|
||
|
#include <math.h>
|
||
|
#include <stdio.h>
|
||
|
#include <random>
|
||
|
#include <fstream>
|
||
|
#include <stdbool.h>
|
||
|
|
||
|
#define MAX_COLLISIONS 10000
|
||
|
#define MAX_STATIONS 5
|
||
|
|
||
|
using namespace std;
|
||
|
struct timeSlot {
|
||
|
int timeOfTransmission; // time slot
|
||
|
int totalCollisions;
|
||
|
// stations A;
|
||
|
bool collided, transmitted;
|
||
|
};
|
||
|
|
||
|
struct stations {
|
||
|
int arrivalTimeSlot; // time slot
|
||
|
int collisions;
|
||
|
int backoff;
|
||
|
timeSlot A;
|
||
|
};
|
||
|
|
||
|
|
||
|
// function that generates the
|
||
|
// range of backoff slots
|
||
|
//int rangeOfBackoff(int k);
|
||
|
|
||
|
std::random_device rand_dev;
|
||
|
std::mt19937 generator(rand_dev());
|
||
|
//std::uniform_int_distribution<int> distr(0, k);
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
bool didNotCollide, end;
|
||
|
int collisionArray[MAX_COLLISIONS];
|
||
|
int timeArray[MAX_COLLISIONS];
|
||
|
|
||
|
int k;
|
||
|
// file with numbers
|
||
|
string file = "Project2_part1_rn.txt";
|
||
|
ifstream inputFile;
|
||
|
inputFile.open(file.c_str());
|
||
|
bool transmitted;
|
||
|
int totalTransmitted = 0;
|
||
|
int timeIndex = 0;
|
||
|
int modOperand =0;
|
||
|
struct stations st[6];
|
||
|
int timeSlot=0, numFromFile = 0;
|
||
|
const int timeInterval = 512; // time interval in micro seconds
|
||
|
|
||
|
// strings for precise code
|
||
|
string check="Checking time slot: ";
|
||
|
string collisionsStr = " collisions: ";
|
||
|
string backing = ": backing off to slot ";
|
||
|
string dashes = "--------------------------------------------------\n";
|
||
|
string stationStr = "\tStation ";
|
||
|
// define variables
|
||
|
int n, totalCollisions,i;
|
||
|
struct timeSlot t[MAX_COLLISIONS];
|
||
|
|
||
|
|
||
|
//int modOperand = pow(2,st[i].collisions);
|
||
|
|
||
|
// k * timeInterval
|
||
|
// begining at t = 0, all stations collide
|
||
|
|
||
|
while (1)
|
||
|
{
|
||
|
if (timeSlot==0)
|
||
|
{
|
||
|
cout << dashes << check << timeSlot << endl;
|
||
|
for (int i = 0; i < MAX_STATIONS; i++)
|
||
|
{
|
||
|
t[i].totalCollisions++;
|
||
|
t[i].timeOfTransmission = i;
|
||
|
st[i].arrivalTimeSlot=0; // Arrival Time slot
|
||
|
//k= pow(2,st[i].collisions);
|
||
|
st[i].collisions = 1;
|
||
|
k=1;
|
||
|
uniform_int_distribution<int> distr(0, k);
|
||
|
int o = distr(generator);
|
||
|
st[i].backoff = o;
|
||
|
if (o==0)
|
||
|
{
|
||
|
st[i].arrivalTimeSlot=1;
|
||
|
t[i].timeOfTransmission=1;
|
||
|
timeArray[1]++;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
timeArray[2]++;
|
||
|
st[i].arrivalTimeSlot = 2;
|
||
|
}
|
||
|
cout << "\tStation " << i << backing << st[i].arrivalTimeSlot << collisionsStr << st[i].collisions << endl;
|
||
|
}
|
||
|
|
||
|
cout << dashes << endl;
|
||
|
timeSlot++;
|
||
|
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
for (int comp = 0; comp < 5; comp++)
|
||
|
{
|
||
|
if (st[comp].arrivalTimeSlot==st[comp+1].arrivalTimeSlot && st[comp].arrivalTimeSlot>=timeSlot && st[comp+1].arrivalTimeSlot>=timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+1].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
else if (st[comp].arrivalTimeSlot==st[comp+2].arrivalTimeSlot && st[comp].arrivalTimeSlot>=timeSlot && st[comp+2].arrivalTimeSlot>=timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+2].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
else if (st[comp].arrivalTimeSlot==st[comp+3].arrivalTimeSlot && st[comp].arrivalTimeSlot>timeSlot && st[comp+3].arrivalTimeSlot>timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+3].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
else if (st[comp].arrivalTimeSlot==st[comp+4].arrivalTimeSlot && st[comp].arrivalTimeSlot>timeSlot && st[comp+4].arrivalTimeSlot>timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+4].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
else if (st[comp].arrivalTimeSlot==st[comp+5].arrivalTimeSlot && st[comp].arrivalTimeSlot>timeSlot && st[comp+5].arrivalTimeSlot>timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+5].arrivalTimeSlot].collided=true;
|
||
|
// timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
else if (t[st[comp].arrivalTimeSlot].collided=true || st[comp].arrivalTimeSlot>=timeSlot){
|
||
|
//cout << comp << "\t" << st[comp].arrivalTimeSlot << "\tcontinue" << endl;
|
||
|
|
||
|
continue;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
|
||
|
t[st[comp].arrivalTimeSlot].collided=false;
|
||
|
//cout << comp << "\t" << st[comp].arrivalTimeSlot << "\tfalse" << endl;
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
for (int i = 0; i < MAX_COLLISIONS; i++)
|
||
|
{
|
||
|
//cout << dashes << check << timeSlot << endl;
|
||
|
for (int stationCount = 0; stationCount < MAX_STATIONS; stationCount++)
|
||
|
{
|
||
|
t[stationCount].transmitted=true;
|
||
|
if (stationCount==0)
|
||
|
{
|
||
|
for (int comp = 0; comp < MAX_STATIONS; comp++)
|
||
|
{
|
||
|
if (st[comp].arrivalTimeSlot==st[comp+1].arrivalTimeSlot && st[comp].arrivalTimeSlot>=timeSlot && st[comp+1].arrivalTimeSlot>=timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+1].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
if (st[comp].arrivalTimeSlot==st[comp+2].arrivalTimeSlot && st[comp].arrivalTimeSlot>=timeSlot && st[comp+2].arrivalTimeSlot>=timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+2].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
if (st[comp].arrivalTimeSlot==st[comp+3].arrivalTimeSlot && st[comp].arrivalTimeSlot>timeSlot && st[comp+3].arrivalTimeSlot>timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+3].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
if (st[comp].arrivalTimeSlot==st[comp+4].arrivalTimeSlot && st[comp].arrivalTimeSlot>timeSlot && st[comp+4].arrivalTimeSlot>timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+4].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
// timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
if (st[comp].arrivalTimeSlot==st[comp+5].arrivalTimeSlot && st[comp].arrivalTimeSlot>timeSlot && st[comp+5].arrivalTimeSlot>timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+5].arrivalTimeSlot].collided=true;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
if (t[st[comp].arrivalTimeSlot].collided=true || st[comp].arrivalTimeSlot>timeSlot){
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\tcontinue" << endl;
|
||
|
|
||
|
continue;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
|
||
|
t[st[comp].arrivalTimeSlot].collided=false;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\tfalse" << endl;
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
for (int i = 0; i < 6; i++)
|
||
|
{
|
||
|
// cout << "TimeArray\t"<<i<<"\t" << timeArray[i] << endl;
|
||
|
}
|
||
|
|
||
|
|
||
|
//stationCount++;
|
||
|
cout << dashes << check << timeSlot << endl;
|
||
|
}
|
||
|
transmitted==false;
|
||
|
if(timeArray[timeSlot]==1 && t[timeSlot].totalCollisions<=1 && st[stationCount].arrivalTimeSlot==timeSlot)//st[stationCount].arrivalTimeSlot==timeSlot && t[timeSlot].totalCollisions==0 && !t[timeSlot].collided)
|
||
|
{
|
||
|
t[timeSlot].transmitted=true;
|
||
|
cout << stationStr << stationCount << " Success! " << endl << dashes << endl;
|
||
|
st[stationCount].arrivalTimeSlot=+2;
|
||
|
st[stationCount].collisions=0;
|
||
|
st[stationCount].A.transmitted==true;
|
||
|
totalTransmitted++;
|
||
|
transmitted=true;
|
||
|
}
|
||
|
else if (st[stationCount].arrivalTimeSlot==timeSlot && t[timeSlot].collided)
|
||
|
{
|
||
|
// if (t[timeSlot].totalCollisions==1 && timeArray[timeSlot]==1 && t[timeSlot].totalCollisions==0)
|
||
|
// {
|
||
|
// // t[timeSlot].transmitted=true;
|
||
|
// cout << stationStr << stationCount << " Success! " << endl;
|
||
|
// cout << "End of program\n";
|
||
|
// end = true;
|
||
|
// }
|
||
|
if (end) break;
|
||
|
|
||
|
st[stationCount].arrivalTimeSlot++;
|
||
|
st[stationCount].collisions++;
|
||
|
inputFile >> numFromFile;
|
||
|
//array[arrayIndex] = numFromFile;
|
||
|
//arrayIndex++;
|
||
|
modOperand = pow(2,st[stationCount].collisions);
|
||
|
modOperand--;
|
||
|
k = numFromFile % modOperand;
|
||
|
uniform_int_distribution<int> distr(0, k);
|
||
|
int o = distr(generator);
|
||
|
st[stationCount].backoff = o;
|
||
|
st[stationCount].arrivalTimeSlot = st[stationCount].arrivalTimeSlot+st[stationCount].backoff;
|
||
|
timeArray[st[stationCount].arrivalTimeSlot]++;
|
||
|
if (timeArray[st[stationCount].arrivalTimeSlot]>1)
|
||
|
{
|
||
|
t[st[stationCount].arrivalTimeSlot].collided=false;
|
||
|
|
||
|
}
|
||
|
st[stationCount].A.transmitted=false;
|
||
|
cout << "\tStation " << stationCount << backing << st[stationCount].arrivalTimeSlot << collisionsStr << st[stationCount].collisions << endl;
|
||
|
if (stationCount==MAX_STATIONS-1 && st[MAX_STATIONS-1].arrivalTimeSlot==timeSlot)
|
||
|
{
|
||
|
cout << dashes << endl;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
else if (t[timeSlot].totalCollisions==1 && t[timeSlot].transmitted && st[stationCount].arrivalTimeSlot==timeSlot)
|
||
|
{
|
||
|
// //t[timeSlot].transmitted=true;
|
||
|
// cout << stationStr << stationCount << " Success! " << endl << dashes << endl;
|
||
|
// cout << "End of program\n";
|
||
|
// end = true;
|
||
|
}
|
||
|
else //if (st[stationCount].arrivalTimeSlot != timeSlot && t[timeSlot].totalCollisions<1 && !t[timeSlot].transmitted)
|
||
|
{
|
||
|
if (timeArray[timeSlot]==0)
|
||
|
{
|
||
|
if(stationCount==4)
|
||
|
cout << " NO ATTEMPTS\n";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
if (end) break;
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
if(stationCount == MAX_STATIONS-1)
|
||
|
{
|
||
|
//if(st[stationCount].A.transmitted==false) cout << dashes << endl;
|
||
|
timeSlot++;
|
||
|
for (int comp = 0; comp < 5; comp++)
|
||
|
{
|
||
|
if (st[comp].arrivalTimeSlot==st[comp+1].arrivalTimeSlot && st[comp].arrivalTimeSlot>=timeSlot && st[comp+1].arrivalTimeSlot>=timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+1].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
if (st[comp].arrivalTimeSlot==st[comp+2].arrivalTimeSlot && st[comp].arrivalTimeSlot>=timeSlot && st[comp+2].arrivalTimeSlot>=timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+2].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
if (st[comp].arrivalTimeSlot==st[comp+3].arrivalTimeSlot && st[comp].arrivalTimeSlot>timeSlot && st[comp+3].arrivalTimeSlot>timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+3].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
if (st[comp].arrivalTimeSlot==st[comp+4].arrivalTimeSlot && st[comp].arrivalTimeSlot>timeSlot && st[comp+4].arrivalTimeSlot>timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+4].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
// timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
if (st[comp].arrivalTimeSlot==st[comp+5].arrivalTimeSlot && st[comp].arrivalTimeSlot>timeSlot && st[comp+5].arrivalTimeSlot>timeSlot)
|
||
|
{
|
||
|
t[st[comp].arrivalTimeSlot].collided=true;
|
||
|
t[st[comp+5].arrivalTimeSlot].collided=true;
|
||
|
//timeArray[st[comp].arrivalTimeSlot]++;
|
||
|
t[st[comp].arrivalTimeSlot].totalCollisions++;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\ttrue" << endl;
|
||
|
}
|
||
|
if ((t[st[comp].arrivalTimeSlot].collided=true || st[comp].arrivalTimeSlot>=timeSlot) && t[st[comp].arrivalTimeSlot].totalCollisions>1)
|
||
|
{
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\tcontinue" << endl;
|
||
|
|
||
|
continue;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
|
||
|
t[st[comp].arrivalTimeSlot].collided=false;
|
||
|
// cout << comp << "\t" << st[comp].arrivalTimeSlot << "\tfalse" << endl;
|
||
|
|
||
|
}
|
||
|
}
|
||
|
if (st[MAX_STATIONS-1].A.transmitted==false)
|
||
|
{
|
||
|
cout << dashes <<endl;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
if (end) break;
|
||
|
|
||
|
/* if (t[timeSlot].totalCollisions<2 && t[timeSlot].)
|
||
|
// {
|
||
|
cout << dashes << check << timeSlot << endl << stationStr << stationCount << " Success! " << endl
|
||
|
<< dashes;
|
||
|
} */
|
||
|
//if(transmitted==false) cout << dashes << endl;
|
||
|
|
||
|
}
|
||
|
}
|
||
|
if (end) break;
|
||
|
|
||
|
}
|
||
|
}
|