add network and device structure
This commit is contained in:
commit
aa458dadd6
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
.vscode/
|
||||||
|
build/
|
5
CMakeLists.txt
Normal file
5
CMakeLists.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
|
project(network_simulator)
|
||||||
|
|
||||||
|
add_subdirectory(src)
|
20
include/Device.hpp
Normal file
20
include/Device.hpp
Normal 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];
|
||||||
|
};
|
4
include/Netsim.hpp
Normal file
4
include/Netsim.hpp
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Network.hpp"
|
||||||
|
#include "Device.hpp"
|
21
include/Network.hpp
Normal file
21
include/Network.hpp
Normal 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;
|
||||||
|
};
|
9
src/CMakeLists.txt
Normal file
9
src/CMakeLists.txt
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
add_executable(netsim
|
||||||
|
"main.cpp"
|
||||||
|
"Device.cpp"
|
||||||
|
"Network.cpp"
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(netsim PRIVATE
|
||||||
|
${CMAKE_SOURCE_DIR}/include
|
||||||
|
)
|
39
src/Device.cpp
Normal file
39
src/Device.cpp
Normal 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
src/Network.cpp
Normal file
23
src/Network.cpp
Normal 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;
|
||||||
|
}
|
14
src/main.cpp
Normal file
14
src/main.cpp
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
Loading…
Reference in a new issue