From aa458dadd6cbfddba12c04d60c90c7ebd8b42361 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 30 Jun 2023 01:13:55 +0200 Subject: [PATCH] add network and device structure --- .gitignore | 2 ++ CMakeLists.txt | 5 +++++ README.md | 1 + include/Device.hpp | 20 ++++++++++++++++++++ include/Netsim.hpp | 4 ++++ include/Network.hpp | 21 +++++++++++++++++++++ src/CMakeLists.txt | 9 +++++++++ src/Device.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/Network.cpp | 23 +++++++++++++++++++++++ src/main.cpp | 14 ++++++++++++++ 10 files changed, 138 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 include/Device.hpp create mode 100644 include/Netsim.hpp create mode 100644 include/Network.hpp create mode 100644 src/CMakeLists.txt create mode 100644 src/Device.cpp create mode 100644 src/Network.cpp create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..608b1d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +build/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..5bb4b4d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.10) + +project(network_simulator) + +add_subdirectory(src) \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4fa6f01 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Network Simulator \ No newline at end of file diff --git a/include/Device.hpp b/include/Device.hpp new file mode 100644 index 0000000..0d415a4 --- /dev/null +++ b/include/Device.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +#include +#include + +class Device { +public: + friend std::ostream& operator<<(std::ostream& os, const Device& device); + +public: + Device(const std::string& macAddress); + static std::shared_ptr create(const std::string& macAddress); + +private: + void parseAndSetMac(const std::string& macAddress); + +private: + uint8_t macAddress[6]; +}; \ No newline at end of file diff --git a/include/Netsim.hpp b/include/Netsim.hpp new file mode 100644 index 0000000..3260b9f --- /dev/null +++ b/include/Netsim.hpp @@ -0,0 +1,4 @@ +#pragma once + +#include "Network.hpp" +#include "Device.hpp" \ No newline at end of file diff --git a/include/Network.hpp b/include/Network.hpp new file mode 100644 index 0000000..90168b3 --- /dev/null +++ b/include/Network.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include +#include +#include + +#include "Device.hpp" + +class Network { +public: + friend std::ostream& operator<<(std::ostream& os, const Network& network); + +public: + Network(const std::string& name); + + void addDevice(std::shared_ptr device); + +private: + std::vector> devices; + std::string name; +}; \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..50303b5 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,9 @@ +add_executable(netsim + "main.cpp" + "Device.cpp" + "Network.cpp" +) + +target_include_directories(netsim PRIVATE + ${CMAKE_SOURCE_DIR}/include +) \ No newline at end of file diff --git a/src/Device.cpp b/src/Device.cpp new file mode 100644 index 0000000..0b3e12e --- /dev/null +++ b/src/Device.cpp @@ -0,0 +1,39 @@ +#include "Device.hpp" + +#include +#include + +Device::Device(const std::string& macAddress) { + parseAndSetMac(macAddress); +} + +std::shared_ptr Device::create(const std::string& macAddress) { + return std::make_shared(macAddress); +} + +void Device::parseAndSetMac(const std::string& mac) { + sscanf( + mac.c_str(), + "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", + &(macAddress[0]), + &(macAddress[1]), + &(macAddress[2]), + &(macAddress[3]), + &(macAddress[4]), + &(macAddress[5]) + ); +} + +std::ostream& operator<<(std::ostream& os, const Device& device) { + printf( + "%02X:%02X:%02X:%02X:%02X:%02X", + device.macAddress[0], + device.macAddress[1], + device.macAddress[2], + device.macAddress[3], + device.macAddress[4], + device.macAddress[5] + ); + + return os; +} \ No newline at end of file diff --git a/src/Network.cpp b/src/Network.cpp new file mode 100644 index 0000000..271d4ba --- /dev/null +++ b/src/Network.cpp @@ -0,0 +1,23 @@ +#include "Network.hpp" + +#include + +Network::Network(const std::string& name) : + name(name) +{ +} + +void Network::addDevice(std::shared_ptr device) { + devices.push_back(device); +} + +std::ostream& operator<<(std::ostream& os, const Network& network) { + os << "Network '" << network.name << "'" << std::endl << std::endl; + + os << "Devices:" << std::endl; + for (const auto& dev : network.devices) { + os << "\t" << *dev << std::endl; + } + + return os; +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..c364e37 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,14 @@ +#include + +#include "Netsim.hpp" + +int main(int argc, char** argv) { + Network network("Testnet"); + + network.addDevice(Device::create("FA:56:A7:12:04:ED")); + network.addDevice(Device::create("87:1A:B5:77:3D:33")); + + std::cout << network << std::endl; + + return 0; +} \ No newline at end of file