Skip to content

Commit

Permalink
Merge pull request #28 from Projekt-zespolowy-TIU/dev
Browse files Browse the repository at this point in the history
Version 0.1.0 beta
  • Loading branch information
Mat3oo authored Jun 11, 2020
2 parents 7db6cd3 + 6714535 commit 3ea69ad
Show file tree
Hide file tree
Showing 81 changed files with 3,848 additions and 1,639 deletions.
1 change: 1 addition & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[requires]
boost/1.72.0
jsonformoderncpp/3.7.3@vthiery/stable

[generators]
qmake
20 changes: 8 additions & 12 deletions src/console/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "core/Networkv4.h"
#include "core/SubnetsCalculatorV4.h"
#include "core/coreUtils.h"
#include "core/ReportGeneratorV4.h"

using namespace core;

Expand Down Expand Up @@ -64,18 +65,13 @@ int main(int argc, char *argv[])
return 1; //TODO: should return something more meaningful
}

for(const auto& sub : mainNetwork.Subnets())
{
std::cout << "\nNazwa podsieci: " << sub->SubName()
<< "\nIP: " << sub->Ip()
<< " Maska: " << sub->Mask()
<< " Broadcast: " << *dynamic_cast<Subnetv4&>(*sub).getBroadcast()
<< "\nminHost: " << *sub->getMinHost()
<< " maxHost: " << *sub->getMaxHost() << '\n';
for(const auto& host : sub->HostsList())
std::cout << '\t' << host->Name()
<< " " <<host->Ip() << '\n';
}
std::unique_ptr<IReportGenerator> reportGenerator = std::make_unique<ReportGeneratorV4>();

reportGenerator->generate(mainNetwork, DetaiLevel::Detailed);

std::cout << reportGenerator->getReport();

reportGenerator->save("mainNetwork.txt");

return a.exec();
}
90 changes: 90 additions & 0 deletions src/core/FileIO.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "FileIO.h"

#include <string>
#include <fstream>
#include <nlohmann/json.hpp>

#include "Networkv4.h"
#include "Networkv6.h"

namespace core{

using json = nlohmann::json;

void FileIO::saveIPv4(const INetwork& netv4, const QString& path)
{
std::ofstream o(path.toStdString());

info["IPv4IP"] = netv4.Ip().asStringDec().toStdString();
info["IPv4NetMask"] = netv4.Mask().asStringDec().toStdString();

info["IPv4SubnetNames"].clear();
info["IPv4SubnetHostNumbers"].clear();

for(const auto& i : netv4.Subnets())
{
info["IPv4SubnetNames"].push_back(i->SubName().toStdString());
info["IPv4SubnetHostNumbers"].push_back(i->HostNumber().str());
}

o<<info;
}

void FileIO::loadIPv4(std::shared_ptr<INetwork>& netv4, const QString& path)
{
info.clear();
std::ifstream i(path.toStdString());
i>>info;

std::shared_ptr<INetwork> temp_network = std::make_shared<Networkv4>(
QString{std::string(info["IPv4IP"]).c_str()},
QString{std::string(info["IPv4NetMask"]).c_str()});

for(int i=0; i<int(info["IPv4SubnetNames"].size()) ; i++)
{
temp_network->addSubnet(cpp_int{std::string{info["IPv4SubnetHostNumbers"][i]}},
std::string(info["IPv4SubnetNames"][i]).c_str());
}

netv4 = temp_network;
}

void FileIO::saveIPv6(const INetwork& netv6, const QString& path)
{
std::ofstream o(path.toStdString());

info["IPv6IP"] = netv6.Ip().asStringDec().toStdString();
info["IPv6NetMask"] = netv6.Mask().asStringDec().toStdString();

info["IPv6SubnetNames"].clear();
info["IPv6SubnetHostNumbers"].clear();

for(const auto& i : netv6.Subnets())
{
info["IPv6SubnetNames"].push_back(i->SubName().toStdString());
info["IPv6SubnetHostNumbers"].push_back(i->HostNumber().str());
}

o<<info;
}


void FileIO::loadIPv6(std::shared_ptr<INetwork>& netv6, const QString& path)
{
info.clear();
std::ifstream i(path.toStdString());
i>>info;

std::shared_ptr<INetwork> temp_network = std::make_shared<Networkv6>(
QString{std::string(info["IPv6IP"]).c_str()},
QString{std::string(info["IPv6NetMask"]).c_str()});

for(int i = 0; i < static_cast<int>((info["IPv6SubnetNames"].size())); i++)
{
temp_network->addSubnet(cpp_int{std::string{info["IPv6SubnetHostNumbers"][i]}},
std::string(info["IPv6SubnetNames"][i]).c_str());
}

netv6 = temp_network;
}
}
29 changes: 29 additions & 0 deletions src/core/FileIO.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef FILEIO_H
#define FILEIO_H

#include <memory>
#include <QString>
#include <nlohmann/json.hpp>

#include "INetwork.h"


using json = nlohmann::json;

namespace core {
class FileIO
{
public:
void saveIPv4(const INetwork& netv4, const QString& path);
void loadIPv4(std::shared_ptr<INetwork>& netv4, const QString& path);
void saveIPv6(const INetwork& netv6, const QString& path);
void loadIPv6(std::shared_ptr<INetwork>& netv6, const QString& path);

private:
json info;
};
};



#endif // FILEIO_H
12 changes: 6 additions & 6 deletions src/core/Host.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
#include "Host.h"

namespace core {
Host::Host(std::unique_ptr<IPaddress> ip, const QString& name, const cpp_int& id):
Host::Host(std::unique_ptr<IPaddress> ip, const QString& name, const cpp_int& id) noexcept:
_Ip{std::move(ip)},
_Name{name},
_Id{id}
{}

const IPaddress& Host::Ip() const
const IPaddress& Host::Ip() const noexcept
{
return *_Ip;
}

QString Host::Name() const
QString Host::Name() const noexcept
{
return _Name;
}

boost::multiprecision::cpp_int Host::Id() const
boost::multiprecision::cpp_int Host::Id() const noexcept
{
return _Id;
}

void Host::Ip(std::unique_ptr<IPaddress> ip)
void Host::Ip(std::unique_ptr<IPaddress> ip) noexcept
{
_Ip = std::move(ip);
}

void Host::Name(const QString& name)
void Host::Name(const QString& name) noexcept
{
_Name = name;
}
Expand Down
12 changes: 6 additions & 6 deletions src/core/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ namespace core {
class Host final
{
public:
Host(std::unique_ptr<IPaddress> ip, const QString& name, const cpp_int& id);
Host(std::unique_ptr<IPaddress> ip, const QString& name, const cpp_int& id) noexcept;

const IPaddress& Ip() const;
QString Name() const;
cpp_int Id() const;
const IPaddress& Ip() const noexcept;
QString Name() const noexcept;
cpp_int Id() const noexcept;

void Ip(std::unique_ptr<IPaddress> ip);
void Name(const QString& name);
void Ip(std::unique_ptr<IPaddress> ip) noexcept;
void Name(const QString& name) noexcept;
private:
std::unique_ptr<IPaddress> _Ip;
QString _Name;
Expand Down
4 changes: 2 additions & 2 deletions src/core/IIPaddrPrintable.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace core{
class IIPaddrPrintable
{
public:
virtual QString asStringDec() const = 0;
virtual QString asStringBin() const = 0;
virtual QString asStringDec() const noexcept = 0;
virtual QString asStringBin() const noexcept = 0;

//~~~~~~~~~~~~~~~~INTERFACE OVERHEAD~~~~~~~~~~~~~~~~//
public:
Expand Down
10 changes: 5 additions & 5 deletions src/core/INetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ namespace core {
class INetwork
{
public:
virtual const IPaddress& Ip() const = 0;
virtual const IPaddress& Mask() const = 0;
virtual const std::vector<std::shared_ptr<ISubnet>>& Subnets() const = 0;
virtual const IPaddress& Ip() const noexcept = 0;
virtual const IPaddress& Mask() const noexcept = 0;
virtual const std::vector<std::shared_ptr<ISubnet>>& Subnets() const noexcept = 0;

virtual void Subnets(const std::vector<std::shared_ptr<ISubnet>>& subnets) = 0;
virtual void Subnets(const std::vector<std::shared_ptr<ISubnet>>& subnets) noexcept = 0;

virtual void addSubnet(const cpp_int& hostNumber, const QString& name) = 0;

virtual cpp_int hostsCapacity() const = 0;
virtual bool isSubnet(const IPaddress& hostIP) const = 0;
virtual bool isSubnet(const IPaddress& hostIP) const noexcept = 0;

//~~~~~~~~~~~~~~~~INTERFACE OVERHEAD~~~~~~~~~~~~~~~~//
public:
Expand Down
22 changes: 11 additions & 11 deletions src/core/IPaddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace core {
{
}

std::unique_ptr<IPaddress> IPaddress::clone() const
std::unique_ptr<IPaddress> IPaddress::clone() const noexcept
{
return std::unique_ptr<IPaddress>(_cloneImpl());
}
Expand All @@ -20,29 +20,29 @@ namespace core {
return static_cast<short>(_IpAddress.count());
}

short IPaddress::getAddressLength() const
short IPaddress::getAddressLength() const noexcept
{
return static_cast<short>(_IpAddress.size());
}

boost::dynamic_bitset<> IPaddress::raw() const
boost::dynamic_bitset<> IPaddress::raw() const noexcept
{
return _IpAddress;
}

QString IPaddress::asStringBin() const
QString IPaddress::asStringBin() const noexcept
{
std::string temp;
boost::to_string(_IpAddress, temp);
return temp.c_str();
}

QString IPaddress::asStringDec() const
QString IPaddress::asStringDec() const noexcept
{
return asStringBin();
}

bool IPaddress::isMask() const
bool IPaddress::isMask() const noexcept
{
boost::dynamic_bitset<> bits(_IpAddress.size());
bits.set();
Expand All @@ -59,27 +59,27 @@ namespace core {
return false;
}

IPaddress* IPaddress::_cloneImpl() const
IPaddress* IPaddress::_cloneImpl() const noexcept
{
return new IPaddress(*this);
}

bool operator==(const IPaddress& lhs, const IPaddress& rhs)
bool operator==(const IPaddress& lhs, const IPaddress& rhs) noexcept
{
return lhs.raw() == rhs.raw();
}

bool operator!=(const IPaddress& lhs, const IPaddress& rhs)
bool operator!=(const IPaddress& lhs, const IPaddress& rhs) noexcept
{
return !(lhs == rhs);
}

boost::dynamic_bitset<> operator&(const IPaddress& lhs, const IPaddress& rhs)
boost::dynamic_bitset<> operator&(const IPaddress& lhs, const IPaddress& rhs) noexcept
{
return lhs.raw() & rhs.raw();
};

boost::dynamic_bitset<> operator|(const IPaddress& lhs, const IPaddress& rhs)
boost::dynamic_bitset<> operator|(const IPaddress& lhs, const IPaddress& rhs) noexcept
{
return lhs.raw() | rhs.raw();
}
Expand Down
22 changes: 11 additions & 11 deletions src/core/IPaddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ namespace core {
IPaddress(const boost::dynamic_bitset<> &ip = boost::dynamic_bitset<>());
IPaddress& operator=(const IPaddress&) = delete;

std::unique_ptr<IPaddress> clone() const;
std::unique_ptr<IPaddress> clone() const noexcept;

short getPrefix() const;
short getAddressLength() const;
boost::dynamic_bitset<> raw() const;
short getAddressLength() const noexcept;
boost::dynamic_bitset<> raw() const noexcept;

QString asStringBin() const override;
QString asStringDec() const override;
QString asStringBin() const noexcept override;
QString asStringDec() const noexcept override;
protected:
boost::dynamic_bitset<> _IpAddress;
private:
bool isMask() const;
virtual IPaddress* _cloneImpl() const;
bool isMask() const noexcept;
virtual IPaddress* _cloneImpl() const noexcept;

public:
virtual ~IPaddress() = default;
};

bool operator==(const IPaddress& lhs, const IPaddress& rhs);
bool operator!=(const IPaddress& lhs, const IPaddress& rhs);
boost::dynamic_bitset<> operator&(const IPaddress& lhs, const IPaddress& rhs);
boost::dynamic_bitset<> operator|(const IPaddress& lhs, const IPaddress& rhs);
bool operator==(const IPaddress& lhs, const IPaddress& rhs) noexcept;
bool operator!=(const IPaddress& lhs, const IPaddress& rhs) noexcept;
boost::dynamic_bitset<> operator&(const IPaddress& lhs, const IPaddress& rhs) noexcept;
boost::dynamic_bitset<> operator|(const IPaddress& lhs, const IPaddress& rhs) noexcept;
}

#endif // IPADDRESS_H
Loading

0 comments on commit 3ea69ad

Please sign in to comment.