forked from noahsug/CSMA-CD-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComputer.cpp
141 lines (119 loc) · 3.59 KB
/
Computer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "Computer.h"
#include <iostream>
#include "Clock.h"
#include "Debug.h"
#include "Environment.h"
#include "Events.h"
#include "Router.h"
using std::vector;
extern vector<Computer*> computers;
void Computer::OnArrival() {
PRINT(this << ": " << Clock::GetTime() << ": OnArrival" << std::endl);
Router::GetInstance().OnPacketGenerated(this);
packet_queue_size_++;
if (!Environment::GameOverMan) {
events_->Insert(new ArrivalEvent(this));
}
if (state_ == IDLE) {
state_ = SENSING;
events_->Insert(new MediumSensedEvent(this));
}
}
void Computer::OnMediumSensed() {
PRINT(this << ": " << Clock::GetTime() << ": OnMediumSensed" << std::endl);
if (medium_busy_) {
state_ = WAITING_TO_TRANSMIT;
} else {
state_ = TRANSMITTING;
last_jam_time_ = (unsigned long long)-1;
Event* trans = new TransmittedFrameEvent(this);
events_->Insert(trans);
cancellable_events_.push_back(trans->GetId());
for (unsigned i = 0; i < computers.size(); i++) {
if (computers[i] != this) {
events_->Insert(new MediumBusyEvent(computers[i]));
}
}
}
}
void Computer::OnMediumBusy() {
PRINT(this << ": " << Clock::GetTime() << ": OnMediumBusy" << std::endl);
if (state_ == TRANSMITTING) {
CancelEvents();
for (unsigned i = 0; i < computers.size(); i++) {
if (computers[i] != this) {
events_->Insert(new RaspberryJamEvent(computers[i]));
}
}
state_ = BACKING_OFF;
medium_busy_ = false;
events_->Insert(new BackoffDoneEvent(this));
last_jam_time_ = Clock::GetTime();
} else {
medium_busy_ = true;
Event* free = new MediumFreeEvent(this);
events_->Insert(free);
cancellable_events_.push_back(free->GetId());
}
}
void Computer::OnMediumFree() {
PRINT(this << ": " << Clock::GetTime() << ": OnMediumFree" << std::endl);
medium_busy_ = false;
if (state_ == WAITING_TO_TRANSMIT) {
state_ = SENSING;
events_->Insert(new MediumSensedEvent(this));
}
}
void Computer::OnTransmittedFrame() {
PRINT(this << ": " << Clock::GetTime() << ": OnTransmittedFrame" << std::endl);
Event* rec = new PacketReceivedEvent(this);
events_->Insert(rec);
cancellable_events_.push_back(rec->GetId());
packet_queue_size_--;
if (packet_queue_size_ > 0) {
state_ = SENSING;
events_->Insert(new MediumSensedEvent(this));
} else {
state_ = IDLE;
}
}
void Computer::OnRaspberryJam() {
PRINT(this << ": " << Clock::GetTime() << ": OnRaspberryJam" << std::endl);
last_jam_time_ = Clock::GetTime();
if (state_ == TRANSMITTING) {
CancelEvents();
state_ = BACKING_OFF;
events_->Insert(new BackoffDoneEvent(this));
} else {
medium_busy_ = true;
Event* free = new MediumFreeEvent(this);
events_->Insert(free);
cancellable_events_.push_back(free->GetId());
}
}
void Computer::OnBackoffDone() {
PRINT(this << ": " << Clock::GetTime() << ": OnBackoffDone" << std::endl);
state_ = SENSING;
events_->Insert(new MediumSensedEvent(this));
}
void Computer::OnPacketReceived() {
PRINT(this << ": " << Clock::GetTime() << ": OnPacketReceived" << std::endl);
if (last_jam_time_ - Environment::PROP_TIME < Clock::GetTime()) {
Router::GetInstance().OnPacketDropped(this);
} else {
Router::GetInstance().OnPacketTransmitted(this);
}
}
void Computer::CancelEvents() {
for (unsigned i = 0; i < cancellable_events_.size(); i++) {
events_->Remove(cancellable_events_[i]);
}
cancellable_events_.clear();
}
unsigned long Computer::GetBackoff() {
backoff_count_++;
if (backoff_count_ > Computer::KMAX) {
backoff_count_ = Computer::KMAX;
}
return backoff_count_;
}