-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipBlockchainApp.cc
190 lines (148 loc) · 5.29 KB
/
ipBlockchainApp.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ipBlockchainApp.hpp"
#include "globalNodeContainer.hpp"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("ipBlockchainApp");
NS_OBJECT_ENSURE_REGISTERED (ipBlockchainApp);
TypeId
ipBlockchainApp::GetTypeId ()
{
static TypeId tid =
TypeId ("ns3::ipBlockchainApp")
.AddConstructor<ipBlockchainApp> ()
.SetParent<Application> ()
.AddAttribute ("NodeName", "Name of the Node", StringValue ("/"),
MakeStringAccessor (&ipBlockchainApp::nameOfNode), MakeStringChecker ())
.AddAttribute ("m_thisAddress", "IPv4 address of itself", ObjectVectorValue (),
MakeStringAccessor (&ipBlockchainApp::m_thisAddress),
MakeStringChecker ());
return tid;
}
void
ipBlockchainApp::SetNodeContainer (NodeContainer nodes)
{
allNodes = nodes;
}
TypeId
ipBlockchainApp::GetInstanceTypeId () const
{
return ipBlockchainApp::GetTypeId ();
}
ipBlockchainApp::ipBlockchainApp ()
{
// Ptr<Ipv4> ipv4 = GetNode()->GetObject<Ipv4>();
// Ipv4InterfaceAddress iaddr = ipv4->GetAddress(1, 0);
m_port = 80;
}
void
ipBlockchainApp::SetupRecieveSocket (Ptr<Socket> socket, uint16_t port)
{
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), port);
if (socket->Bind (local) == -1)
{
NS_FATAL_ERROR ("Failed to bind socket");
}
}
void
ipBlockchainApp::StartApplication ()
{
cout << nameOfNode << " started application" << endl;
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
m_recvSocket = Socket::CreateSocket (GetNode (), tid);
SetupRecieveSocket (m_recvSocket, m_port);
m_recvSocket->SetRecvCallback (MakeCallback (&ipBlockchainApp::OnPacket, this));
m_recvSocket->SetAllowBroadcast (true);
m_sendSocket = Socket::CreateSocket (GetNode (), tid);
m_sendSocket->SetAllowBroadcast (true);
}
void
ipBlockchainApp::StopApplication ()
{
}
void
ipBlockchainApp::OnPacket (Ptr<Socket> socket)
{
NS_LOG_FUNCTION (this << socket);
NS_LOG_DEBUG (m_thisAddress << ": Recieved a packet!");
cout << m_thisAddress << " recieved a packet!" << endl;
Ptr<Packet> packet;
Address from, localAddress;
// if (packet = socket->RecvFrom (from))
// {
// // std::string recvdStr = packet->ToString();
// Ipv4Header ipHeader;
// packet->RemoveHeader (ipHeader);
// uint8_t buffer[packet->GetSize ()];
// int len = packet->CopyData (buffer, packet->GetSize ());
// std::string recvdStr (buffer, buffer + packet->GetSize () - 1);
// NS_LOG_INFO ("Received a Packet: \"" << recvdStr << "\" at time " << Now ().GetSeconds ()
// << " on " << m_thisAddress);
// if (recvdStr == "")
// return;
// std::string sendStr = m_responseString[recvdStr];
// Ptr<Packet> sendPacket = Create<Packet> ((uint8_t *) sendStr.c_str (), sendStr.size () + 1);
// SendPacket (sendPacket, ipHeader.GetSource (), 80);
// // PppHeader PppHeader;
// // copy->RemoveHeader(PppHeader);
// // // Ipv4Address dest =
// // std::cout << "Recieved from ";
// // ipHeader.GetSource().Print(std::cout);
// // std::cout << std::endl;
// // ipHeader.GetDestination().Print(std::cout);
// // std::cout << std::endl;
// // NS_LOG_DEBUG(sendStr);
// // if (recvdStr == "hello1") {
// // Ipv4Address dest("10.1.1.1");
// // SendPacket(sendPacket, dest, 80);
// // }
// }
}
void
ipBlockchainApp::SendPacket (Ptr<Packet> packet, Ipv4Address destination, uint16_t port)
{
cout << m_thisAddress << ": Sending data to " << destination << " on port " << port << endl;
uint8_t buffer[packet->GetSize ()];
int len = packet->CopyData (buffer, packet->GetSize ());
std::string recvdStr (buffer, buffer + packet->GetSize () - 1);
NS_LOG_INFO ("Sending " << recvdStr);
NS_LOG_FUNCTION (this << packet << destination << port);
m_sendSocket->Connect (InetSocketAddress (destination, port));
m_sendSocket->Send (packet);
}
void
ipBlockchainApp::NewTransaction (string trxData)
{
istringstream is (trxData);
string user, rcpnt;
is >> user;
is >> rcpnt;
this->BroadcastTransaction (user, rcpnt);
cout << "Transaction created by " << nameOfNode << endl;
}
void
ipBlockchainApp::BroadcastTransaction (string sender, string reciever)
{
string trxData = "/udp.blockchain/" + reciever;
trxData += "/" + nameOfNode;
trxData += "/vote";
cout << "Broadcasting transaction: " << trxData << endl;
Ptr<Packet> sendPacket = Create<Packet> ((uint8_t *) trxData.c_str (), trxData.size () + 1);
NS_LOG_DEBUG ("Size is: " << allNodesContainer.size ());
// for (auto it = allNodes.begin (); it != allNodes.end (); it++)
// {
// Ptr<Node> node = *it;
// Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
// Ipv4InterfaceAddress iaddr = ipv4->GetAddress (1, 0);
// Ipv4Address ipAddr = iaddr.GetLocal ();
// NS_LOG_DEBUG (ipAddr);
// // if (Names::FindName (node)[0] == 'r')
// // continue;
// // if (Names::FindName (node) == nameOfNode)
// // continue;
// this->SendPacket (sendPacket, ipAddr, 80);
// }
}