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

The Zoo Management System - entity relationship diagram & MS Access Database

Zoo Management System - Project Details: You are the employee of a big, worldwide working Zoo Management Company. Your company is responsible for the Zoo management. Your boss thinks it would be a great idea to store all data for each Zoo in a brand new self-developed ZOO Management System. Up to now, the ZOO management company has maps of each ZOO available. Your boss knows that you took a course in introduction on an ERP system, so he asks you if you could help designing such a system. Each ZOO must have the same organizational structure, which should look like this: Each Zoo has a Zoo-Address. Each Zoo has many visitors (Visitor Ticket Process (VTP). Many Zoo-Attractions belong to a Zoo. Module 1: Entity Relationship Diagram Design a ER (entity-relationship) diagram for your ZOO Management System. Use the information provided below with the entities and its attributes. Put the entities in the correct relationship to each other (organizational structure). Module 2: DB Implem...

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

Student Registration System Web Project using HTML, CSS & PHP

Student Registry System In this task, you’re to further develop the student registry you started on lab-task. The solution is based the on the use of HTML5, stylesheets, and PHP, and will use a database for storage of information, MySQL on (school website) is used for this. Information of students is saved in the table student, ID is primary key and is set In addition to this, you need a table named class where id a is the primary key and is set up with AUTOINCREMENT, other columns and data types in accordance with the figure. The property class in the student table needs to be a foreign key that refers to the ID column in the class table. In MySQL you need to use tables of the type INNODB with support for foreign keys, reference integrity shall remain. The task: Write a class with the name StudentRegister (means Student registry), the class should use PDO and only prepared statements/questions. The class shall implement the interface StudentInterface, and use the two classes, Student,...