diff --git a/include/state.hpp b/include/state.hpp index 8ee5a60..baa1413 100644 --- a/include/state.hpp +++ b/include/state.hpp @@ -36,6 +36,7 @@ class State { uint64_t y; uint64_t width; uint64_t height; + bool active = false; bool ERROR = false; void error(); @@ -56,6 +57,8 @@ class State { vector getExposure() { return exposure; } vector getPosition() { return { x, y, width, height }; } bool isError () { return ERROR; } + void setActive () { active = true; } + bool isActive () { return active; } }; #endif diff --git a/src/main.cpp b/src/main.cpp index d0a8bfe..fbedd2e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,7 @@ using namespace std; uint16_t PORT = 8081; -int BUFFER_SIZE = 1024; +uint16_t BUFFER_SIZE = 2048; Image img; State state; @@ -30,11 +30,16 @@ condition_variable receivedCondVar; mutex sendMutex; condition_variable sendCondVar; -void performAction () { +void processMessage () { if (receivedMessage) { cout << "RECEIVED" << endl; state.receiveMessage(incomingMessage); receivedMessage = false; + + while (state.isActive()) { + // + } + if (state.isError()) { outgoingMessage = state.createMessage(false); } else { @@ -51,20 +56,21 @@ void handleTCPConnection(int clientSocket) { while (true) { int bytesRead = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0); if (bytesRead <= 0) { - // Client disconnected or error occurred + cerr << "Client disconnected or errored out" << endl; break; } - std::string iMessage(buffer, bytesRead); + string localIncomingMessage(buffer, bytesRead); { lock_guard lock(receivedMutex); receivedMessage = true; - incomingMessage = iMessage; + sendMessage = false; + incomingMessage = localIncomingMessage; receivedCondVar.notify_all(); } - performAction(); + processMessage(); bool localSendMessage; string localOutgoingMessage; @@ -80,7 +86,7 @@ void handleTCPConnection(int clientSocket) { const char* msg = localOutgoingMessage.c_str(); int bytesSent = send(clientSocket, msg, strlen(msg), 0); if (bytesSent < 0) { - std::cerr << "Error sending ACK to client" << std::endl; + cerr << "Error sending ACK to client" << endl; break; } } @@ -96,7 +102,7 @@ void runTCPServer (int serverSocket) { int clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen); if (clientSocket < 0) { - std::cerr << "Error accepting client connection" << std::endl; + cerr << "Error accepting client connection" << endl; continue; } @@ -120,7 +126,7 @@ int main (int argc, char** argv) { int bindResult = ::bind(serverSocket, reinterpret_cast(&serverAddr), sizeof(serverAddr)); if (bindResult < 0) { - std::cerr << "Error binding socket" << std::endl; + cerr << "Error binding socket" << endl; close(serverSocket); return 1; } @@ -133,8 +139,10 @@ int main (int argc, char** argv) { cout << "TCP server listening on port " << PORT << endl; thread serverThread(runTCPServer, serverSocket); - serverThread.join(); + + + close(serverSocket); return 0; diff --git a/src/state.cpp b/src/state.cpp index 0f63dc3..e6b988e 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -66,7 +66,7 @@ void State::receiveMessage (string msgString) { return; } - if (msgData.contains("image")) { + if (action == LOAD && msgData.contains("image")) { setImage(msgData); } diff --git a/test/test_messages.js b/test/test_messages.js index 61a4419..a683d71 100644 --- a/test/test_messages.js +++ b/test/test_messages.js @@ -18,15 +18,14 @@ client.connect(serverPort, serverAddress, async () => { action : 1, image: 'filename.tif', }; - const jsonData = JSON.stringify(data); console.log('SENDING'); - console.log(jsonData); - client.write(jsonData); + console.log(data); + client.write(JSON.stringify(data)); await delay(2000); - jsonData.action = 2; + data.action = 2; console.log('SENDING'); - console.log(jsonData); - client.write(jsonData); + console.log(data); + client.write(JSON.stringify(data)); }); client.on('data', (data) => {