#include #include #include #include #include #define MAX_TIME 100000 #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 packetsTransmitted; int backoff; timeSlot A; }; int main() { bool end = false; int timeArray[MAX_TIME]; int k = 0; // file with numbers string file = "Project2_part1_rn.txt"; ifstream inputFile; inputFile.open(file.c_str()); int firstTransmissions = 0; 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++) { if (stationCount==0) { //cout << dashes << check << timeSlot << endl; } // station is going to transmit if(timeArray[timeSlot]==1 && st[stationCount].arrivalTimeSlot==timeSlot)//st[stationCount].arrivalTimeSlot==timeSlot && t[timeSlot].totalCollisions==0 && !t[timeSlot].collided) { t[timeSlot].transmitted=true; st[stationCount].packetsTransmitted++; if (st[stationCount].packetsTransmitted==1) { firstTransmissions++; } // the last station transmits if (firstTransmissions == 5 && st[stationCount].packetsTransmitted == 1) { cout << "\n****************************************************\n" << "Part1c: \n"; cout << "****************************************************\n"; cout << check << timeSlot << endl; cout << "Station " << stationCount << " has successfully transmitted\n" << "in slot number " << timeSlot << " which is " << timeInterval*timeSlot << " micro seconds\n"; end = true; break; } st[stationCount].arrivalTimeSlot=st[stationCount].arrivalTimeSlot+3; timeArray[st[stationCount].arrivalTimeSlot]++; //t[st[stationCount].arrivalTimeSlot].totalCollisions++; st[stationCount].collisions=0; // cout << stationStr << stationCount << " Success! \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].packetsTransmitted = 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) { // cout << " NO ATTEMPTS\n"; } } if (end) break; if(stationCount==4) { //cout << dashes << endl; timeSlot++; } } if (end) break; } }