1
0
UAHCode/CPE348/Project2/project2PtAB.cpp

141 lines
4.7 KiB
C++
Raw Permalink Normal View History

2022-08-28 21:12:16 +00:00
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <fstream>
#include <stdbool.h>
#define MAX_TIME 100000
#define MAX_STATIONS 6
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;
};
int main()
{
bool end;
int timeArray[MAX_TIME];
int transmissions =0;
int k = 0;
// file with numbers
string file = "Project2_part1_rn.txt";
ifstream inputFile;
inputFile.open(file.c_str());
int modOperand = 0;
struct stations st[6];
int timeSlot=0, numFromFile = 0;
const double timeInterval = 51.2; // 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
struct timeSlot t[MAX_TIME];
//int modOperand = pow(2,st[stationCount].collisions);
// k * timeInterval
// begining at t = 0, all stations collide
for (int i = 0; i < MAX_TIME; i++)
{
//cout << dashes << check << timeSlot << endl;
for (int stationCount = 0; stationCount < MAX_STATIONS; stationCount++)
{
t[stationCount].transmitted=true;
if (stationCount==0)
{
cout << dashes << check << timeSlot << endl;
}
// no station has collided and will transmit
if(timeArray[timeSlot]==1 && t[timeSlot].totalCollisions<=2 && st[stationCount].arrivalTimeSlot==timeSlot)//st[stationCount].arrivalTimeSlot==timeSlot && t[timeSlot].totalCollisions==0 && !t[timeSlot].collided)
{
t[timeSlot].transmitted=true;
transmissions++;
if (transmissions==1)
{
//cout << stationStr << stationCount << " Success! ";
cout << "Station " << stationCount << " has successfully transmitted\n" <<
"in slot number " << timeSlot << " which is " << timeInterval*timeSlot << " micro seconds";
cout << "\t\t<====\tSolution Part1a and Partb \n";
}
else if (transmissions==6)
{
//cout << stationStr << stationCount << " Success! ";
cout << "Station " << stationCount << " has successfully transmitted\n" <<
"in slot number " << timeSlot << " which is " << timeInterval*timeSlot << " micro seconds";
cout << "\t\t<====\tSolution Part1b \n" << dashes << endl;
end = true;
break;
}
else
{
//cout << stationStr << stationCount << " Success! ";
cout << "Station " << stationCount << " has successfully transmitted\n" <<
"in slot number " << timeSlot << " which is " << timeInterval*timeSlot << " micro seconds";
cout << "\t\t<==== \tSolution Part1b\n";
}
}
// Two or more stations have collided
else if (st[stationCount].arrivalTimeSlot==timeSlot || timeSlot==0)
{
// initialize values to zero
if (timeSlot==0)
{
st[stationCount].arrivalTimeSlot = 0;
st[stationCount].backoff = 0;
st[stationCount].collisions = 0;
}
st[stationCount].arrivalTimeSlot++;
st[stationCount].collisions++;
inputFile >> numFromFile;
modOperand = pow(2,st[stationCount].collisions);
k = numFromFile % modOperand;
st[stationCount].backoff = k;
st[stationCount].arrivalTimeSlot = st[stationCount].arrivalTimeSlot+st[stationCount].backoff;
timeArray[st[stationCount].arrivalTimeSlot]++;
cout << "\tStation " << stationCount << backing << st[stationCount].arrivalTimeSlot << collisionsStr << st[stationCount].collisions << endl;
t[st[stationCount].arrivalTimeSlot].totalCollisions++;
}
else
{
if (timeArray[timeSlot]==0)
{
if(stationCount==5)
cout << " NO ATTEMPTS\n";
}
}
if (end) break;
if(stationCount==5)
{
cout << dashes << endl;
timeSlot++;
}
}
if (end) break;
}
}