#include #include #include #include #include #include #define MAX_COLLISIONS 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; }; // 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 distr(0, k); int main() { bool didNotCollide, end; int collisionArray[MAX_COLLISIONS]; int timeArray[MAX_COLLISIONS]; int transmissions =0; int k = 0; // file with numbers string file = "Project2_part1_rn.txt"; ifstream inputFile; inputFile.open(file.c_str()); bool transmitted; int timeIndex = 0; int modOperand; struct stations st[6]; int timeSlot=0, numFromFile; 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 (timeSlot < MAX_COLLISIONS) { if (timeSlot==0) { cout << dashes << check << timeSlot << endl; // add total collisions to station time 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 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 { //t[2].totalCollisions++; timeArray[2]++; st[i].arrivalTimeSlot = 2; } cout << "\tStation " << i << backing << st[i].arrivalTimeSlot << collisionsStr << st[i].collisions << endl; t[st[i].arrivalTimeSlot].totalCollisions++; } cout << dashes << endl; timeSlot++; } else { 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) { cout << dashes << check << timeSlot << endl; } transmitted==false; 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; cout << stationStr << stationCount << " Success! "; transmissions++; if (transmissions>1 && transmissions != 6) { cout << "\t\t<=== Project1b Solution\n"; } else if (transmissions==6) { cout << "\t\t<=== Project1b Solution\n" << dashes << endl; end = true; break; } else { cout << "\t\t<=== Project1a Solution\n"; } transmitted=true; } else if (st[stationCount].arrivalTimeSlot==timeSlot && timeArray[timeSlot]>1) { // 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 distr(0, k); // int o = distr(generator); st[stationCount].backoff = k; 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; } else t[st[stationCount].arrivalTimeSlot].collided=false; cout << "\tStation " << stationCount << backing << st[stationCount].arrivalTimeSlot << collisionsStr << st[stationCount].collisions << endl; t[st[stationCount].arrivalTimeSlot].totalCollisions++; } 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==5) cout << " NO ATTEMPTS\n"; } } if (end) break; if(stationCount==5) { cout << dashes << endl; timeSlot++; } } if (end) break; /* if (t[timeSlot].totalCollisions<2 && t[timeSlot].) // { cout << dashes << check << timeSlot << endl << stationStr << stationCount << " Success! " << endl << dashes; } */ // if(transmitted==true) cout << dashes << endl; } if (end) break; } } }