Aggie Student Toolkit — Load Balancer
CSCE 412 Project 4
Arnav Fotedar · Fall 2026 · C++ load balancer simulation
Toggle main menu visibility
Loading...
Searching...
No Matches
Switch.cpp
Go to the documentation of this file.
1
6
7
#include "
Switch.h
"
8
9
#include <stdexcept>
10
11
Switch::Switch
(std::unique_ptr<LoadBalancer>
processing
, std::unique_ptr<LoadBalancer>
streaming
)
12
: processing_(std::move(
processing
)), streaming_(std::move(
streaming
)) {
13
if
(!processing_ || !streaming_) {
14
throw
std::invalid_argument(
"Switch needs two load balancers"
);
15
}
16
}
17
18
bool
Switch::route
(
const
Request
& request,
unsigned
long
cycle) {
19
if
(request.
jobType
==
JobType::Streaming
) {
20
++routedStreaming_;
21
return
streaming_->admit(request, cycle);
22
}
23
++routedProcessing_;
24
return
processing_->admit(request, cycle);
25
}
26
27
void
Switch::start
() {
28
processing_->start();
29
streaming_->start();
30
}
31
32
void
Switch::processCycle
(
unsigned
long
cycle,
const
std::vector<Request>& arrivals) {
33
std::vector<Request> processingArrivals;
34
std::vector<Request> streamingArrivals;
35
for
(
const
Request
& request : arrivals) {
36
if
(request.jobType ==
JobType::Streaming
) {
37
++routedStreaming_;
38
streamingArrivals.push_back(request);
39
}
else
{
40
++routedProcessing_;
41
processingArrivals.push_back(request);
42
}
43
}
44
processing_->processCycle(cycle, processingArrivals);
45
streaming_->processCycle(cycle, streamingArrivals);
46
}
47
48
LoadBalancer
&
Switch::processing
() {
return
*processing_; }
49
50
LoadBalancer
&
Switch::streaming
() {
return
*streaming_; }
51
52
unsigned
long
Switch::routedProcessing
()
const
{
return
routedProcessing_; }
53
54
unsigned
long
Switch::routedStreaming
()
const
{
return
routedStreaming_; }
JobType::Streaming
@ Streaming
Long-lived transfer, such as streaming media or a large download.
Definition
Request.h:21
Switch.h
Declares the Switch class (bonus), which routes requests to separate load balancers by job type.
LoadBalancer
Distributes queued requests across a dynamically sized pool of WebServer objects.
Definition
LoadBalancer.h:116
Switch::processing
LoadBalancer & processing()
Definition
Switch.cpp:48
Switch::routedStreaming
unsigned long routedStreaming() const
Definition
Switch.cpp:54
Switch::Switch
Switch(std::unique_ptr< LoadBalancer > processing, std::unique_ptr< LoadBalancer > streaming)
Creates a switch that owns both load balancers.
Definition
Switch.cpp:11
Switch::processCycle
void processCycle(unsigned long cycle, const std::vector< Request > &arrivals)
Splits this cycle's arrivals by job type and runs one cycle on both load balancers.
Definition
Switch.cpp:32
Switch::streaming
LoadBalancer & streaming()
Definition
Switch.cpp:50
Switch::routedProcessing
unsigned long routedProcessing() const
Definition
Switch.cpp:52
Switch::start
void start()
Starts both load balancers (records starting queues, dispatches at time 0).
Definition
Switch.cpp:27
Switch::route
bool route(const Request &request, unsigned long cycle)
Sends one request to the load balancer for its job type.
Definition
Switch.cpp:18
Request
A single web request waiting for, or receiving, service.
Definition
Request.h:41
Request::jobType
JobType jobType
Processing ('P') or streaming ('S') work.
Definition
Request.h:47
Switch.cpp
Generated by
1.18.0