place netsim in static library

This commit is contained in:
Robert 2023-06-30 01:19:45 +02:00
parent aa458dadd6
commit 8f840ff1fe
10 changed files with 17 additions and 10 deletions

8
netsim/CMakeLists.txt Normal file
View file

@ -0,0 +1,8 @@
add_library(libnetsim STATIC
"src/Device.cpp"
"src/Network.cpp"
)
target_include_directories(libnetsim PUBLIC
include
)

20
netsim/include/Device.hpp Normal file
View file

@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
#include <string>
#include <memory>
class Device {
public:
friend std::ostream& operator<<(std::ostream& os, const Device& device);
public:
Device(const std::string& macAddress);
static std::shared_ptr<Device> create(const std::string& macAddress);
private:
void parseAndSetMac(const std::string& macAddress);
private:
uint8_t macAddress[6];
};

View file

@ -0,0 +1,4 @@
#pragma once
#include "Network.hpp"
#include "Device.hpp"

View file

@ -0,0 +1,21 @@
#pragma once
#include <vector>
#include <memory>
#include <string>
#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> device);
private:
std::vector<std::shared_ptr<Device>> devices;
std::string name;
};

39
netsim/src/Device.cpp Normal file
View file

@ -0,0 +1,39 @@
#include "Device.hpp"
#include <sstream>
#include <iostream>
Device::Device(const std::string& macAddress) {
parseAndSetMac(macAddress);
}
std::shared_ptr<Device> Device::create(const std::string& macAddress) {
return std::make_shared<Device>(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;
}

23
netsim/src/Network.cpp Normal file
View file

@ -0,0 +1,23 @@
#include "Network.hpp"
#include <iostream>
Network::Network(const std::string& name) :
name(name)
{
}
void Network::addDevice(std::shared_ptr<Device> 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;
}