Aggie Student Toolkit — Load Balancer CSCE 412 Project 4
Arnav Fotedar · Fall 2026 · C++ load balancer simulation
Loading...
Searching...
No Matches
Switch.cpp
Go to the documentation of this file.
1
6
7#include "Switch.h"
8
9#include <stdexcept>
10
11Switch::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
18bool 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
28 processing_->start();
29 streaming_->start();
30}
31
32void 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
48LoadBalancer& Switch::processing() { return *processing_; }
49
50LoadBalancer& Switch::streaming() { return *streaming_; }
51
52unsigned long Switch::routedProcessing() const { return routedProcessing_; }
53
54unsigned long Switch::routedStreaming() const { return routedStreaming_; }
@ Streaming
Long-lived transfer, such as streaming media or a large download.
Definition Request.h:21
Declares the Switch class (bonus), which routes requests to separate load balancers by job type.
Distributes queued requests across a dynamically sized pool of WebServer objects.
LoadBalancer & processing()
Definition Switch.cpp:48
unsigned long routedStreaming() const
Definition Switch.cpp:54
Switch(std::unique_ptr< LoadBalancer > processing, std::unique_ptr< LoadBalancer > streaming)
Creates a switch that owns both load balancers.
Definition Switch.cpp:11
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
LoadBalancer & streaming()
Definition Switch.cpp:50
unsigned long routedProcessing() const
Definition Switch.cpp:52
void start()
Starts both load balancers (records starting queues, dispatches at time 0).
Definition Switch.cpp:27
bool route(const Request &request, unsigned long cycle)
Sends one request to the load balancer for its job type.
Definition Switch.cpp:18
A single web request waiting for, or receiving, service.
Definition Request.h:41
JobType jobType
Processing ('P') or streaming ('S') work.
Definition Request.h:47