CSMA/CD Implementation in C++

Background

CSMACD stands for carrier Sense, Multiple Access, and Collision Detection. We use a simplified version where the protocol: 
  1. If a station is ready to send a message (according to a random number R1) it senses the carrier. The station can only sense the part of the carrier that is directly attached to the station. a. If the carrier is free the station start transmission of a random message (length R2 ticks)
    1. Otherwise, the station delays sensing the carrier by a random (R3) number of ticks.
  2. If a station detects collision it stops the current transmission and delays sensing the carrier by a random number of ticks (R4)
  3. As long as a station is attempting to send a message it does not generate new messages.
  4. The number and location of stations on the carrier, the carrier length in meters (R5), and the propagation time of a message through the carrier in tics/meter are given. 
  5. We assume that a tic is a time that it takes for a message to propagate a distance of one meter in the carrier. In other words, a message propagates in the carrier at a speed of one meter per tick.

Introduction

CSMACD scenario with 3 stations {S0, S1, S2} assuming that S0 and S2 are at the two ends of the carrier and S1 is at a distance of 8 meters from S0,
• A station is ready to send a message if R1>0.9
• R2∈[1,3]; R3∈[2,16]; R4∈[1,24]; R5∈[16,24];

Simulation Pseudo Code

/* Generate R5 */
For (I = 0; I < 1500; I++) { /* Loop on tics */
For (J = 0; J < 3; J++) { /* Loop on station */
/* Check the status of station(J) - Generate and check (as needed) R1, R2, R3, R4 */
/* Update the status of station(J) */
/* Update the status of the carrier */ }}

Carrier Sense multiple access with collision detection in C++

Main.cpp

#include <iostream>
#include "Station.h"
#include <time.h>
#include <stdlib.h>
#include <windows.h>
using namespace std;

bool randomBool() {
    return rand() % 2 == 1;
}

int main()
{
    Station *s1, *s2, *s3;
    float r1, r2, r3, r4, r5;
    bool carrierStatus;
    for(int i=0; i<500; i++){
        for(int j=0; j<3; j++){
            carrierStatus = randomBool();
            r1 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
            s1 = new Station(r1, carrierStatus);
            carrierStatus = randomBool();
            r1 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
            s2 = new Station(r1, carrierStatus);
            carrierStatus = randomBool();
            r1 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
            s3 = new Station(r1, carrierStatus);
            cout<<"Sending message form s1 to s2"<<endl<<endl;
            if(s1->getCarrierStatus() && s2->getCarrierStatus()){
                cout<<"Collision occur! Transmiss stop!"<<endl<<endl;
                r4 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                Sleep(r4);
            }
            else if(s1->getCarrierStatus()==false && s2->getCarrierStatus() == false){
                r2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                s2->setMessage(r2);
                cout<<"Data has been transmited successfully!"<<endl<<endl;
            }
            else{
                cout<<"s1 or s2 is looking busy!"<<endl<<endl;
                r3 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                Sleep(r3);
            }

             cout<<"Sending message form s2 to s3"<<endl<<endl;
            if(s2->getCarrierStatus() && s3->getCarrierStatus()){
                cout<<"Collision occur! Transmiss stop!"<<endl<<endl;
                r4 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                Sleep(r4);
            }
            else if(s2->getCarrierStatus()==false && s3->getCarrierStatus() == false){
                r2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                s3->setMessage(r2);
                cout<<"Data has been transmited successfully!"<<endl<<endl;
            }
            else{
                cout<<"s2 or s3 is looking busy!"<<endl<<endl;
                r3 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                Sleep(r3);
            }

             cout<<"Sending message form s3 to s1"<<endl<<endl;
            if(s3->getCarrierStatus() && s1->getCarrierStatus()){
                cout<<"Collision occur! Transmiss stop!"<<endl<<endl;
                r4 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                Sleep(r4);
            }
            else if(s3->getCarrierStatus()==false && s1->getCarrierStatus() == false){
                r2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                s1->setMessage(r2);
                cout<<"Data has been transmited successfully!"<<endl<<endl;
            }
            else{
                cout<<"s3 or s1 is looking busy!"<<endl<<endl;
                r3 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                Sleep(r3);
            }

             cout<<"Sending message form s3 to s2"<<endl<<endl;
            if(s3->getCarrierStatus() && s2->getCarrierStatus()){
                cout<<"Collision occur! Transmiss stop!"<<endl<<endl;
                r4 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                Sleep(r4);
            }
            else if(s3->getCarrierStatus()==false && s2->getCarrierStatus() == false){
                r2 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                s2->setMessage(r2);
                cout<<"Data has been transmited successfully!"<<endl<<endl;
            }
            else{
                cout<<"s3 or s2 is looking busy!"<<endl<<endl;
                r3 = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
                Sleep(r3);
            }
        }
    }
}

Station.cpp

#include "Station.h"

Station :: Station(int message, int status){
    _message = message;
    _carrierStatus = status;
}

int Station::getMessage(){
    return _message;
}

bool Station::getCarrierStatus(){
    return _carrierStatus;
}

int Station::getReceivedMessage(){
    return _receivedMessage;
}

void Station::setMessage(int message){
    _message=message;
}

void Station::setCarrierStatus(bool status){
    _carrierStatus = status;
}

void Station::setReceivedMessage(int message){
    _receivedMessage = message;
}

Station.h

using namespace std;

class Station{
    private:
        int _message;
        bool _carrierStatus;
        int _receivedMessage;
    public:
        Station(int message, int carrierStatus);
        int getMessage();
        bool getCarrierStatus();
        int getReceivedMessage();
        void setMessage(int message);
        void setCarrierStatus(bool status);
        void setReceivedMessage(int receivedMessage);
};


Get Project Solution Now

Comments

  1. Replies
    1. It can also used in the same sense like r1

      r1 = static_cast (rand()) / static_cast (RAND_MAX);
      s1 = new Station(r1, carrierStatus);
      carrierStatus = randomBool();

      Delete
  2. jeosd asodpma dsaipd asdopsd oaspd

    ReplyDelete
  3. Pls make a code for csma ca also na...and if possibl on slotted aloha

    ReplyDelete

Post a Comment