Skip to content

Commit

Permalink
Adding new attribs to vector and allow algorithm to get new values
Browse files Browse the repository at this point in the history
  • Loading branch information
grf14003 committed Jan 16, 2020
1 parent b947e5a commit a01e7a7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
35 changes: 34 additions & 1 deletion breadcrumbs/include/Attribute.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,42 @@
#ifndef ATTRIBUTE_HPP
#define ATTRIBUTE_HPP

#include <stdio.h>
#include <string>

#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
13 changes: 11 additions & 2 deletions breadcrumbs/include/DataSyncThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>

#include "Attribute.hpp"
#include "CMakeConfig.h"
Expand All @@ -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<Attribute> incomingAttributes;
public:
DataSyncThread(SOCKET s) { sock = s; };
DataSyncThread(SOCKET s)
{
sock = s;
};

// Synchronous functions (only called from thread)
void threadRuntime();
Expand All @@ -41,13 +48,15 @@ class DataSyncThread
return NULL;
}
int recvBytes(void* buffer, size_t numBytes);
void addIncomingAttribute(Attribute attrib);

// Async control (only called outside of thread)
void startComms();
bool stopComms();
bool isClientConnected() { return continueThread; };
unsigned int getSocketNumber() { return (unsigned int) socket; };
int connectToAlgorithm(char* serverName);
std::vector<Attribute> *getIncomingAttributes();
};

#endif
46 changes: 38 additions & 8 deletions breadcrumbs/src/comms/DataSyncThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

void DataSyncThread::threadRuntime()
{
attribMutex = CreateMutex(NULL, false, NULL);

/*
Handles the data sync between the other end of the socket
*/
Expand All @@ -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)
Expand All @@ -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()
{
/*
Expand Down Expand Up @@ -150,3 +166,17 @@ int DataSyncThread::connectToAlgorithm(char* serverName)
return 1;
}
}

std::vector<Attribute> *DataSyncThread::getIncomingAttributes()
{
vector<Attribute>* newVector = new vector<Attribute>;

DWORD result = WaitForSingleObject(attribMutex, INFINITE);
if (result == WAIT_OBJECT_0) {
for (Attribute attrib : incomingAttributes)
(*newVector).push_back(attrib);
ReleaseMutex(attribMutex);
}

return newVector;
}

0 comments on commit a01e7a7

Please sign in to comment.