generated from usnistgov/opensource-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-gateway-server.cc
193 lines (169 loc) · 7.27 KB
/
simple-gateway-server.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
191
192
193
/*
* NIST-developed software is provided by NIST as a public service. You may use,
* copy, and distribute copies of the software in any medium, provided that you
* keep intact this entire notice. You may improve, modify, and create
* derivative works of the software or any portion of the software, and you may
* copy and distribute such modifications or works. Modified works should carry
* a notice stating that you changed the software and should note the date and
* nature of any such change. Please explicitly acknowledge the National
* Institute of Standards and Technology as the source of the software.
*
* NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY
* OF ANY KIND, EXPRESS, IMPLIED, IN FACT, OR ARISING BY OPERATION OF LAW,
* INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND DATA ACCURACY. NIST
* NEITHER REPRESENTS NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE
* UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES
* NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR
* THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY,
* RELIABILITY, OR USEFULNESS OF THE SOFTWARE.
*
* You are solely responsible for determining the appropriateness of using and
* distributing the software and you assume all risks associated with its use,
* including but not limited to the risks and costs of program errors,
* compliance with applicable laws, damage to or loss of data, programs or
* equipment, and the unavailability or interruption of operation. This software
* is not intended to be used in any situation where a failure could cause risk
* of injury or damage to property. The software developed by NIST employees is
* not subject to copyright protection within the United States.
*
* Author: Thomas Roth <thomas.roth@nist.gov>
*/
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include "ns3/core-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("SimpleGatewayServer");
int
main(int argc, char* argv[])
{
bool verboseLogs = false;
uint32_t timeStart = 0; // s
uint32_t timeDelta = 1; // s
uint32_t iterations = 20;
uint16_t numberOfNodes = 3;
uint16_t positionDeltaX = 25; // m
uint16_t serverPort = 8000;
CommandLine cmd(__FILE__);
cmd.AddValue("verbose", "Enable/disable detailed log output", verboseLogs);
cmd.AddValue("timeStart", "Starting simulation time in seconds", timeStart);
cmd.AddValue("timeDelta", "Simulation step size in seconds", timeDelta);
cmd.AddValue("iterations", "Number of time steps to simulate", iterations);
cmd.AddValue("numberOfNodes", "Number of vehicle nodes to simulate", numberOfNodes);
cmd.AddValue("positionDeltaX", "Maximum increase per time step to a node's x-coordinate", positionDeltaX);
cmd.AddValue("serverPort", "Port number of the UDP Server", serverPort);
cmd.Parse(argc, argv);
std::srand(std::time(NULL));
if (verboseLogs)
{
LogComponentEnable("SimpleGatewayServer", LOG_LEVEL_ALL);
}
else
{
LogComponentEnable("SimpleGatewayServer", LOG_LEVEL_INFO);
}
// create the server socket
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1)
{
NS_FATAL_ERROR("ERROR: failed to create the socket");
}
int reuse = 1;
if (setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
{
NS_FATAL_ERROR("ERROR: failed to set the socket options");
}
// set the server address
struct sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(serverPort);
serverAddress.sin_addr.s_addr = INADDR_ANY;
// bind the socket to the server address
if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) == -1)
{
NS_FATAL_ERROR("ERROR: failed to bind the socket to port " << serverPort);
}
// listen for client connections
if (listen(serverSocket, 1) == -1)
{
NS_FATAL_ERROR("ERROR: failed to listen for client connections");
}
NS_LOG_INFO("Started server on Port " << serverPort);
// accept a client connection
int clientSocket = accept(serverSocket, NULL, NULL);
if (clientSocket == -1)
{
NS_FATAL_ERROR("ERROR: failed to accept the client connection");
}
NS_LOG_INFO("Accepted a client connection");
/* ========== START MESSAGE PROTOCOL =====================================*/
const size_t BUFFER_SIZE = 4096;
char recvBuffer[BUFFER_SIZE];
std::vector<uint16_t> xVelocity(numberOfNodes, 0);
std::vector<uint16_t> xPosition(numberOfNodes, 0);
std::vector<uint16_t> broadcast(numberOfNodes, 0);
for (uint32_t i = 0; i < iterations; i++)
{
uint32_t timeNow = timeStart + timeDelta * i;
NS_LOG_INFO("t = " << timeNow);
// create the next message
std::string message = std::to_string(timeNow) + " 0"; // timestamp header
for (uint16_t n = 0; n < numberOfNodes; n++)
{
message += " " + std::to_string(xPosition[n]) + " " + std::to_string(n) + " 0"; // position vector
message += " " + std::to_string(xVelocity[n]) + " 0 0"; // velocity vector
message += " " + std::to_string(broadcast[n]); // broadcast bool
}
NS_LOG_DEBUG("next message: " << message);
message += "\r\n"; // end of message
// send the next message
if (send(clientSocket, message.c_str(), message.size(), 0) == -1)
{
NS_FATAL_ERROR("ERROR: failed to send a message");
}
// receive client response
int bytesReceived = recv(clientSocket, recvBuffer, BUFFER_SIZE - 1, 0);
if (bytesReceived == -1)
{
NS_FATAL_ERROR("ERROR: failed to receive response");
}
else if (bytesReceived == 0)
{
NS_LOG_WARN("WARNING: client socket terminated connection");
break;
}
else
{
recvBuffer[bytesReceived] = '\0'; // bytesReceived < BUFFER_SIZE
NS_LOG_DEBUG("received message: " << recvBuffer);
}
if (i == iterations - 1) // last iteration
{
message = "-1 0\r\n"; // terminate message
if (send(clientSocket, message.c_str(), message.size(), 0) == -1)
{
NS_FATAL_ERROR("ERROR: failed to send a message");
}
NS_LOG_INFO("Sent terminate message");
}
else
{
// simulate node movement
for (uint16_t n = 0; n < numberOfNodes; n++)
{
xVelocity[n] = std::rand() % positionDeltaX + 1;
xPosition[n] = xPosition[n] + xVelocity[n];
broadcast[n] = (i % 5 == 0) && (std::rand() % 2 == 0); // on multiples of 5, 50 % chance
}
}
}
close(clientSocket);
close(serverSocket);
return 0;
}