From 1cbf34c7f1a47c78af790262a2bc1dcb3733aa94 Mon Sep 17 00:00:00 2001 From: mattmcw Date: Fri, 5 Apr 2024 09:21:41 -0400 Subject: [PATCH] Create project in current state. Send works. Threading is a little sloppy --- .gitignore | 1 + .gitmodules | 3 + CMakeLists.txt | 24 ++++++++ compile.sh | 9 +++ json | 1 + main.cpp | 147 +++++++++++++++++++++++++++++++++++++++++++++++++ send.py | 24 ++++++++ 7 files changed, 209 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 compile.sh create mode 160000 json create mode 100644 main.cpp create mode 100644 send.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c971d9f --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "json"] + path = json + url = https://github.com/nlohmann/json.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..742b8e0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,24 @@ +cmake_minimum_required(VERSION 3.9) + +project(OpengGL_Timing_Example) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_VERBOSE_MAKEFILE ON) + +find_package(OpenGL REQUIRED) +find_package(GLUT REQUIRED) + +set( NAME_SRC + main.cpp +) + +set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) +add_executable( opengl_timing_example ${NAME_SRC} ${NAME_HEADERS} ) + +if (APPLE) + # Equivalent to pass flags -framework OpenGL + target_link_libraries(opengl_timing_example OpenGL::GL GLUT::GLUT) +else() + # Equivalent to pass flags -lGL, -lGLU and -lglut + target_link_libraries(opengl_timing_example GL GLU glut ) +endif() \ No newline at end of file diff --git a/compile.sh b/compile.sh new file mode 100644 index 0000000..cc7bb39 --- /dev/null +++ b/compile.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +#gcc -o opengl_timing_example main.cpp -lGL -lGLU -lglut -lm + +rm -rf build +mkdir -p build +cd build +cmake .. +make -j4 \ No newline at end of file diff --git a/json b/json new file mode 160000 index 0000000..199dea1 --- /dev/null +++ b/json @@ -0,0 +1 @@ +Subproject commit 199dea11b17c533721b26249e2dcaee6ca1d51d3 diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..31d04f6 --- /dev/null +++ b/main.cpp @@ -0,0 +1,147 @@ +#ifdef __APPLE__ + #include + #include + #include +#else + #include + #include +#endif +#include +#include +#include +#include +#include +#include +#include "json/single_include/nlohmann/json.hpp" +#include +#include +#include + +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(currentTime - startTime).count(); + + bool localShouldDisplayQuad; + { + unique_lock 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 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; +} \ No newline at end of file diff --git a/send.py b/send.py new file mode 100644 index 0000000..d607eef --- /dev/null +++ b/send.py @@ -0,0 +1,24 @@ +import socket +import json + +client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + +server_address = ('localhost', 8080) +print(f'Connecting to {server_address}...') +client_socket.connect(server_address) + +try: + data = { + 'message': 'Hello from Python', + 'value': 42, + 'enabled': True + } + + json_data = json.dumps(data) + + print(f'Sending: {json_data}') + client_socket.sendall(json_data.encode()) + +finally: + print('Closing connection') + client_socket.close() \ No newline at end of file