Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Packet::Execute()
- {
- active_ = true;
- if (active_ == true)
- {
- TX* CurrentTX = this->network_->GetTX(this->devices_id_);
- RX* CurrentRX = this->network_->GetRX(this->devices_id_);
- std::string pid = std::to_string(this->packet_id_);
- switch (state_)
- {
- case State::CREATED:
- {
- logger_->Info("Packet of ID : " + pid + "has been created.");
- state_ = State::WAITING;
- if (network_->GetTX(this->devices_id_)->BufferEmpty())
- {
- logger_->Info("Buffer of TX of ID : " + std::to_string(CurrentTX->GetTXID()) + "is empty. Packet passed.");
- network_->GetTX(this->devices_id_)->SetTXPacket(this);
- }
- else
- {
- logger_->Info("Buffer of TX of ID : " + std::to_string(CurrentTX->GetTXID()) + "not empty. Packet put in the buffer.");
- network_->GetTX(this->devices_id_)->PushToBuffer(this);
- }
- }
- case State::WAITING:
- {
- ++this->t_;
- if (network_->channel_->ChannelBusy() == true)
- {
- logger_->Info("Channel busy, packet of ID : " + pid + "is still waiting.");
- this->WaitCP();
- }
- else
- {
- logger_->Info("Channel free, packet of ID : " + pid + "passed to channel.");
- this->t_ = 0;
- state_ = State::SENT;
- }
- }
- case State::SENT:
- {
- network_->channel_->PushPacketToChannel(this);
- state_ = State::IN_CHANNEL;
- }
- case State::IN_CHANNEL:
- {
- this->WaitCTP();
- if (this->CheckForErrors(network_->channel_))
- this->error_ = true;
- if (this->CheckForCollision(network_->channel_))
- this->collision_ = true;
- network_->channel_->RemovePacketFromChannel();
- state_ = State::RECEIVED;
- }
- case State::RECEIVED:
- {
- CurrentRX->SetRXPacket(this);
- logger_->Info("Packet of ID : " + pid + "received by RX of ID : " + std::to_string(CurrentRX->GetRXID()));
- if ((this->error_ == true) || (this->collision_ == true))
- {
- logger_->Info("Packet of ID : " + pid + "contains errors. ACK FALSE.");
- ack_ = false;
- retransmission_ = true;
- }
- else
- {
- logger_->Info("Packet of ID : " + pid + "contains errors. ACK FALSE.");
- ack_ = true;
- }
- state_ = State::SENT_BACK;
- }
- case State::SENT_BACK:
- {
- network_->channel_->PushPacketToChannel(this);
- this->WaitCTIZ();
- logger_->Info("Packet of ID : " + pid + "returning with ACK flag set through the channel.");
- network_->channel_->RemovePacketFromChannel();
- state_ = State::CHECK;
- }
- case State::CHECK:
- {
- CurrentTX->SetTXPacket(this);
- if ((this->ack_ == false) && (this->retransmission_ == true))
- {
- logger_->Info("Transmission of packet of ID : " + pid + " failed. Necessary retransmission.");
- ++this->r_;
- if (this->r_ < network_->kMaxRetransmissions)
- {
- WaitCRP();
- state_ = State::WAITING;
- }
- else
- delete this;
- }
- else
- {
- logger_->Info("Packet of ID : " + pid + "transmitted successfully");
- delete this;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement