Advertisement
Skylighty

Untitled

Apr 21st, 2020
3,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <string>
  2. #include "packet.h"
  3. #include "channel.h"
  4. #include "wireless_network.h"
  5. #include "rx.h"
  6. #include "tx.h"
  7.  
  8. Packet::Packet(uint32_t did, WirelessNetwork* network)
  9. {
  10.   state_ = State::CREATED;
  11.   error_ = false;
  12.   ack_ = false;
  13.   packet_id_ = network->GetPacketCount() + 1;
  14.   devices_id_ = did;
  15.   logger_ = new Logger();
  16.   active_ = false;
  17.   network_ = network;
  18.   retransmission_ = false;
  19.   collision_ = false;
  20.   r_ = 0;
  21.   t_ = 0;
  22.   this->Execute();
  23. }
  24.  
  25. Packet::~Packet()
  26. {
  27.   this->logger_->Info("Packet of ID : " + std::to_string(this->packet_id_) + " has been DELETED from system.");
  28. }
  29.  
  30. bool Packet::CheckForCollision(Channel* channel)
  31. {
  32.   if ((this->network_->channel_->CountOfPacketsInBuffer()) > 1)
  33.   {
  34.     return true;
  35.   }
  36.   else
  37.     return false;
  38. }
  39.  
  40.  
  41. void Packet::Execute()
  42. {
  43.   active_ = true;
  44.   if (active_ == true)
  45.   {
  46.     TX* CurrentTX = this->network_->GetTX(this->devices_id_);
  47.     RX* CurrentRX = this->network_->GetRX(this->devices_id_);
  48.     std::string pid = std::to_string(this->packet_id_);
  49.     switch (state_)
  50.     {
  51.     case State::CREATED:  //State that packet process is in just after being created
  52.     {
  53.       logger_->Info("Packet of ID : " + pid + " has been created.");
  54.       state_ = State::WAITING;  //Automatically we change state to WAITING, because here we decide what to do next
  55.       if (CurrentTX->BufferEmpty()) //Checking if TX's packet buffer is empty or if it contains any packets
  56.       {   //If the buffer is empty, packet is immediately set as TX's current and transmitted
  57.         logger_->Info("Buffer of TX of ID : " + std::to_string(CurrentTX->GetTXID()) + " is empty. Packet passed.");
  58.         CurrentTX->SetTXPacket(this);
  59.       }
  60.       else
  61.       {   //If the buffer is NOT empty, the packet is put at the end of the TX's FIFO queue
  62.         logger_->Info("Buffer of TX of ID : " + std::to_string(CurrentTX->GetTXID()) + " not empty. Packet put in the buffer.");
  63.         CurrentTX->PushToBuffer(this);
  64.       }
  65.     }
  66.     case State::WAITING:
  67.     {
  68.         //Here, the packet checks if the channel is busy, automatically incrementing "t" parameter - counter of channel checks
  69.       ++this->t_;
  70.       if (this->network_->channel_->ChannelBusy() == true)
  71.       {
  72.         //If channel busy, state is not being changed, packet should wait for next channel check.
  73.         this->network_->channel_->SetBusy(true); //Channel set to busy if we start transmitting
  74.         logger_->Info("Channel busy, packet of ID : " + pid + " is still waiting.");
  75.         this->WaitCP();
  76.       }
  77.       else
  78.       {
  79.         //If channel ain't busy, the packet is passed to channet, simultaneously "t" counter resters back to 0.
  80.         logger_->Info("Channel free, packet of ID : " + pid + " passed to channel.");
  81.         this->t_ = 0;
  82.         state_ = State::SENT;
  83.       }
  84.     }
  85.     case State::SENT:
  86.     {
  87.        //Packet is pushed to the channel
  88.       this->network_->channel_->PushPacketToChannel(this);
  89.       state_ = State::IN_CHANNEL;
  90.     }
  91.     case State::IN_CHANNEL:
  92.     {
  93.       //Packet is inside of the channel (it's in channel's vector container for packets too)
  94.       this->WaitCTP();  //Trasmission time should be added to global time counter
  95.       if (this->CheckForErrors(this->network_->channel_)) //Automatically occurance of errors IN CHANNEL is checked
  96.         this->error_ = true;
  97.       if (this->CheckForCollision(this->network_->channel_)) //Same goes for collisions (more than one packet in channel at the same moment)
  98.         this->collision_ = true;
  99.       //network_->channel_->RemovePacketFromChannel();
  100.       logger_->Info("Packet of ID : " + pid + " passed through channel, REMOVED from channel.");
  101.       this->network_->channel_->RemovePacketFromChannel();  //Packet is removed from channel's packet container
  102.       this->network_->channel_->SetBusy(false); //Channel set to free again
  103.       state_ = State::RECEIVED;
  104.     }
  105.     case State::RECEIVED:
  106.     {
  107.       //Packet reaches it's destination - the receiver
  108.       CurrentRX->SetRXPacket(this);   //As packet leaves the channel it's set as a current packet serviced by particular RX
  109.       logger_->Info("Packet of ID : " + pid + " received by RX of ID : " + std::to_string(CurrentRX->GetRXID()));
  110.       if ((this->error_ == true) || (this->collision_ == true))
  111.       {
  112.         //If error/collisions flags were set in channel - ack flag is set to false and the relay is needed
  113.         logger_->Info("Packet of ID : " + pid + " contains errors. ACK FALSE.");
  114.         ack_ = false;
  115.         retransmission_ = true;
  116.       }
  117.       else
  118.       {
  119.         //If packet was claimed successfully ack flag set to true
  120.         logger_->Info("Packet of ID : " + pid + " clear. ACK TRUE.");
  121.         ack_ = true;
  122.       }
  123.       state_ = State::SENT_BACK;
  124.     }
  125.     case State::SENT_BACK:
  126.     {
  127.       //Packet is sent back to TX throught channel
  128.       network_->channel_->PushPacketToChannel(this);
  129.       //TODO - Should channel be set as busy during ACK feedback? Also Ph.D Sroka said to ask about that.
  130.       this->WaitCTIZ(); //ACK transmission time to TX is being waited
  131.       logger_->Info("Packet of ID : " + pid + " returning with ACK flag set through the channel.");
  132.       network_->channel_->RemovePacketFromChannel(); //Packet removed from channel again.
  133.       state_ = State::CHECK;
  134.     }
  135.     case State::CHECK:
  136.     {
  137.       //Packet is set as currently serviced in TX and it's flags are being checked
  138.       CurrentTX->SetTXPacket(this);
  139.       if ((this->ack_ == false) && (this->retransmission_ == true))
  140.       {
  141.         //As my channel availability algorithim (A4) says - if retransmission is needed here we go:
  142.         logger_->Info("Transmission of packet of ID : " + pid + " failed. Necessary retransmission.");
  143.         ++this->r_; //"r" counter incremented because it enumerates number of retransmissions
  144.         if (this->r_ < network_->kMaxRetransmissions) //If retransmissions peak not reached - one time CRP time is waited
  145.         {
  146.           //The packet state changed back to waiting, because after first channel check, it should wait CP time, not CRP again.
  147.           WaitCRP();
  148.           state_ = State::WAITING;
  149.         }
  150.         else
  151.           //If ack flag set to true, delete packet and output a success transmission message.
  152.           delete this;
  153.       }
  154.       else
  155.       {
  156.         logger_->Info("Packet of ID : " + pid + " transmitted successfully");
  157.         delete this;
  158.       }
  159.     }
  160.     }
  161.   }
  162. }
  163.  
  164. void Packet::Activate(double time)
  165. {
  166. }
  167.  
  168. bool Packet::CheckForErrors(Channel* channel)
  169. {
  170.   return false;
  171. }
  172.  
  173. void Packet::WaitCP()
  174. {
  175.  
  176. }
  177.  
  178. void Packet::WaitCRP()
  179. {
  180.  
  181. }
  182.  
  183. void Packet::WaitCTIZ()
  184. {
  185.  
  186. }
  187.  
  188. void Packet::WaitCTP()
  189. {
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement