From a01e7a7e7a8cb794092db974a0be20faf79f0f37 Mon Sep 17 00:00:00 2001 From: Greg Foss Date: Thu, 16 Jan 2020 13:18:08 -0500 Subject: [PATCH] Adding new attribs to vector and allow algorithm to get new values --- breadcrumbs/include/Attribute.hpp | 35 +++++++++++++++++- breadcrumbs/include/DataSyncThread.hpp | 13 +++++-- breadcrumbs/src/comms/DataSyncThread.cpp | 46 +++++++++++++++++++----- 3 files changed, 83 insertions(+), 11 deletions(-) diff --git a/breadcrumbs/include/Attribute.hpp b/breadcrumbs/include/Attribute.hpp index fc1ed4e..399af93 100644 --- a/breadcrumbs/include/Attribute.hpp +++ b/breadcrumbs/include/Attribute.hpp @@ -2,9 +2,42 @@ #ifndef ATTRIBUTE_HPP #define ATTRIBUTE_HPP +#include +#include + +#define ATTRIB_KEY_SIZE 8 + +using namespace std; + typedef struct BinaryAttributeStructure { - char key[8]; + char key[ATTRIB_KEY_SIZE]; char length; } bAttrib; +class Attribute +{ +private: + string key; + size_t length; + void* value; +public: + Attribute(BinaryAttributeStructure bin, void* value) { + key = ""; + for (int i = 0; i < ATTRIB_KEY_SIZE; i++) + key += bin.key[i]; + length = bin.length; + this->value = value; + } + + ~Attribute() { + + } + + // Getters and setters + string getKey() { return key; }; + size_t getLength() { return length; }; + void* getValue() { return value; }; + void* setValue(void* newValue) { void* old = value; value = newValue; return old; }; +}; + #endif \ No newline at end of file diff --git a/breadcrumbs/include/DataSyncThread.hpp b/breadcrumbs/include/DataSyncThread.hpp index 05a7998..4c428da 100644 --- a/breadcrumbs/include/DataSyncThread.hpp +++ b/breadcrumbs/include/DataSyncThread.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "Attribute.hpp" #include "CMakeConfig.h" @@ -26,11 +27,17 @@ class DataSyncThread { private: SOCKET sock; - HANDLE hThread; + HANDLE hThread = INVALID_HANDLE_VALUE; DWORD dwThreadId; bool continueThread = false; + + HANDLE attribMutex; + std::vector incomingAttributes; public: - DataSyncThread(SOCKET s) { sock = s; }; + DataSyncThread(SOCKET s) + { + sock = s; + }; // Synchronous functions (only called from thread) void threadRuntime(); @@ -41,6 +48,7 @@ class DataSyncThread return NULL; } int recvBytes(void* buffer, size_t numBytes); + void addIncomingAttribute(Attribute attrib); // Async control (only called outside of thread) void startComms(); @@ -48,6 +56,7 @@ class DataSyncThread bool isClientConnected() { return continueThread; }; unsigned int getSocketNumber() { return (unsigned int) socket; }; int connectToAlgorithm(char* serverName); + std::vector *getIncomingAttributes(); }; #endif \ No newline at end of file diff --git a/breadcrumbs/src/comms/DataSyncThread.cpp b/breadcrumbs/src/comms/DataSyncThread.cpp index b18e238..a2144ac 100644 --- a/breadcrumbs/src/comms/DataSyncThread.cpp +++ b/breadcrumbs/src/comms/DataSyncThread.cpp @@ -4,6 +4,8 @@ void DataSyncThread::threadRuntime() { + attribMutex = CreateMutex(NULL, false, NULL); + /* Handles the data sync between the other end of the socket */ @@ -24,26 +26,31 @@ void DataSyncThread::threadRuntime() { case 0: printf("Attribute update...\n"); - bAttrib attrib; - iResult = recvBytes(&attrib, sizeof(bAttrib)); + bAttrib bAttr; + iResult = recvBytes(&bAttr, sizeof(bAttrib)); if (iResult <= 0) break; - printf("Attribute update received, key:%.8s len:%x\n", attrib.key, attrib.length); - if (attrib.length <= 0) + printf("Attribute update received, key:%.8s len:%x\n", bAttr.key, bAttr.length); + if (bAttr.length <= 0) break; - void* value = malloc(attrib.length); - iResult = recvBytes(value, attrib.length); + void* value = malloc(bAttr.length); + iResult = recvBytes(value, bAttr.length); if (iResult <= 0) break; - // TODO: Storing the attrib update - + // Storing the attrib update + addIncomingAttribute(*new Attribute(bAttr, value)); + } } continueThread = false; + if (attribMutex != NULL) { + CloseHandle(attribMutex); + attribMutex = NULL; + } } int DataSyncThread::recvBytes(void* buffer, size_t numBytes) @@ -69,6 +76,15 @@ int DataSyncThread::recvBytes(void* buffer, size_t numBytes) return numBytes; } +void DataSyncThread::addIncomingAttribute(Attribute attrib) +{ + DWORD result = WaitForSingleObject(attribMutex, INFINITE); + if (result == WAIT_OBJECT_0) { + incomingAttributes.push_back(attrib); + ReleaseMutex(attribMutex); + } +} + void DataSyncThread::startComms() { /* @@ -150,3 +166,17 @@ int DataSyncThread::connectToAlgorithm(char* serverName) return 1; } } + +std::vector *DataSyncThread::getIncomingAttributes() +{ + vector* newVector = new vector; + + DWORD result = WaitForSingleObject(attribMutex, INFINITE); + if (result == WAIT_OBJECT_0) { + for (Attribute attrib : incomingAttributes) + (*newVector).push_back(attrib); + ReleaseMutex(attribMutex); + } + + return newVector; +}