Advertisement
Skylighty

Untitled

Apr 21st, 2020
1,467
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void Packet::Execute()
  2. {
  3.   active_ = true;
  4.   if (active_ == true)
  5.   {
  6.     TX* CurrentTX = this->network_->GetTX(this->devices_id_);
  7.     RX* CurrentRX = this->network_->GetRX(this->devices_id_);
  8.     std::string pid = std::to_string(this->packet_id_);
  9.     switch (state_)
  10.     {
  11.     case State::CREATED:
  12.     {
  13.       logger_->Info("Packet of ID : " + pid + "has been created.");
  14.       state_ = State::WAITING;
  15.       if (network_->GetTX(this->devices_id_)->BufferEmpty())
  16.       {
  17.         logger_->Info("Buffer of TX of ID : " + std::to_string(CurrentTX->GetTXID()) + "is empty. Packet passed.");
  18.         network_->GetTX(this->devices_id_)->SetTXPacket(this);
  19.       }
  20.       else
  21.       {
  22.         logger_->Info("Buffer of TX of ID : " + std::to_string(CurrentTX->GetTXID()) + "not empty. Packet put in the buffer.");
  23.         network_->GetTX(this->devices_id_)->PushToBuffer(this);
  24.       }
  25.     }
  26.     case State::WAITING:
  27.     {
  28.       ++this->t_;
  29.       if (network_->channel_->ChannelBusy() == true)
  30.       {
  31.         logger_->Info("Channel busy, packet of ID : " + pid + "is still waiting.");
  32.         this->WaitCP();
  33.       }
  34.       else
  35.       {
  36.         logger_->Info("Channel free, packet of ID : " + pid + "passed to channel.");
  37.         this->t_ = 0;
  38.         state_ = State::SENT;
  39.       }
  40.     }
  41.     case State::SENT:
  42.     {
  43.       network_->channel_->PushPacketToChannel(this);
  44.       state_ = State::IN_CHANNEL;
  45.     }
  46.     case State::IN_CHANNEL:
  47.     {
  48.       this->WaitCTP();
  49.       if (this->CheckForErrors(network_->channel_))
  50.         this->error_ = true;
  51.       if (this->CheckForCollision(network_->channel_))
  52.         this->collision_ = true;
  53.       network_->channel_->RemovePacketFromChannel();
  54.       state_ = State::RECEIVED;
  55.     }
  56.     case State::RECEIVED:
  57.     {
  58.       CurrentRX->SetRXPacket(this);
  59.       logger_->Info("Packet of ID : " + pid + "received by RX of ID : " + std::to_string(CurrentRX->GetRXID()));
  60.       if ((this->error_ == true) || (this->collision_ == true))
  61.       {
  62.         logger_->Info("Packet of ID : " + pid + "contains errors. ACK FALSE.");
  63.         ack_ = false;
  64.         retransmission_ = true;
  65.       }
  66.       else
  67.       {
  68.         logger_->Info("Packet of ID : " + pid + "contains errors. ACK FALSE.");
  69.         ack_ = true;
  70.       }
  71.       state_ = State::SENT_BACK;
  72.     }
  73.     case State::SENT_BACK:
  74.     {
  75.       network_->channel_->PushPacketToChannel(this);
  76.       this->WaitCTIZ();
  77.       logger_->Info("Packet of ID : " + pid + "returning with ACK flag set through the channel.");
  78.       network_->channel_->RemovePacketFromChannel();
  79.       state_ = State::CHECK;
  80.     }
  81.     case State::CHECK:
  82.     {
  83.       CurrentTX->SetTXPacket(this);
  84.       if ((this->ack_ == false) && (this->retransmission_ == true))
  85.       {
  86.         logger_->Info("Transmission of packet of ID : " + pid + " failed. Necessary retransmission.");
  87.         ++this->r_;
  88.         if (this->r_ < network_->kMaxRetransmissions)
  89.         {
  90.           WaitCRP();
  91.           state_ = State::WAITING;
  92.         }
  93.         else
  94.           delete this;
  95.       }
  96.       else
  97.       {
  98.         logger_->Info("Packet of ID : " + pid + "transmitted successfully");
  99.         delete this;
  100.       }
  101.     }
  102.     }
  103.   }
  104. }
Advertisement
RAW Paste Data Copied
Advertisement