Skip to content

Commit

Permalink
Adding select to data sync listen thread
Browse files Browse the repository at this point in the history
  • Loading branch information
grf14003 committed Jan 29, 2020
1 parent 7808d97 commit a53e2bc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions breadcrumbs/include/DataSyncThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class DataSyncThread

// Synchronous functions (only called from thread)
void threadRuntime();
bool readyToReceive(int interval = 1);
static DWORD threadInit(LPVOID pThreadArgs)
{
DataSyncThread* pDataSyncThread = (DataSyncThread*)pThreadArgs;
Expand Down
16 changes: 15 additions & 1 deletion breadcrumbs/src/comms/DataSyncThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ void DataSyncThread::threadRuntime()
{

printf("Waiting to receive bytes\n");
if (!readyToReceive())
continue;

// Master while loop that will receive incoming messages
char commandByte;
iResult = recvBytes(&commandByte, 1);
if (iResult <= 0)
Expand Down Expand Up @@ -58,6 +59,19 @@ void DataSyncThread::threadRuntime()
threadRunning = false;
}

bool DataSyncThread::readyToReceive(int interval)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(sock, &fds);

timeval tv;
tv.tv_sec = interval;
tv.tv_usec = 0;

return (select(sock + 1, &fds, 0, 0, &tv) == 1);
}

int DataSyncThread::recvBytes(void* buffer, size_t numBytes)
{
size_t bytesRecved = 0;
Expand Down
10 changes: 7 additions & 3 deletions breadcrumbs/src/main/VirtualOutputProcessor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "DataSyncThread.hpp"
#include <iostream>


int main()
Expand All @@ -8,9 +9,12 @@ int main()
client.connectToAlgorithm("localhost");
client.startComms();

char testValue = 'a';
Attribute attrib = *new Attribute("testKey1", 1, &testValue);
client.sendAttribute(attrib);
for (int i = 0; i < 10; i++)
{
char testValue = 'a' + i;
Attribute attrib("testKey1", 1, &testValue);
client.sendAttribute(attrib);
}

client.stopComms();

Expand Down

0 comments on commit a53e2bc

Please sign in to comment.