147 lines
3.6 KiB
C++
147 lines
3.6 KiB
C++
#ifdef __APPLE__
|
|
#include <OpenGL/gl.h>
|
|
#include <OpenGL/gl3.h>
|
|
#include <GLUT/glut.h>
|
|
#else
|
|
#include <GL/glew.h>
|
|
#include <GL/glut.h>
|
|
#endif
|
|
#include <chrono>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
#include "json/single_include/nlohmann/json.hpp"
|
|
#include <mutex>
|
|
#include <thread>
|
|
#include <condition_variable>
|
|
|
|
using namespace std;
|
|
using namespace std::chrono;
|
|
|
|
const int DISPLAY_DURATION = 2000;
|
|
|
|
steady_clock::time_point startTime;
|
|
bool shouldDisplayQuad = false;
|
|
mutex quadMutex;
|
|
condition_variable quadCondVar;
|
|
|
|
void displayQuad() {
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
auto currentTime = steady_clock::now();
|
|
auto elapsedTime = duration_cast<milliseconds>(currentTime - startTime).count();
|
|
|
|
bool localShouldDisplayQuad;
|
|
{
|
|
unique_lock<mutex> lock(quadMutex);
|
|
quadCondVar.wait(lock, [&] { return shouldDisplayQuad; });
|
|
localShouldDisplayQuad = shouldDisplayQuad;
|
|
}
|
|
|
|
if (localShouldDisplayQuad && elapsedTime < DISPLAY_DURATION) {
|
|
glColor3f(1.0f, 0.0f, 0.0f);
|
|
|
|
glBegin(GL_QUADS);
|
|
glVertex2f(-0.5f, -0.5f);
|
|
glVertex2f(-0.5f, 0.5f);
|
|
glVertex2f(0.5f, 0.5f);
|
|
glVertex2f(0.5f, -0.5f);
|
|
glEnd();
|
|
} else {
|
|
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
}
|
|
|
|
glutSwapBuffers();
|
|
}
|
|
|
|
void timer(int value) {
|
|
glutPostRedisplay();
|
|
glutTimerFunc(1, timer, 0);
|
|
}
|
|
|
|
void handleTCPConnection(int serverSocket) {
|
|
struct sockaddr_in clientAddr;
|
|
socklen_t clientAddrLen = sizeof(clientAddr);
|
|
|
|
int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
|
|
if (clientSocket < 0) {
|
|
cerr << "Error accepting client connection" << endl;
|
|
return;
|
|
}
|
|
|
|
char buffer[1024] = {0};
|
|
int bytesRead = recv(clientSocket, buffer, sizeof(buffer) - 1, 0);
|
|
if (bytesRead < 0) {
|
|
cerr << "Error reading from client socket" << endl;
|
|
close(clientSocket);
|
|
return;
|
|
}
|
|
|
|
string jsonString(buffer, bytesRead);
|
|
try {
|
|
nlohmann::json jsonData = nlohmann::json::parse(jsonString);
|
|
cout << "Received JSON: " << jsonData << endl;
|
|
|
|
{
|
|
lock_guard<mutex> lock(quadMutex);
|
|
shouldDisplayQuad = true;
|
|
startTime = steady_clock::now();
|
|
quadCondVar.notify_all();
|
|
}
|
|
} catch (const exception& e) {
|
|
cerr << "Error parsing JSON: " << e.what() << endl;
|
|
}
|
|
|
|
close(clientSocket);
|
|
}
|
|
|
|
void runTCPServer(int serverSocket) {
|
|
while (true) {
|
|
handleTCPConnection(serverSocket);
|
|
}
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
glutInit(&argc, argv);
|
|
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
|
|
glutInitWindowSize(500, 500);
|
|
glutCreateWindow("Red Quad");
|
|
|
|
glutDisplayFunc(displayQuad);
|
|
glutTimerFunc(0, timer, 0);
|
|
|
|
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (serverSocket < 0) {
|
|
cerr << "Error creating socket" << endl;
|
|
return 1;
|
|
}
|
|
|
|
struct sockaddr_in serverAddr;
|
|
serverAddr.sin_family = AF_INET;
|
|
serverAddr.sin_addr.s_addr = INADDR_ANY;
|
|
serverAddr.sin_port = htons(8080);
|
|
|
|
if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
|
|
cerr << "Error binding socket" << endl;
|
|
return 1;
|
|
}
|
|
|
|
if (listen(serverSocket, 5) < 0) {
|
|
cerr << "Error listening on socket" << endl;
|
|
return 1;
|
|
}
|
|
|
|
cout << "TCP server listening on port 8080" << endl;
|
|
|
|
thread serverThread(runTCPServer, serverSocket);
|
|
|
|
glutMainLoop();
|
|
|
|
serverThread.join();
|
|
close(serverSocket);
|
|
|
|
return 0;
|
|
} |