Skip to main content

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);
};
 download now!





CONTACT DETAILS

For any other questions or other tasks please feel free to contact me
via email: mhassnainjamil@gmail.com
via WhatsApp: +92-324-7042178
via skype: hassnainjamil1

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

Post a Comment

Popular posts from this blog

Are you looking for the article. Congratulations! you are at the right place

Please comment on this post for Article requests Kindly Write Following things in comment: 1)  Name 2)  Article Link 3)  Your Email Id We will process your request as soon as possible. Note: Share this post with friends to help them.

Human Physiology by Stuart Ira Fox [PDF] (12th edition) free download

Addition/Multiplication of two integer numbers in Java using Graphical User Interface (GUI) using Netbeans with source code free download

In this post I will teach you how you can add two integer numbers in Java using Graphical User Interface. I made it very simple and easy for you. At the end of this tutorial you will be able create the following Graphical User Interface with many different properties. I'm using NetBeans IDE (Recommended for you) for this purpose. Just follow the following steps. Open NetBeans editor and click on new project as shown blow Select this project as a Java Application and click on next as show blow After this name your project, what you want. I named is " Addition GUI " as shown in blow fig. After the creating project make a new JFrame Form Class as show blow in fig. Name this class (i.e Addition) and a JFrame Form panel create to move forward as shown blow. Now drag a text field in you frame from Swing Controls as show shown blow After adding text field perform the following function on it. Right Click on text field Press Backspace button to remove all the text from textFiel...