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 |
#include <iostream> #include <vector> #include <string> #include <queue> #include <random> #include <iomanip> // Class representing a task class Task { public: Task(int id, int dataSize) : id(id), dataSize(dataSize), isCompleted(false) {} int getId() const { return id; } int getDataSize() const { return dataSize; } bool isTaskCompleted() const { return isCompleted; } void markAsCompleted() { isCompleted = true; } private: int id; int dataSize; bool isCompleted; }; // Class representing an edge device class EdgeDevice { public: EdgeDevice(int id) : id(id), taskQueue() {} int getId() const { return id; } void addTask(const Task& task) { taskQueue.push(task); } void processTasks() { while (!taskQueue.empty()) { Task task = taskQueue.front(); taskQueue.pop(); std::cout << "Edge Device " << id << " processing Task " << task.getId() << "\n"; // Simulate task processing with a delay // Here we just mark the task as completed task.markAsCompleted(); } } private: int id; std::queue<Task> taskQueue; }; // Class representing the central server class CentralServer { public: void addTask(const Task& task) { tasks.push_back(task); } void distributeTasks(std::vector<EdgeDevice>& edgeDevices) { std::cout << "Distributing tasks to edge devices...\n"; std::default_random_engine generator; std::uniform_int_distribution<int> distribution(0, edgeDevices.size() - 1); for (auto& task : tasks) { int deviceIndex = distribution(generator); edgeDevices[deviceIndex].addTask(task); std::cout << "Task " << task.getId() << " assigned to Edge Device " << edgeDevices[deviceIndex].getId() << "\n"; } } private: std::vector<Task> tasks; }; int main() { int numTasks = 5; int numDevices = 3; // Create tasks std::vector<Task> tasks; for (int i = 0; i < numTasks; ++i) { tasks.emplace_back(i, rand() % 100 + 1); // Random data size between 1 and 100 } // Create edge devices std::vector<EdgeDevice> edgeDevices; for (int i = 0; i < numDevices; ++i) { edgeDevices.emplace_back(i); } // Create central server and add tasks CentralServer server; for (const auto& task : tasks) { server.addTask(task); } // Distribute tasks to edge devices server.distributeTasks(edgeDevices); // Process tasks on edge devices std::cout << "Processing tasks on edge devices...\n"; for (auto& device : edgeDevices) { device.processTasks(); } return 0; } |
Explanation
- Class
Task
:- Purpose: Represents a task with an ID and a data size.
- Attributes:
id
: Identifier for the task.dataSize
: Size of the task’s data.isCompleted
: Status of whether the task has been completed.
- Methods:
getId()
,getDataSize()
: Accessors for task attributes.isTaskCompleted()
: Checks if the task is completed.markAsCompleted()
: Marks the task as completed.
Advertisement
- Class
EdgeDevice
:- Purpose: Represents an edge device capable of processing tasks.
- Attributes:
id
: Identifier for the edge device.taskQueue
: Queue of tasks to be processed.
- Methods:
addTask(const Task&)
: Adds a task to the device’s queue.processTasks()
: Processes and completes all tasks in the queue.
- Class
CentralServer
:- Purpose: Manages task distribution to edge devices.
- Attributes:
tasks
: List of tasks to be distributed.
- Methods:
addTask(const Task&)
: Adds a task to the server’s list.distributeTasks(std::vector<EdgeDevice>&)
: Distributes tasks randomly among edge devices.
- Main Function:
- Setup: Creates tasks and edge devices.
- Server Actions:
- Adds tasks to the central server.
- Distributes tasks to edge devices.
Advertisement - Processing:
- Each edge device processes its tasks.
Usage
- Task Distribution: Demonstrates how tasks can be distributed to edge devices from a central server.
- Processing Simulation: Simulates the processing of tasks on edge devices.