-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
437 lines (381 loc) · 14.4 KB
/
main.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include "ConcurrentQueue.h"
#include "ECSApp.h"
#include "QueueCollection.h"
#include "EntityCreationMessageV2.h"
#include <iostream>
#include <thread>
#include <unordered_map>
#include <cstdlib>
#include <ctime>
#include <vector>
// Note: For Windows, include Winsock2.h and link against Ws2_32.lib
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <chrono>
#include <json.hpp>
#include "civetweb.h"
using json = nlohmann::json;
struct EntityDefinition
{
float x, y, z;
std::string type;
EntityDefinition(float x, float y, float z, std::string type)
: x(x), y(y), z(z), type(type) {}
};
void parseCommandV2(const std::string &command, QueueCollection &queues)
{
using json = nlohmann::json;
json j = json::parse(command);
EntityCreationMessageV2 msg;
try
{
msg.id = j.at("id").get<int>();
}
catch (const json::type_error &e)
{
std::string idStr = j.at("id").get<std::string>();
msg.id = std::stoi(idStr);
}
msg.shapeType = j.at("shapeType").get<std::string>();
msg.transform.position = j["transform"]["position"].get<std::vector<float>>();
msg.transform.rotation = j["transform"]["rotation"].get<std::vector<float>>();
msg.transform.scale = j["transform"]["scale"].get<std::vector<float>>();
msg.shaders.vertexShader = j["shaders"]["vertex"].get<std::string>();
msg.shaders.fragmentShader = j["shaders"]["fragment"].get<std::string>();
// Extracting uniform data
for (auto &[key, val] : j["uniforms"].items())
{
std::string type = val["type"];
std::vector<float> values = val["value"].get<std::vector<float>>();
if (type == "floatVecUniforms" && values.size() >= 4)
{
msg.uniforms.floatVecUniforms[key] = values;
}
}
// Extracting vertex data
if (j.find("vertexData") != j.end())
{
if (j["vertexData"].find("positions") != j["vertexData"].end())
msg.vertexData.positions = j["vertexData"]["positions"].get<std::vector<float>>();
if (j["vertexData"].find("normals") != j["vertexData"].end())
msg.vertexData.normals = j["vertexData"]["normals"].get<std::vector<float>>();
if (j["vertexData"].find("texCoords") != j["vertexData"].end())
msg.vertexData.texCoords = j["vertexData"]["texCoords"].get<std::vector<float>>();
if (j["vertexData"].find("colors") != j["vertexData"].end())
msg.vertexData.colors = j["vertexData"]["colors"].get<std::vector<float>>();
}
// Pushing the parsed message into the creation queue
std::vector<EntityCreationMessageV2> creationMessages{msg};
queues.entityCreationV2Queue.Push(creationMessages);
// Additional commands like DELETE or COLOR can be similarly handled
}
void parseCommand(const std::string &command, QueueCollection &queues)
{
std::istringstream iss(command);
std::string cmd;
iss >> cmd;
if (cmd == "CREATE")
{
// Parse parameters
std::string type;
float x, y, z;
int id;
iss >> id >> type >> x >> y >> z;
// Add to entity creation queue in your system
std::vector<EntityCreationMessage> creationMessages{EntityCreationMessage(id, type, x, y, z)};
queues.entityCreationQueue.Push(creationMessages);
}
else if (cmd == "DELETE")
{
// Parse parameters
int id;
iss >> id;
// Add to entity deletion queue in your system
std::vector<EntityDeletionMessage> deletionMessages{EntityDeletionMessage(id)};
queues.entityDeletionQueue.Push(deletionMessages);
}
else if (cmd == "COLOR")
{
// Parse parameters
float r, g, b;
iss >> r >> g >> b;
// Add to color change queue or set color directly
queues.colorQueue.Push(std::make_tuple(r, g, b));
}
else
{
std::cout << "command: " << command << std::endl;
parseCommandV2(command, queues);
}
}
int handlePostRequest(struct mg_connection *conn, void *cbdata)
{
const struct mg_request_info *req_info = mg_get_request_info(conn);
if (req_info->content_length > 0)
{
std::string post_data(req_info->content_length, '\0');
mg_read(conn, &post_data[0], req_info->content_length);
try
{
// Assuming QueueCollection is globally accessible or passed as callback data
QueueCollection &queues = *static_cast<QueueCollection *>(cbdata);
// Parse and process the request data
parseCommandV2(post_data, queues);
// Respond to the request indicating success
mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
"Content-Type: application/json\r\n\r\n"
"{\"status\": \"success\"}\n");
}
catch (const std::exception &e)
{
// Handle parsing error or processing error
mg_printf(conn,
"HTTP/1.1 400 Bad Request\r\n"
"Content-Type: application/json\r\n\r\n"
"{\"error\": \"%s\"}\n",
e.what());
}
}
else
{
// Handle case with no content provided
mg_printf(conn,
"HTTP/1.1 411 Length Required\r\n"
"Content-Type: application/json\r\n\r\n"
"{\"error\": \"No data provided\"}\n");
}
return 200; // Return an HTTP status code
}
void ServerThreadV2(QueueCollection &queues)
{
const char *options[] = {
"listening_ports", "8080",
0};
mg_callbacks callbacks{};
memset(&callbacks, 0, sizeof(callbacks));
struct mg_context *ctx = mg_start(&callbacks, nullptr, options);
if (!ctx)
{
std::cerr << "Could not start CivetWeb server." << std::endl;
return;
}
mg_set_request_handler(ctx, "/entity", handlePostRequest, static_cast<void *>(&queues));
std::cout << "CivetWeb server started. Press Enter to stop.\n";
std::cin.get();
mg_stop(ctx);
}
void ServerThread(QueueCollection &queues)
{
int server_fd;
struct sockaddr_in address;
int addrlen = sizeof(address);
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8081);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
std::cout << "Server started. Listening for connections..." << std::endl;
// Outer loop added to continuously accept new connections
while (true)
{
int new_socket;
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0)
{
perror("accept");
continue; // Don't exit the program, just wait for the next connection
}
// Process the connection
char buffer[1024] = {0};
while (true)
{
int bytes_read = read(new_socket, buffer, 1024);
if (bytes_read <= 0)
{
std::cout << "Client disconnected or error." << std::endl;
break; // Break from processing loop, not from accept loop
}
std::cout << "Message Received: beg " << buffer << " end" << std::endl;
parseCommand(buffer, queues);
send(new_socket, "ACK\n", 4, 0); // Send back an acknowledgement
}
close(new_socket); // Close the current connection before accepting a new one
}
}
std::string createEntityCommand(int id, const std::string &type, float x, float y, float z)
{
return "CREATE " + std::to_string(id) + " " + type + " " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(z) + "\n";
}
std::string deleteEntityCommand(int id)
{
return "DELETE " + std::to_string(id) + "\n";
}
std::string colorChangeCommand(float r, float g, float b)
{
return "COLOR " + std::to_string(r) + " " + std::to_string(g) + " " + std::to_string(b) + "\n";
}
// Example parsing logic for one command
void parseCommand(const std::string &command)
{
std::istringstream iss(command);
std::string cmd;
iss >> cmd;
if (cmd == "CREATE")
{
std::string type;
float x, y, z;
iss >> type >> x >> y >> z;
// Create entity logic
}
else if (cmd == "DELETE")
{
int id;
iss >> id;
// Delete entity logic
}
else if (cmd == "COLOR")
{
float r, g, b;
iss >> r >> g >> b;
// Color change logic
}
}
// Function to wait for an acknowledgement
void waitForAck(int sock)
{
std::cout << "ack" << std::endl;
char ackBuffer[10];
recv(sock, ackBuffer, sizeof(ackBuffer), 0);
}
void ClientThread()
{
srand(static_cast<unsigned int>(time(nullptr))); // Seed for random number generation
// Predefined entity positions and types
std::vector<EntityDefinition> entityDefinitions = {
{-0.75f, 0.75f, 0.0f, "pyramid"},
{0.75f, 0.75f, 0.0f, "square"},
{0.75f, -0.75f, 0.0f, "triangle"},
{-0.75f, -0.75f, 0.0f, "cube"},
{2.0f, 2.0f, 0.0f, "pyramid"},
{2.0f, -2.0f, 0.0f, "square"},
{1.5f, 1.0f, 0.0f, "triangle"},
{-1.5f, 2.0f, 0.0f, "cube"}};
// {-0.75f, 0.75f, 0.0f, "triangle"},
// {0.75f, 0.75f, 0.0f, "square"},
// {0.75f, -0.75f, 0.0f, "triangle"},
// {-0.75f, -0.75f, 0.0f, "square"}};
// Track active entities by id
std::unordered_set<int> activeEntities;
int sock = 0, valread;
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8081); // Set to match server port
while (true)
{
// Creating socket file descriptor each loop to ensure fresh start
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
std::cout << "\n Socket creation error \n";
continue; // Try again on the next loop
}
// Convert IPv4 and IPv6 addresses from text to binary form
// Assuming the server is running on the same machine (localhost)
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
{
std::cout << "\nInvalid address/ Address not supported \n";
close(sock);
continue; // Try again on the next loop
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
std::cout << "\nConnection Failed. Retrying...\n";
close(sock);
std::this_thread::sleep_for(std::chrono::seconds(1)); // Wait a bit before retrying
continue; // Try again on the next loop
}
// Connection is successful
// Color
float r = static_cast<float>(rand()) / RAND_MAX;
float g = static_cast<float>(rand()) / RAND_MAX;
float b = static_cast<float>(rand()) / RAND_MAX;
std::string colorCommand = colorChangeCommand(r, g, b);
send(sock, colorCommand.c_str(), colorCommand.length(), 0);
waitForAck(sock);
// Decide randomly on actions: add or remove entities
int action = rand() % 2; // 0 for remove, 1 for add, 3 for v2
// Containers for messages
std::vector<EntityCreationMessage> creations;
std::vector<EntityDeletionMessage> deletions;
// Random action: Add or Remove entities
if (action == 1)
{
// Attempt to add a random entity if not already active
// int entityId = rand() % entityDefinitions.size();
int entityId = rand() % entityDefinitions.size();
if (activeEntities.find(entityId) == activeEntities.end())
{
// const auto &def = entityDefinitions[entityId];
// activeEntities.insert(entityId);
// std::string createMessage = createMockEntityCreationMessage(entityId, def.type, {def.x, def.y, def.z});
// std::cout << createMessage << " length: " << createMessage.length() << std::endl;
// send(sock, createMessage.c_str(), createMessage.length(), 0);
// waitForAck(sock);
// close(sock); // Close the socket after sending message
}
}
else if (action == 0)
{
// Attempt to remove a random entity if there exists any
if (!activeEntities.empty())
{
// int removeIndex = rand() % activeEntities.size();
// auto it = activeEntities.begin();
// std::advance(it, removeIndex);
// int entityIdToRemove = *it; // Obtain the actual entityId to remove
// deletions.push_back(EntityDeletionMessage(entityIdToRemove));
// activeEntities.erase(it);
// std::string entityCommand = deleteEntityCommand(entityIdToRemove); // Now correctly referencing the ID
// send(sock, entityCommand.c_str(), entityCommand.length(), 0);
// waitForAck(sock);
}
}
// create mock entity with V2
// int entityId = rand() % entityDefinitions.size();
// std::string message = createMockEntityCreationMessage(99, entityDefinitions[entityId].type, {0.0, 0.0, 0.0});
// std::cout << message << " length: " << message.length() << std::endl;
// send(sock, message.c_str(), message.length(), 0);
// waitForAck(sock);
close(sock); // Close the socket after sending message
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // Throttle message sending
}
}
int main()
{
QueueCollection queues;
std::thread server2Thread(ServerThread, std::ref(queues));
std::thread serverThread(ServerThreadV2, std::ref(queues));
std::thread clientThread(ClientThread);
OpenGLApp openglApp(queues); // Adjust constructor to accept QueueCollection
openglApp.Initialize();
openglApp.Run();
server2Thread.join();
serverThread.join();
// Ensure the server thread has completed before exiting the program
clientThread.join(); // This line is commented out along with the initiation of the client thread
return 0;
}