Skip to content

Commit

Permalink
Merge remote-tracking branch 'CA2/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
alumpish committed Sep 30, 2023
2 parents 9281c3f + f477baf commit c05da76
Show file tree
Hide file tree
Showing 17 changed files with 1,234 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CA2 - Wireless LAN Simulation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/ns-3.35/*
!/ns-3.35/scratch

.vscode
510 changes: 510 additions & 0 deletions CA2 - Wireless LAN Simulation/README.md

Large diffs are not rendered by default.

Binary file not shown.
120 changes: 120 additions & 0 deletions CA2 - Wireless LAN Simulation/ns-3.35/scratch/client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#ifndef CLIENT_HPP
#define CLIENT_HPP

#include <cstdlib>
#include <time.h>
#include <stdio.h>
#include <string>
#include <fstream>

#include "ns3/applications-module.h"
#include "ns3/internet-module.h"

#include "my_header.hpp"
#include "utils.hpp"
#include "consts.hpp"

using namespace ns3;

int idx = 0;
class client : public Application
{
public:
client(uint16_t port, Ipv4InterfaceContainer &ip, uint16_t master_port, Ipv4InterfaceContainer &master_ip);
virtual ~client();
uint16_t GetPort(void);
Ipv4InterfaceContainer GetIp(void);
Ptr<Socket> GetSocket();

private:
virtual void StartApplication(void);
void HandleRead(Ptr<Socket> socket);
static void GenerateTraffic(Ptr<Socket> socket, Ipv4InterfaceContainer ip, uint16_t port, uint16_t data);

uint16_t port;
Ipv4InterfaceContainer ip;
Ptr<Socket> socketUDPmaster, socketUDPmapper;
uint16_t master_port;
Ipv4InterfaceContainer master_ip;
std::string msg;
};


client::client(uint16_t port, Ipv4InterfaceContainer &ip, uint16_t master_port, Ipv4InterfaceContainer &master_ip)
: port(port),
ip(ip),
master_port(master_port),
master_ip(master_ip)
{
std::srand(time(0));
}

client::~client()
{
std::cout << "Recieved: " << msg << std::endl;
}

Ipv4InterfaceContainer client::GetIp()
{
return ip;
}

uint16_t client::GetPort()
{
return port;
}

Ptr<Socket> client::GetSocket()
{
return socketUDPmaster;
}

void client::StartApplication(void)
{
// Create socket for UDP between client and master
Ptr<Socket> sock = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());
InetSocketAddress sockAddr(master_ip.GetAddress(0), master_port);
sock->Connect(sockAddr);

// Create socket for UDP between client and mapper
Ptr<Socket> sock2 = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());
InetSocketAddress sockAddr2(ip.GetAddress(0), port);
sock2->Bind(sockAddr2);
sock2->SetRecvCallback(MakeCallback(&client::HandleRead, this));

GenerateTraffic(sock, ip, port, 0);
}

void client::HandleRead(Ptr<Socket> socket)
{
Ptr<Packet> packet;
while ((packet = socket->Recv()))
{
if (packet->GetSize() == 0)
{
break;
}
MyHeader m;
packet->RemoveHeader(m);
std::cout << "Received packet from mapper " << m.GetId() << ": " << static_cast<char>(m.GetData()) << std::endl;
msg += static_cast<char>(m.GetData());

}
}

void client::GenerateTraffic(Ptr<Socket> socket, Ipv4InterfaceContainer ip, uint16_t port, uint16_t data) {
Ptr<Packet> packet = new Packet();
MyHeader m;
m.SetData(data);
m.SetIp(ip.GetAddress(0));
m.SetPort(port);

packet->AddHeader(m);
packet->Print(std::cout);
socket->Send(packet);

Simulator::Schedule(Seconds(0.1), &GenerateTraffic, socket, ip, port, MSG[idx] - 97);
idx = (idx + 1) % MSG.size();
}

#endif
13 changes: 13 additions & 0 deletions CA2 - Wireless LAN Simulation/ns-3.35/scratch/consts.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef CONSTS_HPP
#define CONSTS_HPP

#include <map>


const std::map<int, char> kMapper1 = {{0, 'a'}, {1, 'b'}, {2, 'c'}, {3, 'd'}, {4, 'e'}, {5, 'f'}, {6, 'g'}, {7, 'h'}, {8, 'i'}};
const std::map<int, char> kMapper2 = {{9, 'j'}, {10, 'k'}, {11, 'l'}, {12, 'm'}, {13, 'n'}, {14, 'o'}, {15, 'p'}, {16, 'q'}, {17, 'r'}};
const std::map<int, char> kMapper3 = {{18, 's'}, {19, 't'}, {20, 'u'}, {21, 'v'}, {22, 'w'}, {23, 'x'}, {24, 'y'}, {25, 'z'}};

const std::string MSG = "thisisatestmessage";

#endif
106 changes: 106 additions & 0 deletions CA2 - Wireless LAN Simulation/ns-3.35/scratch/mapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#ifndef MAPPER_HPP
#define MAPPER_HPP

#include <cstdlib>
#include <time.h>
#include <stdio.h>
#include <string>
#include <fstream>

#include "ns3/applications-module.h"
#include "ns3/internet-module.h"

#include "my_header.hpp"

using namespace ns3;

class mapper : public Application
{
public:
mapper(uint16_t id, uint16_t port, Ipv4InterfaceContainer &ip, std::map<int, char> map_set, int size, int i);
virtual ~mapper();

private:
virtual void StartApplication(void);
void HandleRead(Ptr<Socket> socket);
void HandleAccept(Ptr<Socket> socket, const Address &from);

uint16_t id;
uint16_t port;
Ptr<Socket> server_socket;
Ipv4InterfaceContainer ip;
Ptr<Socket> client_socket;
std::map<int, char> map_set;
int size;
int i;
};

mapper::mapper(uint16_t id, uint16_t port, Ipv4InterfaceContainer &ip, std::map<int, char> map, int size, int i)
: id(id),
port(port),
ip(ip),
map_set(map),
size(size),
i(i)

{
std::srand(time(0));
}

mapper::~mapper()
{
}

void mapper::StartApplication(void)
{
server_socket = Socket::CreateSocket(GetNode(), TcpSocketFactory::GetTypeId());
InetSocketAddress local = InetSocketAddress(ip.GetAddress(i), port);
server_socket->Bind(local);
server_socket->Listen();
server_socket->SetAcceptCallback(MakeNullCallback<bool, Ptr<Socket>, const Address &>(), MakeCallback(&mapper::HandleAccept, this));

client_socket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());
}

void mapper::HandleRead(Ptr<Socket> socket)
{
Ptr<Packet> g_packet;

while ((g_packet = socket->Recv()))
{
if (g_packet->GetSize() == 0)
break;

MyHeader g_header;
g_packet->RemoveHeader(g_header);

uint16_t data = g_header.GetData();
Ipv4Address ip = g_header.GetIp();
uint16_t port = g_header.GetPort();

for (const auto &kv : map_set)
{
if (kv.first == data)
{
MyHeader s_header;
char new_data = kv.second;
s_header.SetData(new_data);
s_header.SetId(id);
Ptr<Packet> s_packet = Create<Packet>(s_header.GetSerializedSize());
s_packet->AddHeader(s_header);

InetSocketAddress remote = InetSocketAddress (ip, port);
client_socket->SendTo(s_packet, 0, remote);
// socket->Close();
break;
}
}
}
}

void mapper::HandleAccept(Ptr<Socket> socket, const Address &from)
{
socket->SetRecvCallback(MakeCallback(&mapper::HandleRead, this));
}

#endif
105 changes: 105 additions & 0 deletions CA2 - Wireless LAN Simulation/ns-3.35/scratch/master.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#ifndef MASTER_HPP
#define MASTER_HPP

#include <cstdlib>
#include <time.h>
#include <stdio.h>
#include <string>
#include <fstream>
#include <array>

#include "ns3/applications-module.h"
#include "ns3/internet-module.h"

#include "my_header.hpp"

using namespace ns3;

const int MAPPERS_COUNT = 3;

class master : public Application
{
public:
master(uint16_t port, Ipv4InterfaceContainer &ip,
uint16_t mapper_1_port, uint16_t mapper_2_port, uint16_t mapper_3_port,
Ipv4InterfaceContainer &mapper_ip);
virtual ~master();

private:
virtual void StartApplication(void);
void HandleRead(Ptr<Socket> socket);

uint16_t port;
Ipv4InterfaceContainer ip;
std::array<uint16_t, MAPPERS_COUNT> mapper_ports;
Ipv4InterfaceContainer mapper_ip;
Ptr<Socket> socket;
std::array<Ptr<Socket>, MAPPERS_COUNT> mapper_sockets;
};


master::master(uint16_t port, Ipv4InterfaceContainer &ip,
uint16_t mapper_1_port, uint16_t mapper_2_port, uint16_t mapper_3_port,
Ipv4InterfaceContainer &mapper_ip)
: port(port),
ip(ip),
mapper_ip(mapper_ip)
{
std::srand(time(0));
mapper_ports[0] = mapper_1_port;
mapper_ports[1] = mapper_2_port;
mapper_ports[2] = mapper_3_port;
}

master::~master()
{
}

void master::StartApplication(void)
{
socket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());
InetSocketAddress local = InetSocketAddress(ip.GetAddress(0), port);
socket->Bind(local);

mapper_sockets[0] = Socket::CreateSocket(GetNode(), TcpSocketFactory::GetTypeId());
InetSocketAddress remote_1 = InetSocketAddress(mapper_ip.GetAddress(0), mapper_ports[0]);
mapper_sockets[0]->Connect(remote_1);

mapper_sockets[1] = Socket::CreateSocket(GetNode(), TcpSocketFactory::GetTypeId());
InetSocketAddress remote_2 = InetSocketAddress(mapper_ip.GetAddress(1), mapper_ports[1]);
mapper_sockets[1]->Connect(remote_2);

mapper_sockets[2] = Socket::CreateSocket(GetNode(), TcpSocketFactory::GetTypeId());
InetSocketAddress remote_3 = InetSocketAddress(mapper_ip.GetAddress(2), mapper_ports[2]);
mapper_sockets[2]->Connect(remote_3);

socket->SetRecvCallback(MakeCallback(&master::HandleRead, this));
}

void master::HandleRead(Ptr<Socket> socket)
{
Ptr<Packet> packet;

while ((packet = socket->Recv()))
{
if (packet->GetSize() == 0)
{
break;
}
MyHeader header;
packet->RemoveHeader(header);
Ptr<Packet> packet0 = new Packet();
Ptr<Packet> packet1 = new Packet();
Ptr<Packet> packet2 = new Packet();
packet0->AddHeader(header);
packet1->AddHeader(header);
packet2->AddHeader(header);

mapper_sockets[0]->Send(packet0);
mapper_sockets[1]->Send(packet1);
mapper_sockets[2]->Send(packet2);
}
}


#endif
Loading

0 comments on commit c05da76

Please sign in to comment.